Author: kmorin Date: 2014-01-28 18:58:49 +0100 (Tue, 28 Jan 2014) New Revision: 35 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/35 Log: fixes #4238 NAVIRE : proposer la recherche par l'immatriculation ou nom bref combiner les deux champs Modified: trunk/src/fr/ifremer/wlo/VesselFormActivity.java trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java trunk/src/fr/ifremer/wlo/models/VesselModel.java trunk/src/fr/ifremer/wlo/models/referentials/Vessel.java trunk/src/fr/ifremer/wlo/utils/BaseTextWatcher.java Modified: trunk/src/fr/ifremer/wlo/VesselFormActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/VesselFormActivity.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/VesselFormActivity.java 2014-01-28 17:58:49 UTC (rev 35) @@ -84,9 +84,7 @@ // init editors Collection<Vessel> vessels = DataCache.getAllVessels(this); - List<String> vesselCodes = Lists.newArrayList(Collections2.transform(vessels, HasCode.GET_CODE_FUNCTION)); - final Map<String, Vessel> vesselsByCode = Maps.uniqueIndex(vessels, HasCode.GET_CODE_FUNCTION); - initAutoCompleteTextView(R.id.vessel_form_registration_number, VesselModel.COLUMN_REGISTRATION_NUMBER, vesselCodes); + initAutoCompleteTextView(R.id.vessel_form_registration_number, VesselModel.COLUMN_REGISTRATION_NUMBER, vessels); initEditText(R.id.vessel_form_name, VesselModel.COLUMN_NAME); @@ -100,21 +98,6 @@ String dateFormat = UIUtils.getDateFormat(this); landingDateEditor.setText(String.format(dateFormat, landingDate.getTime())); } - - model.addPropertyChangeListener(VesselModel.COLUMN_REGISTRATION_NUMBER, new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent event) { - Log.d(TAG, "registration number changed ! " + event.getNewValue()); - VesselModel vesselModel = (VesselModel) event.getSource(); - Log.d(TAG, "name " + vesselModel.getName()); - if (StringUtils.isEmpty(vesselModel.getName())) { - String newCode = (String) event.getNewValue(); - Vessel vessel = vesselsByCode.get(newCode); - vesselModel.setName(vessel != null ? vessel.getName() : getString(R.string.undefined)); - } - } - }); - } /* Method called by the view */ Modified: trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-28 17:58:49 UTC (rev 35) @@ -28,6 +28,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.text.Editable; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; @@ -234,20 +235,16 @@ } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Log.e(TAG, "Error on set" + firtsLetterUpperCaseAttribute + " for class " + clazz, e); } - editText.setError(null); } - }); - model.addPropertyChangeListener(attribute, new PropertyChangeListener() { @Override - public void propertyChange(PropertyChangeEvent event) { - Object newValue = event.getNewValue(); - if (!editText.isFocused()) { - editText.setText(newValue != null ? newValue.toString() : getString(R.string.undefined)); - editText.setError(null); - } + public void afterTextChanged(Editable s) { + editText.setError(null); + editText.setSelection(s.length()); } }); + + addModelPropertyChangeListener(editText, attribute, true); } protected <R> AutoCompleteTextView initAutoCompleteTextView(int autoCompleteTextViewId, final String attribute, Collection<R> data) { @@ -272,8 +269,11 @@ try { R value = (R) clazz.getMethod("get" + firtsLetterUpperCaseAttribute).invoke(model); if (value != null) { - autoCompleteTextView.setText(value.toString()); + String text = value.toString(); + autoCompleteTextView.setText(text); + autoCompleteTextView.setSelection(text.length()); autoCompleteTextView.setError(null); + autoCompleteTextView.dismissDropDown(); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { @@ -284,6 +284,7 @@ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { R selectedData = (R) parent.getItemAtPosition(position); + Log.d(TAG, "selectedData.getClass() " + selectedData.getClass()); try { clazz.getMethod("set" + firtsLetterUpperCaseAttribute, selectedData.getClass()).invoke(model, selectedData); @@ -293,8 +294,23 @@ } }); + addModelPropertyChangeListener(autoCompleteTextView, attribute, false); } + protected void addModelPropertyChangeListener(final EditText editText, String property, final boolean checkFocus) { + model.addPropertyChangeListener(property, new PropertyChangeListener() { + @Override + public void propertyChange(PropertyChangeEvent event) { + if (!checkFocus || !editText.isFocused()) { + Object newValue = event.getNewValue(); + String text = newValue != null ? newValue.toString() : getString(fr.ifremer.wlo.R.string.undefined); + editText.setText(text); + editText.setSelection(text.length()); + } + } + }); + } + protected void openNewModelEdition() { Intent intent = new Intent(WloModelEditionActivity.this, WloModelEditionActivity.this.getClass()); if (HierarchicalModel.class.isAssignableFrom(model.getClass())) { Modified: trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/measurement/MeasurementActivity.java 2014-01-28 17:58:49 UTC (rev 35) @@ -3,8 +3,8 @@ /* * #%L * WLO - * $Id:$ - * $HeadURL:$ + * $Id$ + * $HeadURL$ * %% * Copyright (C) 2013 - 2014 Ifremer * %% @@ -31,6 +31,7 @@ import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; +import android.text.Editable; import android.text.InputType; import android.util.Log; import android.view.LayoutInflater; @@ -180,11 +181,6 @@ sizeText.setRawInputType(inputType); sizeText.addTextChangedListener(new BaseTextWatcher() { @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - Log.d(TAG, "beforetextchnage " + s); - } - - @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Mensuration.Precision precision = measurements.getPrecision(); int multiplier = precision.getUnitDivider(); @@ -200,6 +196,11 @@ measurement.setSize(null); } } + + @Override + public void afterTextChanged(Editable s) { + sizeText.setSelection(s.length()); + } }); Spinner genderSpinner = (Spinner) findViewById(R.id.gender_spinner); Modified: trunk/src/fr/ifremer/wlo/models/VesselModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/VesselModel.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/models/VesselModel.java 2014-01-28 17:58:49 UTC (rev 35) @@ -30,8 +30,10 @@ import android.util.Log; import com.google.common.collect.Sets; import fr.ifremer.wlo.models.referentials.Location; +import fr.ifremer.wlo.models.referentials.Vessel; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.utils.UIUtils; +import org.apache.commons.lang3.StringUtils; import java.util.Calendar; import java.util.Set; @@ -81,16 +83,28 @@ } public void setRegistrationNumber(String registrationNumber) { + Log.d(TAG, "setRegistrationNumber string " + registrationNumber); Object oldValue = this.registrationNumber; this.registrationNumber = registrationNumber; firePropertyChange(COLUMN_REGISTRATION_NUMBER, oldValue, registrationNumber); } + public void setRegistrationNumber(Vessel vessel) { + Log.d(TAG, "setRegistrationNumber vessel " + vessel.getCode()); + if (vessel != null) { + setRegistrationNumber(vessel.getCode()); + if (StringUtils.isEmpty(name)) { + setName(vessel.getName()); + } + } + } + public String getName() { return name; } public void setName(String name) { + Log.d(TAG, "setName " + name); Object oldValue = this.name; this.name = name; firePropertyChange(COLUMN_NAME, oldValue, name); Modified: trunk/src/fr/ifremer/wlo/models/referentials/Vessel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/referentials/Vessel.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/models/referentials/Vessel.java 2014-01-28 17:58:49 UTC (rev 35) @@ -25,6 +25,7 @@ */ import android.content.ContentValues; +import android.content.Context; import android.database.Cursor; import fr.ifremer.wlo.models.BaseModel; import fr.ifremer.wlo.utils.UIUtils; @@ -92,7 +93,12 @@ } @Override - public String toString(android.content.Context context) { + public String toString() { + return code + " - " + name; + } + + @Override + public String toString(Context context) { return UIUtils.getStringOrUndefined(code, context) + " - " + UIUtils.getStringOrUndefined(name, context); } Modified: trunk/src/fr/ifremer/wlo/utils/BaseTextWatcher.java =================================================================== --- trunk/src/fr/ifremer/wlo/utils/BaseTextWatcher.java 2014-01-28 16:18:29 UTC (rev 34) +++ trunk/src/fr/ifremer/wlo/utils/BaseTextWatcher.java 2014-01-28 17:58:49 UTC (rev 35) @@ -43,5 +43,6 @@ @Override public void afterTextChanged(Editable s) { + } }