]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryActivity.java
Arrows in UI to display HtmlEntry on Tokens.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryActivity.java
index 75c1905dce56ba0115ef8a60ba419a596d055a98..279451b1dc40b03065d3a93ad4c4896ad610caf9 100644 (file)
 
 package com.hughes.android.dictionary;
 
-import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
-import com.hughes.android.dictionary.engine.Dictionary;
-import com.hughes.android.dictionary.engine.EntrySource;
-import com.hughes.android.dictionary.engine.HtmlEntry;
-import com.hughes.android.dictionary.engine.Index;
-import com.hughes.android.dictionary.engine.Index.IndexEntry;
-import com.hughes.android.dictionary.engine.PairEntry;
-import com.hughes.android.dictionary.engine.PairEntry.Pair;
-import com.hughes.android.dictionary.engine.RowBase;
-import com.hughes.android.dictionary.engine.TokenRow;
-import com.hughes.android.dictionary.engine.TransliteratorManager;
-import com.hughes.android.util.IntentLauncher;
-import com.hughes.android.util.NonLinkClickableSpan;
-
 import android.app.Dialog;
 import android.app.ListActivity;
 import android.content.Context;
@@ -62,9 +48,11 @@ import android.view.WindowManager;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.AdapterView;
 import android.widget.AdapterView.AdapterContextMenuInfo;
+import android.widget.LinearLayout.LayoutParams;
 import android.widget.BaseAdapter;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ListAdapter;
@@ -74,6 +62,20 @@ import android.widget.TableRow;
 import android.widget.TextView;
 import android.widget.Toast;
 
+import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
+import com.hughes.android.dictionary.engine.Dictionary;
+import com.hughes.android.dictionary.engine.EntrySource;
+import com.hughes.android.dictionary.engine.HtmlEntry;
+import com.hughes.android.dictionary.engine.Index;
+import com.hughes.android.dictionary.engine.Index.IndexEntry;
+import com.hughes.android.dictionary.engine.PairEntry;
+import com.hughes.android.dictionary.engine.PairEntry.Pair;
+import com.hughes.android.dictionary.engine.RowBase;
+import com.hughes.android.dictionary.engine.TokenRow;
+import com.hughes.android.dictionary.engine.TransliteratorManager;
+import com.hughes.android.util.IntentLauncher;
+import com.hughes.android.util.NonLinkClickableSpan;
+
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -293,11 +295,6 @@ public class DictionaryActivity extends ListActivity {
 
         setContentView(R.layout.dictionary_activity);
         searchText = (EditText) findViewById(R.id.SearchText);
-        searchText.setTypeface(typeface);
-        searchText.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);
-
-        langButton = (Button) findViewById(R.id.LangButton);
-
         searchText.requestFocus();
         searchText.addTextChangedListener(searchTextWatcher);
 
@@ -312,17 +309,17 @@ public class DictionaryActivity extends ListActivity {
         setSearchText(text, true);
         Log.d(LOG, "Trying to restore searchText=" + text);
 
-        final Button clearSearchTextButton = (Button) findViewById(R.id.ClearSearchTextButton);
+        final View clearSearchTextButton = findViewById(R.id.ClearSearchTextButton);
         clearSearchTextButton.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
-                onClearSearchTextButton(clearSearchTextButton);
+                onClearSearchTextButton();
             }
         });
         clearSearchTextButton.setVisibility(PreferenceManager.getDefaultSharedPreferences(this)
                 .getBoolean(getString(R.string.showClearSearchTextButtonKey), true) ? View.VISIBLE
                 : View.GONE);
 
-        final Button langButton = (Button) findViewById(R.id.LangButton);
+        langButton = (Button) findViewById(R.id.LangButton);
         langButton.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 onLanguageButton();
@@ -337,13 +334,13 @@ public class DictionaryActivity extends ListActivity {
         });
         updateLangButton();
 
-        final Button upButton = (Button) findViewById(R.id.UpButton);
+        final View upButton = findViewById(R.id.UpButton);
         upButton.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 onUpDownButton(true);
             }
         });
-        final Button downButton = (Button) findViewById(R.id.DownButton);
+        final View downButton = findViewById(R.id.DownButton);
         downButton.setOnClickListener(new OnClickListener() {
             public void onClick(View v) {
                 onUpDownButton(false);
@@ -446,7 +443,7 @@ public class DictionaryActivity extends ListActivity {
     // Buttons
     // --------------------------------------------------------------------------
 
-    private void onClearSearchTextButton(final Button clearSearchTextButton) {
+    private void onClearSearchTextButton() {
         setSearchText("", true);
         Log.d(LOG, "Trying to show soft keyboard.");
         final InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
@@ -991,22 +988,40 @@ public class DictionaryActivity extends ListActivity {
 
     final class IndexAdapter extends BaseAdapter {
 
+        private static final float PADDING_DEFAULT_DP = 8;
+
+        private static final float PADDING_LARGE_DP = 16;
+
         final Index index;
 
         final List<RowBase> rows;
 
         final Set<String> toHighlight;
 
+        private int mPaddingDefault;
+
+        private int mPaddingLarge;
+
         IndexAdapter(final Index index) {
             this.index = index;
             rows = index.rows;
             this.toHighlight = null;
+            getMetrics();
         }
 
         IndexAdapter(final Index index, final List<RowBase> rows, final List<String> toHighlight) {
             this.index = index;
             this.rows = rows;
             this.toHighlight = new LinkedHashSet<String>(toHighlight);
+            getMetrics();
+        }
+
+        private void getMetrics() {
+            // Get the screen's density scale
+            final float scale = getResources().getDisplayMetrics().density;
+            // Convert the dps to pixels, based on density scale
+            mPaddingDefault = (int) (PADDING_DEFAULT_DP * scale + 0.5f);
+            mPaddingLarge = (int) (PADDING_LARGE_DP * scale + 0.5f);
         }
 
         @Override
@@ -1052,6 +1067,7 @@ public class DictionaryActivity extends ListActivity {
 
             final TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
             layoutParams.weight = 0.5f;
+            layoutParams.leftMargin = mPaddingLarge;
 
             for (int r = 0; r < rowCount; ++r) {
                 final TableRow tableRow = new TableRow(result.getContext());
@@ -1173,19 +1189,48 @@ public class DictionaryActivity extends ListActivity {
 
         private TableLayout getView(TokenRow row, ViewGroup parent, final TableLayout result) {
             final Context context = parent.getContext();
+            
+            final TableRow tableRow = new TableRow(result.getContext());
+            tableRow.setBackgroundResource(row.hasMainEntry ? theme.tokenRowMainBg
+                    : theme.tokenRowOtherBg);
+            tableRow.setPadding(mPaddingDefault, mPaddingDefault, mPaddingDefault, 0);
+            result.addView(tableRow);
+            
+            final IndexEntry indexEntry = row.getIndexEntry();
+
             final TextView textView = new TextView(context);
-            textView.setText(row.getToken());
+            textView.setText(indexEntry.token);
             // Doesn't work:
             // textView.setTextColor(android.R.color.secondary_text_light);
             textView.setTextAppearance(context, theme.tokenRowFg);
             textView.setTypeface(typeface);
             textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 5 * fontSizeSp / 4);
-
-            final TableRow tableRow = new TableRow(result.getContext());
+            TableRow.LayoutParams lp = new TableRow.LayoutParams(0);
+            lp.weight = 1.0f;
+            textView.setLayoutParams(lp);
             tableRow.addView(textView);
-            tableRow.setBackgroundResource(row.hasMainEntry ? theme.tokenRowMainBg
-                    : theme.tokenRowOtherBg);
-            result.addView(tableRow);
+
+            
+            if (!indexEntry.htmlEntries.isEmpty()) {
+                final ImageButton button = new ImageButton(context);
+                button.setImageResource(R.drawable.ic_menu_forward);
+                button.setOnClickListener(new OnClickListener() {
+                    @Override
+                    public void onClick(View v) {
+                        final String html = HtmlEntry.htmlBody(indexEntry.htmlEntries);
+                        startActivity(HtmlDisplayActivity.getHtmlIntent(String.format(
+                                "<html><head></head><body>%s</body></html>", html)));
+                    }
+                });
+                tableRow.addView(button);
+                lp = new TableRow.LayoutParams(1);
+                lp.weight = 0.0f;
+                button.setLayoutParams(lp);
+                //result.setColumnStretchable(0, true);
+                //result.setColumnStretchable(1, false);
+            }
+            
+            
             return result;
         }