Author: tchemit Date: 2008-05-14 16:58:23 +0000 (Wed, 14 May 2008) New Revision: 668 Removed: trunk/lutinui/src/main/java/org/codelutin/ui/config/ModifyAction.java Modified: trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUI.java trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIHandler.java trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIModel.java trunk/lutinui/src/main/java/org/codelutin/ui/config/SaveAction.java Log: propri?\195?\169t?\195?\169 typ?\195?\169 + r?\195?\169cup?\195?\169ration Modified: trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUI.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUI.java 2008-05-13 23:16:31 UTC (rev 667) +++ trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUI.java 2008-05-14 16:58:23 UTC (rev 668) @@ -24,7 +24,6 @@ import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JTextField; -import java.util.Arrays; /** * A abstract dialog contract to be realised by a dialogUI (WindowEvent adapter) @@ -42,43 +41,53 @@ public abstract AbstractButton getCancel(); public JComponent getElement(E key) { - return (JComponent) getObjectById(key.name()); + 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 String getElementValue(E key) { + public Object getElementValue(E key) { JComponent o = getElement(key); if (o instanceof JPasswordField) { - return Arrays.toString(((JPasswordField) o).getPassword()); + return new String(((JPasswordField) o).getPassword()); } if (o instanceof JTextField) { return ((JTextField) o).getText(); } if (o instanceof JRadioButton) { - return String.valueOf(((JRadioButton) o).isSelected()); + return ((JRadioButton) o).isSelected(); } if (o instanceof JCheckBox) { - return String.valueOf(((JCheckBox) o).isSelected()); + return ((JCheckBox) o).isSelected(); } if (o instanceof JComboBox) { - return String.valueOf(((JComboBox) o).getSelectedItem()); + 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(value + ""); + ((JPasswordField) o).setText(strValue); } if (o instanceof JTextField) { - ((JTextField) o).setText(value + ""); + ((JTextField) o).setText(strValue); } if (o instanceof JRadioButton) { - ((JRadioButton) o).setSelected(Boolean.valueOf(value + "")); + ((JRadioButton) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); } if (o instanceof JCheckBox) { - ((JCheckBox) o).setSelected(Boolean.valueOf(value + "")); + ((JCheckBox) o).setSelected(Boolean.valueOf(strValue.isEmpty() ? "false" : strValue)); } if (o instanceof JComboBox) { ((JComboBox) o).setSelectedItem(value); @@ -93,5 +102,4 @@ getHandler().doCheck(key); } - } \ No newline at end of file Modified: trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIHandler.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIHandler.java 2008-05-13 23:16:31 UTC (rev 667) +++ trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIHandler.java 2008-05-14 16:58:23 UTC (rev 668) @@ -15,13 +15,15 @@ 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; -import java.util.Map; /** * DialogUI handler @@ -30,8 +32,6 @@ */ public abstract class DialogConfigUIHandler<E extends Enum<E>, M extends DialogConfigUIModel<E, ?>, U extends DialogConfigUI<E, ?>> extends DialogUIHandler<M, U> { - protected ModifyAction<E, ? extends DialogConfigUIHandler<E, ?, ?>> modifyAction; - protected DialogConfigUIHandler(U ui, M model) { super(ui, model); } @@ -46,7 +46,7 @@ // update ui with model values, populateUI(); // revalidate form - doCheck(null); + doCheckAll(); return; } @@ -67,22 +67,75 @@ } public void doCheck(E key) { - if (key == null) { - getModifyAction().doCheckAll(); + Object uiValue = getUi().getElementValue(key); + DialogConfigUIModel<E, ?> model = getModel(); + Object currentValue = model.getCurrent().getProperty(key); + if (currentValue == null) { + currentValue = ""; } else { - getModifyAction().doCheck(key); + 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(); - M model = getModel(); - EnumMap<E, Object> map = model.getCurrent().getProperties(); - for (Map.Entry<E, Object> entry : map.entrySet()) { - ui.setElementValue(entry.getKey(), entry.getValue()); + 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) { @@ -100,12 +153,4 @@ } } - protected ModifyAction<E, ? extends DialogConfigUIHandler<E, ?, ?>> getModifyAction() { - if (modifyAction == null) { - modifyAction = new ModifyAction<E, DialogConfigUIHandler<E, ?, ?>>(getUi().getHandler().getUi()); - } - return modifyAction; - } - - } \ No newline at end of file Modified: trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIModel.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIModel.java 2008-05-13 23:16:31 UTC (rev 667) +++ trunk/lutinui/src/main/java/org/codelutin/ui/config/DialogConfigUIModel.java 2008-05-14 16:58:23 UTC (rev 668) @@ -56,8 +56,15 @@ /** 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(); @@ -65,6 +72,17 @@ 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; } @@ -131,23 +149,23 @@ } public void removeModified(EnumSet<E> keys) { - for (E elementname : keys) { - if (modifieds.contains(elementname)) { - modifieds.remove(elementname); + for (E key : keys) { + if (modifieds.contains(key)) { + modifieds.remove(key); } } setModified(!modifieds.isEmpty()); } - public void addUnvalid(EnumSet<E> keys) { - for (E name : keys) { - if (!unvalids.contains(name)) { - unvalids.add(name); + public void setUnvalids(EnumSet<E> keys) { + for (E key : keys) { + if (!unvalids.contains(key)) { + unvalids.add(key); } } - for (E name : EnumSet.complementOf(keys)) { - if (unvalids.contains(name)) { - unvalids.remove(name); + for (E key : EnumSet.complementOf(keys)) { + if (unvalids.contains(key)) { + unvalids.remove(key); } } setUnvalid(!unvalids.isEmpty()); @@ -174,4 +192,26 @@ // redisplay config firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); } + + public void changeModifiedState(E key, Object uiValue, Object currentValue) { + if (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/lutinui/src/main/java/org/codelutin/ui/config/ModifyAction.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/config/ModifyAction.java 2008-05-13 23:16:31 UTC (rev 667) +++ trunk/lutinui/src/main/java/org/codelutin/ui/config/ModifyAction.java 2008-05-14 16:58:23 UTC (rev 668) @@ -1,109 +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 java.awt.event.ActionEvent; -import java.util.EnumSet; - -/** @author chemit */ -public class ModifyAction<E extends Enum<E>, H extends DialogConfigUIHandler<E, ?, ?>> extends AbstractUIAction<H> { - - private static final long serialVersionUID = 1L; - - protected transient Boolean checkAll; - - protected transient E key; - - protected transient String uiValue; - - protected transient Object currentValue; - - protected transient EnumSet<E> unvalids; - - public ModifyAction(DialogConfigUI<E, ? extends H> dialogUI) { - super(null, null, dialogUI); - } - - public void doCheckAll() { - if (unvalids == null) { - unvalids = EnumSet.noneOf(getHandler().getModel().klass); - } - checkAll = true; - try { - for (E e : getHandler().getModel().getCurrent().getUniverse()) { - doCheck(e); - } - getHandler().getModel().addUnvalid(unvalids); - } finally { - unvalids.clear(); - this.key = null; - this.uiValue = null; - this.currentValue = null; - this.checkAll = null; - } - } - - public void doCheck(E key) { - this.key = key; - this.uiValue = getHandler().getUi().getElementValue(key); - DialogConfigUIModel<E, ?> model = getHandler().getModel(); - Object currentValue = model.getCurrent().getProperty(key); - if (currentValue == null) { - currentValue = ""; - } - this.currentValue = String.valueOf(currentValue); - if (checkAll!=null && checkAll) { - if (!model.isValid(key, uiValue)) { - unvalids.add(key); - } - } else { - actionPerformed(new ActionEvent(getHandler().getUi().getElement(key), 0, "modify-" + key.name())); - } - } - - public void actionPerformed(ActionEvent e) { - try { - DialogConfigUIModel<E, ?> model = getHandler().getModel(); - - validateProperty(model); - - changeModifiedState(model); - - } finally { - this.key = null; - this.uiValue = null; - this.currentValue = null; - this.checkAll = null; - } - } - - public void changeModifiedState(DialogConfigUIModel<E, ?> model) { - if (uiValue.equals(currentValue)) { - model.removeModified(key); - } else { - model.addModified(key); - } - } - - protected void validateProperty(DialogConfigUIModel<E, ?> model) { - if (model.isValid(key, uiValue)) { - model.removeUnvalid(key); - } else { - model.addUnvalid(key); - } - } -} \ No newline at end of file Modified: trunk/lutinui/src/main/java/org/codelutin/ui/config/SaveAction.java =================================================================== --- trunk/lutinui/src/main/java/org/codelutin/ui/config/SaveAction.java 2008-05-13 23:16:31 UTC (rev 667) +++ trunk/lutinui/src/main/java/org/codelutin/ui/config/SaveAction.java 2008-05-14 16:58:23 UTC (rev 668) @@ -17,7 +17,6 @@ import org.codelutin.ui.AbstractUIAction; import org.codelutin.ui.DialogUI; import org.codelutin.ui.UIHelper; -import org.codelutin.util.config.Config; import java.awt.event.ActionEvent; @@ -35,33 +34,17 @@ } String libelle = org.codelutin.i18n.I18n._("lutinui.config.save.tooltip"); putValue(SHORT_DESCRIPTION, libelle); - } public void actionPerformed(ActionEvent e) { - H handler = getHandler(); - DialogConfigUI<E, ?> ui = handler.getUi(); - DialogConfigUIModel<E, ?> model = handler.getModel(); - Config<E> current = model.getCurrent(); - if (!model.isConfigValid()) { - log.warn("do not save a unvalid config : " + model.getUnvalids()); - return; - } + if (getHandler().prepareSave()) { - if (!model.isModified()) { - log.warn("nothing to save"); - return; - } - // transfert values from ui to model - for (E key : model.getModifieds()) { - String value = ui.getElementValue(key); - current.setProperty(key, value); - } - // save model to src - model.save(); + // save model to src + getHandler().getModel().save(); - // close ui - ui.dispose(); + // close ui + getUi().dispose(); + } } }