Author: tchemit Date: 2008-05-13 23:11:26 +0000 (Tue, 13 May 2008) New Revision: 665 Modified: trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/IdentityUI.java trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/handler/IdentityUIHandler.java trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/model/IdentityUIModel.java trunk/lutinvcs/ui/jaxx/src/main/uimodel/org/codelutin/vcs/ui/JaxxIdentityUI.jaxx Log: use generic config dialog for identity Modified: trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/IdentityUI.java =================================================================== --- trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/IdentityUI.java 2008-05-13 23:08:48 UTC (rev 664) +++ trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/IdentityUI.java 2008-05-13 23:11:26 UTC (rev 665) @@ -1,5 +1,5 @@ /** - * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * ##% 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 @@ -10,104 +10,15 @@ * 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.vcs.ui; -import org.codelutin.ui.DialogUI; -import org.codelutin.ui.FormElement; +import org.codelutin.ui.config.DialogConfigUI; +import org.codelutin.util.config.IdentityConfigProperty; import org.codelutin.vcs.ui.handler.IdentityUIHandler; -import javax.swing.AbstractButton; -import javax.swing.JCheckBox; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPasswordField; -import javax.swing.JRadioButton; -import javax.swing.JTextField; -import java.util.Arrays; - /** @author chemit */ -public abstract class IdentityUI extends DialogUI<IdentityUIHandler> { +public abstract class IdentityUI extends DialogConfigUI<IdentityConfigProperty, IdentityUIHandler> { - public enum Element implements FormElement<IdentityUI> { - firstName, - lastName, - email; - - public Object getValue(IdentityUI ui) { - return ui.getElementValue(this); - } - - public void setValue(IdentityUI ui, String value) { - //TODO - JComponent component = ui.getElement(this); - } - - public JLabel getLabel(IdentityUI ui) { - return ui.getElementLabel(this); - } - } - - public abstract AbstractButton getOk(); - - public abstract AbstractButton getReset(); - - public abstract AbstractButton getCancel(); - - - public String getElementValue(Element element) { - JComponent o = getElement(element); - if (o instanceof JPasswordField) { - return Arrays.toString(((JPasswordField) o).getPassword()); - } - if (o instanceof JTextField) { - return ((JTextField) o).getText(); - } - if (o instanceof JRadioButton) { - return String.valueOf(((JRadioButton) o).isSelected()); - } - if (o instanceof JCheckBox) { - return String.valueOf(((JCheckBox) o).isSelected()); - } - return ""; - } - - public JLabel getElementLabel(Element element) { - return (JLabel) getObjectById(element.name() + "Label"); - } - - public JComponent getElement(Element element) { - return (JComponent) getObjectById(element.name()); - } - - public abstract JTextField getFirstName(); - - public abstract JTextField getLastName(); - - public abstract JTextField getEmail(); - - public abstract JLabel getFirstNameLabel(); - - public abstract JLabel getLastNameLabel(); - - public abstract JLabel getEmailLabel(); - - public boolean isConfigValid() { - return getHandler().isConfigValid(); - } - - protected void reset() { - getHandler().reset(); - } - - protected void save() { - getHandler().save(); - dispose(); - } - - protected void doCheck(Element element) { - getHandler().doCheck(element); - } - } \ No newline at end of file Modified: trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/handler/IdentityUIHandler.java =================================================================== --- trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/handler/IdentityUIHandler.java 2008-05-13 23:08:48 UTC (rev 664) +++ trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/handler/IdentityUIHandler.java 2008-05-13 23:11:26 UTC (rev 665) @@ -14,149 +14,17 @@ */ package org.codelutin.vcs.ui.handler; -import org.codelutin.ui.DialogUIHandler; +import org.codelutin.ui.config.DialogConfigUIHandler; +import org.codelutin.util.config.IdentityConfigProperty; import org.codelutin.vcs.ui.IdentityUI; -import static org.codelutin.vcs.ui.IdentityUI.Element; -import static org.codelutin.vcs.ui.IdentityUI.Element.email; -import static org.codelutin.vcs.ui.IdentityUI.Element.firstName; -import static org.codelutin.vcs.ui.IdentityUI.Element.lastName; -import static org.codelutin.vcs.ui.IdentityUI.Element.values; import org.codelutin.vcs.ui.model.IdentityUIModel; -import javax.swing.JComponent; -import javax.swing.text.JTextComponent; -import java.awt.Color; -import java.beans.PropertyChangeEvent; -import java.util.ArrayList; -import java.util.List; - /** @author chemit */ -public class IdentityUIHandler extends DialogUIHandler<IdentityUIModel, IdentityUI> { +public class IdentityUIHandler extends DialogConfigUIHandler<IdentityConfigProperty, IdentityUIModel, IdentityUI> { - protected List<Element> errors; - - public IdentityUIHandler(IdentityUI ui,IdentityUIModel model) { + public IdentityUIHandler(IdentityUI ui, IdentityUIModel model) { super(ui, model); ui.setHandler(this); - errors = new ArrayList<Element>(); } - - public void propertyChange(PropertyChangeEvent evt) { - if (log.isDebugEnabled()) { - log.debug(evt.getPropertyName() + " old:" + evt.getOldValue() + ", new:" + evt.getNewValue()); - } - String action = evt.getPropertyName(); - - if (IdentityUIModel.CONFIG_PROPERTY_CHANGED.equals(action)) { - // update ui with model values, - populateUI((IdentityUIModel) evt.getNewValue()); - doCheck(null); - return; - } - - if (IdentityUIModel.MODIFIED_PROPERTY_CHANGED.equals(action)) { - Boolean newValue = (Boolean) evt.getNewValue(); - boolean modified = newValue != null && newValue; - getUi().getReset().setEnabled(modified); - return; - } - throw new IllegalStateException("unimplemented property changed : " + evt + " for " + this); - } - - public synchronized List<Element> validateModel(Element element) { - errors.clear(); - IdentityUI ui = getUi(); - if (element != null) { - // compute modifed - setModified(element, ui); - } - checkData(ui.getFirstName(), firstName); - checkData(ui.getLastName(), lastName); - checkData(ui.getEmail(), email); - - return errors; - } - - public void setModified(Element element, IdentityUI ui) { - boolean modify = false; - IdentityUIModel model = getModel(); - - String value = ui.getElementValue(element); - - switch (element) { - case email: - modify = !model.getCurrent().getEmail().equals(value); - break; - case firstName: - modify = !model.getCurrent().getFirstName().equals(value); - break; - case lastName: - modify = !model.getCurrent().getLastName().equals(value); - break; - } - if (modify) { - model.addModified(element); - } else if (model.getModifieds().contains(element)) { - model.removeModified(element); - } - } - - public void doCheck(Element element) { - List<Element> errors = validateModel(element); - updateUI(errors); - } - - public boolean isConfigValid() { - return errors.isEmpty(); - } - - public void reset() { - getModel().reset(); - } - - protected void populateUI(IdentityUIModel model) { - IdentityUI ui = getUi(); - ui.getFirstName().setText(model.getCurrent().getFirstName()); - ui.getLastName().setText(model.getCurrent().getLastName()); - ui.getEmail().setText(model.getCurrent().getEmail()); - } - - protected void updateUI(List<Element> errors) { - IdentityUI ui = getUi(); - boolean valid = isConfigValid(); - ui.getOk().setEnabled(valid && getModel().isModified()); - for (Element element : values()) { - setLabelColor(errors, element); - } - } - - protected void checkData(JTextComponent component, Element errorName) { - if (component.getText().isEmpty()) { - errors.add(errorName); - } - } - - protected void checkData(boolean notValid, Element errorName) { - if (notValid) { - errors.add(errorName); - } - } - - protected void setLabelColor(List<Element> errors, Element element) { - JComponent component = getUi().getElementLabel(element); - if (component != null && component.isVisible()) { - component.setForeground(errors.contains(element) ? Color.red : Color.black); - } - } - - public void save() { - if (!getModel().isModified()) { - log.warn("nothing to save"); - } - for (Element element : getModel().getModifieds()) { - String value = getUi().getElementValue(element); - getModel().save(element, value); - log.info("save " + element); - } - } + } \ No newline at end of file Modified: trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/model/IdentityUIModel.java =================================================================== --- trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/model/IdentityUIModel.java 2008-05-13 23:08:48 UTC (rev 664) +++ trunk/lutinvcs/ui/common/src/main/java/org/codelutin/vcs/ui/model/IdentityUIModel.java 2008-05-13 23:11:26 UTC (rev 665) @@ -15,104 +15,39 @@ package org.codelutin.vcs.ui.model; -import org.apache.commons.beanutils.BeanUtils; -import org.codelutin.ui.DialogUIModel; +import org.codelutin.ui.config.DialogConfigUIModel; import org.codelutin.util.config.IdentityConfig; import org.codelutin.util.config.IdentityConfigProperty; -import org.codelutin.vcs.VCSConfigFactory; -import org.codelutin.vcs.ui.IdentityUI.Element; +import org.codelutin.util.config.SimpleIdentityConfig; -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.List; - /** - * Model of a a confirmation of action on entries (single type of action possible) + * Model of Identity configuration edit ui * * @author chemit */ -public class IdentityUIModel extends DialogUIModel { +public class IdentityUIModel extends DialogConfigUIModel<IdentityConfigProperty, IdentityConfig> { - public static final String CONFIG_PROPERTY_CHANGED = "config"; - public static final String MODIFIED_PROPERTY_CHANGED = "modify"; - - protected Object src; - - protected IdentityConfig current; - protected EnumSet<IdentityConfigProperty> mods; - - protected List<Element> modifieds; - public IdentityUIModel() { - mods = EnumSet.noneOf(IdentityConfigProperty.class); - - modifieds = new ArrayList<Element>(); - current = VCSConfigFactory.newIdentity(null, null, null); + super(IdentityConfigProperty.class); } - public void populate(Object src) { - this.src = src; - this.current.copyFrom(src); - - setModified(false); - firePropertyChange(CONFIG_PROPERTY_CHANGED, null, this); + protected IdentityConfig newConfig() { + return new SimpleIdentityConfig(); } - public void reset() { - populate(src); - } - - public IdentityConfig getCurrent() { - return current; - } - - public boolean isModified() { - return !modifieds.isEmpty(); - } - - public List<Element> getModifieds() { - return modifieds; - } - - public void addModified(Element elementname) { - if (!modifieds.contains(elementname)) { - modifieds.add(elementname); - } - setModified(!modifieds.isEmpty()); - } - - public void removeModified(Element... elementnames) { - for (Element elementname : elementnames) { - if (modifieds.contains(elementname)) { - modifieds.remove(elementname); - } - setModified(!modifieds.isEmpty()); - } - - } - - public void setModified(boolean modified) { - if (!modified) { - modifieds.clear(); - } - firePropertyChange(MODIFIED_PROPERTY_CHANGED, null, modified); - } - - public void save(Element element, String value) { - switch (element) { + protected boolean isValid(IdentityConfigProperty key, Object value) { + // each propertie can not be null, nor empty + boolean result = value != null && !String.valueOf(value).isEmpty(); + switch (key) { case email: + //TODO Check email validity + break; case firstName: case lastName: - try { - BeanUtils.setProperty(src, element.name(), value); - } catch (IllegalAccessException e) { - throw new IllegalStateException(e); - } catch (InvocationTargetException e) { - throw new IllegalStateException(e); - } + // nothing more to check break; } + return result; } } \ No newline at end of file Modified: trunk/lutinvcs/ui/jaxx/src/main/uimodel/org/codelutin/vcs/ui/JaxxIdentityUI.jaxx =================================================================== --- trunk/lutinvcs/ui/jaxx/src/main/uimodel/org/codelutin/vcs/ui/JaxxIdentityUI.jaxx 2008-05-13 23:08:48 UTC (rev 664) +++ trunk/lutinvcs/ui/jaxx/src/main/uimodel/org/codelutin/vcs/ui/JaxxIdentityUI.jaxx 2008-05-13 23:11:26 UTC (rev 665) @@ -1,4 +1,5 @@ <IdentityUI modal='true' resizable='false'> + <script>import org.codelutin.util.config.IdentityConfigProperty;</script> <style source="config.css"/> <JToolBar id='identityHead' opaque='false'> <JLabel text='lutinvcs.identity.message'/> @@ -7,7 +8,7 @@ <cell fill='both'/> </row> </Table> - <JButton id='reset' icon='{org.codelutin.ui.UIHelper.createActionIcon("revert")}' onActionPerformed="reset()" borderPainted='false'/> + <JButton id='reset' action='{newAction(org.codelutin.ui.config.ResetAction.class,false)}' borderPainted='false'/> <JButton id='help' action='{newAction(org.codelutin.vcs.ui.action.HelpAction.class)}' borderPainted='false'/> </JToolBar> <Table fill='both' insets="1,1,1,1"> @@ -20,7 +21,7 @@ <JLabel id='firstNameLabel'/> </cell> <cell> - <JTextField id='firstName' onKeyReleased="doCheck(Element.firstName)"/> + <JTextField id='firstName' onKeyReleased="doCheck(IdentityConfigProperty.firstName)"/> </cell> </row> <row fill='horizontal'> @@ -28,7 +29,7 @@ <JLabel id='lastNameLabel'/> </cell> <cell> - <JTextField id='lastName' onKeyReleased="doCheck(Element.lastName)"/> + <JTextField id='lastName' onKeyReleased="doCheck(IdentityConfigProperty.lastName)"/> </cell> </row> <row fill='horizontal'> @@ -36,7 +37,7 @@ <JLabel id='emailLabel'/> </cell> <cell> - <JTextField id='email' onKeyReleased="doCheck(Element.email)"/> + <JTextField id='email' onKeyReleased="doCheck(IdentityConfigProperty.email)"/> </cell> </row> </Table> @@ -45,10 +46,10 @@ </row> <row fill='horizontal'> <cell weightx='1'> - <JButton id='ok' onActionPerformed="save()"/> + <JButton id='ok' action='{newAction(org.codelutin.ui.config.SaveAction.class,true)}'/> </cell> <cell weightx='1'> - <JButton id='cancel' onActionPerformed="dispose()"/> + <JButton id='cancel' action='{newAction(org.codelutin.ui.config.CancelAction.class,true)}'/> </cell> </row> </Table>