Author: tchemit Date: 2012-10-10 23:15:58 +0200 (Wed, 10 Oct 2012) New Revision: 2504 Url: http://nuiton.org/repositories/revision/jaxx/2504 Log: fixed #2344: Improve ConfigUIModelBuilder api Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHelper.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/ConfigUIHelper.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHelper.java 2012-10-10 20:26:05 UTC (rev 2503) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHelper.java 2012-10-10 21:15:58 UTC (rev 2504) @@ -25,6 +25,7 @@ */ package jaxx.runtime.swing.editor.config; +import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import jaxx.runtime.JAXXContext; import jaxx.runtime.context.JAXXInitialContext; @@ -78,62 +79,80 @@ return model; } - public void addCategory(String categoryName, String categoryLabel) + public ConfigUIModelBuilder addCategory(String categoryName, String categoryLabel) throws IllegalStateException, NullPointerException { modelBuilder.addCategory(categoryName, categoryLabel); + return modelBuilder; } - public void addOption(ApplicationConfig.OptionDef def) + public ConfigUIModelBuilder addCategory(String categoryName, String categoryLabel, String categoryCallback) throws IllegalStateException, NullPointerException { + modelBuilder.addCategory(categoryName, categoryLabel, categoryCallback); + return modelBuilder; + } + + public ConfigUIModelBuilder addOption(ApplicationConfig.OptionDef def) + throws IllegalStateException, NullPointerException { modelBuilder.addOption(def); + return modelBuilder; } - public void addOption(ApplicationConfig.OptionDef def, String propertyName) + public ConfigUIModelBuilder addOption(ApplicationConfig.OptionDef def, String propertyName) throws IllegalStateException, NullPointerException { modelBuilder.addOption(def, propertyName); + return modelBuilder; } - public void setOptionPropertyName(String propertyName) + public ConfigUIModelBuilder setOptionPropertyName(String propertyName) throws IllegalStateException, NullPointerException { modelBuilder.setOptionPropertyName(propertyName); + return modelBuilder; } - public void setOptionEditor(TableCellEditor editor) + public ConfigUIModelBuilder setOptionEditor(TableCellEditor editor) throws IllegalStateException, NullPointerException { modelBuilder.setOptionEditor(editor); + return modelBuilder; } - public void registerCallBack(String name, - String description, - Icon icon, - Runnable action) { + public ConfigUIModelBuilder registerCallBack(String name, + String description, + Icon icon, + Runnable action) { modelBuilder.registerCallBack(name, description, icon, action); + return modelBuilder; } - public void setOptionCallBack(String name) { + public ConfigUIModelBuilder setOptionCallBack(String name) { modelBuilder.setOptionCallBack(name); + return modelBuilder; } - public void setModel(ConfigUIModel model) throws IllegalStateException { + public ConfigUIModelBuilder setModel(ConfigUIModel model) throws IllegalStateException { modelBuilder.setModel(model); + return modelBuilder; } - public void setCategory(CategoryModel categoryModel) + public ConfigUIModelBuilder setCategory(CategoryModel categoryModel) throws IllegalStateException { modelBuilder.setCategory(categoryModel); + return modelBuilder; } - public void setOption(OptionModel optionModel) + public ConfigUIModelBuilder setOption(OptionModel optionModel) throws IllegalStateException { modelBuilder.setOption(optionModel); + return modelBuilder; } - public void setFinalizer(CallBackFinalizer finalizer) { + public ConfigUIModelBuilder setFinalizer(CallBackFinalizer finalizer) { modelBuilder.setFinalizer(finalizer); + return modelBuilder; } - public void setCloseAction(Runnable runnable) { + public ConfigUIModelBuilder setCloseAction(Runnable runnable) { modelBuilder.setCloseAction(runnable); + return modelBuilder; } /** @@ -158,6 +177,7 @@ } public void displayUI(Frame parentUI, boolean undecorated) { + Preconditions.checkNotNull(ui, "UI was not build, use before the *buildUI* method"); ui.getHandler().displayUI(parentUI, undecorated); } } 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 2012-10-10 20:26:05 UTC (rev 2503) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java 2012-10-10 21:15:58 UTC (rev 2504) @@ -24,6 +24,7 @@ */ package jaxx.runtime.swing.editor.config.model; +import com.google.common.base.Preconditions; import com.google.common.base.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -51,6 +52,13 @@ /** current category used */ CategoryModel category; + /** + * Current default callback used by a category. + * + * @since 2.5.6 + */ + String categoryDefaultCallBack; + /** current option used */ OptionModel option; @@ -58,10 +66,11 @@ * Create a new model and set it as current model. * * @param config the configuration used in model + * @return the builder * @throws IllegalStateException if there is already a current model * @throws NullPointerException if config is {@code null} */ - public void createModel(Supplier<ApplicationConfig> config) + public ConfigUIModelBuilder createModel(Supplier<ApplicationConfig> config) throws IllegalStateException, NullPointerException { checkNoCurrent(model, "model"); checkNotNull(config, "createModel", "config"); @@ -69,16 +78,18 @@ if (log.isDebugEnabled()) { log.debug("model created : " + model); } + return this; } /** * Create a new model and set it as current model. * * @param config the configuration used in model + * @return the builder * @throws IllegalStateException if there is already a current model * @throws NullPointerException if config is {@code null} */ - public void createModel(ApplicationConfig config) + public ConfigUIModelBuilder createModel(ApplicationConfig config) throws IllegalStateException, NullPointerException { checkNoCurrent(model, "model"); checkNotNull(config, "createModel", "config"); @@ -86,6 +97,7 @@ if (log.isDebugEnabled()) { log.debug("model created : " + model); } + return this; } /** @@ -98,11 +110,12 @@ * (can not to be {@code null}) * @param categoryLabel the label of the new category * (can not to be {@code null}) + * @return the builder * @throws IllegalStateException if there is not a current model, * nor category * @throws NullPointerException if any of parameter is {@code null} */ - public void addCategory(String categoryName, String categoryLabel) + public ConfigUIModelBuilder addCategory(String categoryName, String categoryLabel) throws IllegalStateException, NullPointerException { checkCurrent(model, "model"); checkNotNull(categoryName, "addCategory", "categoryName"); @@ -112,20 +125,51 @@ if (log.isDebugEnabled()) { log.debug("category created : " + category); } + return this; } /** + * Add a new category, and set it as current. + * <p/> + * <b>Note:</b> As side effets, if a previous category, then store it to + * the model. + * + * @param categoryName the name of the new category + * (can not to be {@code null}) + * @param categoryLabel the label of the new category + * (can not to be {@code null}) + * @return the builder + * @throws IllegalStateException if there is not a current model, + * nor category + * @throws NullPointerException if any of parameter is {@code null} + */ + public ConfigUIModelBuilder addCategory(String categoryName, String categoryLabel, String defaultCallback) + throws IllegalStateException, NullPointerException { + checkCurrent(model, "model"); + checkNotNull(categoryName, "addCategory", "categoryName"); + checkNotNull(categoryLabel, "addCategory", "categoryLabel"); + flushCategory(); + categoryDefaultCallBack = defaultCallback; + category = new CategoryModel(categoryName, categoryLabel); + if (log.isDebugEnabled()) { + log.debug("category created : " + category); + } + return this; + } + + /** * Add a new option, and set it as current. * <p/> * <b>Note:</b> As side effets, if a previous option, then store it to * the model. * * @param def the def ot the new option + * @return the builder * @throws IllegalStateException if there is not a current model, * nor category * @throws NullPointerException if any of parameter is {@code null} */ - public void addOption(ApplicationConfig.OptionDef def) + public ConfigUIModelBuilder addOption(ApplicationConfig.OptionDef def) throws IllegalStateException, NullPointerException { checkCurrent(model, "model"); checkCurrent(category, "category"); @@ -133,9 +177,15 @@ flushOption(); Object value = model.getApplicationConfig().getOption(def); option = new OptionModel(def, value); + if (categoryDefaultCallBack != null) { + + // use default callback + setOptionCallBack(categoryDefaultCallBack); + } if (log.isDebugEnabled()) { log.debug("option created : " + option); } + return this; } /** @@ -150,45 +200,52 @@ * * @param def the def ot the new option * @param propertyName the propertyName to set on the option + * @return the builder * @throws IllegalStateException if there is not a current model, nor * category * @throws NullPointerException if any of parameter is {@code null} */ - public void addOption(ApplicationConfig.OptionDef def, String propertyName) + public ConfigUIModelBuilder addOption(ApplicationConfig.OptionDef def, + String propertyName) throws IllegalStateException, NullPointerException { addOption(def); checkNotNull(propertyName, "setOptionPropertyName", "propertyName"); option.setPropertyName(propertyName); + return this; } /** * Set the propertyName on the current option. * * @param propertyName the propertyName to set in the current option. + * @return the builder * @throws IllegalStateException if there is not a current option set. * @throws NullPointerException if any of parameter is {@code null} * @see OptionModel#setPropertyName(String) */ - public void setOptionPropertyName(String propertyName) + public ConfigUIModelBuilder setOptionPropertyName(String propertyName) throws IllegalStateException, NullPointerException { checkCurrent(option, "option"); checkNotNull(propertyName, "setOptionPropertyName", "propertyName"); option.setPropertyName(propertyName); + return this; } /** * Set the editor on the current option. * * @param editor the editor to set in the current option. + * @return the builder * @throws IllegalStateException if there is not a current option set. * @throws NullPointerException if any of parameter is {@code null} * @see OptionModel#setEditor(TableCellEditor) */ - public void setOptionEditor(TableCellEditor editor) + public ConfigUIModelBuilder setOptionEditor(TableCellEditor editor) throws IllegalStateException, NullPointerException { checkCurrent(option, "option"); checkNotNull(editor, "setOptionEditor", "editor"); option.setEditor(editor); + return this; } /** @@ -201,34 +258,68 @@ * @param description the i18n key to describe the action * @param icon the icon of the callBack (used in ui) * @param action the action of the callback + * @return the builder */ - public void registerCallBack(String name, - String description, - Icon icon, - Runnable action) { + public ConfigUIModelBuilder registerCallBack(String name, + String description, + Icon icon, + Runnable action) { checkCurrent(model, "model"); checkNotNull(name, "registerCallBack", "name"); checkNotNull(description, "registerCallBack", "description"); checkNotNull(action, "registerCallBack", "action"); model.registerCallBack(name, description, icon, action); + return this; } /** * Registers the current option into a known callback. * * @param name the name of the callback + * @return the builder */ - public void setOptionCallBack(String name) { + public ConfigUIModelBuilder setOptionCallBack(String name) { checkCurrent(option, "option"); checkNotNull(name, "setOptionCallBack", "name"); + Preconditions.checkArgument( + categoryDefaultCallBack == null, + "You can not use the method *setOptionCallBack* when " + + "a default callback has been assigned to a category"); + model.registerOptionCallBack(name, option); + return this; } - public void setFinalizer(CallBackFinalizer finalizer) { + /** + * Sets the callback finalizet + * + * @param finalizer callback finalize + * @return the builder + * @see CallBackFinalizer + */ + public ConfigUIModelBuilder setFinalizer(CallBackFinalizer finalizer) { model.setFinalizer(finalizer); + return this; } /** + * Sets the default callback to use for this category (the call back + * will be add to all options of this category until you set another one or + * nullify it). + * + * @param categoryDefaultCallBack + * @return the buider + * @since 2.5.6 + */ + public ConfigUIModelBuilder setCategoryDefaultCallBack(String categoryDefaultCallBack) { + checkCurrent(model, "model"); + checkCurrent(category, "category"); + this.categoryDefaultCallBack = categoryDefaultCallBack; + return this; + } + + + /** * Flush the model and return it. * <p/> * <b>Note:</b> As a side effect, nothing is available in the builder @@ -252,16 +343,19 @@ * <b>Note:</b> As side effets, il will clean current category and option. * * @param model the model to use + * @return the buider * @throws IllegalStateException if there is already a current model */ - public void setModel(ConfigUIModel model) throws IllegalStateException { + public ConfigUIModelBuilder setModel(ConfigUIModel model) throws IllegalStateException { checkNoCurrent(model, "model"); this.model = model; if (log.isDebugEnabled()) { log.debug("new current model : " + this.model); } category = null; + categoryDefaultCallBack = null; option = null; + return this; } /** @@ -270,28 +364,32 @@ * <b>Note:</b> As side effets, il will clean current option. * * @param categoryModel the category to use + * @return the buider * @throws IllegalStateException if there is not a current model or a * current category */ - public void setCategory(CategoryModel categoryModel) + public ConfigUIModelBuilder setCategory(CategoryModel categoryModel) throws IllegalStateException { checkCurrent(model, "model"); checkNoCurrent(category, "category"); category = categoryModel; + categoryDefaultCallBack = null; if (log.isDebugEnabled()) { log.debug("new current category : " + category); } option = null; + return this; } /** * Sets the given option as current option. * * @param optionModel the option to use + * @return the buider * @throws IllegalStateException if there is not a current model, nor * category, or a current option */ - public void setOption(OptionModel optionModel) + public ConfigUIModelBuilder setOption(OptionModel optionModel) throws IllegalStateException { checkCurrent(model, "model"); checkCurrent(category, "category"); @@ -300,13 +398,14 @@ if (log.isDebugEnabled()) { log.debug("new current option : " + option); } + return this; } - public void setCloseAction(Runnable runnable) { + public ConfigUIModelBuilder setCloseAction(Runnable runnable) { checkNotNull(runnable, "setCloseAction", "runnable"); checkCurrent(model, "model"); model.setCloseAction(runnable); - + return this; } protected CategoryModel flushCategory() { @@ -316,6 +415,7 @@ // add the previous category to the model model.addCategory(category); category = null; + categoryDefaultCallBack = null; } return result; }