r1842 - in trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config: . model
Author: tchemit Date: 2010-04-14 19:38:50 +0200 (Wed, 14 Apr 2010) New Revision: 1842 Log: - introduce CallBackFinalizer api - improve configUI design (add a handler) Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackFinalizer.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackMap.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/MainCallBackFinalizer.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCallBackUIHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigHelper.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBacksManager.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModel.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCallBackUIHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCallBackUIHandler.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCallBackUIHandler.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -29,10 +29,10 @@ import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.editor.config.model.CallBackEntry; +import jaxx.runtime.swing.editor.config.model.CallBackMap; import jaxx.runtime.swing.editor.config.model.OptionModel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import static org.nuiton.i18n.I18n._; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; @@ -42,6 +42,8 @@ import java.util.List; import java.util.Map; +import static org.nuiton.i18n.I18n._; + /** * callBackUI handler * @@ -50,21 +52,18 @@ */ public class ConfigCallBackUIHandler { - /** - * Logger - */ - private static final Log log = LogFactory.getLog(ConfigCallBackUIHandler.class); + /** Logger */ + private static final Log log = + LogFactory.getLog(ConfigCallBackUIHandler.class); public void init(ConfigCallBackUI ui) { // build tree model DefaultMutableTreeNode root = new DefaultMutableTreeNode(); - Map<CallBackEntry, List<OptionModel>> forSaved; + CallBackMap forSaved = ui.getContextValue( + CallBackMap.class, ConfigUIHandler.CALLBACKS_WITH_OPTIONS); - forSaved = ui.getContextValue(Map.class, - ConfigUIBuilder.CALLBACKS_WITH_OPTIONS); - for (Map.Entry<CallBackEntry, List<OptionModel>> e : forSaved.entrySet()) { @@ -181,7 +180,7 @@ List<CallBackEntry> callBacks; callBacks = ui.getContextValue( List.class, - ConfigUIBuilder.CALLBACKS); + ConfigUIHandler.CALLBACKS); for (CallBackEntry e : callBacks) { if (log.isInfoEnabled()) { log.info("launch callBack " + _(e.getDescription())); Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigHelper.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigHelper.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigHelper.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -3,10 +3,7 @@ import jaxx.runtime.JAXXContext; import jaxx.runtime.SwingUtil; import jaxx.runtime.context.JAXXInitialContext; -import jaxx.runtime.swing.editor.config.model.CategoryModel; -import jaxx.runtime.swing.editor.config.model.ConfigUIModel; -import jaxx.runtime.swing.editor.config.model.ConfigUIModelBuilder; -import jaxx.runtime.swing.editor.config.model.OptionModel; +import jaxx.runtime.swing.editor.config.model.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.util.ApplicationConfig; @@ -30,7 +27,7 @@ */ public class ConfigHelper { - public static final Log log = LogFactory.getLog(ConfigUIBuilder.class); + public static final Log log = LogFactory.getLog(ConfigHelper.class); /** internal model builder */ protected final ConfigUIModelBuilder modelBuilder; @@ -92,6 +89,10 @@ modelBuilder.setOption(optionModel); } + public void setFinalizer(CallBackFinalizer finalizer) { + modelBuilder.setFinalizer(finalizer); + } + /** * Construire l'ui de configuration (sous forme de panel) * @@ -146,6 +147,5 @@ f.setVisible(true); } }); - } } Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHandler.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHandler.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -1,10 +1,7 @@ package jaxx.runtime.swing.editor.config; import jaxx.runtime.context.JAXXInitialContext; -import jaxx.runtime.swing.editor.config.model.CallBackEntry; -import jaxx.runtime.swing.editor.config.model.CategoryModel; -import jaxx.runtime.swing.editor.config.model.ConfigUIModel; -import jaxx.runtime.swing.editor.config.model.OptionModel; +import jaxx.runtime.swing.editor.config.model.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -13,8 +10,6 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.util.ArrayList; -import java.util.List; -import java.util.Map; import static org.nuiton.i18n.I18n._; @@ -131,8 +126,7 @@ return; } - Map<CallBackEntry, List<OptionModel>> forSaved; - forSaved = model.getCallBacksForSaved(); + CallBackMap forSaved = model.getCallBacksForSaved(); if (forSaved.isEmpty()) { // just quit, no callBack to call Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackFinalizer.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackFinalizer.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackFinalizer.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -0,0 +1,18 @@ +package jaxx.runtime.swing.editor.config.model; + +/** + * Contract to finalize the callbacks to launch. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.2 + */ +public interface CallBackFinalizer { + + /** + * Do finalize the result. + * + * @param result unfinalize result + * @return the finalized result + */ + CallBackMap finalize(CallBackMap result); +} Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackFinalizer.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackMap.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackMap.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackMap.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -0,0 +1,34 @@ +package jaxx.runtime.swing.editor.config.model; + +import java.util.LinkedHashMap; +import java.util.List; + +/** + * A convient map of callback entry for a list of option. + * <p/> + * This model is used to store callbacks to use before a saving action. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.2 + */ +public class CallBackMap extends LinkedHashMap<CallBackEntry, List<OptionModel>> { + private static final long serialVersionUID = 1L; + + public boolean containsCallBack(String callBackName) { + for (CallBackEntry entry : keySet()) { + if (callBackName.equals(entry.getName())) { + return true; + } + } + return false; + } + + public CallBackEntry getCallBack(String callBackName) { + for (CallBackEntry entry : keySet()) { + if (callBackName.equals(entry.getName())) { + return entry; + } + } + return null; + } +} Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBackMap.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBacksManager.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBacksManager.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CallBacksManager.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -29,7 +29,6 @@ import javax.swing.*; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -41,16 +40,37 @@ */ public class CallBacksManager { - /** - * lists of registred callback. - */ + /** lists of registred callback. */ protected List<CallBackEntry> callbacks; + /** optional callbacks finalizer */ + protected CallBackFinalizer finalizer; + public CallBacksManager() { callbacks = new ArrayList<CallBackEntry>(); } /** + * Obtain the finalizer to use. + * + * @return the finalizer (can be {@code null}). + * @see CallBackFinalizer + */ + public CallBackFinalizer getFinalizer() { + return finalizer; + } + + /** + * Sets the finalizer. + * + * @param finalizer the new finalizer + * @see CallBackFinalizer + */ + public void setFinalizer(CallBackFinalizer finalizer) { + this.finalizer = finalizer; + } + + /** * Registers a new callback. * <p/> * <b>Note:</b> the order of registred callback is used to determine @@ -66,21 +86,23 @@ Icon icon, Runnable action) { if (name == null) { - throw new NullPointerException("parameter 'name' can not be null"); + throw new NullPointerException( + "parameter 'name' can not be null"); } if (action == null) { - throw new NullPointerException("parameter 'action' can not be null"); + throw new NullPointerException( + "parameter 'action' can not be null"); } if (description == null) { - throw new NullPointerException("parameter 'description' can " + - "not be null"); + throw new NullPointerException( + "parameter 'description' can not be null"); } if (icon == null) { throw new NullPointerException("parameter 'icon' can not be null"); } if (getCallBack(name) != null) { - throw new IllegalArgumentException("there is already a callback " + - "with name '" + name + "'"); + throw new IllegalArgumentException( + "there is already a callback with name '" + name + "'"); } callbacks.add(new CallBackEntry(name, description, icon, action)); } @@ -96,12 +118,13 @@ throw new NullPointerException("parameter 'name' can not be null"); } if (option == null) { - throw new NullPointerException("parameter 'option' can not be null"); + throw new NullPointerException( + "parameter 'option' can not be null"); } CallBackEntry callback = getCallBack(name); if (callback == null) { - throw new IllegalArgumentException("could not find a callback " + - "with name '" + name + "'"); + throw new IllegalArgumentException( + "could not find a callback with name '" + name + "'"); } callback.addOption(option); } @@ -112,11 +135,11 @@ * @param model the model to scan * @return the dictionnary of options for each callback to launch */ - public Map<CallBackEntry, List<OptionModel>> getCallBacksForSaved( + public CallBackMap getCallBacksForSaved( ConfigUIModel model) { - Map<CallBackEntry, List<OptionModel>> result; - result = new LinkedHashMap<CallBackEntry, List<OptionModel>>(); + CallBackMap result; + result = new CallBackMap(); for (CategoryModel categoryModel : model) { Map<CallBackEntry, List<OptionModel>> callBacks = @@ -133,6 +156,11 @@ } callBacks.clear(); } + + CallBackFinalizer finalizer = getFinalizer(); + if (finalizer != null) { + result = finalizer.finalize(result); + } return result; } @@ -145,8 +173,8 @@ public Map<CallBackEntry, List<OptionModel>> getCallBacksForSaved( CategoryModel category) { - Map<CallBackEntry, List<OptionModel>> result; - result = new LinkedHashMap<CallBackEntry, List<OptionModel>>(); + CallBackMap result; + result = new CallBackMap(); for (OptionModel optionModel : category) { if (optionModel.isSaved()) { Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModel.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModel.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModel.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -49,18 +49,16 @@ public class ConfigUIModel implements Iterable<CategoryModel> { public static final String CATEGORY_MODEL_PROPERTY_NAME = "categoryModel"; - /** - * le dictionnaire des options disponibles par categorie - */ + + /** le dictionnaire des options disponibles par categorie */ protected final Map<String, CategoryModel> categories; - /** - * La configuration de l'application - */ + + /** La configuration de l'application */ protected final ApplicationConfig config; - /** - * la cateogrie en cours d'utilisation - */ + + /** la cateogrie en cours d'utilisation */ protected CategoryModel categoryModel; + /** * un drapeau pour savoir si la configuration a été modifiée au moins une * fois. @@ -69,6 +67,7 @@ * redemarer l'application (si non en mode standalone) */ protected boolean saved; + /** * un drapeau pour savoir si l'ui de configuration a été lancée en mode * standalone ou pas. @@ -77,14 +76,11 @@ * s'il ne faut pas relancer l'application. */ protected boolean standalone; - /** - * Callbacks manager - */ + + /** Callbacks manager */ protected CallBacksManager callBacksManager; - /** - * suport of modification - */ + /** suport of modification */ protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); public ConfigUIModel(ApplicationConfig config) { @@ -102,7 +98,7 @@ if (categories.containsKey(category.getCategory())) { throw new IllegalArgumentException( _("config.error.category.already.exists", - category.getCategory())); + category.getCategory())); } categories.put(category.getCategory(), category); } @@ -155,12 +151,16 @@ callBacksManager.registerOption(name, option); } + public void setFinalizer(CallBackFinalizer finalizer) { + callBacksManager.setFinalizer(finalizer); + } + /** * Obtain the dictionnary of callback for all to saved modified options. - * + * * @return the dictonnary */ - public Map<CallBackEntry, List<OptionModel>> getCallBacksForSaved() { + public CallBackMap getCallBacksForSaved() { return callBacksManager.getCallBacksForSaved(this); } @@ -199,10 +199,7 @@ // compute transients keys (to never be saved) List<String> transients = new ArrayList<String>(); - //TODO TC-20091222 : will be in ApplicationConfig, for the moment - // catch in your config it... -// config.setAdjusting(true); - config.setOption("adjusting", "true"); + config.setAdjusting(true); try { for (OptionModel option : categoryModel) { @@ -213,18 +210,18 @@ // this is a javaBean option try { PropertyUtils.setProperty(config, - option.getPropertyName(), value); + option.getPropertyName(), value); } catch (Exception e) { throw new RuntimeException( "could not set property [" + - option.getPropertyName() + - "] with value = " + value, e); + option.getPropertyName() + + "] with value = " + value, e); } } else { // mutator could have extra code to be done when // modify an option config.setOption(option.getKey(), value == null ? - null : value.toString()); + null : value.toString()); } // l'option a été sauvegardée, on la marque option.setSaved(true); @@ -236,10 +233,7 @@ } } } finally { - //TODO TC-20091222 : will be in ApplicationConfig, - // for the moment catch in your config it... -// config.setAdjusting(false); - config.setOption("adjusting", "false"); + config.setAdjusting(false); } setSaved(true); Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java 2010-04-14 14:46:41 UTC (rev 1841) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -217,6 +217,10 @@ model.registerOptionCallBack(name, option); } + public void setFinalizer(CallBackFinalizer finalizer) { + model.setFinalizer(finalizer); + } + /** * Flush the model and return it. * <p/> Added: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/MainCallBackFinalizer.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/MainCallBackFinalizer.java (rev 0) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/MainCallBackFinalizer.java 2010-04-14 17:38:50 UTC (rev 1842) @@ -0,0 +1,41 @@ +package jaxx.runtime.swing.editor.config.model; + +import java.util.ArrayList; +import java.util.List; + +/** + * A finalizer to mark a special category to eat every others categories + * when finalizing. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.0.2 + */ +public class MainCallBackFinalizer implements CallBackFinalizer { + + /** name of call back which eats everybody */ + protected final String mainCallBack; + + public MainCallBackFinalizer(String mainCallBack) { + this.mainCallBack = mainCallBack; + } + + @Override + public CallBackMap finalize(CallBackMap result) { + CallBackEntry applicationEntry = result.getCallBack(mainCallBack); + CallBackMap newResult; + if (applicationEntry == null) { + // rien n'a change + newResult = result; + } else { + newResult = new CallBackMap(); + // on passe toutes les options sur ce callback + List<OptionModel> options = new ArrayList<OptionModel>(); + + for (List<OptionModel> optionModels : result.values()) { + options.addAll(optionModels); + } + newResult.put(applicationEntry, options); + } + return newResult; + } +} Property changes on: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/MainCallBackFinalizer.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL
participants (1)
-
tchemit@users.nuiton.org