Author: chatellier Date: 2009-06-09 17:50:12 +0000 (Tue, 09 Jun 2009) New Revision: 2358 Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleListModel.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableCellEditor.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableModel.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterTableCellEditor.java Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/ParamsUI.jaxx isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/SimulAction.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/EquationTableEditor.java isis-fish/trunk/src/main/resources/i18n/isis-fish-fr_FR.properties Log: Rule & rule parameters refactoring (they have thier own editors now) Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-06-09 17:49:09 UTC (rev 2357) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/SimulationParameter.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -811,8 +811,14 @@ getRules().add(rule); } - public void removeRule(Rule rule) { - getRules().remove(rule); + /** + * Remove a rule. + * + * @param rule rule to remove + * @return <tt>true</tt> if this list contained the specified element + */ + public boolean removeRule(Rule rule) { + return getRules().remove(rule); } public void addAnalysePlan(AnalysePlan plan) { Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/ParamsUI.jaxx =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/ParamsUI.jaxx 2009-06-09 17:49:09 UTC (rev 2357) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/ParamsUI.jaxx 2009-06-09 17:50:12 UTC (rev 2358) @@ -36,6 +36,7 @@ import javax.swing.table.TableModel; import fr.ifremer.isisfish.ui.widget.editor.GenericCell; import fr.ifremer.isisfish.ui.widget.editor.ParameterColumnEditor; +import fr.ifremer.isisfish.ui.widget.editor.ParameterTableCellEditor; import javax.swing.text.JTextComponent; import org.dom4j.DocumentHelper; import org.dom4j.Element; @@ -47,18 +48,22 @@ import fr.ifremer.isisfish.ui.widget.renderer.SimpleToolTipTableColumnRenderer; import fr.ifremer.isisfish.ui.widget.renderer.SimpleToolTipComboBoxRenderer; import fr.ifremer.isisfish.simulator.launcher.SimulationServiceListener; +import fr.ifremer.isisfish.ui.simulator.rule.RuleListModel; +import fr.ifremer.isisfish.ui.simulator.rule.RuleParametersTableModel; +import fr.ifremer.isisfish.ui.simulator.rule.RuleParametersTableCellEditor; +import fr.ifremer.isisfish.rule.Rule; // instances variables déclaration SimulationServiceListener simulationListener; SimulAction simulAction = getContextValue(SimulAction.class); getContextValue(RegionMonitor.class).addRegionPropertyChangeListener(new PropertyChangeListener() { - @Override public void propertyChange(PropertyChangeEvent evt) { fieldSimulParamsRegion.setModel(new DefaultComboBoxModel(Common.getRegionItem())); } }); + // end constructor code simulationListener = new SimulationServiceListener() { @Override @@ -92,7 +97,7 @@ fieldSimulParamsDesc.setText(simulAction.getSimulationParameter().getDescription()); setListSimulParamsStrategiesItems(); setListSimulParamsPopulationsItems(); - setRulesItems(); + setRulesListModel(); } protected void regionChange(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { @@ -175,20 +180,8 @@ }); } } -protected void addRules(){ - simulAction.addRules(fieldSimulParamsMesuresSelect.getSelectedItem().toString()); - setRulesItems(); -} -protected void removeRules(){ - simulAction.removeRules(listSimulParamsMesuresList.getSelectedValue().toString()); - setRulesItems(); -} -protected void clearRules(){ - simulAction.getSimulationParameter().clearRules(); - setRulesItems(); -} -protected void simul(){ - if (!isSensitivity()){ +protected void simul() { + if (!isSensitivity()) { simulAction.launchSimulation(fieldSimulParamsName.getText(), (SimulatorLauncher)comboSelLauncher.getSelectedItem()); } else{ @@ -196,21 +189,21 @@ } getParentContainer(WelcomeTabUI.class).selTab(5); } -protected void enablePreSimul(){ +protected void enablePreSimul() { simulAction.getSimulationParameter().setUsePreScript(fieldUseSimulPreScripts.isSelected()); if (!isSensitivity()){ SimulUI simul = getParentContainer(SimulUI.class); simul.enablePreSimul(fieldUseSimulPreScripts.isSelected()); } } -protected void enableAnalysePlan(){ +protected void enableAnalysePlan() { simulAction.getSimulationParameter().setUseAnalysePlan(fieldSimulUseAnalysePlan.isSelected()); - if (!isSensitivity()){ + if (!isSensitivity()) { SimulUI simul = getParentContainer(SimulUI.class); simul.enableAnalysePlan(fieldSimulUseAnalysePlan.isSelected()); } } -protected void selTab(MouseEvent e, int i){ +protected void selTab(MouseEvent e, int i) { if (((JCheckBox)e.getSource()).isSelected()){ SimulUI simul = getParentContainer(SimulUI.class); simul.selTab(i); @@ -265,12 +258,6 @@ populationSelected(); } } -protected void setRulesItems(){ - listSimulParamsMesuresList.setListData(simulAction.getRules().toArray()); - listSimulParamsMesuresList.setCellRenderer(new SimpleToolTipComboBoxRenderer(ToolTipListModelFactory.createRuleDescriptionsModel2(simulAction.getSimulationParameter().getRules(),buttonSimulParamsMesuresClear,listSimulParamsMesuresList))); - - setRulesButton(); -} protected void populationSelected(){ if (listSimulParamsPopulations.getSelectedIndex() != -1){ simulAction.setPopulations(listSimulParamsPopulations.getSelectedValues()); @@ -280,65 +267,63 @@ protected void strategySelected(){ simulAction.setStrategies(listSimulParamsStrategies.getSelectedValues()); } -protected void saveDescription(){ +protected void saveDescription() { simulAction.getSimulationParameter().setDescription(fieldSimulParamsDesc.getText()); } -protected void setRulesButton(){ - if (simulAction.getRules().toArray().length == 0){ - buttonSimulParamsMesuresClear.setEnabled(false); - buttonSimulParamsMesuresRemove.setEnabled(false); - } - else{ - if (listSimulParamsMesuresList.getSelectedIndex() != -1){ + +protected void setRulesListModel() { + RuleListModel model = new RuleListModel(simulAction.getRules()); + listSimulParamsMesuresList.setModel(model); + listSimulParamsMesuresList.setCellRenderer(model); + + onRuleSelectionChange(); +} +/** + * Called when selection change on rule list. + */ +protected void onRuleSelectionChange() { + if (simulAction.getRules().isEmpty()) { + buttonSimulParamsMesuresClear.setEnabled(false); + buttonSimulParamsMesuresRemove.setEnabled(false); + } + else { + if (listSimulParamsMesuresList.getSelectedIndex() != -1) { buttonSimulParamsMesuresRemove.setEnabled(true); setSimulParamsRulesModel(); } buttonSimulParamsMesuresClear.setEnabled(true); - } + } } - protected void setSimulParamsRulesModel(){ - DefaultTableModel model = new DefaultTableModel(); - Map<String, Class> values; - if (listSimulParamsMesuresList.getSelectedIndex() != -1){ - values = simulAction.getRuleParameterName(listSimulParamsMesuresList.getSelectedValue().toString()); - model = new DefaultTableModel(){ - @Override - public boolean isCellEditable(int row, int column) { - boolean result = false; - if (column > 0){ - result =true; - } - return result; - } - }; - java.util.List<Object> properties = new ArrayList<Object>(); - java.util.List<GenericCell> cells = new ArrayList<GenericCell>(); - for (Iterator<String> it = values.keySet().iterator(); it.hasNext();) { - String name = it.next(); - Object propertie = simulAction.getRuleParameterValue(name, listSimulParamsMesuresList.getSelectedValue().toString()); - GenericCell value = new GenericCell(name, values.get(name), null); - properties.add(propertie); - cells.add(value); - } - model.addColumn(_("isisfish.common.name"), cells.toArray()); - model.addColumn(_("isisfish.common.value"), properties.toArray()); + +protected void setSimulParamsRulesModel() { + int selectedRuleIndex = listSimulParamsMesuresList.getSelectedIndex(); + if (selectedRuleIndex != -1) { + RuleListModel listModel = (RuleListModel)listSimulParamsMesuresList.getModel(); + Rule selectedRule = listModel.getRules().get(selectedRuleIndex); + RuleParametersTableModel model = new RuleParametersTableModel(selectedRule); + RuleParametersTableCellEditor cellEditor = new RuleParametersTableCellEditor(selectedRule); + cellEditor.setRegion(simulAction.getRegionStorage()); + simulParamsRules.setModel(model); + simulParamsRules.getColumnModel().getColumn(1).setCellEditor(cellEditor); } - simulParamsRules.setModel(model); - ParameterColumnEditor cellEditor = new ParameterColumnEditor(); - cellEditor.setRegion(simulAction.getRegionStorage()); - simulParamsRules.getColumnModel().getColumn(1).setCellEditor(cellEditor); - simulParamsRules.getModel().addTableModelListener(new TableModelListener() { - @Override - public void tableChanged(TableModelEvent e) { - int row = e.getFirstRow(); - TableModel model = (TableModel)e.getSource(); - GenericCell data = (GenericCell) model.getValueAt(row, 0); - simulAction.setRuleParameterValue(data.getName(), listSimulParamsMesuresList.getSelectedValue().toString(), model.getValueAt(row, 1)); - } - }); - simulParamsRules.getColumnModel().getColumn(0).setCellRenderer(new SimpleToolTipTableColumnRenderer(ToolTipListModelFactory.createRuleParamsModel(listSimulParamsMesuresList.getSelectedValue().toString()))); } -protected void selectFilter(){ +protected void addRules() { + String selectedRuleName = fieldSimulParamsMesuresSelect.getSelectedItem().toString(); + simulAction.addRules(selectedRuleName); + setRulesListModel(); +} +protected void removeRules() { + RuleListModel listModel = (RuleListModel)listSimulParamsMesuresList.getModel(); + Rule selectedRule = listModel.getRules().get(listSimulParamsMesuresList.getSelectedIndex()); + simulAction.removeRule(selectedRule); + setRulesListModel(); +} +protected void clearRules() { + simulAction.getSimulationParameter().clearRules(); + setRulesListModel(); +} + +protected void selectFilter() { try { simulAction.setOldSimulatorNames(fr.ifremer.isisfish.ui.simulator.filter.SimulationFilterUtil.filterSimulation(simulAction.getOldSimulationItem())); refresh(); @@ -352,9 +337,11 @@ } } } + protected ComboBoxModel getSimulParamsSelectModel() { return getSimulParamsSelectModel(false); } + protected ComboBoxModel getSimulParamsSelectModel(boolean force) { java.util.List<String> items = simulAction.getFilteredOldSimulatorNames(force); DefaultComboBoxModel result = new DefaultComboBoxModel(); @@ -365,78 +352,35 @@ } return result; } -protected ComboBoxModel getParamsMesuresSelectModel(){ - java.util.List values = simulAction.getRuleNames(); - DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray()); - fieldSimulParamsMesuresSelect.setRenderer(new SimpleToolTipComboBoxRenderer(ToolTipListModelFactory.createRuleDescriptionsModel(values))); + +protected ComboBoxModel getParamsMesuresSelectModel() { + java.util.List<String> rulesNames = simulAction.getAvailableRuleNames(); + DefaultComboBoxModel model = new DefaultComboBoxModel(rulesNames.toArray()); + fieldSimulParamsMesuresSelect.setRenderer(new SimpleToolTipComboBoxRenderer(ToolTipListModelFactory.createRuleDescriptionsModel(rulesNames))); return model; } + protected void resetFilter(){ simulAction.resetOldSimulatorNames(); } protected void saveName(){ simulAction.setName(fieldSimulParamsName.getText()); } -public Element save(){ - Element result = DocumentHelper.createElement("paramsUI"); - for (String name : $objectMap.keySet()){ - Object o = $objectMap.get(name); - if (!name.matches("^\\$.*")){ - Element child = DocumentHelper.createElement(name); - if (JComboBox.class.isInstance(o)){ - Object value = ((JComboBox)o).getSelectedItem(); - child.addAttribute("value", value.toString()); - } - if (JList.class.isInstance(o)){ - Object[] values = ((JList)o).getSelectedValues(); - for (Object value : values){ - child.add(DocumentHelper.createElement("item").addAttribute("value", value.toString())); - } - } - else if (JTextComponent.class.isInstance(o)){ - child.addText(((JTextComponent)o).getText()); - } - else if (AbstractButton.class.isInstance(o)){ - child.addAttribute("selected", ((AbstractButton)o).isSelected() + ""); - } - else if (JTable.class.isInstance(o)){ - JTable table = (JTable)o; - for (int i=0;i<table.getColumnCount();i++){ - Element column = DocumentHelper.createElement("column"); - for (int j=0;j<table.getRowCount();j++){ - Element row = DocumentHelper.createElement("row"); - Object value = table.getValueAt(j, i); - if (value != null){ - row.add(DocumentHelper.createElement("cell").addAttribute("value", value.toString())); - } - else { - row.add(DocumentHelper.createElement("cell").addAttribute("value", null)); - } - column.add(row); - } - child.add(column); - } - } - result.add(child); - } - } - return result; -} - /** - * TODO : Common code with {@link SimulUI#saveSimulation()} - * TODO duplicated code (simulUI, Params UI, SensitivityUI) - */ - protected void saveSimulation() { - JFileChooser fc = new JFileChooser(); - int returnVal = fc.showSaveDialog(null); +/** + * TODO : Common code with {@link SimulUI#saveSimulation()} + * TODO duplicated code (simulUI, Params UI, SensitivityUI) + */ +protected void saveSimulation() { + JFileChooser fc = new JFileChooser(); + int returnVal = fc.showSaveDialog(null); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = fc.getSelectedFile(); - //This is where a real application would open the file. - simulAction.saveSimulation(file); - } + if (returnVal == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + //This is where a real application would open the file. + simulAction.saveSimulation(file); } +} ]]> </script> @@ -575,9 +519,9 @@ </cell> </row> <row> - <cell fill="both" weightx="1.0" weighty="1.0"> - <JScrollPane minimumSize='{new Dimension(0,0)}' preferredSize='{new Dimension(0,0)}'> - <JList id="listSimulParamsMesuresList" selectionMode="0" model='{new DefaultComboBoxModel(simulAction.getRules().toArray())}' onMouseClicked='setRulesButton()'/> + <cell fill="both" weightx="1.0" weighty="1.0"> + <JScrollPane minimumSize='{new Dimension(0,0)}' preferredSize='{new Dimension(0,0)}'> + <JList id="listSimulParamsMesuresList" selectionMode="0" model='{new DefaultComboBoxModel(simulAction.getRules().toArray())}' onMouseClicked='onRuleSelectionChange()'/> </JScrollPane> </cell> </row> Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/SimulAction.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/SimulAction.java 2009-06-09 17:49:09 UTC (rev 2357) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/SimulAction.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -107,7 +107,7 @@ protected SimulationStorage simulStorage = null; protected RuleStorage ruleStorage = null; protected List<String> analysePlan = null; - protected Map<Rule, String> rules = new HashMap<Rule, String>(); + protected List<Rule> rules = new ArrayList<Rule>(); protected Map<SensitivityExport, String> sensitivityExports = new HashMap<SensitivityExport, String>(); protected List<String> oldSimulNames = null; @@ -380,12 +380,24 @@ ************ */ - // Gestion des rules + /** + * Get simulation parameters rule list. + * + * @return rules + */ + public List<Rule> getRules() { + return param.getRules(); + } + + /** + * Add new rule by name. + * + * @param name rule name + */ public void addRules(String name) { ruleStorage = RuleStorage.getRule(name); try { Rule ruleTmp = ruleStorage.getNewRuleInstance(); - rules.put(ruleTmp, name); param.addRule(ruleTmp); } catch (IsisFishException e) { if (log.isErrorEnabled()) { @@ -396,11 +408,21 @@ } /** + * Retire une regle. + * + * @param rule rule to removes + * @return <tt>true</tt> if this list contained the specified element + */ + public boolean removeRule(Rule rule) { + return param.removeRule(rule); + } + + /** * Return rules name without .java extention. * * @return rule names list */ - public List<String> getRuleNames() { + public List<String> getAvailableRuleNames() { List<String> result = new ArrayList<String>(); for (String r : RuleStorage.getRuleNames()) { // Remove .java extention @@ -410,97 +432,6 @@ } return result; } - - public List<String> getRules() { - List<String> result = new ArrayList<String>(); - for (Rule r : param.getRules()) { - try { - result.add(RuleStorage.getName(r)); - } catch (Exception e) { - if (log.isErrorEnabled()) { - log.error("Can't get rules", e); - } - showMsgBox(e); - } - } - return result; - } - - /** - * - * FIXME on ne peut pas retirer une regles par son nom (le nom n'est pas la clé) - * il faudrait passer l'instance en parametre - */ - public void removeRules(String name) { - if (name != null) { - /*for (Map.Entry<Rule, String> entry : rules.entrySet()) { - if (entry.getValue().equals(name)) { - param.removeRule(entry.getKey()); - rules.remove(entry.getKey()); - } - }*/ - Iterator<Entry<Rule, String>> itEntries = rules.entrySet().iterator(); - while (itEntries.hasNext()) { - Entry<Rule, String> currentEntry = itEntries.next(); - if (currentEntry.getValue().equals(name)) { - param.removeRule(currentEntry.getKey()); - itEntries.remove(); - } - } - } - } - - public Rule getRule(String ruleName) { - Rule r = null; - for (Map.Entry<Rule, String> entry : rules.entrySet()) { - if (entry.getValue().equals(ruleName)) { - r = entry.getKey(); - } - } - return r; - } - - public Map<String, Class> getRuleParameterName(String ruleName) { - Map<String, Class> result = null; - if (ruleName != null) { - Rule r = getRule(ruleName); - if (r != null) { - result = RuleStorage.getParameterNames(r); - } - } - return result; - } - - public Object getRuleParameterValue(String paramName, String ruleName) { - Object result = null; - if (ruleName != null) { - try { - result = RuleStorage.getParameterValue(getRule(ruleName), - paramName); - } catch (IsisFishException e) { - if (log.isErrorEnabled()) { - log.error("Can't get rule param value", e); - } - showMsgBox(e); - } - } - return result; - } - - public void setRuleParameterValue(String paramName, String ruleName, - Object o) { - if (ruleName != null) { - try { - RuleStorage.setParameterValue(getRule(ruleName), paramName, o); - } catch (IsisFishException e) { - if (log.isErrorEnabled()) { - log.error("Can't set rule param value", e); - } - showMsgBox(e); - } - } - } - // Strategies public List<Strategy> getStrategies() { Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleListModel.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleListModel.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleListModel.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -0,0 +1,130 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * 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 fr.ifremer.isisfish.ui.simulator.rule; + +import java.awt.Component; +import java.util.List; + +import javax.swing.AbstractListModel; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.ListCellRenderer; + +import fr.ifremer.isisfish.datastore.RuleStorage; +import fr.ifremer.isisfish.rule.Rule; + +/** + * Model pour la liste des regles des parametres d'une simulation. + * + * @author chatellier + * @version $Revision: 2350 $ + * + * Last update : $Date: 2009-06-09 13:52:58 +0200 (mar. 09 juin 2009) $ + * By : $Author: chatellier $ + */ +public class RuleListModel extends AbstractListModel implements ListCellRenderer { + + /** serialVersionUID. */ + private static final long serialVersionUID = -4070846632975105788L; + + protected List<Rule> rules; + + /** + * Empty constructor. + */ + public RuleListModel() { + this(null); + } + + /** + * Constructor with rule list. + * + * @param rules rules + */ + public RuleListModel(List<Rule> rules) { + super(); + this.rules = rules; + } + + /** + * Get rules list. + * + * @return the rules + */ + public List<Rule> getRules() { + return rules; + } + + /** + * Set rules list. + * + * @param rules the rules to set + */ + public void setRules(List<Rule> rules) { + this.rules = rules; + } + + /* + * @see javax.swing.ListModel#getElementAt(int) + */ + @Override + public Object getElementAt(int index) { + return rules.get(index); + } + + /* + * @see javax.swing.ListModel#getSize() + */ + @Override + public int getSize() { + int size = 0; + + if (rules != null) { + size = rules.size(); + } + return size; + } + + /* + * @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean) + */ + @Override + public Component getListCellRendererComponent(JList list, Object value, + int index, boolean isSelected, boolean cellHasFocus) { + + Rule rule = rules.get(index); + + JLabel label = new JLabel(); + + try { + label.setToolTipText(rule.getDescription()); + } catch (Exception e) { + // can't get exception + } + label.setText(RuleStorage.getName(rule)); + + if(isSelected) { + label.setBackground(list.getSelectionBackground()); + label.setForeground(list.getSelectionForeground()); + label.setOpaque(true); // background won't work + } + + return label; + } +} Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableCellEditor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableCellEditor.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableCellEditor.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -0,0 +1,70 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * 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 fr.ifremer.isisfish.ui.simulator.rule; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import fr.ifremer.isisfish.datastore.RuleStorage; +import fr.ifremer.isisfish.rule.Rule; +import fr.ifremer.isisfish.ui.widget.editor.ParameterTableCellEditor; + +/** + * Specific editor for rule parameters. + * + * @author chatellier + * @version $Revision: 1.0 $ + * + * Last update : $Date: 9 juin 2009 $ + * By : $Author: chatellier $ + */ +public class RuleParametersTableCellEditor extends ParameterTableCellEditor { + + /** serialVersionUID. */ + private static final long serialVersionUID = 8211639776194497615L; + + protected Rule rule; + + protected List<Class<?>> ruleParametersTypes; + + /** + * Constructor with rule parameter. + * + * @param rule rule + */ + public RuleParametersTableCellEditor(Rule rule) { + super(); + + this.rule = rule; + ruleParametersTypes = new ArrayList<Class<?>>(); + //TODO map may be sorted + Map<String, Class> ruleParametersNamesAndTypes = RuleStorage.getParameterNames(rule); + for (Class<?> type : ruleParametersNamesAndTypes.values()) { + ruleParametersTypes.add(type); + } + } + /* + * @see fr.ifremer.isisfish.ui.widget.editor.ParameterTableCellEditor#getType(java.lang.Object, int, int) + */ + @Override + protected Class<?> getType(Object value, int row, int column) { + return ruleParametersTypes.get(row); + } +} Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableModel.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableModel.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/simulator/rule/RuleParametersTableModel.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -0,0 +1,193 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * 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 fr.ifremer.isisfish.ui.simulator.rule; + +import static org.codelutin.i18n.I18n._; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.swing.table.AbstractTableModel; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.datastore.RuleStorage; +import fr.ifremer.isisfish.rule.Rule; +import fr.ifremer.isisfish.ui.input.metier.MetierSeasonInfoTargetSpeciesModel; + +/** + * Model de la table de definition de parametre d'une regle. + * + * Columns : + * <li>Parameter name</li> + * <li>Parameter value</li> + * + * @author chatellier + * @version $Revision: 2350 $ + * + * Last update : $Date: 2009-06-09 13:52:58 +0200 (mar. 09 juin 2009) $ + * By : $Author: chatellier $ + */ +public class RuleParametersTableModel extends AbstractTableModel { + + /** Log. */ + private static Log log = LogFactory.getLog(MetierSeasonInfoTargetSpeciesModel.class); + + /** serialVersionUID. */ + private static final long serialVersionUID = 3169786638868209920L; + + /** Columns names. */ + public final static String[] COLUMN_NAMES = { + _("isisfish.common.name"), + _("isisfish.common.value") }; + + protected Rule rule; + protected List<String> ruleParametersNames; + + + /** + * Constructor with data. + * + * @param rule rule to edit parameters + */ + public RuleParametersTableModel(Rule rule) { + super(); + this.rule = rule; + + //TODO map may be sorted + ruleParametersNames = new ArrayList<String>(); + Map<String, Class> ruleParametersNamesAndTypes = RuleStorage.getParameterNames(rule); + for (String names : ruleParametersNamesAndTypes.keySet()) { + ruleParametersNames.add(names); + } + } + + /* + * @see javax.swing.table.TableModel#getColumnCount() + */ + @Override + public int getColumnCount() { + return COLUMN_NAMES.length; + } + + /* + * @see javax.swing.table.TableModel#getRowCount() + */ + @Override + public int getRowCount() { + return ruleParametersNames.size(); + } + + /* + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + + Object result = null; + + String name = ruleParametersNames.get(rowIndex); + switch (columnIndex) { + case 0: + result = name; + break; + case 1: + try { + result = RuleStorage.getParameterValue(rule, name); + } catch (IsisFishException e) { + if (log.isErrorEnabled()) { + log.debug("Can't get parameters value", e); + } + } + break; + default: + throw new IndexOutOfBoundsException("No such column " + columnIndex); + } + + return result; + } + + /* + * @see javax.swing.table.TableModel#getColumnClass(int) + */ + @Override + public Class<?> getColumnClass(int columnIndex) { + + Class<?> result = null; + + switch (columnIndex) { + case 0: + result = String.class; + break; + case 1: + result = Object.class; + break; + default: + throw new IndexOutOfBoundsException("No such column " + columnIndex); + } + + return result; + } + + /* + * @see javax.swing.table.TableModel#getColumnName(int) + */ + @Override + public String getColumnName(int columnIndex) { + return COLUMN_NAMES[columnIndex]; + } + + /* + * @see javax.swing.table.TableModel#isCellEditable(int, int) + */ + @Override + public boolean isCellEditable(int rowIndex, int columnIndex) { + return columnIndex > 0; + } + + /* + * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int) + */ + @Override + public void setValueAt(Object value, int rowIndex, int columnIndex) { + + if (log.isDebugEnabled()) { + log.debug("Cell edition (column " + columnIndex + ") = " + value); + } + + String name = ruleParametersNames.get(rowIndex); + switch (columnIndex) { + case 1: + try { + RuleStorage.setParameterValue(rule, name, value); + } catch (IsisFishException e) { + if (log.isErrorEnabled()) { + log.error("Can't set parameter value"); + } + } + break; + default: + throw new IndexOutOfBoundsException("Can't edit column " + columnIndex); + } + + } +} Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/EquationTableEditor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/EquationTableEditor.java 2009-06-09 17:49:09 UTC (rev 2357) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/EquationTableEditor.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -18,8 +18,6 @@ package fr.ifremer.isisfish.ui.widget.editor; -import static org.codelutin.i18n.I18n._; - import java.awt.Component; import java.awt.Window; import java.awt.event.ActionEvent; @@ -27,6 +25,7 @@ import java.util.EventObject; import java.util.HashSet; import java.util.Iterator; +import java.util.Set; import javax.swing.JButton; import javax.swing.JTable; @@ -57,9 +56,10 @@ /** serialVersionUID. */ private static final long serialVersionUID = -2483612426979170213L; - protected HashSet<CellEditorListener> listeners = new HashSet<CellEditorListener>(); + protected Set<CellEditorListener> listeners = new HashSet<CellEditorListener>(); protected EquationEditorPaneUI frame = null; + protected Equation equation = null; public EquationTableEditor() { @@ -89,6 +89,7 @@ return frame; } + @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { equation = (Equation) value; @@ -104,16 +105,18 @@ return this; } + @Override public void addCellEditorListener(CellEditorListener l) { listeners.add(l); } + @Override public void removeCellEditorListener(CellEditorListener l) { listeners.remove(l); } + @Override public Object getCellEditorValue() { - // Equation equation = getFrame().getEquationEditorPane().getEquation(); if (equation != null) { String content = getFrame().getEditor().getText(); equation.setContent(content); @@ -121,18 +124,21 @@ return equation; } + @Override public boolean isCellEditable(EventObject anEvent) { return true; } + @Override public boolean shouldSelectCell(EventObject anEvent) { return true; } + @Override public void cancelCellEditing() { ChangeEvent e = new ChangeEvent(this); - for (Iterator i = listeners.iterator(); i.hasNext();) { - CellEditorListener l = (CellEditorListener) i.next(); + for (Iterator<CellEditorListener> i = listeners.iterator(); i.hasNext();) { + CellEditorListener l = i.next(); l.editingCanceled(e); } @@ -141,10 +147,11 @@ } } + @Override public boolean stopCellEditing() { ChangeEvent e = new ChangeEvent(this); - for (Iterator i = listeners.iterator(); i.hasNext();) { - CellEditorListener l = (CellEditorListener) i.next(); + for (Iterator<CellEditorListener> i = listeners.iterator(); i.hasNext();) { + CellEditorListener l = i.next(); l.editingStopped(e); } @@ -155,4 +162,3 @@ } } // EquationTableEditor - Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterTableCellEditor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterTableCellEditor.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/widget/editor/ParameterTableCellEditor.java 2009-06-09 17:50:12 UTC (rev 2358) @@ -0,0 +1,493 @@ +/* *##% + * Copyright (C) 2006 - 2009 + * Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin + * + * 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 fr.ifremer.isisfish.ui.widget.editor; + +import java.awt.Component; +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.util.ArrayList; +import java.util.EventObject; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JPanel; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.event.CellEditorListener; +import javax.swing.event.ChangeEvent; +import javax.swing.table.TableCellEditor; + +import org.apache.commons.beanutils.ConvertUtilsBean; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.persistence.TopiaEntity; +import org.codelutin.util.EnumEditor; +import org.codelutin.util.MonthEnum; + +import fr.ifremer.isisfish.datastore.RegionStorage; +import fr.ifremer.isisfish.types.Date; +import fr.ifremer.isisfish.types.Month; +import fr.ifremer.isisfish.util.ConverterUtil; + +/** + * ParameterTableCellEditor. + * + * Un editeur generic de champs qui se base sur le le model de données + * a editer. + * + * Created: 25 sept. 06 12:35:21 + * + * @author poussin + * @author chemit + * @version $Revision: 2336 $ + * + * Last update: $Date: 2009-06-08 14:47:41 +0200 (lun. 08 juin 2009) $ + * by : $Author: chatellier $ + */ +public class ParameterTableCellEditor extends JComponent implements TableCellEditor { + + /** Class logger. */ + private static Log log = LogFactory.getLog(ParameterTableCellEditor.class); + + private static enum TypeClassMapping { + /** for simple int. */ + Int(int.class), + /** for a topia entity. */ + Topia(TopiaEntity.class), + /** for a mounth. */ + Mounth(Month.class), + /** for a simple date. */ + Date(Date.class), + /** for a simple boolean. */ + Double(double.class), + /** for a simple boolean. */ + Boolean(boolean.class), + /** for a file location. */ + File(java.io.File.class), + /** for a simple String. */ + String(java.lang.String.class); + + private final Class<?> klazz; + + private TypeClassMapping(Class<?> klazz) { + this.klazz = klazz; + } + + protected static TypeClassMapping getMapping(Class<?> klazz) { + for (TypeClassMapping t : TypeClassMapping.values()) { + if (t.klazz.isAssignableFrom(klazz)) { + return t; + } + } + throw new RuntimeException( + "could not found a TypeClassMapping for this class " + + klazz); + } + } + + private static final long serialVersionUID = 6860330126841984303L; + + protected Set<CellEditorListener> listeners = new HashSet<CellEditorListener>(); + + protected RegionStorage region = null; + + protected Component editor = null; + + protected Class<?> type = null; + + /** + * @return Returns the topiaContext. + */ + public RegionStorage getRegion() { + return this.region; + } + + /** + * @param region + * The topiaContext to set. + */ + public void setRegion(RegionStorage region) { + this.region = region; + } + + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, int column) { + + // get value type + type = getType(value, row, column); + JComboBox c; + //if (!table.hasFocus()) return null; + + TypeClassMapping mapping = TypeClassMapping.getMapping(type); + switch (mapping) { + case Topia: + // on a un bean comme parametre + try { + if (region == null) { + return null; + } + TopiaContext context = region.getStorage().beginTransaction(); + if (context != null) { + List list = context.find("from " + type.getName()); + c = new JComboBox(list.toArray()); + c.setSelectedItem(value); + editor = c; + context.closeContext(); + } + + } catch (Exception eee) { + if (log.isWarnEnabled()) { + log.warn("Can't get entity object for combobox", eee); + } + } + break; + case Boolean: + List<Boolean> listB = new ArrayList<Boolean>(); + listB.add(Boolean.TRUE); + listB.add(Boolean.FALSE); + c = new JComboBox(listB.toArray()); + listB.clear(); + // TODO See how to convert + c.setSelectedItem(value); + editor = c; + break; + case Date: + Date date = (Date) value; + if (date == null) { + date = new Date(0); + } + int month = date.getMonth().getMonthNumber(); + int yea = date.getYear(); + editor = new DateComponent(month, yea, region == null ? null : region + .getStorage()); + //editor = new JTextField(String.valueOf(date.getDate())); + break; + case Mounth: + editor = DateComponent.createMounthCombo(((Month) value) + .getMonthNumber(), region == null ? null : region + .getStorage()); + break; + case File: + // break; + default: + editor = new JTextField(value.toString()); + } + if (editor != null) { + if (JComboBox.class.isInstance(editor)) { + ((JComboBox) editor).addActionListener(getComboListener()); + } else if (JTextField.class.isInstance(editor)) { + ((JTextField) editor).addFocusListener(new FocusListener() { + + @Override + public void focusGained(FocusEvent e) { + + } + + @Override + public void focusLost(FocusEvent e) { + stopCellEditing(); + } + }); + } else if (DateComponent.class.isInstance(editor)) { + DateComponent date = ((DateComponent) editor); + if (date.getMounthCombo() != null) { + date.getMounthCombo().addActionListener(getComboListener()); + } + if (date.getYearCombo() != null) { + date.getYearCombo().addActionListener(getComboListener()); + } + } + } + if (log.isInfoEnabled()) { + log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + "=" + + editor); + } + //+ editor==null?null:editor.getClass().getSimpleName()); + return editor; + } + + /** + * Get type for value. + * + * Must be overriden (value can be null). + * + * @param value + * @param row + * @param column + * @return + */ + protected Class<?> getType(Object value, int row, int column) { + // default to value's class + return value.getClass(); + } + + protected ActionListener getComboListener() { + return new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + stopCellEditing(); + } + }; + } + + public Object getCellEditorValue() { + + Object result = null; + TopiaContext context = null; + if (editor == null) + return ""; + TypeClassMapping mapping = TypeClassMapping.getMapping(type); + switch (mapping) { + case Topia: + result = getComboBoxValue(editor); + break; + case Boolean: + result = getComboBoxValue(editor); + // TODO test + break; + case Mounth: + result = getComboBoxIndex(editor) + ""; + break; + case Date: + result = ((DateComponent) editor).getSelectedValue() + ""; + break; + case File: + //TODO + // result = getTextFieldValue(editor);break; + default: + if (editor instanceof JTextField) { + result = ((JTextField) editor).getText(); + } else if (editor instanceof JComboBox) { + result = ((JComboBox) editor).getSelectedItem(); + } + } + + if (result instanceof String && !String.class.equals(type)) { + if (region != null) + context = region.getStorage(); + ConvertUtilsBean cub = ConverterUtil.getConverter(context); + result = cub.convert((String) result, type); + } + + if (log.isInfoEnabled()) { + log.info("getCellEditorValue [" + type + "] [mapping:" + mapping + + "]= " + result); + } + + return result; + } + + protected Object getTextFieldValue(Component editor) { + return ((JTextField) editor).getText(); + } + + protected Object getComboBoxValue(Component editor) { + return ((JComboBox) editor).getSelectedItem(); + } + + protected int getComboBoxIndex(Component editor) { + return (((JComboBox) editor)).getSelectedIndex(); + } + + public Object getCellEditorValueOld() { + + Object result = null; + if (editor instanceof JTextField) { + result = ((JTextField) editor).getText(); + } else if (editor instanceof JComboBox) { + result = ((JComboBox) editor).getSelectedItem(); + } + + if (result instanceof String && !String.class.equals(type)) { + TopiaContext context = null; + if (region != null) { + context = region.getStorage(); + } + ConvertUtilsBean cub = ConverterUtil.getConverter(context); + result = cub.convert((String) result, type); + } + + return result; + } + + /* + * (non-Javadoc) + * + * @see javax.swing.CellEditor#addCellEditorListener(javax.swing.event.CellEditorListener) + */ + public void addCellEditorListener(CellEditorListener l) { + listeners.add(l); + } + + /* + * @see javax.swing.CellEditor#cancelCellEditing() + */ + public void cancelCellEditing() { + if (log.isDebugEnabled()) { + log.debug("cancelCellEditing"); + } + ChangeEvent e = new ChangeEvent(this); + for (CellEditorListener listener : listeners) { + listener.editingCanceled(e); + } + } + + /* + * @see javax.swing.CellEditor#isCellEditable(java.util.EventObject) + */ + public boolean isCellEditable(EventObject anEvent) { + return true; + } + + /* + * @see javax.swing.CellEditor#removeCellEditorListener(javax.swing.event.CellEditorListener) + */ + public void removeCellEditorListener(CellEditorListener l) { + listeners.remove(l); + } + + /* + * @see javax.swing.CellEditor#shouldSelectCell(java.util.EventObject) + */ + public boolean shouldSelectCell(EventObject anEvent) { + return true; + } + + /* + * @see javax.swing.CellEditor#stopCellEditing() + */ + public boolean stopCellEditing() { + // to prevent concurent modification exception + if (log.isDebugEnabled()) { + log.debug("stopCellEditing"); + } + CellEditorListener[] list = listeners + .toArray(new CellEditorListener[listeners.size()]); + + ChangeEvent e = new ChangeEvent(this); + for (CellEditorListener l : list) { + l.editingStopped(e); + } + return true; + } + + public static class DateComponent extends JPanel { + + private static final long serialVersionUID = -6694461572642939712L; + + protected JComboBox mounth; + + protected JComboBox year; + + public DateComponent(int mounth, int year, TopiaContext context) { + super(new GridLayout(0, 2)); + + this.mounth = createMounthCombo(mounth, context); + this.year = createYearCombo(200, year); + + this.add(this.mounth); + this.add(this.year); + } + + public JComboBox getYearCombo() { + return year; + } + + public JComboBox getMounthCombo() { + return mounth; + } + + public int getSelectedMounth() { + return mounth.getSelectedIndex(); + } + + public int getSelectedYear() { + return year.getSelectedIndex(); + } + + public void setSelectedValue(int year, int mounth) { + this.mounth.setSelectedIndex(mounth); + this.year.setSelectedIndex(year); + } + + public int getSelectedValue() { + int selectedYear = getSelectedYear(); + int selectedMounth = getSelectedMounth(); + if (log.isInfoEnabled()) { + log.info("selected mounth " + selectedMounth); + log.info("selected year " + selectedYear); + } + return selectedMounth + selectedYear * 12; + } + + public static JComboBox createMounthCombo(int mounth, + TopiaContext context) { + JComboBox combo = EnumEditor.newEditor(MonthEnum.class); + combo.setSelectedIndex(mounth); + return combo; + } + + /*public static JComboBox createMounthCombo(int mounth, TopiaContext context) { + JComboBox combo = createMounthCombo0(context); + combo.setSelectedIndex(mounth); + return combo; + } + + private static JComboBox createMounthCombo0(TopiaContext context) { + + List<String> listM = new ArrayList<String>(); + // TODO Should use database ? data instead of thoses awfull hardcore + // code... + listM.add("janvier"); + listM.add("fevrier"); + listM.add("mars"); + listM.add("avril"); + listM.add("mai"); + listM.add("juin"); + listM.add("juillet"); + listM.add("aout"); + listM.add("septembre"); + listM.add("octobre"); + listM.add("novembre"); + listM.add("decembre"); + JComboBox combo = new JComboBox(listM.toArray()); + listM.clear(); + return combo; + }*/ + + public static JComboBox createYearCombo(int nb, int selectedYear) { + List<Integer> listI = new ArrayList<Integer>(); + for (int i = 0; i < nb; i++) + listI.add(i); + JComboBox year = new JComboBox(listI.toArray()); + listI.clear(); + year.setSelectedIndex(selectedYear); + return year; + } + } +} Modified: isis-fish/trunk/src/main/resources/i18n/isis-fish-fr_FR.properties =================================================================== --- isis-fish/trunk/src/main/resources/i18n/isis-fish-fr_FR.properties 2009-06-09 17:49:09 UTC (rev 2357) +++ isis-fish/trunk/src/main/resources/i18n/isis-fish-fr_FR.properties 2009-06-09 17:50:12 UTC (rev 2358) @@ -1077,7 +1077,7 @@ isisfish.wizardGroupCreation.groupWidth=Longueur du groupe isisfish.wizardGroupCreation.groupWidthHelp=Longueur du groupe \= Longueur du groupe isisfish.wizardGroupCreation.lastAge=Dernier age -isisfish.wizardGroupCreation.lastAgeHelp=Dernier age \= Age maximal +isisfish.wizardGroupCreation.lastAgeHelp=Dernier age \= age maximal isisfish.wizardGroupCreation.maxGroupsLength=Taille maximale de groupes isisfish.wizardGroupCreation.maxGroupsLengthHelp=Un point-virgule ';' doit s\u00E9parer les tailles maximales de chaque groupe isisfish.wizardGroupCreation.numberGroup=Nombre de groupe
participants (1)
-
chatellier@users.labs.libre-entreprise.org