mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2024-12-30 10:26:58 +01:00
implement search through loc dialog
This commit is contained in:
parent
7e16d7abc5
commit
a351b2a5b9
6 changed files with 172 additions and 24 deletions
|
@ -22,15 +22,24 @@ package org.eehouse.android.xw4.loc;
|
|||
import android.app.ListActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.ListView;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import org.eehouse.android.xw4.DbgUtils;
|
||||
import org.eehouse.android.xw4.DelegateBase;
|
||||
import org.eehouse.android.xw4.R;
|
||||
import org.eehouse.android.xw4.Utils;
|
||||
|
||||
|
||||
public class LocDelegate extends DelegateBase {
|
||||
public class LocDelegate extends DelegateBase implements View.OnClickListener {
|
||||
|
||||
private ListActivity m_activity;
|
||||
private LocListAdapter m_adapter;
|
||||
private EditText m_searchField;
|
||||
private ImageButton m_searchButton;
|
||||
private LocSearcher m_searcher;
|
||||
private String m_curSearch;
|
||||
|
||||
protected LocDelegate( ListActivity activity, Bundle savedInstanceState )
|
||||
{
|
||||
|
@ -45,13 +54,38 @@ public class LocDelegate extends DelegateBase {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick( View view )
|
||||
{
|
||||
String newText = m_searchField.getText().toString();
|
||||
if ( null == m_curSearch || ! m_curSearch.equals( newText ) ) {
|
||||
m_curSearch = newText;
|
||||
m_searcher.start( newText ); // synchronous for now
|
||||
makeNewAdapter();
|
||||
}
|
||||
}
|
||||
|
||||
private void makeNewAdapter()
|
||||
{
|
||||
ListView listview = m_activity.getListView();
|
||||
m_adapter = new LocListAdapter( m_activity, listview, m_searcher );
|
||||
m_activity.setListAdapter( m_adapter );
|
||||
}
|
||||
|
||||
private void init( Bundle savedInstanceState )
|
||||
{
|
||||
m_activity.setContentView( R.layout.loc_main );
|
||||
|
||||
ListView listview = m_activity.getListView();
|
||||
m_adapter = new LocListAdapter( m_activity, listview );
|
||||
m_activity.setListAdapter( m_adapter );
|
||||
View root = Utils.getContentView( m_activity );
|
||||
m_searchButton = (ImageButton)root.findViewById( R.id.loc_search_button );
|
||||
m_searchButton.setOnClickListener( this );
|
||||
|
||||
m_searchField = (EditText)root.findViewById( R.id.loc_search_field );
|
||||
|
||||
LocSearcher.Pair[] pairs = LocUtils.makePairs( m_activity );
|
||||
m_searcher = new LocSearcher( pairs );
|
||||
|
||||
makeNewAdapter();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -42,17 +42,15 @@ public class LocIDs extends LocIDsData {
|
|||
|
||||
protected static int getID( String key )
|
||||
{
|
||||
return LocIDsData.s_map.get( key );
|
||||
int result = LocIDsData.NOT_FOUND;
|
||||
if ( null != key ) {
|
||||
result = LocIDsData.s_map.get( key );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected static int size()
|
||||
{
|
||||
return LocIDsData.s_map.size();
|
||||
}
|
||||
|
||||
protected static int get( String key )
|
||||
{
|
||||
return LocIDsData.s_map.get( key );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,17 +32,20 @@ public class LocListAdapter extends XWListAdapter {
|
|||
|
||||
private Context m_context;
|
||||
private ListView m_listView;
|
||||
private LocSearcher m_searcher;
|
||||
|
||||
protected LocListAdapter( Context context, ListView listView )
|
||||
protected LocListAdapter( Context context, ListView listView,
|
||||
LocSearcher searcher )
|
||||
{
|
||||
m_context = context;
|
||||
m_listView = listView;
|
||||
m_searcher = searcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount()
|
||||
{
|
||||
int count = LocIDs.size();
|
||||
int count = m_searcher.matchSize();
|
||||
DbgUtils.logf(" LocListAdapter.getCount() => %d", count );
|
||||
return count;
|
||||
}
|
||||
|
@ -50,7 +53,7 @@ public class LocListAdapter extends XWListAdapter {
|
|||
public View getView( int position, View convertView, ViewGroup parent )
|
||||
{
|
||||
DbgUtils.logf( "LocListAdapter.getView(position=%d)", position );
|
||||
String key = LocIDs.getNthKey( position );
|
||||
return LocListItem.create( m_context, key, position );
|
||||
LocSearcher.Pair pair = m_searcher.getNthMatch( position );
|
||||
return LocListItem.create( m_context, pair, position );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.eehouse.android.xw4.DbgUtils;
|
|||
public class LocListItem extends LinearLayout implements OnFocusChangeListener {
|
||||
|
||||
private Context m_context;
|
||||
private String m_key;
|
||||
private LocSearcher.Pair m_pair;
|
||||
private int m_position;
|
||||
private EditText m_edit;
|
||||
private String m_xlation;
|
||||
|
@ -58,7 +58,7 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener {
|
|||
|
||||
private void setEnglish()
|
||||
{
|
||||
int id = LocIDs.get( m_key );
|
||||
int id = LocIDs.getID( m_pair.getKey() );
|
||||
String str = m_context.getString( id );
|
||||
TextView tv = (TextView)findViewById( R.id.english_view );
|
||||
tv.setText( str );
|
||||
|
@ -67,8 +67,9 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener {
|
|||
|
||||
private void setXlated()
|
||||
{
|
||||
m_xlation = LocUtils.getXlation( m_context, m_key );
|
||||
m_xlation = LocUtils.getXlation( m_context, m_pair.getKey() );
|
||||
if ( null != m_xlation ) {
|
||||
m_pair.setXlation( m_xlation );
|
||||
m_edit.setText( m_xlation );
|
||||
}
|
||||
}
|
||||
|
@ -80,17 +81,19 @@ public class LocListItem extends LinearLayout implements OnFocusChangeListener {
|
|||
CharSequence txt = m_edit.getText();
|
||||
DbgUtils.logf( "view with text %s lost focus", txt );
|
||||
if ( ! txt.equals( m_xlation ) ) {
|
||||
LocUtils.setXlation( m_context, m_key, txt.toString() );
|
||||
LocUtils.setXlation( m_context, m_pair.getKey(),
|
||||
txt.toString() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected static LocListItem create( Context context, String key,
|
||||
protected static LocListItem create( Context context,
|
||||
LocSearcher.Pair pair,
|
||||
int position )
|
||||
{
|
||||
LocListItem result =
|
||||
(LocListItem)Utils.inflate( context, R.layout.loc_list_item );
|
||||
result.m_key = key;
|
||||
result.m_pair = pair;
|
||||
result.m_position = position;
|
||||
|
||||
result.setEnglish();
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
/* -*- compile-command: "find-and-ant.sh debug install"; -*- */
|
||||
/*
|
||||
* Copyright 2014 by Eric House (xwords@eehouse.org). All rights
|
||||
* reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
package org.eehouse.android.xw4.loc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.eehouse.android.xw4.DbgUtils;
|
||||
|
||||
public class LocSearcher {
|
||||
|
||||
public static class Pair {
|
||||
public Pair( String key, String eng, String xlation ) {
|
||||
m_key = key;
|
||||
m_english = eng;
|
||||
m_xlation = xlation;
|
||||
}
|
||||
|
||||
public boolean matches( String term )
|
||||
{
|
||||
return m_english.contains( term ) ||
|
||||
(null != m_xlation && m_xlation.contains( term ) );
|
||||
}
|
||||
|
||||
public void setXlation( String xlation )
|
||||
{
|
||||
m_xlation = xlation;
|
||||
}
|
||||
|
||||
public String getKey() { return m_key; }
|
||||
|
||||
private String m_key;
|
||||
private String m_english;
|
||||
private String m_xlation;
|
||||
}
|
||||
|
||||
private Pair[] m_pairs;
|
||||
private Pair[] m_matchingPairs;
|
||||
private String m_lastTerm;
|
||||
|
||||
public LocSearcher( Pair pairs[] )
|
||||
{
|
||||
m_pairs = m_matchingPairs = pairs;
|
||||
}
|
||||
|
||||
protected void start( String term )
|
||||
{
|
||||
if ( 0 == term.length() ) {
|
||||
m_matchingPairs = m_pairs;
|
||||
} else {
|
||||
Pair[] usePairs = null != m_lastTerm && term.contains(m_lastTerm)
|
||||
? m_matchingPairs : m_pairs;
|
||||
DbgUtils.logf( "start: searching %d pairs", usePairs.length );
|
||||
ArrayList<Pair> matchers = new ArrayList<Pair>();
|
||||
for ( Pair pair : usePairs ) {
|
||||
if ( pair.matches( term ) ) {
|
||||
matchers.add( pair );
|
||||
}
|
||||
}
|
||||
m_matchingPairs = matchers.toArray( new Pair[matchers.size()] );
|
||||
}
|
||||
m_lastTerm = term;
|
||||
}
|
||||
|
||||
protected Pair getNthMatch( int nn )
|
||||
{
|
||||
return m_matchingPairs[nn];
|
||||
}
|
||||
|
||||
protected int matchSize()
|
||||
{
|
||||
return m_matchingPairs.length;
|
||||
}
|
||||
|
||||
}
|
|
@ -28,6 +28,7 @@ import android.view.MenuItem.OnMenuItemClickListener;
|
|||
import android.view.MenuItem;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
|
@ -121,17 +122,34 @@ public class LocUtils {
|
|||
DBUtils.saveXlations( context, "te_ST", s_xlations );
|
||||
}
|
||||
|
||||
|
||||
protected static LocSearcher.Pair[] makePairs( Context context )
|
||||
{
|
||||
loadXlations( context );
|
||||
|
||||
HashMap<String,Integer> map = LocIDsData.s_map;
|
||||
int siz = map.size();
|
||||
LocSearcher.Pair[] result = new LocSearcher.Pair[siz];
|
||||
Iterator<String> iter = map.keySet().iterator();
|
||||
|
||||
for ( int ii = 0; iter.hasNext(); ++ii ) {
|
||||
String key = iter.next();
|
||||
String english = context.getString( map.get( key ) );
|
||||
String xlation = s_xlations.get( key );
|
||||
result[ii] = new LocSearcher.Pair( key, english, xlation );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void xlateMenu( final Activity activity, Menu menu,
|
||||
int depth )
|
||||
{
|
||||
int count = menu.size();
|
||||
DbgUtils.logf( "xlateMenu: menu has %d items", count );
|
||||
for ( int ii = 0; ii < count; ++ii ) {
|
||||
MenuItem item = menu.getItem( ii );
|
||||
CharSequence ts = item.getTitle();
|
||||
if ( null != ts ) {
|
||||
String title = ts.toString();
|
||||
DbgUtils.logf( "xlateMenu: %d; %s", ii, title );
|
||||
if ( title.startsWith( LOC_PREFIX ) ) {
|
||||
String asKey = title.substring( LOC_PREFIX.length() );
|
||||
int id = LocIDs.getID( asKey );
|
||||
|
|
Loading…
Reference in a new issue