r2479 - in trunk: jaxx-demo/src/main/java/jaxx/demo jaxx-demo/src/main/resources/i18n jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model
Author: tchemit Date: 2012-08-08 14:23:08 +0200 (Wed, 08 Aug 2012) New Revision: 2479 Url: http://nuiton.org/repositories/revision/jaxx/2479 Log: fixes #2237: The Class type in configuration is not properly saved fixes #2238: Improve ConfigUI to use configuration with no inheritanceon ApplicationConfig Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfig.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfigTest.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfig.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfigTest.java Removed: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilderTest.java trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyConfig.java Modified: trunk/jaxx-demo/src/main/java/jaxx/demo/DemoConfig.java trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUI.css trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUIHandler.java trunk/jaxx-demo/src/main/java/jaxx/demo/RunDemo.java trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_en_GB.properties trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_es_ES.properties trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_fr_FR.properties trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUIHandler.java trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigTableRenderer.java 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/CategoryModel.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-demo/src/main/java/jaxx/demo/DemoConfig.java =================================================================== --- trunk/jaxx-demo/src/main/java/jaxx/demo/DemoConfig.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/java/jaxx/demo/DemoConfig.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -25,20 +25,28 @@ package jaxx.demo; -import java.beans.PropertyChangeListener; -import java.io.IOException; -import java.io.InputStream; -import java.util.Locale; -import java.util.Properties; -import javax.swing.KeyStroke; +import com.google.common.base.Supplier; import jaxx.demo.feature.nav.NavDemo; import jaxx.runtime.JAXXUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.jdesktop.beans.AbstractBean; import org.nuiton.util.ApplicationConfig; +import org.nuiton.util.ArgumentsParserException; import org.nuiton.util.Version; import org.nuiton.util.VersionUtil; +import javax.swing.KeyStroke; +import java.awt.Color; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Date; +import java.util.Locale; +import java.util.Properties; + import static org.nuiton.i18n.I18n._; /** @@ -49,10 +57,10 @@ * @author tchemit <chemit@codelutin.com> * @since 1.7.2 */ -public class DemoConfig extends ApplicationConfig { +public class DemoConfig extends AbstractBean implements Supplier<ApplicationConfig> { /** Logger */ - static private Log log = LogFactory.getLog(DemoConfig.class); + private static final Log log = LogFactory.getLog(DemoConfig.class); /** * le fichier de configuration de l'application avec les informations sur @@ -66,16 +74,28 @@ public static final String PROPERTY_FONT_SIZE = "fontSize"; + public static final String PROPERTY_DEMO_COLOR = "demoColor"; + + public static final String PROPERTY_DEMO_CLASS = "demoClass"; + public static final String PROPERTY_LOG_LEVEL = "logLevel"; public static final String PROPERTY_LOG_PATTERN_LAYOUT = "logPatternLayout"; public static final String PROPERTY_KEY_OPEN_CONFIG = "keyOpenConfig"; - public DemoConfig() { + private final ApplicationConfig applicationConfig; - setConfigFileName(Option.CONFIG_FILE.defaultValue); + @Override + public ApplicationConfig get() { + return applicationConfig; + } + public DemoConfig(String... args) { + + applicationConfig = new ApplicationConfig(); + applicationConfig.setConfigFileName(Option.CONFIG_FILE.defaultValue); + // chargement de la configuration interne InputStream stream = @@ -90,79 +110,156 @@ if (log.isDebugEnabled()) { log.debug("install properties " + k + " : " + value); } - setDefaultOption(key, "" + value); + applicationConfig.setDefaultOption(key, "" + value); } } catch (IOException ex) { throw new RuntimeException(ex); } - for (Option o : Option.values()) { - setDefaultOption(o.key, o.defaultValue); - } + applicationConfig.loadDefaultOptions(Option.values()); // on supprime le stamp de snapshot s'il existe String sVersion = VersionUtil.removeSnapshot( - getOption("application.version")); + applicationConfig.getOption("application.version")); Version version = VersionUtil.valueOf(sVersion); - setDefaultOption("version", version.getVersion()); + applicationConfig.setDefaultOption("version", version.getVersion()); installSaveUserAction(PROPERTY_FULLSCREEN, PROPERTY_FONT_SIZE, - PROPERTY_LOCALE); + PROPERTY_LOCALE, + PROPERTY_DEMO_CLASS, + PROPERTY_DEMO_COLOR); + + try { + applicationConfig.parse(args); + } catch (ArgumentsParserException e) { + throw new IllegalStateException("Could not parse configuration",e); + } } + /** + * TODO Remove this when the method in ApplicationConfig will be public + * <p/> + * Action to save user configuration. + * <p/> + * Add it as a listener of the configuration for a given property. + * <p/> + * <b>Note:</b> Will not save if {@link ApplicationConfig#isAdjusting()} is {@code true}. + * + * @since 2.5.4 + */ + private final PropertyChangeListener saveUserAction = + new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (applicationConfig.isAdjusting()) { + if (log.isDebugEnabled()) { + log.debug("Skip save while adjusting"); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Saving configuration fired by property [" + + evt.getPropertyName() + "] at " + + new Date()); + } + saveForUser(); + } + } + }; + + /** + * TODO Remove this when the method in ApplicationConfig will be public + * <p/> + * 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 + applicationConfig.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 + applicationConfig.setAdjusting(false); + } + } + public String getCopyrightText() { return "Version " + getVersion() + " Codelutin @ 2008-2009"; } /** @return la version de l'application. */ public Version getVersion() { - Version option = getOption(Version.class, "version"); + Version option = applicationConfig.getOption(Version.class, "version"); return option; } public boolean isFullScreen() { - Boolean result = getOptionAsBoolean(Option.FULL_SCREEN.key); + Boolean result = applicationConfig.getOptionAsBoolean(Option.FULL_SCREEN.key); return result != null && result; } public Locale getLocale() { - Locale result = getOption(Locale.class, Option.LOCALE.key); + Locale result = applicationConfig.getOption(Locale.class, Option.LOCALE.key); return result; } public String getDemoPath() { - String result = getOption(Option.DEMO_PATH.key); + String result = applicationConfig.getOption(Option.DEMO_PATH.key); return result; } public Float getFontSize() { - Float result = getOption(Float.class, Option.FONT_SIZE.key); + Float result = applicationConfig.getOption(Float.class, Option.FONT_SIZE.key); return result; } + public Color getDemoColor() { + Color result = applicationConfig.getOptionAsColor(Option.DEMO_COLOR.key); + return result; + } + + public Class<?> getDemoClass() { + Class<?> result = applicationConfig.getOptionAsClass(Option.DEMO_CLASS.key); + return result; + } + public String getLogLevel() { - String level = getOption(Option.LOG_LEVEL.key); + String level = applicationConfig.getOption(Option.LOG_LEVEL.key); return level; } public String getLogPatternLayout() { - String result = getOption(Option.LOG_PATTERN_LAYOUT.key); + String result = applicationConfig.getOption(Option.LOG_PATTERN_LAYOUT.key); return result; } public KeyStroke getKeyOpenConfig() { - return getOptionAsKeyStroke(Option.KEY_OPEN_CONFIG.key); + return applicationConfig.getOptionAsKeyStroke(Option.KEY_OPEN_CONFIG.key); } public void setFullscreen(boolean fullscreen) { Object oldValue = null; - setOption(Option.FULL_SCREEN.key, fullscreen + ""); + applicationConfig.setOption(Option.FULL_SCREEN.key, fullscreen + ""); firePropertyChange(PROPERTY_FULLSCREEN, oldValue, fullscreen); } public void setLocale(Locale newLocale) { - setOption(Option.LOCALE.key, newLocale.toString()); + applicationConfig.setOption(Option.LOCALE.key, newLocale.toString()); firePropertyChange(PROPERTY_LOCALE, null, newLocale); } @@ -171,54 +268,69 @@ if (log.isDebugEnabled()) { log.debug("changing font-size to " + newFontSize); } - setOption(Option.FONT_SIZE.key, newFontSize.toString()); + applicationConfig.setOption(Option.FONT_SIZE.key, newFontSize.toString()); firePropertyChange(PROPERTY_FONT_SIZE, oldValue, newFontSize); } + public void setDemoColor(Color color) { + Color oldValue = getDemoColor(); + if (log.isDebugEnabled()) { + log.debug("changing demo-color to " + color); + } + applicationConfig.setOption(Option.DEMO_COLOR.key, color.toString()); + firePropertyChange(PROPERTY_DEMO_COLOR, oldValue, color); + } + + public void setDemoClass(Class<?> newClass) { + Class<?> oldValue = getDemoClass(); + if (log.isDebugEnabled()) { + log.debug("changing demo-class to " + newClass); + } + applicationConfig.setOption(Option.DEMO_CLASS.key, newClass.getName()); + firePropertyChange(PROPERTY_DEMO_CLASS, oldValue, newClass); + } + public void setLogLevel(String logLevel) { String oldValue = getLogLevel(); - setOption(Option.LOG_LEVEL.key, logLevel); + applicationConfig.setOption(Option.LOG_LEVEL.key, logLevel); firePropertyChange(PROPERTY_LOG_LEVEL, oldValue, logLevel); } public void setLogPatternLayout(String logPatternLayout) { String oldValue = getLogPatternLayout(); - setOption(Option.LOG_PATTERN_LAYOUT.key, logPatternLayout); + applicationConfig.setOption(Option.LOG_PATTERN_LAYOUT.key, logPatternLayout); firePropertyChange(PROPERTY_LOG_PATTERN_LAYOUT, oldValue, logPatternLayout); } public void setKeyOpenConfig(KeyStroke keyStroke) { KeyStroke oldValue = getKeyOpenConfig(); - setOption(Option.KEY_OPEN_CONFIG.key, keyStroke.toString()); + applicationConfig.setOption(Option.KEY_OPEN_CONFIG.key, keyStroke.toString()); firePropertyChange(PROPERTY_KEY_OPEN_CONFIG, oldValue, keyStroke); } - /** - * Save configuration, in user home directory using the - * {@link #getConfigFileName}. Default, env and commande line note saved - */ + public void saveForUser() { // shoudl we never save any conf ? - super.saveForUser(); + applicationConfig.saveForUser(); } public static final String[] DEFAULT_JAXX_PCS = { PROPERTY_FULLSCREEN, PROPERTY_LOCALE, PROPERTY_FONT_SIZE, - ADJUSTING_PROPERTY + ApplicationConfig.ADJUSTING_PROPERTY }; public void removeJaxxPropertyChangeListener() { PropertyChangeListener[] toRemove; toRemove = JAXXUtil.findJaxxPropertyChangeListener( DEFAULT_JAXX_PCS, - getPropertyChangeListeners()); + applicationConfig.getPropertyChangeListeners()); if (toRemove == null || toRemove.length == 0) { return; } if (log.isDebugEnabled()) { - log.debug("before remove : " + getPropertyChangeListeners().length); + log.debug("before remove : " + applicationConfig.getPropertyChangeListeners().length); log.debug("toRemove : " + toRemove.length); } for (PropertyChangeListener listener : toRemove) { @@ -229,14 +341,30 @@ } } + public URL getApplicationSiteUrl() { + return applicationConfig.getOptionAsURL("application.site.url"); + } + + public String getIconPath() { + return applicationConfig.getOption("application.icon.path"); + } + + public String getLicensePath() { + return applicationConfig.getOption("application.license.path"); + } + + public String getThirdParty() { + return applicationConfig.getOption("application.third-party.path"); + } + ////////////////////////////////////////////////// // Toutes les options disponibles ////////////////////////////////////////////////// - public enum Option implements OptionDef { + public enum Option implements ApplicationConfig.OptionDef { CONFIG_FILE( - CONFIG_FILE_NAME, + ApplicationConfig.CONFIG_FILE_NAME, _("jaxxdemo.config.configFileName.description"), "jaxxdemo", String.class, @@ -263,6 +391,21 @@ Float.class, false, false), + DEMO_COLOR( + "ui." + PROPERTY_DEMO_COLOR, + _("jaxxdemo.config.ui.demoColor"), + "#ffffff", + Color.class, + false, + false), + DEMO_CLASS( + "ui." + PROPERTY_DEMO_CLASS, + _("jaxxdemo.config.ui.demoClass"), + "java.io.File", + Class.class, + false, + false), + LOG_LEVEL( "ui." + PROPERTY_LOG_LEVEL, _("jaxxdemo.config.ui.logLevel"), Modified: trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUI.css =================================================================== --- trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUI.css 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUI.css 2012-08-08 12:23:08 UTC (rev 2479) @@ -37,7 +37,7 @@ #mainFrame { title:"JAXX Demo"; - iconImage:{SwingUtil.createIcon(getConfig().getOption("application.icon.path")).getImage()}; + iconImage:{SwingUtil.createIcon(getConfig().getIconPath()).getImage()}; undecorated:{getConfig().isFullScreen()}; } Modified: trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUIHandler.java =================================================================== --- trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUIHandler.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/java/jaxx/demo/DemoUIHandler.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -44,25 +44,6 @@ */ package jaxx.demo; -import java.awt.Component; -import java.awt.Desktop; -import java.awt.event.ActionEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.lang.reflect.Constructor; -import java.net.URL; -import java.util.Arrays; -import java.util.Locale; -import javax.swing.AbstractAction; -import javax.swing.InputMap; -import javax.swing.JComponent; -import javax.swing.JPanel; -import javax.swing.JTree; -import javax.swing.KeyStroke; -import javax.swing.SwingUtilities; -import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; -import javax.swing.tree.TreePath; import jaxx.demo.tree.DemoNode; import jaxx.demo.tree.DemoTreeHelper; import jaxx.runtime.JAXXContext; @@ -81,6 +62,26 @@ import org.nuiton.i18n.I18n; import org.nuiton.util.decorator.DecoratorProvider; +import javax.swing.AbstractAction; +import javax.swing.InputMap; +import javax.swing.JComponent; +import javax.swing.JPanel; +import javax.swing.JTree; +import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.TreePath; +import java.awt.Component; +import java.awt.Desktop; +import java.awt.event.ActionEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.lang.reflect.Constructor; +import java.net.URL; +import java.util.Arrays; +import java.util.Locale; + import static org.nuiton.i18n.I18n._; import static org.nuiton.i18n.I18n.n_; @@ -477,28 +478,36 @@ helper.addCategory(n_("jaxxdemo.config.category.other"), n_("jaxxdemo.config.category.other.description")); - helper.addOption(DemoConfig.Option.FULL_SCREEN); - helper.setOptionPropertyName(DemoConfig.PROPERTY_FULLSCREEN); + helper.addOption(DemoConfig.Option.FULL_SCREEN, + DemoConfig.PROPERTY_FULLSCREEN); helper.setOptionCallBack("ui"); - helper.addOption(DemoConfig.Option.FONT_SIZE); - helper.setOptionPropertyName(DemoConfig.PROPERTY_FONT_SIZE); - helper.setOptionCallBack("application"); + helper.addOption(DemoConfig.Option.FONT_SIZE, + DemoConfig.PROPERTY_FONT_SIZE); + helper.setOptionCallBack("ui"); - helper.addOption(DemoConfig.Option.LOCALE); - helper.setOptionPropertyName(DemoConfig.PROPERTY_LOCALE); + helper.addOption(DemoConfig.Option.LOCALE, + DemoConfig.PROPERTY_LOCALE); helper.setOptionCallBack("ui"); - helper.addOption(DemoConfig.Option.KEY_OPEN_CONFIG); - helper.setOptionPropertyName(DemoConfig.PROPERTY_KEY_OPEN_CONFIG); -// helper.setOptionCallBack("keymap"); + helper.addOption(DemoConfig.Option.DEMO_COLOR, + DemoConfig.PROPERTY_DEMO_COLOR); + helper.setOptionCallBack("ui"); - helper.addOption(DemoConfig.Option.LOG_LEVEL); - helper.setOptionPropertyName(DemoConfig.PROPERTY_LOG_LEVEL); + helper.addOption(DemoConfig.Option.DEMO_CLASS, + DemoConfig.PROPERTY_DEMO_CLASS); + helper.setOptionCallBack("ui"); + + helper.addOption(DemoConfig.Option.KEY_OPEN_CONFIG, + DemoConfig.PROPERTY_KEY_OPEN_CONFIG); + helper.setOptionCallBack("ui"); + + helper.addOption(DemoConfig.Option.LOG_LEVEL, + DemoConfig.PROPERTY_LOG_LEVEL); helper.setOptionCallBack("log"); - helper.addOption(DemoConfig.Option.LOG_PATTERN_LAYOUT); - helper.setOptionPropertyName(DemoConfig.PROPERTY_LOG_PATTERN_LAYOUT); + helper.addOption(DemoConfig.Option.LOG_PATTERN_LAYOUT, + DemoConfig.PROPERTY_LOG_PATTERN_LAYOUT); helper.setOptionCallBack("log"); helper.buildUI(context, "jaxxdemo.config.category.other"); @@ -531,7 +540,7 @@ DemoUI ui = getUI(context); DemoConfig config = ui.getConfig(); - URL siteURL = config.getOptionAsURL("application.site.url"); + URL siteURL = config.getApplicationSiteUrl(); log.info(_("jaxxdemo.message.goto.site", siteURL)); @@ -552,9 +561,9 @@ DemoConfig config = ui.getConfig(); - String iconPath = config.getOption("application.icon.path"); - String licensePath = config.getOption("application.license.path"); - String thirdPartyPath = config.getOption("application.third-party.path"); + String iconPath = config.getIconPath(); + String licensePath = config.getLicensePath(); + String thirdPartyPath = config.getThirdParty(); AboutPanel about = new AboutPanel(); about.setTitle(_("jaxxdemo.title.about")); Modified: trunk/jaxx-demo/src/main/java/jaxx/demo/RunDemo.java =================================================================== --- trunk/jaxx-demo/src/main/java/jaxx/demo/RunDemo.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/java/jaxx/demo/RunDemo.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -25,8 +25,6 @@ package jaxx.demo; -import java.util.Arrays; -import java.util.Date; import jaxx.demo.entities.DemoDecoratorProvider; import jaxx.demo.tree.DemoDataProvider; import jaxx.demo.tree.DemoTreeHelper; @@ -41,6 +39,9 @@ import org.nuiton.util.StringUtil; import org.nuiton.util.decorator.DecoratorProvider; +import java.util.Arrays; +import java.util.Date; + import static org.nuiton.i18n.I18n._; /** @author tchemit <chemit@codelutin.com> */ @@ -78,9 +79,9 @@ rootContext.setContextValue(new DemoTreeHelper(new DemoDataProvider())); // init config - DemoConfig config = new DemoConfig(); - config.parse(args); + DemoConfig config = new DemoConfig(args); + // share the config rootContext.setContextValue(config); Modified: trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_en_GB.properties =================================================================== --- trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_en_GB.properties 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_en_GB.properties 2012-08-08 12:23:08 UTC (rev 2479) @@ -194,6 +194,8 @@ jaxxdemo.config.category.other.description=Others preferences jaxxdemo.config.configFileName.description=Configuration file name jaxxdemo.config.ui.demo.path=Path of demo to select when starting application +jaxxdemo.config.ui.demoClass= +jaxxdemo.config.ui.demoColor= jaxxdemo.config.ui.fontSize=default font size to use in sources jaxxdemo.config.ui.fullscreen=To change the screen mode (true for full screen) jaxxdemo.config.ui.keyOpenConfig=To change key for open config ui Modified: trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_es_ES.properties =================================================================== --- trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_es_ES.properties 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_es_ES.properties 2012-08-08 12:23:08 UTC (rev 2479) @@ -197,6 +197,8 @@ jaxxdemo.config.category.other.description=Autres options jaxxdemo.config.configFileName.description=Le nom du fichier de configuration jaxxdemo.config.ui.demo.path=Chemin de la démo par défaut +jaxxdemo.config.ui.demoClass= +jaxxdemo.config.ui.demoColor= jaxxdemo.config.ui.fontSize=La taille de la police à utiliser pour visualiser dans les sources jaxxdemo.config.ui.fullscreen=Pour afficher l'aplication en mode pleine écran jaxxdemo.config.ui.keyOpenConfig=Pour changer le racourcie clavier pour ouvrir l'écran de configuration Modified: trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_fr_FR.properties =================================================================== --- trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_fr_FR.properties 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-demo/src/main/resources/i18n/jaxx-demo_fr_FR.properties 2012-08-08 12:23:08 UTC (rev 2479) @@ -194,6 +194,8 @@ jaxxdemo.config.category.other.description=Autres options jaxxdemo.config.configFileName.description=Le nom du fichier de configuration jaxxdemo.config.ui.demo.path=Chemin de la démo par défaut +jaxxdemo.config.ui.demoClass= +jaxxdemo.config.ui.demoColor= jaxxdemo.config.ui.fontSize=La taille de la police à utiliser pour visualiser dans les sources jaxxdemo.config.ui.fullscreen=Pour afficher l'aplication en mode pleine écran jaxxdemo.config.ui.keyOpenConfig=Pour changer le racourcie clavier pour ouvrir l'écran de configuration Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUIHandler.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUIHandler.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigCategoryUIHandler.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -26,6 +26,7 @@ import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.editor.config.model.ConfigTableModel; import jaxx.runtime.swing.editor.config.model.OptionModel; +import jaxx.runtime.swing.renderer.ClassTableCellRenderer; import jaxx.runtime.swing.renderer.ColorCellRenderer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -72,7 +73,6 @@ n_("config.defaultValue.tip")); ConfigTableRenderer renderer = new ConfigTableRenderer(); - ColorCellRenderer colorCellRenderer = new ColorCellRenderer(); SwingUtil.setTableColumnRenderer(table, 0, renderer); SwingUtil.setTableColumnRenderer(table, 1, renderer); SwingUtil.setTableColumnRenderer(table, 2, renderer); @@ -80,7 +80,8 @@ int width = SwingUtil.computeTableColumnWidth(table, f, 0, "___*"); SwingUtil.fixTableColumnWidth(table, 0, width); SwingUtil.setTableColumnEditor(table, 1, new ConfigTableEditor((ConfigTableModel) table.getModel())); - table.setDefaultRenderer(Color.class, colorCellRenderer); + table.setDefaultRenderer(Color.class, new ColorCellRenderer()); + table.setDefaultRenderer(Class.class, new ClassTableCellRenderer()); } public void updateDescriptionText() { Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigTableRenderer.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigTableRenderer.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigTableRenderer.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -43,7 +43,8 @@ * ##%* */ package jaxx.runtime.swing.editor.config; -import static org.nuiton.i18n.I18n._; +import jaxx.runtime.swing.editor.config.model.ConfigTableModel; +import jaxx.runtime.swing.editor.config.model.OptionModel; import javax.swing.JComponent; import javax.swing.JTable; @@ -52,9 +53,9 @@ import java.awt.Color; import java.awt.Component; import java.awt.Font; -import jaxx.runtime.swing.editor.config.model.ConfigTableModel; -import jaxx.runtime.swing.editor.config.model.OptionModel; +import static org.nuiton.i18n.I18n._; + /** * Pour le rendu du tableau des options d'une categorie * @@ -86,7 +87,7 @@ font = getFont(); font2 = font.deriveFont(Font.ITALIC | Font.BOLD); } - Component cellRenderer = null; + Component cellRenderer; switch (modelColumn) { case 0: cellRenderer = getKeyCellRenderer(table, value, isSelected, hasFocus, modelRow, modelColumn, key, isValid, isModified); 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-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/ConfigUIHelper.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -25,6 +25,7 @@ */ package jaxx.runtime.swing.editor.config; +import com.google.common.base.Supplier; import jaxx.runtime.JAXXContext; import jaxx.runtime.context.JAXXInitialContext; import jaxx.runtime.swing.editor.config.model.CallBackFinalizer; @@ -60,6 +61,11 @@ protected ConfigUI ui; + public ConfigUIHelper(Supplier<ApplicationConfig> config) { + modelBuilder = new ConfigUIModelBuilder(); + modelBuilder.createModel(config); + } + public ConfigUIHelper(ApplicationConfig config) { modelBuilder = new ConfigUIModelBuilder(); modelBuilder.createModel(config); Modified: trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CategoryModel.java =================================================================== --- trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CategoryModel.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/CategoryModel.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -76,7 +76,9 @@ } @Deprecated - public CategoryModel(String category, String categoryLabel, OptionModel[] entries) { + public CategoryModel(String category, + String categoryLabel, + OptionModel[] entries) { this.category = category; this.categoryLabel = categoryLabel; this.entries = Collections.unmodifiableList(Arrays.asList(entries)); @@ -116,6 +118,16 @@ return valid; } + public OptionModel getOptionModel(String optionModelKey) { + OptionModel result = null; + for (OptionModel optionModel : this) { + if (optionModelKey.endsWith(optionModel.getKey())) { + result = optionModel; + break; + } + } + return result; + } public void setValue(OptionModel key, Object val) { boolean wasModified = isModified(); boolean wasValid = isValid(); 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 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModel.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -25,6 +25,7 @@ package jaxx.runtime.swing.editor.config.model; +import com.google.common.base.Supplier; import jaxx.runtime.JAXXUtil; import org.apache.commons.beanutils.PropertyUtils; import org.nuiton.util.ApplicationConfig; @@ -55,8 +56,11 @@ protected final Map<String, CategoryModel> categories; /** La configuration de l'application */ - protected final ApplicationConfig config; + protected final Object configBean; + /** La configuration de l'application */ + protected final ApplicationConfig applicationConfig; + /** la cateogrie en cours d'utilisation */ protected CategoryModel categoryModel; @@ -83,6 +87,7 @@ /** * optional action to execute (if not null) if no call backs fits. + * * @since 1.4.2 */ protected Runnable closeAction; @@ -90,8 +95,17 @@ /** support of modification notifications */ protected final PropertyChangeSupport pcs; - public ConfigUIModel(ApplicationConfig config) { - this.config = config; + public ConfigUIModel(Supplier<ApplicationConfig> config) { + this(config, config.get()); + } + + public ConfigUIModel(ApplicationConfig applicationConfig) { + this(applicationConfig, applicationConfig); + } + + public ConfigUIModel(Object configBean, ApplicationConfig applicationConfig) { + this.configBean = configBean; + this.applicationConfig = applicationConfig; categories = new LinkedHashMap<String, CategoryModel>(); callBacksManager = new CallBacksManager(); pcs = new PropertyChangeSupport(this); @@ -168,7 +182,7 @@ } public void setCloseAction(Runnable closeAction) { - this.closeAction=closeAction; + this.closeAction = closeAction; } /** @@ -215,7 +229,7 @@ // compute transients keys (to never be saved) List<String> transients = new ArrayList<String>(); - config.setAdjusting(true); + applicationConfig.setAdjusting(true); try { for (OptionModel option : categoryModel) { @@ -225,7 +239,7 @@ // this is a javaBean option, push value via mutator try { - PropertyUtils.setProperty(config, + PropertyUtils.setProperty(configBean, option.getPropertyName(), value); } catch (Exception e) { throw new RuntimeException( @@ -237,8 +251,9 @@ // simple option with no javabeans, just push the option // value - config.setOption(option.getKey(), value == null ? - null : value.toString()); + applicationConfig.setOption(option.getKey(), + value == null ? + null : value.toString()); } // l'option a été sauvegardée, on la marque option.setSaved(true); @@ -250,12 +265,12 @@ } } } finally { - config.setAdjusting(false); + applicationConfig.setAdjusting(false); } setSaved(true); // save config - config.saveForUser(transients.toArray(new String[transients.size()])); + applicationConfig.saveForUser(transients.toArray(new String[transients.size()])); // notify data has changed categoryModel.firePropertyChange( CategoryModel.MODIFIED_PROPERTY_NAME, @@ -344,10 +359,22 @@ destroy(); } + /** + * @return the underlined application config + * @deprecated since 2.5.4 use now {@link #getApplicationConfig()}. + */ protected ApplicationConfig getConfig() { - return config; + return applicationConfig; } + /** + * @return the underlined application config + * @since 2.5.4 + */ + protected ApplicationConfig getApplicationConfig() { + return applicationConfig; + } + protected CallBacksManager getCallBacksManager() { return callBacksManager; } 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-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/main/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilder.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -24,6 +24,7 @@ */ package jaxx.runtime.swing.editor.config.model; +import com.google.common.base.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.util.ApplicationConfig; @@ -39,8 +40,10 @@ * @since 2.0.0 */ public class ConfigUIModelBuilder { + /** Logger */ - private static final Log log = LogFactory.getLog(ConfigUIModelBuilder.class); + private static final Log log = + LogFactory.getLog(ConfigUIModelBuilder.class); /** current model used */ ConfigUIModel model; @@ -58,6 +61,23 @@ * @throws IllegalStateException if there is already a current model * @throws NullPointerException if config is {@code null} */ + public void createModel(Supplier<ApplicationConfig> config) + throws IllegalStateException, NullPointerException { + checkNoCurrent(model, "model"); + checkNotNull(config, "createModel", "config"); + model = new ConfigUIModel(config); + if (log.isDebugEnabled()) { + log.debug("model created : " + model); + } + } + + /** + * Create a new model and set it as current model. + * + * @param config the configuration used in model + * @throws IllegalStateException if there is already a current model + * @throws NullPointerException if config is {@code null} + */ public void createModel(ApplicationConfig config) throws IllegalStateException, NullPointerException { checkNoCurrent(model, "model"); @@ -111,7 +131,7 @@ checkCurrent(category, "category"); checkNotNull(def, "addOption", "def"); flushOption(); - Object value = model.getConfig().getOption(def); + Object value = model.getApplicationConfig().getOption(def); option = new OptionModel(def, value); if (log.isDebugEnabled()) { log.debug("option created : " + option); Deleted: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilderTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilderTest.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/ConfigUIModelBuilderTest.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -1,298 +0,0 @@ -/* - * #%L - * JAXX :: Widgets - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package jaxx.runtime.swing.editor.config.model; - -import jaxx.runtime.SwingUtil; -import jaxx.runtime.swing.editor.MyDefaultCellEditor; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import javax.swing.*; -import javax.swing.table.TableCellEditor; - -/** - * Created: 22 déc. 2009 - * - * @author tchemit <chemit@codelutin.com> - * @since 2.0.0 - */ -public class ConfigUIModelBuilderTest { - - ConfigUIModelBuilder builder; - - static MyConfig config; - - @BeforeClass - public static void beforeeClass() { - config = new MyConfig(); - } - - - @Before - public void setup() { - builder = new ConfigUIModelBuilder(); - } - - @Test(expected = NullPointerException.class) - public void testCreateModelLimitCase0() throws Exception { - builder.createModel(null); - } - - @Test(expected = IllegalStateException.class) - public void testFlushModelLimitCase0() throws Exception { - builder.flushModel(); - } - - @Test - public void testCreateModel() throws Exception { - builder.createModel(config); - ConfigUIModel configModel = builder.flushModel(); - Assert.assertNotNull(configModel); - Assert.assertNotNull(configModel.getConfig()); - } - - @Test(expected = IllegalStateException.class) - public void testRegisterCallbackLimitCase() throws Exception { - builder.registerCallBack(null, null, null, null); - } - - @Test(expected = NullPointerException.class) - public void testRegisterCallbackLimitCase0() throws Exception { - builder.createModel(config); - builder.registerCallBack(null, null, null, null); - } - - @Test(expected = NullPointerException.class) - public void testRegisterCallbackLimitCase1() throws Exception { - builder.createModel(config); - builder.registerCallBack("yo", null, null, null); - } - - @Test(expected = NullPointerException.class) - public void testRegisterCallbackLimitCase2() throws Exception { - builder.createModel(config); - builder.registerCallBack("yo", "yo description", null, null); - } - - @Test(expected = NullPointerException.class) - public void testRegisterCallbackLimitCase4() throws Exception { - builder.createModel(config); - Runnable callback = new Runnable() { - - @Override - public void run() { - } - }; - builder.registerCallBack("yo", "yo description", null, callback); - } - - @Test - public void testRegisterCallback() throws Exception { - builder.createModel(config); - Runnable callback = new Runnable() { - - @Override - public void run() { - } - }; - ImageIcon icon = SwingUtil.createActionIcon("add"); - builder.registerCallBack("yo","yo description", icon, callback); - ConfigUIModel configModel = builder.flushModel(); - Assert.assertNotNull(configModel); - Assert.assertNotNull(configModel.getConfig()); - CallBackEntry callBackEntry = - configModel.getCallBacksManager().getCallBack("yo"); - Assert.assertNotNull(callBackEntry ); - - Assert.assertEquals(callback, callBackEntry.getAction()); - } - - @Test(expected = IllegalStateException.class) - public void testAddCategoryLimitCase0() throws Exception { - builder.addCategory(null, null); - } - - @Test(expected = NullPointerException.class) - public void testAddCategoryLimitCase1() throws Exception { - builder.createModel(config); - builder.addCategory(null, null); - } - - @Test(expected = NullPointerException.class) - public void testAddCategoryLimitCase2() throws Exception { - builder.createModel(config); - builder.addCategory("", null); - } - - @Test - public void testAddCategory() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - CategoryModel categoryModel = builder.flushCategory(); - Assert.assertNotNull(categoryModel); - Assert.assertEquals("cat0", categoryModel.category); - Assert.assertEquals("cat0 label", categoryModel.categoryLabel); - Assert.assertEquals(0, categoryModel.entries.size()); - - } - - @Test(expected = IllegalStateException.class) - public void testAddOptionLimitCase0() throws Exception { - builder.addOption(null); - } - - @Test(expected = IllegalStateException.class) - public void testAddOptionLimitCase1() throws Exception { - builder.createModel(config); - builder.addOption(null); - } - - @Test(expected = NullPointerException.class) - public void testAddOptionLimitCase2() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(null); - } - - @Test - public void testAddOption() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(MyConfig.Option.LOCALE); - OptionModel optionModel = builder.flushOption(); - Assert.assertNotNull(optionModel); - Assert.assertEquals(MyConfig.Option.LOCALE, optionModel.def); - - CategoryModel categoryModel = builder.flushCategory(); - Assert.assertNotNull(categoryModel); - Assert.assertEquals("cat0", categoryModel.category); - Assert.assertEquals("cat0 label", categoryModel.categoryLabel); - Assert.assertEquals(1, categoryModel.entries.size()); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionPropertyNameLimitCase0() throws Exception { - builder.setOptionPropertyName(null); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionPropertyNameLimitCase1() throws Exception { - builder.createModel(config); - builder.setOptionPropertyName(null); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionPropertyNameLimitCase2() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.setOptionPropertyName(null); - } - - @Test(expected = NullPointerException.class) - public void testSetOptionPropertyNameLimitCase3() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(MyConfig.Option.LOCALE); - builder.setOptionPropertyName(null); - } - - @Test - public void testSetOptionPropertyName() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(MyConfig.Option.LOCALE); - builder.setOptionPropertyName(MyConfig.PROPERTY_LOCALE); - OptionModel optionModel = builder.flushOption(); - Assert.assertNotNull(optionModel); - Assert.assertEquals(MyConfig.Option.LOCALE, optionModel.def); - Assert.assertEquals(MyConfig.PROPERTY_LOCALE, optionModel.propertyName); - Assert.assertNull(optionModel.editor); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionEditorLimitCase0() throws Exception { - builder.setOptionEditor(null); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionEditorLimitCase1() throws Exception { - builder.createModel(config); - builder.setOptionEditor(null); - } - - @Test(expected = IllegalStateException.class) - public void testSetOptionEditorLimitCase2() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.setOptionEditor(null); - } - - - @Test(expected = NullPointerException.class) - public void testSetOptionEditorLimitCase3() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(MyConfig.Option.LOCALE); - builder.setOptionEditor(null); - } - - @Test - public void testSetOptionEditor() throws Exception { - builder.createModel(config); - builder.addCategory("cat0", "cat0 label"); - builder.addOption(MyConfig.Option.LOCALE); - TableCellEditor cellEditor = MyDefaultCellEditor.newBooleanEditor(); - builder.setOptionEditor(cellEditor); - OptionModel optionModel = builder.flushOption(); - Assert.assertNotNull(optionModel); - Assert.assertEquals(MyConfig.Option.LOCALE, optionModel.def); - Assert.assertNull(optionModel.propertyName); - Assert.assertEquals(cellEditor, optionModel.editor); - } - - @Test - public void testFlushModel() throws Exception { - builder.createModel(config); - ConfigUIModel configModel = builder.flushModel(); - Assert.assertNotNull(configModel); - Assert.assertNull(builder.model); - } - - @Test - public void testSetModel() throws Exception { - - } - - @Test - public void testSetCategory() throws Exception { - } - - @Test - public void testSetOption() throws Exception { - } - -} Deleted: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyConfig.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyConfig.java 2012-08-08 12:22:10 UTC (rev 2478) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyConfig.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -1,241 +0,0 @@ -/* - * #%L - * JAXX :: Widgets - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package jaxx.runtime.swing.editor.config.model; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.util.ApplicationConfig; - -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.Date; -import java.util.Locale; - -import static org.nuiton.i18n.I18n._; - -/** - * @author tchemit <chemit@codelutin.com> - * @since 2.0.0 - */ -public class MyConfig extends ApplicationConfig { - - /** Logger */ - static private Log log = LogFactory.getLog(MyConfig.class); - - public static final String PROPERTY_FULLSCREEN = "fullscreen"; - - public static final String PROPERTY_LOCALE = "locale"; - - public static final String PROPERTY_FONT_SIZE = "fontSize"; - - public static final String PROPERTY_ADJUSTING = "adjusting"; - - /** - * un drapeau pour bloquer la sauvegarde lors de la modification en masse des options - * via les setter. - */ - protected boolean adjusting; - - protected final PropertyChangeListener saveAction = new PropertyChangeListener() { - - @Override - public void propertyChange(PropertyChangeEvent evt) { - if (adjusting) { - if (log.isDebugEnabled()) { - log.debug("skip save while adjusting"); - } - return; - } - if (log.isDebugEnabled()) { - log.debug("Saving configuration at " + new Date()); - } - saveForUser(); - } - }; - - @Override - public void saveForUser(String... excludeKeys) { - // never save anything :) - } - - public MyConfig() { - - setConfigFileName(Option.CONFIG_FILE.defaultValue); - - // chargement de la configuration interne - - for (Option o : Option.values()) { - setDefaultOption(o.key, o.defaultValue); - } - - adjusting = true; - addPropertyChangeListener(PROPERTY_FULLSCREEN, saveAction); - addPropertyChangeListener(PROPERTY_FONT_SIZE, saveAction); - addPropertyChangeListener(PROPERTY_LOCALE, saveAction); - adjusting = false; - } - - @Override - public void setOption(String key, String value) { - if (key.equals(PROPERTY_ADJUSTING)) { - setAdjusting(Boolean.valueOf(value)); - return; - } - super.setOption(key, value); - } - - - public boolean isFullScreen() { - Boolean result = getOptionAsBoolean(Option.FULL_SCREEN.key); - return result != null && result; - } - - public Locale getLocale() { - Locale result = getOption(Locale.class, Option.LOCALE.key); - return result; - } - - public Float getFontSize() { - Float result = getOption(Float.class, Option.FONT_SIZE.key); - return result; - } - - public void setFullscreen(boolean fullscreen) { - Object oldValue = null; - setOption(Option.FULL_SCREEN.key, fullscreen + ""); - firePropertyChange(PROPERTY_FULLSCREEN, oldValue, fullscreen); - } - - public void setLocale(Locale newLocale) { - setOption(Option.LOCALE.key, newLocale.toString()); - firePropertyChange(PROPERTY_LOCALE, null, newLocale); - } - - public void setFontSize(Float newFontSize) { - Float oldValue = getFontSize(); - if (log.isDebugEnabled()) { - log.debug("changing font-size to " + newFontSize); - } - setOption(Option.FONT_SIZE.key, newFontSize.toString()); - firePropertyChange(PROPERTY_FONT_SIZE, oldValue, newFontSize); - } - - public boolean isAdjusting() { - return adjusting; - } - - public void setAdjusting(boolean adjusting) { - if (log.isDebugEnabled()) { - log.debug("changing adjusting to " + adjusting); - } - boolean oldValue = this.adjusting; - this.adjusting = adjusting; - firePropertyChange(PROPERTY_ADJUSTING, oldValue, adjusting); - } - - ////////////////////////////////////////////////// - // Toutes les options disponibles - ////////////////////////////////////////////////// - - public enum Option implements OptionDef { - - CONFIG_FILE(CONFIG_FILE_NAME, _("jaxxdemo.config.configFileName.description"), "jaxxdemo", String.class, true, true), - FULL_SCREEN("ui.fullscreen", _("jaxxdemo.config.ui.fullscreen"), "false", Boolean.class, false, false), - LOCALE("ui." + PROPERTY_LOCALE, _("jaxxdemo.config.ui." + PROPERTY_LOCALE), Locale.FRANCE.toString(), Locale.class, false, false), - FONT_SIZE("ui." + PROPERTY_FONT_SIZE, _("jaxxdemo.config.ui." + PROPERTY_FONT_SIZE), "10f", Float.class, false, false); - - public final String key; - - public final String description; - - public String defaultValue; - - public final Class<?> type; - - public boolean _transient; - - public boolean _final; - - Option(String key, - String description, - String defaultValue, - Class<?> type, - boolean _transient, - boolean _final) { - this.key = key; - this.description = description; - this.defaultValue = defaultValue; - this.type = type; - this._final = _final; - this._transient = _transient; - } - - @Override - public boolean isFinal() { - return _final; - } - - @Override - public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; - } - - @Override - public void setTransient(boolean _transient) { - this._transient = _transient; - } - - @Override - public void setFinal(boolean _final) { - this._final = _final; - } - - @Override - public boolean isTransient() { - return _transient; - } - - @Override - public String getDefaultValue() { - return defaultValue; - } - - @Override - public String getDescription() { - return description; - } - - @Override - public String getKey() { - return key; - } - - @Override - public Class<?> getType() { - return type; - } - } -} Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfig.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfig.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfig.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -0,0 +1,271 @@ +package jaxx.runtime.swing.editor.config.model; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Supplier; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jdesktop.beans.AbstractBean; +import org.nuiton.util.ApplicationConfig; + +import java.awt.Color; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Date; +import java.util.Locale; + +import static org.nuiton.i18n.I18n._; + +/** + * A config to test config ui api when using a delegation + * on {@link ApplicationConfig}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.4 + */ +public class MyDelegateConfig extends AbstractBean implements Supplier<ApplicationConfig> { + + /** Logger */ + private static final Log log = LogFactory.getLog(MyDelegateConfig.class); + + public static final String PROPERTY_FULLSCREEN = "fullscreen"; + + public static final String PROPERTY_LOCALE = "locale"; + + public static final String PROPERTY_FONT_SIZE = "fontSize"; + + public static final String PROPERTY_ADJUSTING = "adjusting"; + + public static final String PROPERTY_DEMO_COLOR = "demoColor"; + + public static final String PROPERTY_DEMO_CLASS = "demoClass"; + + protected final ApplicationConfig applicationConfig; + + public MyDelegateConfig() { + this.applicationConfig = new ApplicationConfig(); + + applicationConfig.setConfigFileName(Option.CONFIG_FILE.defaultValue); + + // chargement de la configuration interne + + for (Option o : Option.values()) { + applicationConfig.setDefaultOption(o.key, o.defaultValue); + } + + applicationConfig.setAdjusting(true); + try { + addPropertyChangeListener(PROPERTY_FULLSCREEN, saveAction); + addPropertyChangeListener(PROPERTY_FONT_SIZE, saveAction); + addPropertyChangeListener(PROPERTY_LOCALE, saveAction); + addPropertyChangeListener(PROPERTY_DEMO_COLOR, saveAction); + addPropertyChangeListener(PROPERTY_DEMO_CLASS, saveAction); + } finally { + applicationConfig.setAdjusting(false); + } + } + + protected final PropertyChangeListener saveAction = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + if (isAdjusting()) { + if (log.isDebugEnabled()) { + log.debug("skip save while adjusting"); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Saving configuration at " + new Date()); + } + saveForUser(); + } + } + }; + + public void saveForUser(String... excludeKeys) { + // never save anything :) + } + + public boolean isFullScreen() { + Boolean result = applicationConfig.getOptionAsBoolean(Option.FULL_SCREEN.key); + return result != null && result; + } + + public Locale getLocale() { + Locale result = applicationConfig.getOption(Locale.class, Option.LOCALE.key); + return result; + } + + public Float getFontSize() { + Float result = applicationConfig.getOption(Float.class, Option.FONT_SIZE.key); + return result; + } + + public Color getDemoColor() { + Color result = applicationConfig.getOptionAsColor(Option.DEMO_COLOR.key); + return result; + } + + public Class<?> getDemoClass() { + Class<?> result = applicationConfig.getOptionAsClass(Option.DEMO_CLASS.key); + return result; + } + + public void setFullscreen(boolean fullscreen) { + Object oldValue = null; + applicationConfig.setOption(Option.FULL_SCREEN.key, fullscreen + ""); + firePropertyChange(PROPERTY_FULLSCREEN, oldValue, fullscreen); + } + + public void setLocale(Locale newLocale) { + applicationConfig.setOption(Option.LOCALE.key, newLocale.toString()); + firePropertyChange(PROPERTY_LOCALE, null, newLocale); + } + + public void setFontSize(Float newFontSize) { + Float oldValue = getFontSize(); + if (log.isDebugEnabled()) { + log.debug("changing font-size to " + newFontSize); + } + applicationConfig.setOption(Option.FONT_SIZE.key, newFontSize.toString()); + firePropertyChange(PROPERTY_FONT_SIZE, oldValue, newFontSize); + } + + public void setDemoColor(Color color) { + Color oldValue = getDemoColor(); + if (log.isDebugEnabled()) { + log.debug("changing demo-color to " + color); + } + applicationConfig.setOption(Option.DEMO_COLOR.key, color.toString()); + firePropertyChange(PROPERTY_DEMO_COLOR, oldValue, color); + } + + public void setDemoClass(Class<?> newClass) { + Class<?> oldValue = getDemoClass(); + if (log.isDebugEnabled()) { + log.debug("changing demo-class to " + newClass); + } + applicationConfig.setOption(Option.DEMO_CLASS.key, newClass.getName()); + firePropertyChange(PROPERTY_DEMO_CLASS, oldValue, newClass); + } + + public boolean isAdjusting() { + return applicationConfig.isAdjusting(); + } + + public void setAdjusting(boolean adjusting) { + applicationConfig.setAdjusting(adjusting); + } + + @Override + public ApplicationConfig get() { + return applicationConfig; + } + + ////////////////////////////////////////////////// + // Toutes les options disponibles + ////////////////////////////////////////////////// + + public enum Option implements ApplicationConfig.OptionDef { + + CONFIG_FILE(ApplicationConfig.CONFIG_FILE_NAME, _("jaxxdemo.config.configFileName.description"), "jaxxdemo", String.class, true, true), + FULL_SCREEN("ui.fullscreen", _("jaxxdemo.config.ui.fullscreen"), "false", Boolean.class, false, false), + LOCALE("ui." + PROPERTY_LOCALE, _("jaxxdemo.config.ui." + PROPERTY_LOCALE), Locale.FRANCE.toString(), Locale.class, false, false), + FONT_SIZE("ui." + PROPERTY_FONT_SIZE, _("jaxxdemo.config.ui." + PROPERTY_FONT_SIZE), "10f", Float.class, false, false), + DEMO_COLOR("ui." + PROPERTY_DEMO_COLOR, _("jaxxdemo.config.ui.demoColor"), "#ffffff", Color.class, false, false), + DEMO_CLASS("ui." + PROPERTY_DEMO_CLASS, _("jaxxdemo.config.ui.demoClass"), "java.io.File", Class.class, false, false); + + public final String key; + + public final String description; + + public String defaultValue; + + public final Class<?> type; + + public boolean _transient; + + public boolean _final; + + Option(String key, + String description, + String defaultValue, + Class<?> type, + boolean _transient, + boolean _final) { + this.key = key; + this.description = description; + this.defaultValue = defaultValue; + this.type = type; + this._final = _final; + this._transient = _transient; + } + + @Override + public boolean isFinal() { + return _final; + } + + @Override + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + @Override + public void setTransient(boolean _transient) { + this._transient = _transient; + } + + @Override + public void setFinal(boolean _final) { + this._final = _final; + } + + @Override + public boolean isTransient() { + return _transient; + } + + @Override + public String getDefaultValue() { + return defaultValue; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getKey() { + return key; + } + + @Override + public Class<?> getType() { + return type; + } + } +} + Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfig.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfigTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfigTest.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfigTest.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -0,0 +1,330 @@ +package jaxx.runtime.swing.editor.config.model; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import jaxx.runtime.SwingUtil; +import jaxx.runtime.swing.editor.MyDefaultCellEditor; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.util.ApplicationConfig; + +import javax.swing.ImageIcon; +import javax.swing.table.TableCellEditor; +import java.awt.Color; +import java.io.File; +import java.util.Collection; + +/** + * To test ConfigUI api on {@link MyDelegateConfig}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.4 + */ +public class MyDelegateConfigTest { + + + public static final String CATEGORY = "cat0"; + + protected ConfigUIModelBuilder builder; + + protected MyDelegateConfig config; + + @Before + public void setup() { + config = new MyDelegateConfig(); + builder = new ConfigUIModelBuilder(); + } + + @Test(expected = NullPointerException.class) + public void testCreateModelLimitCase0() throws Exception { + builder.createModel((ApplicationConfig) null); + } + + @Test(expected = IllegalStateException.class) + public void testFlushModelLimitCase0() throws Exception { + builder.flushModel(); + } + + @Test + public void testCreateModel() throws Exception { + builder.createModel(config); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNotNull(configModel.getApplicationConfig()); + } + + @Test(expected = IllegalStateException.class) + public void testRegisterCallbackLimitCase() throws Exception { + builder.registerCallBack(null, null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase0() throws Exception { + builder.createModel(config); + builder.registerCallBack(null, null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase1() throws Exception { + builder.createModel(config); + builder.registerCallBack("yo", null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase2() throws Exception { + builder.createModel(config); + builder.registerCallBack("yo", "yo description", null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase4() throws Exception { + builder.createModel(config); + Runnable callback = new Runnable() { + + @Override + public void run() { + } + }; + builder.registerCallBack("yo", "yo description", null, callback); + } + + @Test + public void testRegisterCallback() throws Exception { + builder.createModel(config); + Runnable callback = new Runnable() { + + @Override + public void run() { + } + }; + ImageIcon icon = SwingUtil.createActionIcon("add"); + builder.registerCallBack("yo", "yo description", icon, callback); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNotNull(configModel.getApplicationConfig()); + CallBackEntry callBackEntry = + configModel.getCallBacksManager().getCallBack("yo"); + Assert.assertNotNull(callBackEntry); + + Assert.assertEquals(callback, callBackEntry.getAction()); + } + + @Test(expected = IllegalStateException.class) + public void testAddCategoryLimitCase0() throws Exception { + builder.addCategory(null, null); + } + + @Test(expected = NullPointerException.class) + public void testAddCategoryLimitCase1() throws Exception { + builder.createModel(config); + builder.addCategory(null, null); + } + + @Test(expected = NullPointerException.class) + public void testAddCategoryLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("", null); + } + + @Test + public void testAddCategory() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + CategoryModel categoryModel = builder.flushCategory(); + Assert.assertNotNull(categoryModel); + Assert.assertEquals("cat0", categoryModel.category); + Assert.assertEquals("cat0 label", categoryModel.categoryLabel); + Assert.assertEquals(0, categoryModel.entries.size()); + + } + + @Test(expected = IllegalStateException.class) + public void testAddOptionLimitCase0() throws Exception { + builder.addOption(null); + } + + @Test(expected = IllegalStateException.class) + public void testAddOptionLimitCase1() throws Exception { + builder.createModel(config); + builder.addOption(null); + } + + @Test(expected = NullPointerException.class) + public void testAddOptionLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(null); + } + + @Test + public void testAddOption() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyDelegateConfig.Option.LOCALE); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyDelegateConfig.Option.LOCALE, optionModel.def); + + CategoryModel categoryModel = builder.flushCategory(); + Assert.assertNotNull(categoryModel); + Assert.assertEquals("cat0", categoryModel.category); + Assert.assertEquals("cat0 label", categoryModel.categoryLabel); + Assert.assertEquals(1, categoryModel.entries.size()); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase0() throws Exception { + builder.setOptionPropertyName(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase1() throws Exception { + builder.createModel(config); + builder.setOptionPropertyName(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.setOptionPropertyName(null); + } + + @Test(expected = NullPointerException.class) + public void testSetOptionPropertyNameLimitCase3() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyDelegateConfig.Option.LOCALE); + builder.setOptionPropertyName(null); + } + + @Test + public void testSetOptionPropertyName() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyDelegateConfig.Option.LOCALE); + builder.setOptionPropertyName(MyDelegateConfig.PROPERTY_LOCALE); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyDelegateConfig.Option.LOCALE, optionModel.def); + Assert.assertEquals(MyDelegateConfig.PROPERTY_LOCALE, optionModel.propertyName); + Assert.assertNull(optionModel.editor); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase0() throws Exception { + builder.setOptionEditor(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase1() throws Exception { + builder.createModel(config); + builder.setOptionEditor(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.setOptionEditor(null); + } + + + @Test(expected = NullPointerException.class) + public void testSetOptionEditorLimitCase3() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyDelegateConfig.Option.LOCALE); + builder.setOptionEditor(null); + } + + @Test + public void testSetOptionEditor() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyDelegateConfig.Option.LOCALE); + TableCellEditor cellEditor = MyDefaultCellEditor.newBooleanEditor(); + builder.setOptionEditor(cellEditor); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyDelegateConfig.Option.LOCALE, optionModel.def); + Assert.assertNull(optionModel.propertyName); + Assert.assertEquals(cellEditor, optionModel.editor); + } + + @Test + public void testFlushModel() throws Exception { + builder.createModel(config); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNull(builder.model); + } + + @Test + public void testSaveClassOption() throws Exception { + builder.createModel(config); + builder.addCategory(CATEGORY, "cat0 label"); + builder.addOption(MyDelegateConfig.Option.DEMO_CLASS, MyDelegateConfig.PROPERTY_DEMO_CLASS); + ConfigUIModel configModel = builder.flushModel(); + configModel.setCategory(CATEGORY); + + CategoryModel categoryModel = configModel.getCategoryModel(); + Assert.assertNotNull(categoryModel); + + OptionModel optionModel = categoryModel.getOptionModel(MyDelegateConfig.Option.DEMO_CLASS.getKey()); + Assert.assertNotNull(optionModel); + + categoryModel.setValue(optionModel, Collection.class); + + Assert.assertEquals(File.class, config.getDemoClass()); + + configModel.saveModified(); + + Assert.assertEquals(Collection.class, config.getDemoClass()); + } + + @Test + public void testSaveColorOption() throws Exception { + builder.createModel(config); + builder.addCategory(CATEGORY, "cat0 label"); + builder.addOption(MyDelegateConfig.Option.DEMO_COLOR); + ConfigUIModel configModel = builder.flushModel(); + configModel.setCategory(CATEGORY); + + CategoryModel categoryModel = configModel.getCategoryModel(); + Assert.assertNotNull(categoryModel); + + OptionModel optionModel = categoryModel.getOptionModel(MyDelegateConfig.Option.DEMO_COLOR.getKey()); + Assert.assertNotNull(optionModel); + + Color newColor = new Color(0, 0, 0); + categoryModel.setValue(optionModel, newColor); + + Assert.assertFalse(newColor.equals(config.getDemoColor())); + + configModel.saveModified(); + Assert.assertEquals(newColor, config.getDemoColor()); + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyDelegateConfigTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfig.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfig.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfig.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -0,0 +1,282 @@ +package jaxx.runtime.swing.editor.config.model; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.util.ApplicationConfig; + +import java.awt.Color; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Date; +import java.util.Locale; + +import static org.nuiton.i18n.I18n._; + +/** + * A config to test config ui api when using a delegation + * on {@link ApplicationConfig}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.4 + */ +public class MyInheritedConfig extends ApplicationConfig { + + /** Logger */ + private static final Log log = LogFactory.getLog(MyInheritedConfig.class); + + public static final String PROPERTY_FULLSCREEN = "fullscreen"; + + public static final String PROPERTY_LOCALE = "locale"; + + public static final String PROPERTY_FONT_SIZE = "fontSize"; + + public static final String PROPERTY_ADJUSTING = "adjusting"; + + public static final String PROPERTY_DEMO_COLOR = "demoColor"; + + public static final String PROPERTY_DEMO_CLASS = "demoClass"; + +// /** +// * un drapeau pour bloquer la sauvegarde lors de la modification en masse des options +// * via les setter. +// */ +// protected boolean adjusting; + + protected final PropertyChangeListener saveAction = 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 at " + new Date()); + } + saveForUser(); + } + }; + + @Override + public void saveForUser(String... excludeKeys) { + // never save anything :) + } + + public MyInheritedConfig() { + + setConfigFileName(Option.CONFIG_FILE.defaultValue); + + // chargement de la configuration interne + + for (Option o : Option.values()) { + setDefaultOption(o.key, o.defaultValue); + } + + setAdjusting(true); + try { + addPropertyChangeListener(PROPERTY_FULLSCREEN, saveAction); + addPropertyChangeListener(PROPERTY_FONT_SIZE, saveAction); + addPropertyChangeListener(PROPERTY_LOCALE, saveAction); + addPropertyChangeListener(PROPERTY_DEMO_COLOR, saveAction); + addPropertyChangeListener(PROPERTY_DEMO_CLASS, saveAction); + } finally { + setAdjusting(false); + } + } + + @Override + public void setOption(String key, String value) { + if (key.equals(PROPERTY_ADJUSTING)) { + setAdjusting(Boolean.valueOf(value)); + return; + } + super.setOption(key, value); + } + + + public boolean isFullScreen() { + Boolean result = getOptionAsBoolean(Option.FULL_SCREEN.key); + return result != null && result; + } + + public Locale getLocale() { + Locale result = getOption(Locale.class, Option.LOCALE.key); + return result; + } + + public Float getFontSize() { + Float result = getOption(Float.class, Option.FONT_SIZE.key); + return result; + } + + public Color getDemoColor() { + Color result = getOptionAsColor(Option.DEMO_COLOR.key); + return result; + } + + public Class<?> getDemoClass() { + Class<?> result = getOptionAsClass(Option.DEMO_CLASS.key); + return result; + } + + public void setFullscreen(boolean fullscreen) { + Object oldValue = null; + setOption(Option.FULL_SCREEN.key, fullscreen + ""); + firePropertyChange(PROPERTY_FULLSCREEN, oldValue, fullscreen); + } + + public void setLocale(Locale newLocale) { + setOption(Option.LOCALE.key, newLocale.toString()); + firePropertyChange(PROPERTY_LOCALE, null, newLocale); + } + + public void setFontSize(Float newFontSize) { + Float oldValue = getFontSize(); + if (log.isDebugEnabled()) { + log.debug("changing font-size to " + newFontSize); + } + setOption(Option.FONT_SIZE.key, newFontSize.toString()); + firePropertyChange(PROPERTY_FONT_SIZE, oldValue, newFontSize); + } + + public void setDemoColor(Color color) { + Color oldValue = getDemoColor(); + if (log.isDebugEnabled()) { + log.debug("changing demo-color to " + color); + } + setOption(Option.DEMO_COLOR.key, color.toString()); + firePropertyChange(PROPERTY_DEMO_COLOR, oldValue, color); + } + + public void setDemoClass(Class<?> newClass) { + Class<?> oldValue = getDemoClass(); + if (log.isDebugEnabled()) { + log.debug("changing demo-class to " + newClass); + } + setOption(Option.DEMO_CLASS.key, newClass.getName()); + firePropertyChange(PROPERTY_DEMO_CLASS, oldValue, newClass); + } + +// public boolean isAdjusting() { +// return adjusting; +// } +// +// public void setAdjusting(boolean adjusting) { +// if (log.isDebugEnabled()) { +// log.debug("changing adjusting to " + adjusting); +// } +// boolean oldValue = this.adjusting; +// this.adjusting = adjusting; +// firePropertyChange(PROPERTY_ADJUSTING, oldValue, adjusting); +// } + + ////////////////////////////////////////////////// + // Toutes les options disponibles + ////////////////////////////////////////////////// + + public enum Option implements ApplicationConfig.OptionDef { + + CONFIG_FILE(CONFIG_FILE_NAME, _("jaxxdemo.config.configFileName.description"), "jaxxdemo", String.class, true, true), + FULL_SCREEN("ui.fullscreen", _("jaxxdemo.config.ui.fullscreen"), "false", Boolean.class, false, false), + LOCALE("ui." + PROPERTY_LOCALE, _("jaxxdemo.config.ui." + PROPERTY_LOCALE), Locale.FRANCE.toString(), Locale.class, false, false), + FONT_SIZE("ui." + PROPERTY_FONT_SIZE, _("jaxxdemo.config.ui." + PROPERTY_FONT_SIZE), "10f", Float.class, false, false), + DEMO_COLOR("ui." + PROPERTY_DEMO_COLOR, _("jaxxdemo.config.ui.demoColor"), "#ffffff", Color.class, false, false), + DEMO_CLASS("ui." + PROPERTY_DEMO_CLASS, _("jaxxdemo.config.ui.demoClass"), "java.io.File", Class.class, false, false); + + public final String key; + + public final String description; + + public String defaultValue; + + public final Class<?> type; + + public boolean _transient; + + public boolean _final; + + Option(String key, + String description, + String defaultValue, + Class<?> type, + boolean _transient, + boolean _final) { + this.key = key; + this.description = description; + this.defaultValue = defaultValue; + this.type = type; + this._final = _final; + this._transient = _transient; + } + + @Override + public boolean isFinal() { + return _final; + } + + @Override + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + @Override + public void setTransient(boolean _transient) { + this._transient = _transient; + } + + @Override + public void setFinal(boolean _final) { + this._final = _final; + } + + @Override + public boolean isTransient() { + return _transient; + } + + @Override + public String getDefaultValue() { + return defaultValue; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public String getKey() { + return key; + } + + @Override + public Class<?> getType() { + return type; + } + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfig.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Added: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfigTest.java =================================================================== --- trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfigTest.java (rev 0) +++ trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfigTest.java 2012-08-08 12:23:08 UTC (rev 2479) @@ -0,0 +1,341 @@ +package jaxx.runtime.swing.editor.config.model; +/* + * #%L + * JAXX :: Widgets + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2008 - 2012 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import jaxx.runtime.SwingUtil; +import jaxx.runtime.swing.editor.MyDefaultCellEditor; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.util.ApplicationConfig; + +import javax.swing.ImageIcon; +import javax.swing.table.TableCellEditor; +import java.awt.Color; +import java.io.File; +import java.util.Collection; + +/** + * To test ConfigUI api on {@link MyInheritedConfig}. + * + * @author tchemit <chemit@codelutin.com> + * @since 2.5.4 + */ +public class MyInheritedConfigTest { + + public static final String CATEGORY = "cat0"; + + + protected ConfigUIModelBuilder builder; + + protected MyInheritedConfig config; + + @Before + public void setup() { + config = new MyInheritedConfig(); + builder = new ConfigUIModelBuilder(); + } + + @Test(expected = NullPointerException.class) + public void testCreateModelLimitCase0() throws Exception { + builder.createModel((ApplicationConfig) null); + } + + @Test(expected = IllegalStateException.class) + public void testFlushModelLimitCase0() throws Exception { + builder.flushModel(); + } + + @Test + public void testCreateModel() throws Exception { + builder.createModel(config); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNotNull(configModel.getApplicationConfig()); + } + + @Test(expected = IllegalStateException.class) + public void testRegisterCallbackLimitCase() throws Exception { + builder.registerCallBack(null, null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase0() throws Exception { + builder.createModel(config); + builder.registerCallBack(null, null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase1() throws Exception { + builder.createModel(config); + builder.registerCallBack("yo", null, null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase2() throws Exception { + builder.createModel(config); + builder.registerCallBack("yo", "yo description", null, null); + } + + @Test(expected = NullPointerException.class) + public void testRegisterCallbackLimitCase4() throws Exception { + builder.createModel(config); + Runnable callback = new Runnable() { + + @Override + public void run() { + } + }; + builder.registerCallBack("yo", "yo description", null, callback); + } + + @Test + public void testRegisterCallback() throws Exception { + builder.createModel(config); + Runnable callback = new Runnable() { + + @Override + public void run() { + } + }; + ImageIcon icon = SwingUtil.createActionIcon("add"); + builder.registerCallBack("yo", "yo description", icon, callback); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNotNull(configModel.getApplicationConfig()); + CallBackEntry callBackEntry = + configModel.getCallBacksManager().getCallBack("yo"); + Assert.assertNotNull(callBackEntry); + + Assert.assertEquals(callback, callBackEntry.getAction()); + } + + @Test(expected = IllegalStateException.class) + public void testAddCategoryLimitCase0() throws Exception { + builder.addCategory(null, null); + } + + @Test(expected = NullPointerException.class) + public void testAddCategoryLimitCase1() throws Exception { + builder.createModel(config); + builder.addCategory(null, null); + } + + @Test(expected = NullPointerException.class) + public void testAddCategoryLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("", null); + } + + @Test + public void testAddCategory() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + CategoryModel categoryModel = builder.flushCategory(); + Assert.assertNotNull(categoryModel); + Assert.assertEquals("cat0", categoryModel.category); + Assert.assertEquals("cat0 label", categoryModel.categoryLabel); + Assert.assertEquals(0, categoryModel.entries.size()); + + } + + @Test(expected = IllegalStateException.class) + public void testAddOptionLimitCase0() throws Exception { + builder.addOption(null); + } + + @Test(expected = IllegalStateException.class) + public void testAddOptionLimitCase1() throws Exception { + builder.createModel(config); + builder.addOption(null); + } + + @Test(expected = NullPointerException.class) + public void testAddOptionLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(null); + } + + @Test + public void testAddOption() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyInheritedConfig.Option.LOCALE); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyInheritedConfig.Option.LOCALE, optionModel.def); + + CategoryModel categoryModel = builder.flushCategory(); + Assert.assertNotNull(categoryModel); + Assert.assertEquals("cat0", categoryModel.category); + Assert.assertEquals("cat0 label", categoryModel.categoryLabel); + Assert.assertEquals(1, categoryModel.entries.size()); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase0() throws Exception { + builder.setOptionPropertyName(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase1() throws Exception { + builder.createModel(config); + builder.setOptionPropertyName(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionPropertyNameLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.setOptionPropertyName(null); + } + + @Test(expected = NullPointerException.class) + public void testSetOptionPropertyNameLimitCase3() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyInheritedConfig.Option.LOCALE); + builder.setOptionPropertyName(null); + } + + @Test + public void testSetOptionPropertyName() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyInheritedConfig.Option.LOCALE); + builder.setOptionPropertyName(MyInheritedConfig.PROPERTY_LOCALE); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyInheritedConfig.Option.LOCALE, optionModel.def); + Assert.assertEquals(MyInheritedConfig.PROPERTY_LOCALE, optionModel.propertyName); + Assert.assertNull(optionModel.editor); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase0() throws Exception { + builder.setOptionEditor(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase1() throws Exception { + builder.createModel(config); + builder.setOptionEditor(null); + } + + @Test(expected = IllegalStateException.class) + public void testSetOptionEditorLimitCase2() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.setOptionEditor(null); + } + + + @Test(expected = NullPointerException.class) + public void testSetOptionEditorLimitCase3() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyInheritedConfig.Option.LOCALE); + builder.setOptionEditor(null); + } + + @Test + public void testSetOptionEditor() throws Exception { + builder.createModel(config); + builder.addCategory("cat0", "cat0 label"); + builder.addOption(MyInheritedConfig.Option.LOCALE); + TableCellEditor cellEditor = MyDefaultCellEditor.newBooleanEditor(); + builder.setOptionEditor(cellEditor); + OptionModel optionModel = builder.flushOption(); + Assert.assertNotNull(optionModel); + Assert.assertEquals(MyInheritedConfig.Option.LOCALE, optionModel.def); + Assert.assertNull(optionModel.propertyName); + Assert.assertEquals(cellEditor, optionModel.editor); + } + + @Test + public void testFlushModel() throws Exception { + builder.createModel(config); + ConfigUIModel configModel = builder.flushModel(); + Assert.assertNotNull(configModel); + Assert.assertNull(builder.model); + } + + @Test + public void testSetModel() throws Exception { + + } + + @Test + public void testSetCategory() throws Exception { + } + + @Test + public void testSetOption() throws Exception { + } + + @Test + public void testSaveClassOption() throws Exception { + builder.createModel(config); + builder.addCategory(CATEGORY, "cat0 label"); + builder.addOption(MyDelegateConfig.Option.DEMO_CLASS, MyDelegateConfig.PROPERTY_DEMO_CLASS); + ConfigUIModel configModel = builder.flushModel(); + configModel.setCategory(CATEGORY); + CategoryModel categoryModel = configModel.getCategoryModel(); + Assert.assertNotNull(categoryModel); + + OptionModel optionModel = categoryModel.getOptionModel(MyDelegateConfig.Option.DEMO_CLASS.getKey()); + Assert.assertNotNull(optionModel); + + categoryModel.setValue(optionModel, Collection.class); + + Assert.assertEquals(File.class, config.getDemoClass()); + + configModel.saveModified(); + Assert.assertEquals(Collection.class, config.getDemoClass()); + } + + @Test + public void testSaveColorOption() throws Exception { + builder.createModel(config); + builder.addCategory(CATEGORY, "cat0 label"); + builder.addOption(MyDelegateConfig.Option.DEMO_COLOR); + ConfigUIModel configModel = builder.flushModel(); + configModel.setCategory(CATEGORY); + + CategoryModel categoryModel = configModel.getCategoryModel(); + Assert.assertNotNull(categoryModel); + + OptionModel optionModel = categoryModel.getOptionModel(MyDelegateConfig.Option.DEMO_COLOR.getKey()); + Assert.assertNotNull(optionModel); + + Color newColor = new Color(0, 0, 0); + categoryModel.setValue(optionModel, newColor); + + Assert.assertFalse(newColor.equals(config.getDemoColor())); + + configModel.saveModified(); + Assert.assertEquals(newColor, config.getDemoColor()); + } +} Property changes on: trunk/jaxx-widgets/src/test/java/jaxx/runtime/swing/editor/config/model/MyInheritedConfigTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
tchemit@users.nuiton.org