diff options
author | David A. Madore <david@procyon.(none)> | 2010-04-27 18:07:39 +0200 |
---|---|---|
committer | David A. Madore <david@procyon.(none)> | 2010-04-27 18:10:03 +0200 |
commit | aa9f0171298ffd173b927f7e288593a88e0038ee (patch) | |
tree | 01c373691023a66bacc50a9cf3af7347590958ef | |
parent | 271f1aa10cc8cff82e40ea786d502f7b8669120e (diff) | |
download | UnicodeMap-aa9f0171298ffd173b927f7e288593a88e0038ee.tar.gz UnicodeMap-aa9f0171298ffd173b927f7e288593a88e0038ee.tar.bz2 UnicodeMap-aa9f0171298ffd173b927f7e288593a88e0038ee.zip |
Overhaul the way display history is memorized.
In a nutshell: a UnicodeMapActivity.Display stores the kind of content
the ListView can have (the root, a Unicode range, or a search result).
The doDisplay() method actually performs such a display, and is
generally called after saveDisplay() which stores the current display
(curDisp) in the display stack together with position information.
-rw-r--r-- | src/org/madore/android/unicodeMap/UnicodeMapActivity.java | 192 |
1 files changed, 113 insertions, 79 deletions
diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index d260ff6..9f8194f 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -26,26 +26,62 @@ import android.app.ProgressDialog; public final class UnicodeMapActivity extends ListActivity { protected UnicodeDatabase db; + protected ListView lv; - protected class ViewHistory { - public final UnicodeArrayAdapter adapter; - public final int selPosition; - public final int yOffset; - public final CharSequence title; - public ViewHistory(UnicodeArrayAdapter adapter, - int selPosition, int yOffset, - CharSequence title) { - this.adapter = adapter; + protected static abstract class Display { + protected String title; + public Display(String title) { this.title = title; } + public final String getTitle() { return this.title; } + } + protected static class RootDisplay extends Display { + public RootDisplay() { super(null); } + } + protected static class RangeDisplay extends Display { + protected int from; protected int to; + public RangeDisplay(String title, int from, int to) { + super(title); + this.from = from; this.to = to; + } + public RangeDisplay(UnicodeCharacter.Range rng) { + super(rng.getDescr()); + this.from = rng.getFrom(); this.to = rng.getTo(); + } + public int getFrom() { return this.from; } + public int getTo() { return this.to; } + } + protected static class SearchDisplay extends Display { + protected String like; protected int limit; + protected int sizeHint; + public SearchDisplay(String title, String like, int limit) { + super(title); + this.like = like; this.limit = limit; + this.sizeHint = 128; + } + public void setSizeHint(int hint) { this.sizeHint = hint; } + public String getLike() { return this.like; } + public int getLimit() { return this.limit; } + public int getSizeHint() { return this.sizeHint; } + } + + protected static class DisplayAndPosition { + protected Display disp; + protected int selPosition; protected int yOffset; + public DisplayAndPosition(Display disp, + int selPosition, int yOffset) { + this.disp = disp; this.selPosition = selPosition; this.yOffset = yOffset; - this.title = title; } + public Display getDisp() { return this.disp; } + public int getSelPosition() { return this.selPosition; } + public int getYOffset() { return this.yOffset; } } - protected final List<ViewHistory> historyStack - = new ArrayList<ViewHistory>(10); + protected Display curDisp; + protected final List<DisplayAndPosition> dispHistory + = new ArrayList<DisplayAndPosition>(10); - EditText textForm; + protected EditText textForm; protected void launchPopulation() { final ProgressDialog progress = new ProgressDialog(this); @@ -75,26 +111,18 @@ public final class UnicodeMapActivity extends ListActivity { thr.start(); } - protected void saveView(View selView, int selPosition) { - final ListView lv = getListView(); + protected void saveDisplay(View selView, int selPosition) { lv.clearTextFilter(); - final UnicodeArrayAdapter adapter - = (UnicodeArrayAdapter) lv.getAdapter(); final int yOffset; if ( selView != null ) yOffset = selView.getTop(); else yOffset = 0; - CharSequence title = getTitle(); - historyStack.add(new ViewHistory(adapter, selPosition, yOffset, - title)); + dispHistory.add(new DisplayAndPosition(curDisp, selPosition, yOffset)); } - protected void saveView() { - final ListView lv = getListView(); + protected void saveDisplay() { lv.clearTextFilter(); - final UnicodeArrayAdapter adapter - = (UnicodeArrayAdapter) lv.getAdapter(); int position = lv.getSelectedItemPosition(); int position0 = lv.getFirstVisiblePosition(); if ( position == AdapterView.INVALID_POSITION ) @@ -105,9 +133,54 @@ public final class UnicodeMapActivity extends ListActivity { yOffset = view.getTop(); else yOffset = 0; - CharSequence title = getTitle(); - historyStack.add(new ViewHistory(adapter, position, yOffset, - title)); + dispHistory.add(new DisplayAndPosition(curDisp, position, yOffset)); + } + + protected void doDisplay(Display disp) { + curDisp = disp; + List<UnicodeDisplayable> list; + UnicodeArrayAdapter adapter; + if ( disp instanceof RootDisplay ) { + list = new ArrayList<UnicodeDisplayable>(UnicodeCharacter.Range.values().length); + for ( UnicodeCharacter.Range rng : UnicodeCharacter.Range.values() ) + list.add(rng); + adapter = new UnicodeArrayAdapter(this, list); + setListAdapter(adapter); + } else if ( disp instanceof RangeDisplay ) { + int from = ((RangeDisplay)disp).getFrom(); + int to = ((RangeDisplay)disp).getTo(); + list = new ArrayList<UnicodeDisplayable>(db.countRange(from,to)); + for ( UnicodeCharacter ch : db.getRange(from,to) ) + list.add(ch); + adapter = new UnicodeArrayAdapter(this, list); + setListAdapter(adapter); + } else if ( disp instanceof SearchDisplay ) { + String s = ((SearchDisplay)disp).getLike(); + int limit = ((SearchDisplay)disp).getLimit(); + int sizeHint = ((SearchDisplay)disp).getSizeHint(); + list = new ArrayList<UnicodeDisplayable>(sizeHint); + for ( UnicodeCharacter ch : db.searchNames(s,limit+1) ) + list.add(ch); + int size = list.size(); + ((SearchDisplay)disp).setSizeHint(size); + boolean overflowed = ( size > limit ); + if ( overflowed ) + list.remove(list.size()-1); + adapter = new UnicodeArrayAdapter(this, list); + setListAdapter(adapter); + if ( overflowed ) { + String str = getResources().getString(R.string.list_too_long); + Toast.makeText(UnicodeMapActivity.this, + String.format(str, limit), + Toast.LENGTH_SHORT).show(); + } + } else + throw new AssertionError("unknown UnicodeMapActivity.Display"); + String title = disp.getTitle(); + if ( title != null ) + setTitle(String.format(getResources().getString(R.string.app_name_spec), title)); + else + setTitle(getResources().getString(R.string.app_name)); } protected class MapItemClickListener @@ -120,20 +193,9 @@ public final class UnicodeMapActivity extends ListActivity { if ( it instanceof UnicodeCharacter ) textForm.append(((UnicodeCharacter)it).getChar()); else if ( it instanceof UnicodeCharacter.Range ) { - int from = ((UnicodeCharacter.Range)it).getFrom(); - int to = ((UnicodeCharacter.Range)it).getTo(); - List<UnicodeDisplayable> list - = new ArrayList<UnicodeDisplayable>(db.countRange(from,to)); - for ( UnicodeCharacter ch : db.getRange(from,to) ) - list.add(ch); - UnicodeArrayAdapter adapter - = new UnicodeArrayAdapter(UnicodeMapActivity.this, - list); - saveView(view, position); - setListAdapter(adapter); - setTitle(String.format(getResources() - .getString(R.string.app_name_spec), - ((UnicodeCharacter.Range)it).getDescr())); + Display newDisp = new RangeDisplay((UnicodeCharacter.Range)it); + saveDisplay(view, position); + doDisplay(newDisp); } else throw new AssertionError("unknown UnicodeDisplayable"); } @@ -200,13 +262,7 @@ public final class UnicodeMapActivity extends ListActivity { final boolean needPopulate = db.needPopulate(); if ( needPopulate ) launchPopulation(); - List<UnicodeDisplayable> rootList - = new ArrayList<UnicodeDisplayable>(UnicodeCharacter.Range.values().length); - for ( UnicodeCharacter.Range rng : UnicodeCharacter.Range.values() ) - rootList.add(rng); - UnicodeArrayAdapter rootAdapter - = new UnicodeArrayAdapter(this, rootList); - setListAdapter(rootAdapter); + doDisplay(new RootDisplay()); final ClipboardManager cmgr = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); final Button btn = (Button) findViewById(R.id.copyButton); @@ -219,7 +275,7 @@ public final class UnicodeMapActivity extends ListActivity { Toast.LENGTH_SHORT).show(); } }); - final ListView lv = getListView(); + lv = getListView(); lv.setTextFilterEnabled(true); MapItemClickListener listener = new MapItemClickListener(); lv.setOnItemClickListener(listener); @@ -229,29 +285,10 @@ public final class UnicodeMapActivity extends ListActivity { final static int searchLimit = 1000; protected void doSearch(String s) { - List<UnicodeDisplayable> list - = new ArrayList<UnicodeDisplayable>(128); - for ( UnicodeCharacter ch : db.searchNames(s,searchLimit+1) ) - list.add(ch); - boolean overflowed = ( list.size() > searchLimit ); - if ( overflowed ) - list.remove(list.size()-1); - UnicodeArrayAdapter adapter - = new UnicodeArrayAdapter(UnicodeMapActivity.this, - list); - saveView(); - setListAdapter(adapter); - setTitle(String.format(getResources() - .getString(R.string.app_name_spec), - getResources() - .getString(R.string.search_results))); - if ( overflowed ) { - String str = getResources().getString(R.string.list_too_long); - android.util.Log.e("UnicodeMapActivity", "format is: "+str); - Toast.makeText(UnicodeMapActivity.this, - String.format(str, searchLimit), - Toast.LENGTH_SHORT).show(); - } + Display newDisp = new SearchDisplay(getResources().getString(R.string.search_results), + s, searchLimit); + saveDisplay(); + doDisplay(newDisp); } protected void querySearch() { @@ -308,13 +345,10 @@ public final class UnicodeMapActivity extends ListActivity { @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - if ( ( keyCode == KeyEvent.KEYCODE_BACK ) - && historyStack.size() > 0 ) { - final ListView lv = getListView(); - ViewHistory saved = historyStack.remove(historyStack.size()-1); - setListAdapter(saved.adapter); - setTitle(saved.title); - lv.setSelectionFromTop(saved.selPosition, saved.yOffset); + if ( ( keyCode == KeyEvent.KEYCODE_BACK ) && dispHistory.size() > 0 ) { + DisplayAndPosition saved = dispHistory.remove(dispHistory.size()-1); + doDisplay(saved.getDisp()); + lv.setSelectionFromTop(saved.getSelPosition(), saved.getYOffset()); return true; } else if ( keyCode == KeyEvent.KEYCODE_SEARCH ) { querySearch(); |