r9 - in trunk: jmexico-model/src/main/java/fr/reseaumexico/model jmexico-model/src/main/java/fr/reseaumexico/model/event jmexico-model/src/main/xmi jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model jmexico-swing-editor/src/main/resources/i18n
Author: sletellier Date: 2011-12-21 12:31:51 +0100 (Wed, 21 Dec 2011) New Revision: 9 Url: http://forge.codelutin.com/repositories/revision/jmexico/9 Log: Create listener api for scenario and input design Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/InputDesignImpl.java trunk/jmexico-model/src/main/java/fr/reseaumexico/model/ScenarioImpl.java trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignFactorEvent.java trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignScenarioEvent.java trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/ScenarioFactorValueEvent.java Modified: trunk/jmexico-model/src/main/xmi/mexico.zargo trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model/InputDesignTableModel.java trunk/jmexico-swing-editor/src/main/resources/i18n/jmexico-swing-editor_fr_FR.properties Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/InputDesignImpl.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/InputDesignImpl.java (rev 0) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/InputDesignImpl.java 2011-12-21 11:31:51 UTC (rev 9) @@ -0,0 +1,170 @@ +/* + * #%L + * JMexico :: Model + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Réseau Mexico, 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 fr.reseaumexico.model; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import fr.reseaumexico.model.event.InputDesignFactorEvent; +import fr.reseaumexico.model.event.InputDesignFactorListener; +import fr.reseaumexico.model.event.InputDesignScenarioEvent; +import fr.reseaumexico.model.event.InputDesignScenarioListener; +import fr.reseaumexico.model.event.ScenarioFactorValueEvent; +import fr.reseaumexico.model.event.ScenarioFactorValueListener; + +import javax.swing.event.EventListenerList; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Collection; +import java.util.Map; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class InputDesignImpl extends InputDesign { + + private static final long serialVersionUID = 1L; + + protected final EventListenerList scenarioListenerList; + protected final EventListenerList factorListenerList; + + protected final Map<Scenario, ScenarioFactorValueListener> scenarioFactorValueListenerMap; + + public InputDesignImpl() { + this.scenarioFactorValueListenerMap = Maps.newHashMap(); + this.scenarioListenerList = new EventListenerList(); + this.factorListenerList = new EventListenerList(); + addPropertyChangeListener(PROPERTY_SCENARIOS, new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent propertyChangeEvent) { + Scenario newValue = (Scenario) propertyChangeEvent.getNewValue(); + Scenario oldValue = (Scenario) propertyChangeEvent.getOldValue(); + if (newValue == null) { + // was removed + fireInputDesignScenarioRemoved(oldValue); + } + + if (oldValue == null) { + // was added + fireInputDesignScenarioAdded(newValue); + } + } + }); + } + + @Override + public void addScenarios(final Scenario scenarios) { + if (this.scenarios == null) { + this.scenarios = Lists.newArrayList(); + } + super.addScenarios(scenarios); + + ScenarioFactorValueListener scenarioFactorValueListener = new ScenarioFactorValueListener() { + + @Override + public void onFactorValueChange(ScenarioFactorValueEvent event) { + fireFactorChanged(scenarios, event.getFactorId(), event.getFactorOldValue(), event.getFactorNewValue()); + } + }; + + // keep listener + scenarioFactorValueListenerMap.put(scenarios, scenarioFactorValueListener); + scenarios.addFactorValueListener(scenarioFactorValueListener); + } + + @Override + public void addAllScenarios(Collection<Scenario> scenarios) { + for (Scenario scenario : scenarios) { + addScenarios(scenario); + } + } + + public void setScenarios(Collection<Scenario> scenarios) { + addAllScenarios(scenarios); + } + + @Override + public boolean removeScenarios(Scenario scenarios) { + + // Remove listener + scenarios.removeFactorValueListener(scenarioFactorValueListenerMap.get(scenarios)); + scenarioFactorValueListenerMap.remove(scenarios); + + return super.removeScenarios(scenarios); + } + + @Override + public boolean removeAllScenarios(Collection<Scenario> scenarios) { + boolean removed = false; + for (Scenario scenario : scenarios) { + removed = removeScenarios(scenario); + } + return removed; + } + + @Override + public void addInputDesignScenarioListener(InputDesignScenarioListener scenarioListener) { + scenarioListenerList.add(InputDesignScenarioListener.class, scenarioListener); + } + + @Override + public void removeInputDesignScenarioListener(InputDesignScenarioListener scenarioListener) { + scenarioListenerList.remove(InputDesignScenarioListener.class, scenarioListener); + } + + @Override + public void addInputDesignFactorListener(InputDesignFactorListener factorListener) { + factorListenerList.add(InputDesignFactorListener.class, factorListener); + } + + @Override + public void removeInputDesignFactorListener(InputDesignFactorListener factorListener) { + factorListenerList.remove(InputDesignFactorListener.class, factorListener); + } + + protected void fireInputDesignScenarioAdded(Scenario scenario) { + InputDesignScenarioEvent event = new InputDesignScenarioEvent(this, scenario); + InputDesignScenarioListener[] listeners = scenarioListenerList.getListeners(InputDesignScenarioListener.class); + for (InputDesignScenarioListener listener : listeners) { + listener.onScenarioAdded(event); + } + } + + protected void fireInputDesignScenarioRemoved(Scenario scenario) { + InputDesignScenarioEvent event = new InputDesignScenarioEvent(this, scenario); + InputDesignScenarioListener[] listeners = scenarioListenerList.getListeners(InputDesignScenarioListener.class); + for (InputDesignScenarioListener listener : listeners) { + listener.onScenarioRemoved(event); + } + } + + protected void fireFactorChanged(Scenario scenario, String factorId, Object oldValue, Object newValue) { + InputDesignFactorEvent event = new InputDesignFactorEvent(this, scenario, factorId, oldValue, newValue); + InputDesignFactorListener[] listeners = factorListenerList.getListeners(InputDesignFactorListener.class); + for (InputDesignFactorListener listener : listeners) { + listener.onFactorValueChange(event); + } + } +} Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/ScenarioImpl.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/ScenarioImpl.java (rev 0) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/ScenarioImpl.java 2011-12-21 11:31:51 UTC (rev 9) @@ -0,0 +1,77 @@ +/* + * #%L + * JMexico :: Model + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Réseau Mexico, 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 fr.reseaumexico.model; + +import com.google.common.collect.Maps; +import fr.reseaumexico.model.event.ScenarioFactorValueEvent; +import fr.reseaumexico.model.event.ScenarioFactorValueListener; + +import javax.swing.event.EventListenerList; +import java.util.Map; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class ScenarioImpl extends Scenario { + + private static final long serialVersionUID = -5655227169239067227L; + + protected final EventListenerList factorListenerList; + + public ScenarioImpl() { + this.factorListenerList = new EventListenerList(); + } + + @Override + public void setFactorValue(Factor factor, Object value) { + Map<Factor, Object> oldFactorValues = getFactorValues(); + if (factorValues == null) { + factorValues = Maps.newHashMap(); + } + Object oldValue = factorValues.get(factor); + factorValues.put(factor, value); + + firePropertyChange(PROPERTY_FACTOR_VALUES, oldFactorValues, factorValues); + fireFactorValueChanged(factor.getId(), oldValue, value); + } + + @Override + public void addFactorValueListener(ScenarioFactorValueListener factorListener) { + factorListenerList.add(ScenarioFactorValueListener.class, factorListener); + } + + @Override + public void removeFactorValueListener(ScenarioFactorValueListener factorListener) { + factorListenerList.remove(ScenarioFactorValueListener.class, factorListener); + } + + protected void fireFactorValueChanged(String factorId, Object oldValue, Object newValue) { + ScenarioFactorValueEvent event = new ScenarioFactorValueEvent(this, factorId, oldValue, newValue); + ScenarioFactorValueListener[] listeners = factorListenerList.getListeners(ScenarioFactorValueListener.class); + for (ScenarioFactorValueListener listener : listeners) { + listener.onFactorValueChange(event); + } + } +} Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignFactorEvent.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignFactorEvent.java (rev 0) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignFactorEvent.java 2011-12-21 11:31:51 UTC (rev 9) @@ -0,0 +1,63 @@ +/* + * #%L + * JMexico :: Model + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Réseau Mexico, 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 fr.reseaumexico.model.event; + +import fr.reseaumexico.model.InputDesign; +import fr.reseaumexico.model.Scenario; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class InputDesignFactorEvent extends InputDesignScenarioEvent { + + private static final long serialVersionUID = -6197099063976776986L; + + protected String factorId; + protected Object factorOldValue; + protected Object factorNewValue; + + public InputDesignFactorEvent(InputDesign inputDesign, + Scenario scenario, + String factorId, + Object oldValue, + Object newValue) { + super(inputDesign, scenario); + this.factorId = factorId; + this.factorOldValue = oldValue; + this.factorNewValue = newValue; + } + + public String getFactorId() { + return factorId; + } + + public Object getFactorOldValue() { + return factorOldValue; + } + + public Object getFactorNewValue() { + return factorNewValue; + } +} Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignScenarioEvent.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignScenarioEvent.java (rev 0) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/InputDesignScenarioEvent.java 2011-12-21 11:31:51 UTC (rev 9) @@ -0,0 +1,54 @@ +/* + * #%L + * JMexico :: Model + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Réseau Mexico, 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 fr.reseaumexico.model.event; + +import fr.reseaumexico.model.InputDesign; +import fr.reseaumexico.model.Scenario; + +import java.util.EventObject; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class InputDesignScenarioEvent extends EventObject { + + private static final long serialVersionUID = 1L; + + protected Scenario scenario; + + public InputDesignScenarioEvent(InputDesign inputDesign, Scenario scenario) { + super(inputDesign); + this.scenario = scenario; + } + + @Override + public InputDesign getSource() { + return (InputDesign) super.getSource(); + } + + public Scenario getScenario() { + return scenario; + } +} Added: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/ScenarioFactorValueEvent.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/ScenarioFactorValueEvent.java (rev 0) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/event/ScenarioFactorValueEvent.java 2011-12-21 11:31:51 UTC (rev 9) @@ -0,0 +1,65 @@ +/* + * #%L + * JMexico :: Model + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 Réseau Mexico, 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 fr.reseaumexico.model.event; + +import fr.reseaumexico.model.Scenario; + +import java.util.EventObject; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class ScenarioFactorValueEvent extends EventObject { + + private static final long serialVersionUID = 1L; + + protected String factorId; + protected Object factorOldValue; + protected Object factorNewValue; + + public ScenarioFactorValueEvent(Scenario scenario, String factorId, Object factorOldValue, Object factorNewValue) { + super(scenario); + this.factorId = factorId; + this.factorOldValue = factorOldValue; + this.factorNewValue = factorNewValue; + } + + @Override + public Scenario getSource() { + return (Scenario) super.getSource(); + } + + public String getFactorId() { + return factorId; + } + + public Object getFactorOldValue() { + return factorOldValue; + } + + public Object getFactorNewValue() { + return factorNewValue; + } +} Modified: trunk/jmexico-model/src/main/xmi/mexico.zargo =================================================================== (Binary files differ) Modified: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model/InputDesignTableModel.java =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model/InputDesignTableModel.java 2011-12-19 17:40:54 UTC (rev 8) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model/InputDesignTableModel.java 2011-12-21 11:31:51 UTC (rev 9) @@ -27,12 +27,12 @@ import fr.reseaumexico.model.Factor; import fr.reseaumexico.model.InputDesign; import fr.reseaumexico.model.Scenario; +import fr.reseaumexico.model.event.InputDesignScenarioEvent; +import fr.reseaumexico.model.event.InputDesignScenarioListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -44,8 +44,10 @@ /** * @author sletellier <letellier@codelutin.com> */ -public class InputDesignTableModel extends AbstractTableModel { +public class InputDesignTableModel extends AbstractTableModel implements InputDesignScenarioListener { + private static final long serialVersionUID = 1L; + protected TableModel delegate; protected InputDesign inputDesign; @@ -55,32 +57,18 @@ public void setInputDesign(InputDesign inputDesign) { this.inputDesign = inputDesign; - inputDesign.addPropertyChangeListener(InputDesign.PROPERTY_SCENARIOS, new PropertyChangeListener() { + inputDesign.addInputDesignScenarioListener(this); - @Override - public void propertyChange(PropertyChangeEvent propertyChangeEvent) { - Object newValue = propertyChangeEvent.getNewValue(); - if (newValue == null) { - // was removed - scenarioRemoved(); - } - - Object oldValue = propertyChangeEvent.getOldValue(); - if (oldValue == null) { - // was added - scenarioAdded(); - } - } - }); - fireTableStructureChanged(); } - protected void scenarioAdded() { + @Override + public void onScenarioAdded(InputDesignScenarioEvent event) { fireTableStructureChanged(); } - protected void scenarioRemoved() { + @Override + public void onScenarioRemoved(InputDesignScenarioEvent event) { fireTableStructureChanged(); } Modified: trunk/jmexico-swing-editor/src/main/resources/i18n/jmexico-swing-editor_fr_FR.properties =================================================================== --- trunk/jmexico-swing-editor/src/main/resources/i18n/jmexico-swing-editor_fr_FR.properties 2011-12-19 17:40:54 UTC (rev 8) +++ trunk/jmexico-swing-editor/src/main/resources/i18n/jmexico-swing-editor_fr_FR.properties 2011-12-21 11:31:51 UTC (rev 9) @@ -7,4 +7,4 @@ jmexico.menu.file.open=Ouvrir jmexico.menu.file.save=Sauvegarder jmexico.remove.scenario=Suppression d'un scenario -jmexico.scenario.inputName=Nom du scénario à ajouter \: \ No newline at end of file +jmexico.scenario.inputName=Nom du scénario à ajouter \:
participants (1)
-
sletellier@users.forge.codelutin.com