Author: tchemit Date: 2010-04-14 13:11:16 +0200 (Wed, 14 Apr 2010) New Revision: 1826 Log: - Evolution #511: [ApplicationConfig] Add a internal state adjusting + a saveUserAction Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java Modified: trunk/src/main/java/org/nuiton/util/ApplicationConfig.java =================================================================== --- trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-04-14 09:26:43 UTC (rev 1825) +++ trunk/src/main/java/org/nuiton/util/ApplicationConfig.java 2010-04-14 11:11:16 UTC (rev 1826) @@ -35,6 +35,7 @@ import static org.nuiton.i18n.I18n._; +import java.beans.PropertyChangeEvent; import java.io.*; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -277,6 +278,13 @@ static final public String APP_NAME = "app.name"; /** + * Property name of {@link #adjusting} internal state. + * + * @since 1.3 + */ + static final public String ADJUSTING_PROPERTY = "adjusting"; + + /** * Configuration directory where config path in located. * * Use default system configuration if nothing is defined: @@ -347,11 +355,56 @@ /** TODO */ protected Map<Integer, List<Action>> actions = new HashMap<Integer, List<Action>>(); - + + /** + * Internal state to manage with masse operations on option and control + * listeners. + * <p/> + * for example, if you want to save options, using javaBeans technology, + * can add a listener to save each time the property is modified. + * <p/> + * Says now you have an algorithm to set new values in configuration using + * setters but you do NOt want to save each time, add in your saving action + * a test to detect if model is adjusting. + * + * @since 1.3 + * @see #saveUserAction + */ + private boolean adjusting; + /** suport of config modification. */ protected PropertyChangeSupport pcs = new PropertyChangeSupport(this); /** + * Action to save user configuration. + * <p/> + * Add it as a listener of the configuration for a given property. + * + * <b>Note:</b> Will not save if {@link #isAdjusting()} is {@code true}. + * + * @since 1.3 + */ + private final PropertyChangeListener saveUserAction = + new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (isAdjusting()) { + if (log.isDebugEnabled()) { + log.debug("Skip save while adjusting"); + } + return; + } + if (log.isDebugEnabled()) { + log.debug("Saving configuration fired by property [" + + evt.getPropertyName() + "] at " + + new java.util.Date()); + } + saveForUser(); + } + }; + + /** * Le contrat de marquage des options, on utilise cette interface pour * caracteriser une option de configuration. * @@ -695,6 +748,16 @@ return result; } + public boolean isAdjusting() { + return adjusting; + } + + public void setAdjusting(boolean adjusting) { + boolean oldvalue = this.adjusting; + this.adjusting = adjusting; + firePropertyChange(ADJUSTING_PROPERTY, oldvalue, adjusting); + } + protected String getConfigFileNameOption() { String optionName = CONFIG_FILE_NAME; if (getOption(APP_NAME) != null) { @@ -867,6 +930,11 @@ * @param value property value */ public void setOption(String key, String value) { + //TC-20100414 special treatment when using adjusting property (internal state) + if (key.equals(ADJUSTING_PROPERTY)) { + setAdjusting(Boolean.valueOf(value)); + return; + } if (inParseOptionPhase) { line.setProperty(key, value); } else { @@ -1152,6 +1220,34 @@ } /** + * Install the {@link #saveUserAction} on givne {@code properties}. + * + * @param properties properties on which insalls the saveUserAction + */ + protected void installSaveUserAction(String... properties) { + + // pass in adjusting state + setAdjusting(true); + + try { + // ajout de tous les listeners pour sauver la configuration + // lors de la modification des options de la configuration + for (String propertyKey : properties) { + // add a listener + if (log.isDebugEnabled()) { + log.debug("register saveUserAction on property [" + + propertyKey + ']'); + } + addPropertyChangeListener(propertyKey, saveUserAction); + } + } finally { + + // ok back to normal adjusting state + setAdjusting(false); + } + } + + /** * Get all set method on this object or super object. * * @return map with method name without set and in lower case as key, and @@ -1494,7 +1590,7 @@ if (!b) { // could not move... String message = String.format( - "could not move old configuration file %S to %s", + "could not move old configuration file %s to %s", oldHomeConfig, homeConfig );