]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
UI fixes, launch with intent not prefs,
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 84497e51ac205c1b233197f2a4f5de1c0a1e3840..d6ec791fc9a214e9c85caa608d26147dce285e2a 100644 (file)
@@ -20,12 +20,18 @@ import java.io.IOException;
 import java.io.PrintWriter;\r
 import java.io.RandomAccessFile;\r
 import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
 import java.util.Date;\r
+import java.util.List;\r
 import java.util.concurrent.Executor;\r
 import java.util.concurrent.Executors;\r
 import java.util.concurrent.ThreadFactory;\r
 import java.util.concurrent.atomic.AtomicBoolean;\r
+import java.util.regex.Matcher;\r
+import java.util.regex.Pattern;\r
 \r
+import android.app.Activity;\r
+import android.app.Dialog;\r
 import android.app.ListActivity;\r
 import android.content.Context;\r
 import android.content.Intent;\r
@@ -36,8 +42,10 @@ import android.os.Handler;
 import android.preference.PreferenceManager;\r
 import android.text.ClipboardManager;\r
 import android.text.Editable;\r
+import android.text.Selection;\r
 import android.text.Spannable;\r
 import android.text.TextWatcher;\r
+import android.text.method.LinkMovementMethod;\r
 import android.text.style.StyleSpan;\r
 import android.util.Log;\r
 import android.util.TypedValue;\r
@@ -47,8 +55,10 @@ import android.view.KeyEvent;
 import android.view.Menu;\r
 import android.view.MenuItem;\r
 import android.view.MenuItem.OnMenuItemClickListener;\r
+import android.view.MotionEvent;\r
 import android.view.View;\r
 import android.view.View.OnClickListener;\r
+import android.view.View.OnLongClickListener;\r
 import android.view.ViewGroup;\r
 import android.view.inputmethod.InputMethodManager;\r
 import android.widget.AdapterView;\r
@@ -56,6 +66,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
 import android.widget.BaseAdapter;\r
 import android.widget.Button;\r
 import android.widget.EditText;\r
+import android.widget.LinearLayout;\r
 import android.widget.ListAdapter;\r
 import android.widget.ListView;\r
 import android.widget.TableLayout;\r
@@ -76,9 +87,7 @@ public class DictionaryActivity extends ListActivity {
 \r
   static final String LOG = "QuickDic";\r
   \r
-  static final int VIBRATE_MILLIS = 100;\r
-\r
-  int dictIndex = 0;\r
+  String dictFile = null;\r
   RandomAccessFile dictRaf = null;\r
   Dictionary dictionary = null;\r
   int indexIndex = 0;\r
@@ -94,6 +103,7 @@ public class DictionaryActivity extends ListActivity {
   });\r
   private SearchOperation currentSearchOperation = null;\r
 \r
+  C.Theme theme = C.Theme.LIGHT;\r
   int fontSizeSp;\r
   EditText searchText;\r
   Button langButton;\r
@@ -113,47 +123,39 @@ public class DictionaryActivity extends ListActivity {
   public DictionaryActivity() {\r
   }\r
   \r
-  public static Intent getIntent(final Context context, final int dictIndex, final int indexIndex, final String searchToken) {\r
-    setDictionaryPrefs(context, dictIndex, indexIndex, searchToken);\r
-    \r
+  public static Intent getLaunchIntent(final String dictFile, final int indexIndex, final String searchToken) {\r
     final Intent intent = new Intent();\r
     intent.setClassName(DictionaryActivity.class.getPackage().getName(), DictionaryActivity.class.getName());\r
+    intent.putExtra(C.DICT_FILE, dictFile);\r
+    intent.putExtra(C.INDEX_INDEX, indexIndex);\r
+    intent.putExtra(C.SEARCH_TOKEN, searchToken);\r
     return intent;\r
   }\r
+  \r
+  // TODO: fix these...\r
 \r
-  public static void setDictionaryPrefs(final Context context,\r
-      final int dictIndex, final int indexIndex, final String searchToken) {\r
-    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
-    prefs.putInt(C.DICT_INDEX, dictIndex);\r
-    prefs.putInt(C.INDEX_INDEX, indexIndex);\r
-    prefs.putString(C.SEARCH_TOKEN, searchToken);\r
-    prefs.commit();\r
-  }\r
-\r
-  public static void clearDictionaryPrefs(final Context context) {\r
-    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
-    prefs.remove(C.DICT_INDEX);\r
-    prefs.remove(C.INDEX_INDEX);\r
-    prefs.remove(C.SEARCH_TOKEN);\r
-    prefs.commit();\r
-    Log.d(LOG, "Removed default dictionary prefs.");\r
+  @Override\r
+  protected void onSaveInstanceState(final Bundle outState) {\r
+    super.onSaveInstanceState(outState);\r
+    outState.putString(C.SEARCH_TOKEN, searchText.getText().toString());\r
   }\r
-\r
+  \r
   @Override\r
   public void onCreate(Bundle savedInstanceState) {\r
-    ((DictionaryApplication)getApplication()).applyTheme(this);\r
+    clearDictionaryPrefs(this);\r
     \r
-    super.onCreate(savedInstanceState);\r
     Log.d(LOG, "onCreate:" + this);\r
+    theme = ((DictionaryApplication)getApplication()).getSelectedTheme();\r
+    super.onCreate(savedInstanceState);\r
     \r
-    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
-    \r
+    final Intent intent = getIntent();\r
+    dictFile = intent.getStringExtra(C.DICT_FILE);\r
+\r
     try {\r
       PersistentObjectCache.init(this);\r
       QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
           this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
-      dictIndex = prefs.getInt(C.DICT_INDEX, 0) ;\r
-      final DictionaryConfig dictionaryConfig = quickDicConfig.dictionaryConfigs.get(dictIndex);\r
+      final DictionaryInfo dictionaryConfig = quickDicConfig.getDictionaryInfoByFile(dictFile);\r
       this.setTitle("QuickDic: " + dictionaryConfig.name);\r
       dictRaf = new RandomAccessFile(dictionaryConfig.localFile, "r");\r
       dictionary = new Dictionary(dictRaf); \r
@@ -168,16 +170,16 @@ public class DictionaryActivity extends ListActivity {
         dictRaf = null;\r
       }\r
       Toast.makeText(this, getString(R.string.invalidDictionary, "", e.getMessage()), Toast.LENGTH_LONG);\r
-      startActivity(DictionaryEditActivity.getIntent(dictIndex));\r
+      startActivity(DictionaryEditActivity.getLaunchIntent(dictFile));\r
       finish();\r
       return;\r
     }\r
 \r
-    indexIndex = prefs.getInt(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
     Log.d(LOG, "Loading index.");\r
+    indexIndex = intent.getIntExtra(C.INDEX_INDEX, 0) % dictionary.indices.size();\r
     index = dictionary.indices.get(indexIndex);\r
     setListAdapter(new IndexAdapter(index));\r
-\r
+    \r
     // Pre-load the collators.\r
     searchExecutor.execute(new Runnable() {\r
       public void run() {\r
@@ -196,7 +198,7 @@ public class DictionaryActivity extends ListActivity {
         });\r
         \r
         for (final Index index : dictionary.indices) {\r
-          Log.d(LOG, "Starting collator load for lang=" + index.sortLanguage.getSymbol());\r
+          Log.d(LOG, "Starting collator load for lang=" + index.sortLanguage.getIsoCode());\r
           \r
           final com.ibm.icu.text.Collator c = index.sortLanguage.getCollator();          \r
           if (c.compare("pre-print", "preppy") >= 0) {\r
@@ -209,9 +211,11 @@ public class DictionaryActivity extends ListActivity {
       }\r
     });\r
     \r
-    final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "12");\r
+    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);\r
+    \r
+    final String fontSize = prefs.getString(getString(R.string.fontSizeKey), "14");\r
     try {\r
-      fontSizeSp = Integer.parseInt(fontSize);\r
+      fontSizeSp = Integer.parseInt(fontSize.trim());\r
     } catch (NumberFormatException e) {\r
       fontSizeSp = 12;\r
     }\r
@@ -245,6 +249,13 @@ public class DictionaryActivity extends ListActivity {
         onLanguageButton();\r
       }\r
     });\r
+    langButton.setOnLongClickListener(new OnLongClickListener() {\r
+      @Override\r
+      public boolean onLongClick(View v) {\r
+        onLanguageButtonLongClick();\r
+        return true;\r
+      }\r
+    });\r
     updateLangButton();\r
     \r
     final Button upButton = (Button) findViewById(R.id.UpButton);\r
@@ -290,17 +301,42 @@ public class DictionaryActivity extends ListActivity {
       // vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);\r
     //}\r
     Log.d(LOG, "wordList=" + wordList + ", saveOnlyFirstSubentry=" + saveOnlyFirstSubentry);\r
+    \r
+    setDictionaryPrefs(this, dictFile, indexIndex, searchText.getText().toString());\r
   }\r
   \r
   @Override\r
   protected void onResume() {\r
     super.onResume();\r
+    if (PreferenceActivity.prefsMightHaveChanged) {\r
+      PreferenceActivity.prefsMightHaveChanged = false;\r
+      finish();\r
+      startActivity(getIntent());\r
+    }\r
   }\r
   \r
   @Override\r
   protected void onPause() {\r
     super.onPause();\r
   }\r
+  \r
+  private static void setDictionaryPrefs(final Context context,\r
+      final String dictFile, final int indexIndex, final String searchToken) {\r
+    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+    prefs.putString(C.DICT_FILE, dictFile);\r
+    prefs.putInt(C.INDEX_INDEX, indexIndex);\r
+    prefs.putString(C.SEARCH_TOKEN, searchToken);\r
+    prefs.commit();\r
+  }\r
+\r
+  private static void clearDictionaryPrefs(final Context context) {\r
+    final SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit();\r
+    prefs.remove(C.DICT_FILE);\r
+    prefs.remove(C.INDEX_INDEX);\r
+    prefs.remove(C.SEARCH_TOKEN);\r
+    prefs.commit();\r
+  }\r
+\r
 \r
   @Override\r
   protected void onDestroy() {\r
@@ -308,7 +344,6 @@ public class DictionaryActivity extends ListActivity {
     if (dictRaf == null) {\r
       return;\r
     }\r
-    setDictionaryPrefs(this, dictIndex, indexIndex, searchText.getText().toString());\r
     \r
     // Before we close the RAF, we have to wind the current search down.\r
     if (currentSearchOperation != null) {\r
@@ -350,7 +385,7 @@ public class DictionaryActivity extends ListActivity {
   }\r
   \r
   void updateLangButton() {\r
-    langButton.setText(index.shortName.toUpperCase());\r
+    langButton.setText(index.shortName);\r
   }\r
 \r
   void onLanguageButton() {\r
@@ -358,13 +393,84 @@ public class DictionaryActivity extends ListActivity {
       currentSearchOperation.interrupted.set(true);\r
       currentSearchOperation = null;\r
     }\r
+    changeIndex((indexIndex + 1)% dictionary.indices.size());\r
+  }\r
+  \r
+  static class OpenIndexButton extends Button implements OnClickListener {\r
+\r
+    final Activity activity;\r
+    final String dictFile;\r
+    final int indexIndex;\r
+\r
+    public OpenIndexButton(final Context context, final Activity activity, final String text, final String dictFile, final int indexIndex) {\r
+      super(context);\r
+      this.activity = activity;\r
+      this.dictFile = dictFile;\r
+      this.indexIndex = indexIndex;\r
+      setOnClickListener(this);\r
+      setText(text, BufferType.NORMAL);\r
+    }\r
+\r
+    @Override\r
+    public void onClick(View v) {\r
+      activity.finish();\r
+      getContext().startActivity(DictionaryActivity.getLaunchIntent(dictFile, indexIndex, ""));\r
+    }\r
     \r
-    indexIndex = (indexIndex + 1) % dictionary.indices.size();\r
+  }\r
+\r
+  void onLanguageButtonLongClick() {\r
+    Context mContext = getApplicationContext();\r
+    Dialog dialog = new Dialog(mContext);\r
+    \r
+    dialog.setContentView(R.layout.select_dictionary_dialog);\r
+    dialog.setTitle(R.string.selectADictionary);\r
+\r
+    ListView listView = (ListView) dialog.findViewById(android.R.id.list);\r
+\r
+    QuickDicConfig quickDicConfig = PersistentObjectCache.init(\r
+        this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class);\r
+    final List<DictionaryInfo> dictionaryInfos = new ArrayList<DictionaryInfo>();\r
+    for (final DictionaryInfo dictionaryInfo : quickDicConfig.dictionaryInfos) {\r
+      if (new File(dictionaryInfo.localFile).canRead()) {\r
+        dictionaryInfos.add(dictionaryInfo);\r
+      }\r
+    }\r
+    listView.setAdapter(new BaseAdapter() {\r
+      \r
+      @Override\r
+      public View getView(int position, View convertView, ViewGroup parent) {\r
+        final LinearLayout result = new LinearLayout(parent.getContext());\r
+        //result.addView(new Butt)\r
+        return result;\r
+      }\r
+      \r
+      @Override\r
+      public long getItemId(int position) {\r
+        return position;\r
+      }\r
+      \r
+      @Override\r
+      public Object getItem(int position) {\r
+        return dictionaryInfos.get(position);\r
+      }\r
+      \r
+      @Override\r
+      public int getCount() {\r
+        return dictionaryInfos.size();\r
+      }\r
+    });\r
+  }\r
+\r
+\r
+  private void changeIndex(final int newIndex) {\r
+    indexIndex = newIndex;\r
     index = dictionary.indices.get(indexIndex);\r
     indexAdapter = new IndexAdapter(index);\r
-    Log.d(LOG, "onLanguageButton, newLang=" + index.longName);\r
+    Log.d(LOG, "changingIndex, newLang=" + index.longName);\r
     setListAdapter(indexAdapter);\r
     updateLangButton();\r
+    searchText.requestFocus();  // Otherwise, nothing may happen.\r
     onSearchTextChange(searchText.getText().toString());\r
   }\r
   \r
@@ -402,6 +508,7 @@ public class DictionaryActivity extends ListActivity {
       final MenuItem preferences = menu.add(getString(R.string.preferences));\r
       preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
         public boolean onMenuItemClick(final MenuItem menuItem) {\r
+          PreferenceActivity.prefsMightHaveChanged = true;\r
           startActivity(new Intent(DictionaryActivity.this,\r
               PreferenceActivity.class));\r
           return false;\r
@@ -410,27 +517,16 @@ public class DictionaryActivity extends ListActivity {
     }\r
 \r
     {\r
-      final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryList));\r
+      final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));\r
       dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
         public boolean onMenuItemClick(final MenuItem menuItem) {\r
-          startActivity(DictionaryListActivity.getIntent(DictionaryActivity.this));\r
+          startActivity(DictionaryManagerActivity.getLaunchIntent());\r
           finish();\r
           return false;\r
         }\r
       });\r
     }\r
 \r
-    {\r
-      final MenuItem dictionaryEdit = menu.add(getString(R.string.editDictionary));\r
-      dictionaryEdit.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
-        public boolean onMenuItemClick(final MenuItem menuItem) {\r
-          final Intent intent = DictionaryEditActivity.getIntent(dictIndex);\r
-          startActivity(intent);\r
-          return false;\r
-        }\r
-      });\r
-    }\r
-\r
     {\r
       final MenuItem about = menu.add(getString(R.string.about));\r
       about.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
@@ -472,17 +568,36 @@ public class DictionaryActivity extends ListActivity {
         return false;\r
       }\r
     });\r
+    \r
+    if (selectedSpannableText != null) {\r
+      final String selectedText = selectedSpannableText;\r
+      final MenuItem searchForSelection = menu.add(getString(R.string.searchForSelection, selectedSpannableText));\r
+      searchForSelection.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+        public boolean onMenuItemClick(MenuItem item) {\r
+          if (indexIndex != selectedSpannableIndex) {\r
+            changeIndex(selectedSpannableIndex);\r
+          }\r
+          setSearchText(selectedText);\r
+          return false;\r
+        }\r
+      });\r
+    }\r
+    \r
 \r
   }\r
   \r
   @Override\r
   protected void onListItemClick(ListView l, View v, int row, long id) {\r
+    defocusSearchText();\r
+    \r
     if (clickOpensContextMenu && dictRaf != null) {\r
       openContextMenu(v);\r
     }\r
   }\r
   \r
   void onAppendToWordList(final RowBase row) {\r
+    defocusSearchText();\r
+    \r
     final StringBuilder rawText = new StringBuilder();\r
     rawText.append(\r
         new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date()))\r
@@ -492,9 +607,6 @@ public class DictionaryActivity extends ListActivity {
     rawText.append(row.getRawText(saveOnlyFirstSubentry));\r
     Log.d(LOG, "Writing : " + rawText);\r
 \r
-    // Request focus so that if we start typing again, it clears the text input.\r
-    getListView().requestFocus();\r
-\r
     try {\r
       wordList.getParentFile().mkdirs();\r
       final PrintWriter out = new PrintWriter(\r
@@ -507,10 +619,22 @@ public class DictionaryActivity extends ListActivity {
     }\r
     return;\r
   }\r
-\r
-  void onCopy(final RowBase row) {\r
+  \r
+  /**\r
+   * Called when user clicks outside of search text, so that they can start\r
+   * typing again immediately.\r
+   */\r
+  void defocusSearchText() {\r
+    //Log.d(LOG, "defocusSearchText");\r
     // Request focus so that if we start typing again, it clears the text input.\r
     getListView().requestFocus();\r
+    \r
+    // Visual indication that a new keystroke will clear the search text.\r
+    searchText.selectAll();\r
+  }\r
+\r
+  void onCopy(final RowBase row) {\r
+    defocusSearchText();\r
 \r
     Log.d(LOG, "Copy, row=" + row);\r
     final StringBuilder result = new StringBuilder();\r
@@ -524,9 +648,7 @@ public class DictionaryActivity extends ListActivity {
   public boolean onKeyDown(final int keyCode, final KeyEvent event) {\r
     if (event.getUnicodeChar() != 0) {\r
       if (!searchText.hasFocus()) {\r
-        searchText.setText("" + (char) event.getUnicodeChar());\r
-        onSearchTextChange(searchText.getText().toString());\r
-        searchText.requestFocus();\r
+        setSearchText("" + (char) event.getUnicodeChar());\r
       }\r
       return true;\r
     }\r
@@ -535,14 +657,21 @@ public class DictionaryActivity extends ListActivity {
       DictionaryActivity.clearDictionaryPrefs(this);\r
     }\r
     if (keyCode == KeyEvent.KEYCODE_ENTER) {\r
-//      Log.d(LOG, "Trying to hide soft keyboard.");\r
-//      final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
-//      manager.hideSoftInputFromWindow(searchText, InputMethodManager.SHOW_FORCED);\r
-\r
+      Log.d(LOG, "Trying to hide soft keyboard.");\r
+      final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
+      inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);\r
+      return true;\r
     }\r
     return super.onKeyDown(keyCode, event);\r
   }\r
 \r
+  private void setSearchText(final String text) {\r
+    searchText.setText(text);\r
+    searchText.requestFocus();\r
+    onSearchTextChange(searchText.getText().toString());\r
+    Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+  }\r
+\r
 \r
   // --------------------------------------------------------------------------\r
   // SearchOperation\r
@@ -665,51 +794,60 @@ public class DictionaryActivity extends ListActivity {
     }\r
 \r
     @Override\r
-    public View getView(int position, View convertView, ViewGroup parent) {\r
+    public View getView(int position, final View convertView, ViewGroup parent) {\r
       final RowBase row = index.rows.get(position);\r
       if (row instanceof PairEntry.Row) {\r
-        return getView((PairEntry.Row) row, parent);\r
+        return getView((PairEntry.Row) row, parent, convertView);\r
       } else if (row instanceof TokenRow) {\r
-        return getView((TokenRow) row, parent);\r
+        return getView((TokenRow) row, parent, convertView);\r
       } else {\r
         throw new IllegalArgumentException("Unsupported Row type: " + row.getClass());\r
       }\r
     }\r
 \r
-    private View getView(PairEntry.Row row, ViewGroup parent) {\r
+    private View getView(PairEntry.Row row, ViewGroup parent, final View convertView) {\r
       final TableLayout result = new TableLayout(parent.getContext());\r
       final PairEntry entry = row.getEntry();\r
       final int rowCount = entry.pairs.size();\r
       for (int r = 0; r < rowCount; ++r) {\r
         final TableRow tableRow = new TableRow(result.getContext());\r
 \r
-        TextView column1 = new TextView(tableRow.getContext());\r
-        TextView column2 = new TextView(tableRow.getContext());\r
+        final TextView col1 = new TextView(tableRow.getContext());\r
+        final TextView col2 = new TextView(tableRow.getContext());\r
         final TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();\r
         layoutParams.weight = 0.5f;\r
 \r
+        // Set the columns in the table.\r
         if (r > 0) {\r
-          final TextView spacer = new TextView(tableRow.getContext());\r
-          spacer.setText(" • ");\r
-          tableRow.addView(spacer);\r
+          final TextView bullet = new TextView(tableRow.getContext());\r
+          bullet.setText(" • ");\r
+          tableRow.addView(bullet);\r
         }\r
-        tableRow.addView(column1, layoutParams);\r
+        tableRow.addView(col1, layoutParams);\r
+        final TextView margin = new TextView(tableRow.getContext());\r
+        margin.setText(" ");\r
+        tableRow.addView(margin);\r
         if (r > 0) {\r
-          final TextView spacer = new TextView(tableRow.getContext());\r
-          spacer.setText(" • ");\r
-          tableRow.addView(spacer);\r
+          final TextView bullet = new TextView(tableRow.getContext());\r
+          bullet.setText(" • ");\r
+          tableRow.addView(bullet);\r
         }\r
-        tableRow.addView(column2, layoutParams);\r
-\r
-        column1.setWidth(1);\r
-        column2.setWidth(1);\r
+        tableRow.addView(col2, layoutParams);\r
+        col1.setWidth(1);\r
+        col2.setWidth(1);\r
+        \r
+        // Set what's in the columns.\r
 \r
         // TODO: color words by gender\r
         final Pair pair = entry.pairs.get(r);\r
         final String col1Text = index.swapPairEntries ? pair.lang2 : pair.lang1;\r
-        column1.setText(col1Text, TextView.BufferType.SPANNABLE);\r
-        final Spannable col1Spannable = (Spannable) column1.getText();\r
+        final String col2Text = index.swapPairEntries ? pair.lang1 : pair.lang2;\r
+        \r
+        col1.setText(col1Text, TextView.BufferType.SPANNABLE);\r
+        col2.setText(col2Text, TextView.BufferType.SPANNABLE);\r
         \r
+        // Bold the token instances in col1.\r
+        final Spannable col1Spannable = (Spannable) col1.getText();\r
         int startPos = 0;\r
         final String token = row.getTokenRow(true).getToken();\r
         while ((startPos = col1Text.indexOf(token, startPos)) != -1) {\r
@@ -717,28 +855,138 @@ public class DictionaryActivity extends ListActivity {
               startPos + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
           startPos += token.length();\r
         }\r
-\r
-        final String col2Text = index.swapPairEntries ? pair.lang1 : pair.lang2;\r
-        column2.setText(col2Text, TextView.BufferType.NORMAL);\r
         \r
-        column1.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
-        column2.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
-\r
+        createTokenLinkSpans(col1, col1Spannable, col1Text);\r
+        createTokenLinkSpans(col2, (Spannable) col2.getText(), col2Text);\r
+        \r
+        col1.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
+        col2.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
+        // col2.setBackgroundResource(theme.otherLangBg);\r
+        \r
+        if (index.swapPairEntries) {\r
+          col2.setOnLongClickListener(textViewLongClickListenerIndex0);\r
+          col1.setOnLongClickListener(textViewLongClickListenerIndex1);\r
+        } else {\r
+          col1.setOnLongClickListener(textViewLongClickListenerIndex0);\r
+          col2.setOnLongClickListener(textViewLongClickListenerIndex1);\r
+        }\r
+        \r
         result.addView(tableRow);\r
       }\r
 \r
       return result;\r
+\r
+      \r
+//      final WebView result = (WebView) (convertView instanceof WebView ? convertView : new WebView(parent.getContext()));\r
+//        \r
+//      final PairEntry entry = row.getEntry();\r
+//      final int rowCount = entry.pairs.size();\r
+//      final StringBuilder html = new StringBuilder();\r
+//      html.append("<html><body><table width=\"100%\">");\r
+//      for (int r = 0; r < rowCount; ++r) {\r
+//        html.append("<tr>");\r
+//\r
+//        final Pair pair = entry.pairs.get(r);\r
+//        // TODO: escape both the token and the text.\r
+//        final String token = row.getTokenRow(true).getToken();\r
+//        final String col1Text = index.swapPairEntries ? pair.lang2 : pair.lang1;\r
+//        final String col2Text = index.swapPairEntries ? pair.lang1 : pair.lang2;\r
+//        \r
+//        col1Text.replaceAll(token, String.format("<b>%s</b>", token));\r
+//\r
+//        // Column1\r
+//        html.append("<td width=\"50%\">");\r
+//        if (r > 0) {\r
+//          html.append("<li>");\r
+//        }\r
+//        html.append(col1Text);\r
+//        html.append("</td>");\r
+//\r
+//        // Column2\r
+//        html.append("<td width=\"50%\">");\r
+//        if (r > 0) {\r
+//          html.append("<li>");\r
+//        }\r
+//        html.append(col2Text);\r
+//        html.append("</td>");\r
+//\r
+////        column1.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
+////        column2.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);\r
+//\r
+//        html.append("</tr>");\r
+//      }\r
+//      html.append("</table></body></html>");\r
+//      \r
+//      Log.i(LOG, html.toString());\r
+//      \r
+//      result.getSettings().setRenderPriority(RenderPriority.HIGH);\r
+//      result.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);\r
+//      \r
+//      result.loadData("<html><body><table><tr><td>line (connected series of public conveyances, and hence, an established arrangement for forwarding merchandise, etc.) (noun)</td><td>verbinding</td></tr></table></body></html>", "text/html", "utf-8");\r
+//\r
+//      return result;\r
     }\r
 \r
-    private View getView(TokenRow row, ViewGroup parent) {\r
-      final TextView textView = new TextView(parent.getContext());\r
+    private View getView(TokenRow row, ViewGroup parent, final View convertView) {\r
+      final Context context = parent.getContext();\r
+      final TextView textView = new TextView(context);\r
       textView.setText(row.getToken());\r
+      textView.setBackgroundResource(row.hasMainEntry ? theme.tokenRowMainBg : theme.tokenRowOtherBg);\r
+      // Doesn't work:\r
+      //textView.setTextColor(android.R.color.secondary_text_light);\r
+      textView.setTextAppearance(context, theme.tokenRowFg);\r
       textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 5 * fontSizeSp / 4);\r
       return textView;\r
     }\r
     \r
   }\r
 \r
+  static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}0-9]+");\r
+\r
+  private void createTokenLinkSpans(final TextView textView, final Spannable spannable, final String text) {\r
+    // Saw from the source code that LinkMovementMethod sets the selection!\r
+    // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.1_r1/android/text/method/LinkMovementMethod.java#LinkMovementMethod\r
+    textView.setMovementMethod(LinkMovementMethod.getInstance());\r
+    final Matcher matcher = CHAR_DASH.matcher(text);\r
+    while (matcher.find()) {\r
+      spannable.setSpan(new NonLinkClickableSpan(), matcher.start(), matcher.end(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);\r
+    }\r
+  }\r
+  \r
+\r
+  String selectedSpannableText = null;\r
+  int selectedSpannableIndex = -1;\r
+\r
+  @Override\r
+  public boolean onTouchEvent(MotionEvent event) {\r
+    selectedSpannableText = null;\r
+    selectedSpannableIndex = -1;\r
+    return super.onTouchEvent(event);\r
+  }\r
+\r
+  private class TextViewLongClickListener implements OnLongClickListener {\r
+    final int index;\r
+    \r
+    private TextViewLongClickListener(final int index) {\r
+      this.index = index;\r
+    }\r
+\r
+    @Override\r
+    public boolean onLongClick(final View v) {\r
+      final TextView textView = (TextView) v;\r
+      final int start = textView.getSelectionStart();\r
+      final int end = textView.getSelectionEnd();\r
+      if (start >= 0 &&  end >= 0) {\r
+        selectedSpannableText = textView.getText().subSequence(start, end).toString();\r
+        selectedSpannableIndex = index;\r
+      }\r
+      return false;\r
+    }\r
+  }\r
+  final TextViewLongClickListener textViewLongClickListenerIndex0 = new TextViewLongClickListener(0);\r
+  final TextViewLongClickListener textViewLongClickListenerIndex1 = new TextViewLongClickListener(1);\r
+  \r
+\r
   // --------------------------------------------------------------------------\r
   // SearchText\r
   // --------------------------------------------------------------------------\r