Author: kmorin Date: 2014-01-23 12:29:55 +0100 (Thu, 23 Jan 2014) New Revision: 21 Url: http://forge.codelutin.com/projects/wlo/repository/revisions/21 Log: fixes #4175 Formulaire de cr?\195?\169ation d'un navire Modified: trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java trunk/src/fr/ifremer/wlo/models/BaseModel.java trunk/src/fr/ifremer/wlo/models/VesselModel.java Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-01-23 11:10:59 UTC (rev 20) +++ trunk/res/values/strings.xml 2014-01-23 11:29:55 UTC (rev 21) @@ -21,6 +21,7 @@ <string name="yes">Yes</string> <string name="no">No</string> <string name="required_field_error_message">Required field</string> + <string name="one_required_field_error_message">At least one these fields is required</string> <!-- BigFin communication service --> <string name="bigfin_no_ichtyometer_connected_title">No ichtyometer connected</string> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-01-23 11:10:59 UTC (rev 20) +++ trunk/res/values-fr/strings.xml 2014-01-23 11:29:55 UTC (rev 21) @@ -21,6 +21,7 @@ <string name="yes">Oui</string> <string name="no">Non</string> <string name="required_field_error_message">Champs obligatoire</string> + <string name="one_required_field_error_message">Au moins un de ces champs doit être saisi</string> <!-- BigFin communication service --> <string name="bigfin_no_ichtyometer_connected_title">Aucun ichtyomètre connecté</string> Modified: trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java =================================================================== --- trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-23 11:10:59 UTC (rev 20) +++ trunk/src/fr/ifremer/wlo/WloModelEditionActivity.java 2014-01-23 11:29:55 UTC (rev 21) @@ -138,8 +138,9 @@ } } else { + View root = findViewById(android.R.id.content); + Collection<String> requiredFields = errors.get(BaseModel.ErrorType.REQUIRED); - View root = findViewById(android.R.id.content); String errorMessage = getString(R.string.required_field_error_message); for (String field : requiredFields) { View v = root.findViewWithTag(field); @@ -147,6 +148,15 @@ ((EditText) v).setError(errorMessage); } } + + Collection<String> oneRequiredFields = errors.get(BaseModel.ErrorType.ONE_REQUIRED); + errorMessage = getString(R.string.one_required_field_error_message); + for (String field : oneRequiredFields) { + View v = root.findViewWithTag(field); + if (v != null && EditText.class.isAssignableFrom(v.getClass())) { + ((EditText) v).setError(errorMessage); + } + } } } @@ -191,6 +201,7 @@ } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Log.e(TAG, "Error on set" + firtsLetterUpperCaseAttribute + " for class " + clazz, e); } + editText.setError(null); } }); @@ -200,6 +211,7 @@ Object newValue = event.getNewValue(); if (!editText.isFocused()) { editText.setText(newValue != null ? newValue.toString() : getString(R.string.undefined)); + editText.setError(null); } } }); @@ -226,6 +238,7 @@ R value = (R) clazz.getMethod("get" + firtsLetterUpperCaseAttribute).invoke(model); if (value != null) { autoCompleteTextView.setText(value.toString()); + autoCompleteTextView.setError(null); } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { Modified: trunk/src/fr/ifremer/wlo/models/BaseModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-01-23 11:10:59 UTC (rev 20) +++ trunk/src/fr/ifremer/wlo/models/BaseModel.java 2014-01-23 11:29:55 UTC (rev 21) @@ -31,7 +31,8 @@ }; public enum ErrorType { - REQUIRED + REQUIRED, + ONE_REQUIRED } protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(this); @@ -81,20 +82,47 @@ } /** + * @return A set of sets containing fields. At least one of these fields must have a value. + */ + public Set<Set<String>> getOneRequiredFields() { + return Sets.newHashSet(); + } + + /** * Check if the model is valid * @return a map of the fields in error by error type */ public Multimap<ErrorType, String> checkValidity() { Multimap<ErrorType, String> result = HashMultimap.create(); + // check required for (String requiredField : getRequiredFields()) { try { if (getClass().getDeclaredField(requiredField).get(this) == null) { result.put(ErrorType.REQUIRED, requiredField); } } catch (Exception e) { - Log.e(TAG, "Error while accessing the filed " + requiredField, e); + Log.e(TAG, "Error while accessing the field " + requiredField, e); } } + + // check one required + for (Set<String> oneRequiredFields : getOneRequiredFields()) { + boolean notNull = false; + for (String oneRequiredField : oneRequiredFields) { + try { + if (getClass().getDeclaredField(oneRequiredField).get(this) != null) { + notNull = true; + break; + } + } catch (Exception e) { + Log.e(TAG, "Error while accessing the field " + oneRequiredField, e); + } + } + if (!notNull) { + result.putAll(ErrorType.ONE_REQUIRED, oneRequiredFields); + } + } + return result; } Modified: trunk/src/fr/ifremer/wlo/models/VesselModel.java =================================================================== --- trunk/src/fr/ifremer/wlo/models/VesselModel.java 2014-01-23 11:10:59 UTC (rev 20) +++ trunk/src/fr/ifremer/wlo/models/VesselModel.java 2014-01-23 11:29:55 UTC (rev 21) @@ -4,11 +4,13 @@ import android.content.Context; import android.database.Cursor; import android.util.Log; +import com.google.common.collect.Sets; import fr.ifremer.wlo.models.referentials.Location; import fr.ifremer.wlo.storage.DataCache; import fr.ifremer.wlo.utils.UIUtils; import java.util.Calendar; +import java.util.Set; /** * @author kmorin <kmorin@codelutin.com> @@ -107,4 +109,12 @@ putValue(value, COLUMN_LOCATION_ID, getParentId()); return value; } + + @Override + public Set<Set<String>> getOneRequiredFields() { + Set<Set<String>> result = super.getOneRequiredFields(); + Set<String> oneRequired = Sets.newHashSet(COLUMN_REGISTRATION_NUMBER, COLUMN_NAME); + result.add(oneRequired); + return result; + } }