[Buix-commits] r1256 - in jaxx/trunk/jaxx-runtime-swing: . src/main/java/jaxx/runtime/swing
Author: tchemit Date: 2009-03-03 19:33:04 +0000 (Tue, 03 Mar 2009) New Revision: 1256 Added: jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/WizardModel.java Modified: jaxx/trunk/jaxx-runtime-swing/changelog.txt Log: add a simple wizrd model Modified: jaxx/trunk/jaxx-runtime-swing/changelog.txt =================================================================== --- jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-03 16:38:42 UTC (rev 1255) +++ jaxx/trunk/jaxx-runtime-swing/changelog.txt 2009-03-03 19:33:04 UTC (rev 1256) @@ -1,5 +1,6 @@ 1.3 ?? 200903?? * 20090303 [chemit] - in BlockingLayerUI, add a new state 'useIcon' to enable/disable use of the action icon + - add a simple WizardModel * 20090301 [chemit] - add usefull methods in JAXXButtonGroup 1.2 letellier 2009022? Added: jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/WizardModel.java =================================================================== --- jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/WizardModel.java (rev 0) +++ jaxx/trunk/jaxx-runtime-swing/src/main/java/jaxx/runtime/swing/WizardModel.java 2009-03-03 19:33:04 UTC (rev 1256) @@ -0,0 +1,190 @@ +package jaxx.runtime.swing; + +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.util.List; + +/** + * Un modèle de wizard. + * + * @param <E> le type de l'énumération contenant les etapes. + * + * @author tony + */ +public class WizardModel<E extends Enum<E>> { + + public static final String STEPS_PROPERTY_NAME = "steps"; + public static final String STEP_PROPERTY_NAME = "step"; + public static final String PREVIOUS_STEP_PROPERTY_NAME = "previousStep"; + public static final String NEXT_STEP_PROPERTY_NAME = "nextStep"; + public static final String VALID_STEP_PROPERTY_NAME = "validStep"; + /** + * le type d'une etape du model (doit etre une enumeration) + */ + protected final Class<E> stepClass; + /** + * Toutes les étapes à passer + */ + protected List<E> steps; + /** + * L'étape courante + */ + protected E step; + /** + * drapeau pour valider l'état de l'étape courante + */ + protected boolean validStep; + /** + * pour propager les changements dans le modèle vers l'ui + */ + protected PropertyChangeSupport pcs; + + public WizardModel(Class<E> stepClass, E... steps) { + this.stepClass = stepClass; + this.pcs = new PropertyChangeSupport(this); + this.steps = new java.util.ArrayList<E>(); + if (steps.length > 0) { + setSteps(steps); + } + } + + public void start() { + if (steps.isEmpty()) { + throw new IllegalStateException("can not start, no step found"); + } + step = null; + E startStep = steps.get(0); + setStep(startStep); + } + + public void gotoNextStep() { + E nextStep = getNextStep(); + if (nextStep == null) { + throw new IllegalStateException("no next step to go"); + } + setStep(nextStep); + } + + public void gotoPreviousStep() { + E previousStep = getPreviousStep(); + if (previousStep == null) { + throw new IllegalStateException("no previous step to go"); + } + setStep(previousStep); + } + + public void gotoStep(E e) { + if (e == null) { + throw new NullPointerException("step can not be null"); + } + if (!steps.contains(e)) { + throw new IllegalStateException("step " + e + " is not in universe of steps (" + steps + ")"); + } + setStep(e); + } + + public E getStep() { + return step; + } + + public int getStepIndex(E s) { + int index = steps.indexOf(s); + return index; + } + + public boolean isValidStep() { + return validStep; + } + + public E getPreviousStep() { + int index = getStepIndex(step); + if (index < 1) { + // si pas de step ou sur premier step + return null; + } + return steps.get(index - 1); + } + + public E getNextStep() { + int index = getStepIndex(step); + if (index == -1 || index == steps.size() - 1) { + // si pas de step positionne ou dernier etape + return null; + } + return steps.get(index + 1); + } + + public List<E> getSteps() { + return steps; + } + + public boolean validate(E s) { + return step != null; + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + pcs.addPropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + pcs.removePropertyChangeListener(listener); + } + + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.removePropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListeners() { + for (PropertyChangeListener l : pcs.getPropertyChangeListeners()) { + pcs.removePropertyChangeListener(l); + } + } + protected Class<E> getStepClass() { + return stepClass; + } + + protected void setStep(E step) { + E oldValue = this.step; + this.step = step; + firePropertyChange(STEP_PROPERTY_NAME, oldValue, step); + // la propriete nextStep peut avoir changee + firePropertyChange(NEXT_STEP_PROPERTY_NAME, null, getNextStep()); + // la propriete previousStep peut avoir changee + firePropertyChange(PREVIOUS_STEP_PROPERTY_NAME, null, getPreviousStep()); + validate(); + } + + /** + * Change l'univers des etapes. + * + * Note: on presume ici que l'étape courante est toujours la meme. + * + * @param steps le nouvel univers des etapes + */ + protected void setSteps(E... steps) { + List<E> oldValue = this.steps; + this.steps = java.util.Collections.unmodifiableList(java.util.Arrays.asList(steps)); + firePropertyChange(STEPS_PROPERTY_NAME, oldValue, this.steps); + // la propriete nextStep peut avoir changee + firePropertyChange(NEXT_STEP_PROPERTY_NAME, null, getNextStep()); + } + + protected void validate() { + if (step == null) { + // pas de validation quand aucune etape n'est sélectionnée + return; + } + boolean validate = validate(step); + this.validStep = validate; + // toujours forcer la propagation + firePropertyChange(VALID_STEP_PROPERTY_NAME, null, validStep); + } + + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + pcs.firePropertyChange(propertyName, oldValue, newValue); + } +}
participants (1)
-
tchemit@users.labs.libre-entreprise.org