[Buix-commits] r704 - in trunk/lutinjaxx/util/src/main/java/org/codelutin: jaxx/util/config util/config
Author: tchemit Date: 2008-07-08 21:14:13 +0000 (Tue, 08 Jul 2008) New Revision: 704 Added: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/CancelAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUI.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIHandler.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIModel.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/ResetAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/SaveAction.java Removed: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/CancelAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUI.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIHandler.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIModel.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/ResetAction.java trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/SaveAction.java Log: use org.codelutin.jaxx:util module Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/CancelAction.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/CancelAction.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/CancelAction.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/CancelAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,43 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.AbstractUIAction; +import org.codelutin.ui.DialogUI; +import org.codelutin.ui.UIHelper; + +import java.awt.event.ActionEvent; + +/** @author chemit */ +public class CancelAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { + private static final long serialVersionUID = 1L; + + public CancelAction(DialogUI<? extends H> dialogUI, boolean showLabel) { + super(null, UIHelper.createActionIcon("cancel-config"), dialogUI); + if (showLabel) { + String text = org.codelutin.i18n.I18n._("lutinui.config.cancel"); + putValue(NAME, text); + putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); + putValue(MNEMONIC_KEY, (int) text.charAt(0)); + } + String libelle = org.codelutin.i18n.I18n._("lutinui.config.cancel.tooltip"); + putValue(SHORT_DESCRIPTION, libelle); + + } + + public void actionPerformed(ActionEvent e) { + getUi().dispose(); + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUI.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUI.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUI.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUI.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,105 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.DialogUI; + +import javax.swing.AbstractButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPasswordField; +import javax.swing.JRadioButton; +import javax.swing.JTextField; + +/** + * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) + * <p/> + * TODO : make jaxx authorized implementing interface for root tag :) + * + * @author chemit + */ +public abstract class DialogConfigUI<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends DialogUI<H> { + + public abstract AbstractButton getOk(); + + public abstract AbstractButton getReset(); + + public abstract AbstractButton getCancel(); + + public JComponent getElement(E key) { + Object id = getObjectById(key.name()); + if (id == null) { + log.error(new NullPointerException("no widget for key "+key)); + return null; + } + if (!(id instanceof JComponent)) { + throw new IllegalArgumentException(id + " is not a JComponent"); + } + return (JComponent) id; + } + + public Object getElementValue(E key) { + JComponent o = getElement(key); + if (o instanceof JPasswordField) { + return new String(((JPasswordField) o).getPassword()); + } + if (o instanceof JTextField) { + return ((JTextField) o).getText(); + } + if (o instanceof JRadioButton) { + return ((JRadioButton) o).isSelected(); + } + if (o instanceof JCheckBox) { + return ((JCheckBox) o).isSelected(); + } + + if (o instanceof JComboBox) { + return ((JComboBox) o).getSelectedItem(); + } + return ""; + } + + public void setElementValue(E key, Object value) { + JComponent o = getElement(key); + + String strValue = value == null ? "" : String.valueOf(value); + if (o instanceof JPasswordField) { + ((JPasswordField) o).setText(strValue); + } + if (o instanceof JTextField) { + ((JTextField) o).setText(strValue); + } + if (o instanceof JRadioButton) { + ((JRadioButton) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); + } + if (o instanceof JCheckBox) { + ((JCheckBox) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); + } + if (o instanceof JComboBox) { + ((JComboBox) o).setSelectedItem(value); + } + } + + public JLabel getElementLabel(E key) { + return (JLabel) getObjectById(key.name() + "Label"); + } + + public void doCheck(E key) { + getHandler().doCheck(key); + } + +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIHandler.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIHandler.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIHandler.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIHandler.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,156 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.DialogUIHandler; +import org.codelutin.util.ConverterUtil; +import org.codelutin.util.config.Config; +import org.codelutin.util.config.Property; + +import javax.swing.JComponent; +import java.awt.Color; +import java.beans.PropertyChangeEvent; +import java.util.EnumMap; +import java.util.EnumSet; + +/** + * DialogUI handler + * + * @author chemit + */ +public abstract class DialogConfigUIHandler<E extends Enum<E>, M extends DialogConfigUIModel<E, ?>, U extends DialogConfigUI<E, ?>> extends DialogUIHandler<M, U> { + + protected DialogConfigUIHandler(U ui, M model) { + super(ui, model); + } + + public void propertyChange(PropertyChangeEvent evt) { + if (log.isDebugEnabled()) { + log.debug(evt.getPropertyName() + " old:" + evt.getOldValue() + ", new:" + evt.getNewValue()); + } + String action = evt.getPropertyName(); + + if (DialogConfigUIModel.CONFIG_PROPERTY_CHANGED.equals(action)) { + // update ui with model values, + populateUI(); + // revalidate form + doCheckAll(); + return; + } + + if (DialogConfigUIModel.MODIFIED_PROPERTY_CHANGED.equals(action)) { + Boolean newValue = (Boolean) evt.getNewValue(); + boolean modified = newValue != null && newValue; + getUi().getReset().setEnabled(modified); + getUi().getOk().setEnabled(modified && getModel().isConfigValid()); + return; + } + + if (DialogConfigUIModel.UNVALID_PROPERTY_CHANGED.equals(action)) { + updateUI(); + return; + } + + throw new IllegalStateException("unimplemented property changed : " + evt + " for " + this); + } + + public void doCheck(E key) { + Object uiValue = getUi().getElementValue(key); + DialogConfigUIModel<E, ?> model = getModel(); + Object currentValue = model.getCurrent().getProperty(key); + if (currentValue == null) { + currentValue = ""; + } else { + currentValue = String.valueOf(currentValue); + } + + model.validateProperty(key, uiValue); + model.changeModifiedState(key, uiValue, currentValue); + } + + public void doCheckAll() { + DialogConfigUIModel<E, ?> model = getModel(); + EnumSet<E> unvalids = EnumSet.noneOf(model.klass); + for (E e : model.getCheckedKeysSet()) { + Object uiValue = getUi().getElementValue(e); + if (!model.isValid(e, uiValue)) { + unvalids.add(e); + } + } + model.setUnvalids(unvalids); + unvalids.clear(); + } + + protected boolean prepareSave() { + DialogConfigUI<E, ?> ui = getUi(); + DialogConfigUIModel<E, ?> model = getModel(); + + Config<E> current = model.getCurrent(); + + if (!model.isConfigValid()) { + log.warn("do not save a unvalid config : " + model.getUnvalids()); + return false; + } + + if (!model.isModified()) { + log.warn("nothing to save"); + return false; + } + EnumSet<E> toTreate = model.getCheckedKeysSet(); + // transfert checkable values from ui to model + for (E key : model.getModifieds()) { + if (!toTreate.contains(key)) { + continue; + } + Object value = ui.getElementValue(key); + Class<?> type = ((Property) key).getType(); + Object newValue = ConverterUtil.convert(type, value); + current.setProperty(key, newValue); + } + + return true; + } + + protected void populateUI() { + U ui = getUi(); + EnumMap<E, Object> map = getModel().getCurrent().getProperties(); + for (E e : getModel().getCheckedKeysSet()) { + Object value = map.get(e); + populateUI(ui, e, value); + } + } + + protected void populateUI(U ui, E key, Object value) { + ui.setElementValue(key, value); + } + + protected void updateUI() { + EnumSet<E> unvalids = getModel().getUnvalids(); + for (E key : unvalids) { + setLabelColor(key, false); + } + for (E key : EnumSet.complementOf(unvalids)) { + setLabelColor(key, true); + } + } + + protected void setLabelColor(E key, boolean valid) { + JComponent component = getUi().getElementLabel(key); + if (component != null && component.isVisible()) { + component.setForeground(valid ? Color.black : Color.red); + } + } + +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIModel.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIModel.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIModel.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/DialogConfigUIModel.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,220 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.DialogUIModel; +import org.codelutin.util.config.Config; + +import java.util.EnumSet; + +/** + * Abstract config ui model. + * + * @author chemit + */ +public abstract class DialogConfigUIModel<E extends Enum<E>, C extends Config<E>> extends DialogUIModel { + + public static final String CONFIG_PROPERTY_CHANGED = "config"; + public static final String MODIFIED_PROPERTY_CHANGED = "modify"; + public static final String UNVALID_PROPERTY_CHANGED = "unvalid"; + + /** @return a empty config */ + protected abstract C newConfig(); + + /** + * @param key property key + * @param value value to validate + * @return <code>true</code> if given value is valid for property, <code>false>/code> otherwise + */ + protected abstract boolean isValid(E key, Object value); + + /** + * object used to init model and save model, this is an external object. + * <p/> + * The object must have bean read-write properties for each value of E + */ + protected Object src; + + /** current config used in model */ + protected C current; + + /** set of modified properties */ + protected EnumSet<E> modifieds; + + /** set of unvalid properties */ + protected EnumSet<E> unvalids; + + /** enum class */ + protected Class<E> klass; + + /** set of key not to check */ + protected EnumSet<E> uncheckedKeys; + + /** set of all keys checkable */ + protected EnumSet<E> checkedKeysSet; + + protected DialogConfigUIModel(Class<E> klass) { + this.klass = klass; + this.current = newConfig(); + this.modifieds = EnumSet.noneOf(klass); + this.unvalids = EnumSet.noneOf(klass); + } + + public EnumSet<E> getCheckedKeysSet() { + if (checkedKeysSet == null) { + if (uncheckedKeys != null) { + checkedKeysSet = EnumSet.complementOf(uncheckedKeys); + } else { + checkedKeysSet = EnumSet.allOf(klass); + } + } + return checkedKeysSet; + } + + public Object getSrc() { + return src; + } + + public C getCurrent() { + return current; + } + + public EnumSet<E> getUnivserse() { + return getCurrent().getUniverse(); + } + + public EnumSet<E> getModifieds() { + return modifieds; + } + + public EnumSet<E> getUnvalids() { + return unvalids; + } + + public boolean isModified() { + return !modifieds.isEmpty(); + } + + public boolean isConfigValid() { + return unvalids.isEmpty(); + } + + public void populate(Object src) { + this.src = src; + this.current = newConfig(); + if (src != null) { + this.current.copyFrom(src); + } + this.modifieds.clear(); + this.unvalids.clear(); + setModified(false); + firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); + } + + public void reset() { + populate(src); + } + + public void setModified(boolean modified) { + firePropertyChange(MODIFIED_PROPERTY_CHANGED, null, modified); + } + + public void setUnvalid(boolean unvalid) { + firePropertyChange(UNVALID_PROPERTY_CHANGED, null, unvalid); + } + + public void addModified(E key) { + if (!modifieds.contains(key)) { + modifieds.add(key); + log.debug(key); + } + setModified(!modifieds.isEmpty()); + } + + public void removeModified(E key) { + + if (modifieds.contains(key)) { + modifieds.remove(key); + } + setModified(!modifieds.isEmpty()); + } + + public void removeModified(EnumSet<E> keys) { + for (E key : keys) { + if (modifieds.contains(key)) { + modifieds.remove(key); + } + } + setModified(!modifieds.isEmpty()); + } + + public void setUnvalids(EnumSet<E> keys) { + for (E key : keys) { + if (!unvalids.contains(key)) { + unvalids.add(key); + } + } + for (E key : EnumSet.complementOf(keys)) { + if (unvalids.contains(key)) { + unvalids.remove(key); + } + } + setUnvalid(!unvalids.isEmpty()); + } + + + public void addUnvalid(E key) { + if (!unvalids.contains(key)) { + unvalids.add(key); + } + setUnvalid(!unvalids.isEmpty()); + } + + public void removeUnvalid(E key) { + if (unvalids.contains(key)) { + unvalids.remove(key); + } + setUnvalid(!unvalids.isEmpty()); + } + + public void save() { + current.copyTo(src, modifieds); + modifieds.clear(); + // redisplay config + firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); + } + + public void changeModifiedState(E key, Object uiValue, Object currentValue) { + if (uiValue!=null && uiValue.equals(currentValue)) { + removeModified(key); + } else { + addModified(key); + } + } + + public void clear(E key) { + log.info(key); + modifieds.remove(key); + unvalids.remove(key); + } + + protected void validateProperty(E key, Object uiValue) { + if (isValid(key, uiValue)) { + removeUnvalid(key); + } else { + addUnvalid(key); + } + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/ResetAction.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/ResetAction.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/ResetAction.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/ResetAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,43 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.AbstractUIAction; +import org.codelutin.ui.DialogUI; +import org.codelutin.ui.UIHelper; + +import java.awt.event.ActionEvent; + +/** @author chemit */ +public class ResetAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { + private static final long serialVersionUID = 1L; + + public ResetAction(DialogUI<? extends H> dialogUI, boolean showLabel) { + super(null, UIHelper.createActionIcon("reset-config"), dialogUI); + if (showLabel) { + String text = org.codelutin.i18n.I18n._("lutinui.config.reset"); + putValue(NAME, text); + putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); + putValue(MNEMONIC_KEY, (int) text.charAt(0)); + } + String libelle = org.codelutin.i18n.I18n._("lutinui.config.reset.tooltip"); + putValue(SHORT_DESCRIPTION, libelle); + + } + + public void actionPerformed(ActionEvent e) { + getHandler().getModel().reset(); + } +} \ No newline at end of file Copied: trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/SaveAction.java (from rev 700, trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/SaveAction.java) =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/SaveAction.java (rev 0) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/jaxx/util/config/SaveAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -0,0 +1,50 @@ +/** + * ##% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * 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 Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * ##% + */ +package org.codelutin.jaxx.util.config; + +import org.codelutin.ui.AbstractUIAction; +import org.codelutin.ui.DialogUI; +import org.codelutin.ui.UIHelper; + +import java.awt.event.ActionEvent; + +/** @author chemit */ +public class SaveAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { + private static final long serialVersionUID = 1L; + + public SaveAction(DialogUI<? extends H> dialogUI, boolean showLabel) { + super(null, UIHelper.createActionIcon("save-config"), dialogUI); + if (showLabel) { + String text = org.codelutin.i18n.I18n._("lutinui.config.save"); + putValue(NAME, text); + putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); + putValue(MNEMONIC_KEY, (int) text.charAt(0)); + } + String libelle = org.codelutin.i18n.I18n._("lutinui.config.save.tooltip"); + putValue(SHORT_DESCRIPTION, libelle); + } + + public void actionPerformed(ActionEvent e) { + + if (getHandler().prepareSave()) { + + // save model to src + getHandler().getModel().save(); + + // close ui + getUi().dispose(); + } + } +} Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/CancelAction.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/CancelAction.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/CancelAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,43 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.AbstractUIAction; -import org.codelutin.ui.DialogUI; -import org.codelutin.ui.UIHelper; - -import java.awt.event.ActionEvent; - -/** @author chemit */ -public class CancelAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { - private static final long serialVersionUID = 1L; - - public CancelAction(DialogUI<? extends H> dialogUI, boolean showLabel) { - super(null, UIHelper.createActionIcon("cancel-config"), dialogUI); - if (showLabel) { - String text = org.codelutin.i18n.I18n._("lutinui.config.cancel"); - putValue(NAME, text); - putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); - putValue(MNEMONIC_KEY, (int) text.charAt(0)); - } - String libelle = org.codelutin.i18n.I18n._("lutinui.config.cancel.tooltip"); - putValue(SHORT_DESCRIPTION, libelle); - - } - - public void actionPerformed(ActionEvent e) { - getUi().dispose(); - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUI.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUI.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUI.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,105 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.DialogUI; - -import javax.swing.AbstractButton; -import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPasswordField; -import javax.swing.JRadioButton; -import javax.swing.JTextField; - -/** - * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) - * <p/> - * TODO : make jaxx authorized implementing interface for root tag :) - * - * @author chemit - */ -public abstract class DialogConfigUI<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends DialogUI<H> { - - public abstract AbstractButton getOk(); - - public abstract AbstractButton getReset(); - - public abstract AbstractButton getCancel(); - - public JComponent getElement(E key) { - Object id = getObjectById(key.name()); - if (id == null) { - log.error(new NullPointerException("no widget for key "+key)); - return null; - } - if (!(id instanceof JComponent)) { - throw new IllegalArgumentException(id + " is not a JComponent"); - } - return (JComponent) id; - } - - public Object getElementValue(E key) { - JComponent o = getElement(key); - if (o instanceof JPasswordField) { - return new String(((JPasswordField) o).getPassword()); - } - if (o instanceof JTextField) { - return ((JTextField) o).getText(); - } - if (o instanceof JRadioButton) { - return ((JRadioButton) o).isSelected(); - } - if (o instanceof JCheckBox) { - return ((JCheckBox) o).isSelected(); - } - - if (o instanceof JComboBox) { - return ((JComboBox) o).getSelectedItem(); - } - return ""; - } - - public void setElementValue(E key, Object value) { - JComponent o = getElement(key); - - String strValue = value == null ? "" : String.valueOf(value); - if (o instanceof JPasswordField) { - ((JPasswordField) o).setText(strValue); - } - if (o instanceof JTextField) { - ((JTextField) o).setText(strValue); - } - if (o instanceof JRadioButton) { - ((JRadioButton) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); - } - if (o instanceof JCheckBox) { - ((JCheckBox) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); - } - if (o instanceof JComboBox) { - ((JComboBox) o).setSelectedItem(value); - } - } - - public JLabel getElementLabel(E key) { - return (JLabel) getObjectById(key.name() + "Label"); - } - - public void doCheck(E key) { - getHandler().doCheck(key); - } - -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIHandler.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIHandler.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIHandler.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,156 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.DialogUIHandler; -import org.codelutin.util.ConverterUtil; -import org.codelutin.util.config.Config; -import org.codelutin.util.config.Property; - -import javax.swing.JComponent; -import java.awt.Color; -import java.beans.PropertyChangeEvent; -import java.util.EnumMap; -import java.util.EnumSet; - -/** - * DialogUI handler - * - * @author chemit - */ -public abstract class DialogConfigUIHandler<E extends Enum<E>, M extends DialogConfigUIModel<E, ?>, U extends DialogConfigUI<E, ?>> extends DialogUIHandler<M, U> { - - protected DialogConfigUIHandler(U ui, M model) { - super(ui, model); - } - - public void propertyChange(PropertyChangeEvent evt) { - if (log.isDebugEnabled()) { - log.debug(evt.getPropertyName() + " old:" + evt.getOldValue() + ", new:" + evt.getNewValue()); - } - String action = evt.getPropertyName(); - - if (DialogConfigUIModel.CONFIG_PROPERTY_CHANGED.equals(action)) { - // update ui with model values, - populateUI(); - // revalidate form - doCheckAll(); - return; - } - - if (DialogConfigUIModel.MODIFIED_PROPERTY_CHANGED.equals(action)) { - Boolean newValue = (Boolean) evt.getNewValue(); - boolean modified = newValue != null && newValue; - getUi().getReset().setEnabled(modified); - getUi().getOk().setEnabled(modified && getModel().isConfigValid()); - return; - } - - if (DialogConfigUIModel.UNVALID_PROPERTY_CHANGED.equals(action)) { - updateUI(); - return; - } - - throw new IllegalStateException("unimplemented property changed : " + evt + " for " + this); - } - - public void doCheck(E key) { - Object uiValue = getUi().getElementValue(key); - DialogConfigUIModel<E, ?> model = getModel(); - Object currentValue = model.getCurrent().getProperty(key); - if (currentValue == null) { - currentValue = ""; - } else { - currentValue = String.valueOf(currentValue); - } - - model.validateProperty(key, uiValue); - model.changeModifiedState(key, uiValue, currentValue); - } - - public void doCheckAll() { - DialogConfigUIModel<E, ?> model = getModel(); - EnumSet<E> unvalids = EnumSet.noneOf(model.klass); - for (E e : model.getCheckedKeysSet()) { - Object uiValue = getUi().getElementValue(e); - if (!model.isValid(e, uiValue)) { - unvalids.add(e); - } - } - model.setUnvalids(unvalids); - unvalids.clear(); - } - - protected boolean prepareSave() { - DialogConfigUI<E, ?> ui = getUi(); - DialogConfigUIModel<E, ?> model = getModel(); - - Config<E> current = model.getCurrent(); - - if (!model.isConfigValid()) { - log.warn("do not save a unvalid config : " + model.getUnvalids()); - return false; - } - - if (!model.isModified()) { - log.warn("nothing to save"); - return false; - } - EnumSet<E> toTreate = model.getCheckedKeysSet(); - // transfert checkable values from ui to model - for (E key : model.getModifieds()) { - if (!toTreate.contains(key)) { - continue; - } - Object value = ui.getElementValue(key); - Class<?> type = ((Property) key).getType(); - Object newValue = ConverterUtil.convert(type, value); - current.setProperty(key, newValue); - } - - return true; - } - - protected void populateUI() { - U ui = getUi(); - EnumMap<E, Object> map = getModel().getCurrent().getProperties(); - for (E e : getModel().getCheckedKeysSet()) { - Object value = map.get(e); - populateUI(ui, e, value); - } - } - - protected void populateUI(U ui, E key, Object value) { - ui.setElementValue(key, value); - } - - protected void updateUI() { - EnumSet<E> unvalids = getModel().getUnvalids(); - for (E key : unvalids) { - setLabelColor(key, false); - } - for (E key : EnumSet.complementOf(unvalids)) { - setLabelColor(key, true); - } - } - - protected void setLabelColor(E key, boolean valid) { - JComponent component = getUi().getElementLabel(key); - if (component != null && component.isVisible()) { - component.setForeground(valid ? Color.black : Color.red); - } - } - -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIModel.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIModel.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/DialogConfigUIModel.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,220 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.DialogUIModel; -import org.codelutin.util.config.Config; - -import java.util.EnumSet; - -/** - * Abstract config ui model. - * - * @author chemit - */ -public abstract class DialogConfigUIModel<E extends Enum<E>, C extends Config<E>> extends DialogUIModel { - - public static final String CONFIG_PROPERTY_CHANGED = "config"; - public static final String MODIFIED_PROPERTY_CHANGED = "modify"; - public static final String UNVALID_PROPERTY_CHANGED = "unvalid"; - - /** @return a empty config */ - protected abstract C newConfig(); - - /** - * @param key property key - * @param value value to validate - * @return <code>true</code> if given value is valid for property, <code>false>/code> otherwise - */ - protected abstract boolean isValid(E key, Object value); - - /** - * object used to init model and save model, this is an external object. - * <p/> - * The object must have bean read-write properties for each value of E - */ - protected Object src; - - /** current config used in model */ - protected C current; - - /** set of modified properties */ - protected EnumSet<E> modifieds; - - /** set of unvalid properties */ - protected EnumSet<E> unvalids; - - /** enum class */ - protected Class<E> klass; - - /** set of key not to check */ - protected EnumSet<E> uncheckedKeys; - - /** set of all keys checkable */ - protected EnumSet<E> checkedKeysSet; - - protected DialogConfigUIModel(Class<E> klass) { - this.klass = klass; - this.current = newConfig(); - this.modifieds = EnumSet.noneOf(klass); - this.unvalids = EnumSet.noneOf(klass); - } - - public EnumSet<E> getCheckedKeysSet() { - if (checkedKeysSet == null) { - if (uncheckedKeys != null) { - checkedKeysSet = EnumSet.complementOf(uncheckedKeys); - } else { - checkedKeysSet = EnumSet.allOf(klass); - } - } - return checkedKeysSet; - } - - public Object getSrc() { - return src; - } - - public C getCurrent() { - return current; - } - - public EnumSet<E> getUnivserse() { - return getCurrent().getUniverse(); - } - - public EnumSet<E> getModifieds() { - return modifieds; - } - - public EnumSet<E> getUnvalids() { - return unvalids; - } - - public boolean isModified() { - return !modifieds.isEmpty(); - } - - public boolean isConfigValid() { - return unvalids.isEmpty(); - } - - public void populate(Object src) { - this.src = src; - this.current = newConfig(); - if (src != null) { - this.current.copyFrom(src); - } - this.modifieds.clear(); - this.unvalids.clear(); - setModified(false); - firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); - } - - public void reset() { - populate(src); - } - - public void setModified(boolean modified) { - firePropertyChange(MODIFIED_PROPERTY_CHANGED, null, modified); - } - - public void setUnvalid(boolean unvalid) { - firePropertyChange(UNVALID_PROPERTY_CHANGED, null, unvalid); - } - - public void addModified(E key) { - if (!modifieds.contains(key)) { - modifieds.add(key); - log.debug(key); - } - setModified(!modifieds.isEmpty()); - } - - public void removeModified(E key) { - - if (modifieds.contains(key)) { - modifieds.remove(key); - } - setModified(!modifieds.isEmpty()); - } - - public void removeModified(EnumSet<E> keys) { - for (E key : keys) { - if (modifieds.contains(key)) { - modifieds.remove(key); - } - } - setModified(!modifieds.isEmpty()); - } - - public void setUnvalids(EnumSet<E> keys) { - for (E key : keys) { - if (!unvalids.contains(key)) { - unvalids.add(key); - } - } - for (E key : EnumSet.complementOf(keys)) { - if (unvalids.contains(key)) { - unvalids.remove(key); - } - } - setUnvalid(!unvalids.isEmpty()); - } - - - public void addUnvalid(E key) { - if (!unvalids.contains(key)) { - unvalids.add(key); - } - setUnvalid(!unvalids.isEmpty()); - } - - public void removeUnvalid(E key) { - if (unvalids.contains(key)) { - unvalids.remove(key); - } - setUnvalid(!unvalids.isEmpty()); - } - - public void save() { - current.copyTo(src, modifieds); - modifieds.clear(); - // redisplay config - firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); - } - - public void changeModifiedState(E key, Object uiValue, Object currentValue) { - if (uiValue!=null && uiValue.equals(currentValue)) { - removeModified(key); - } else { - addModified(key); - } - } - - public void clear(E key) { - log.info(key); - modifieds.remove(key); - unvalids.remove(key); - } - - protected void validateProperty(E key, Object uiValue) { - if (isValid(key, uiValue)) { - removeUnvalid(key); - } else { - addUnvalid(key); - } - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/ResetAction.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/ResetAction.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/ResetAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,43 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.AbstractUIAction; -import org.codelutin.ui.DialogUI; -import org.codelutin.ui.UIHelper; - -import java.awt.event.ActionEvent; - -/** @author chemit */ -public class ResetAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { - private static final long serialVersionUID = 1L; - - public ResetAction(DialogUI<? extends H> dialogUI, boolean showLabel) { - super(null, UIHelper.createActionIcon("reset-config"), dialogUI); - if (showLabel) { - String text = org.codelutin.i18n.I18n._("lutinui.config.reset"); - putValue(NAME, text); - putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); - putValue(MNEMONIC_KEY, (int) text.charAt(0)); - } - String libelle = org.codelutin.i18n.I18n._("lutinui.config.reset.tooltip"); - putValue(SHORT_DESCRIPTION, libelle); - - } - - public void actionPerformed(ActionEvent e) { - getHandler().getModel().reset(); - } -} \ No newline at end of file Deleted: trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/SaveAction.java =================================================================== --- trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/SaveAction.java 2008-07-08 21:13:41 UTC (rev 703) +++ trunk/lutinjaxx/util/src/main/java/org/codelutin/util/config/SaveAction.java 2008-07-08 21:14:13 UTC (rev 704) @@ -1,50 +0,0 @@ -/** - * ##% Copyright (C) 2008 Code Lutin, Tony Chemit - * This program is free software; you - * can redistribute it and/or modify it under the terms of the GNU General - * Public License as published by the Free Software Foundation; either version 2 - * 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 Public License for more details. You - * should have received a copy of the GNU General Public License along with this - * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - * - Suite 330, Boston, MA 02111-1307, USA. - * ##% - */ -package org.codelutin.ui.config; - -import org.codelutin.ui.AbstractUIAction; -import org.codelutin.ui.DialogUI; -import org.codelutin.ui.UIHelper; - -import java.awt.event.ActionEvent; - -/** @author chemit */ -public class SaveAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { - private static final long serialVersionUID = 1L; - - public SaveAction(DialogUI<? extends H> dialogUI, boolean showLabel) { - super(null, UIHelper.createActionIcon("save-config"), dialogUI); - if (showLabel) { - String text = org.codelutin.i18n.I18n._("lutinui.config.save"); - putValue(NAME, text); - putValue(DISPLAYED_MNEMONIC_INDEX_KEY, 0); - putValue(MNEMONIC_KEY, (int) text.charAt(0)); - } - String libelle = org.codelutin.i18n.I18n._("lutinui.config.save.tooltip"); - putValue(SHORT_DESCRIPTION, libelle); - } - - public void actionPerformed(ActionEvent e) { - - if (getHandler().prepareSave()) { - - // save model to src - getHandler().getModel().save(); - - // close ui - getUi().dispose(); - } - } -}
participants (1)
-
tchemit@users.labs.libre-entreprise.org