Author: sletellier Date: 2012-01-06 15:39:48 +0100 (Fri, 06 Jan 2012) New Revision: 19 Url: http://forge.codelutin.com/repositories/revision/jmexico/19 Log: - Refacoring of editor package - Use specific editor and renderer for factor values edition - Allow to use specific FactorValueEditorFactory in InputDesignEditor Added: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignTableModel.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellEditor.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellRenderer.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditor.jaxx trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditorFactory.java Removed: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueCellEditor.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueEditorFactory.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/model/ Modified: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/parser/ExperimentDesignParser.java trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditor.jaxx trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditorHandler.java Modified: trunk/jmexico-model/src/main/java/fr/reseaumexico/model/parser/ExperimentDesignParser.java =================================================================== --- trunk/jmexico-model/src/main/java/fr/reseaumexico/model/parser/ExperimentDesignParser.java 2012-01-04 13:58:53 UTC (rev 18) +++ trunk/jmexico-model/src/main/java/fr/reseaumexico/model/parser/ExperimentDesignParser.java 2012-01-06 14:39:48 UTC (rev 19) @@ -188,7 +188,7 @@ if (parser.getEventType() == XmlPullParser.START_TAG && parserEqual(parser, FEATURE)) { - List<Feature> features = Lists.newArrayList(); + List<Feature> features = Lists.newLinkedList(); while (parserEqual(parser, FEATURE)) { Deleted: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueCellEditor.java =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueCellEditor.java 2012-01-04 13:58:53 UTC (rev 18) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueCellEditor.java 2012-01-06 14:39:48 UTC (rev 19) @@ -1,92 +0,0 @@ -/* - * #%L - * JMexico :: Swing Editor - * - * $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.editor.ui.editor; - -import fr.reseaumexico.model.Factor; -import java.awt.Component; -import java.awt.Insets; -import java.awt.TextField; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import javax.swing.AbstractCellEditor; -import javax.swing.JButton; -import javax.swing.JTable; -import javax.swing.SwingConstants; -import javax.swing.table.TableCellEditor; - -/** - * @author sletellier <letellier@codelutin.com> - * @since 0.1 - */ -public class FactorValueCellEditor extends AbstractCellEditor - implements TableCellEditor, ActionListener { - - private static final long serialVersionUID = 1L; - - protected JTable table; - protected Factor factor; - protected Object currentValue; - protected JButton button; - - public FactorValueCellEditor() { - //Set up the editor (from the table's point of view), - //which is a button. - //This button brings up the color chooser dialog, - //which is the editor from the user's point of view. - button = new JButton(); - button.addActionListener(this); - button.setBorderPainted(false); - button.setHorizontalAlignment(SwingConstants.LEFT); - button.setMargin(new Insets(0,0,0,0)); - } - - @Override - public Object getCellEditorValue() { - return currentValue; - } - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - // get factor - // TODO sletellier 20111221 : Take care of type - factor = (Factor) table.getValueAt(row, 0); - - currentValue = value; - button.setText(String.valueOf(currentValue)); - return button; - } - - @Override - public void actionPerformed(ActionEvent e) { - // get factor - // TODO sletellier 20111221 : Take care of type - Object editedValue = FactorValueEditorFactory.getInstance().edit(table, factor, currentValue); - if (editedValue != null) { - currentValue = editedValue; - button.setText(String.valueOf(currentValue)); - } - fireEditingStopped(); - } -} Deleted: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueEditorFactory.java =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueEditorFactory.java 2012-01-04 13:58:53 UTC (rev 18) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueEditorFactory.java 2012-01-06 14:39:48 UTC (rev 19) @@ -1,58 +0,0 @@ -/* - * #%L - * JMexico :: Swing Editor - * - * $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.editor.ui.editor; - -import fr.reseaumexico.model.Factor; -import java.awt.Component; -import javax.swing.JOptionPane; - -import static org.nuiton.i18n.I18n._; - -/** - * Factory used to open specific editor to input factor value - * - * @author sletellier <letellier@codelutin.com> - * @since 0.1 - */ -public class FactorValueEditorFactory { - - private static FactorValueEditorFactory instance; - - // use singleton - public static FactorValueEditorFactory getInstance() { - if (instance == null) { - instance = new FactorValueEditorFactory(); - } - return instance; - } - - public Object edit(Component parent, Factor factor, Object oldValue) { - - // TODO sletellier 20121221 : open specific editor - String inputValue = JOptionPane.showInputDialog(parent, _("jmexico.input.msg"), oldValue); - - return inputValue; - } -} Modified: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditor.jaxx =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditor.jaxx 2012-01-04 13:58:53 UTC (rev 18) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditor.jaxx 2012-01-06 14:39:48 UTC (rev 19) @@ -25,22 +25,25 @@ <JPanel id='inputDesignEditor' layout='{new BorderLayout()}'> <import> -javax.swing.table.TableModel -javax.swing.table.DefaultTableModel -fr.reseaumexico.model.InputDesign -fr.reseaumexico.model.Factor -fr.reseaumexico.editor.ui.model.InputDesignTableModel -fr.reseaumexico.editor.ui.editor.FactorValueCellEditor -fr.reseaumexico.editor.ui.decorator.MexicoDecoratorProvider -jaxx.runtime.swing.renderer.DecoratorProviderTableCellRenderer + javax.swing.table.TableModel + javax.swing.table.DefaultTableModel + fr.reseaumexico.model.InputDesign + fr.reseaumexico.model.Factor + fr.reseaumexico.editor.ui.editor.InputDesignTableModel + fr.reseaumexico.editor.ui.editor.factorValue.FactorValueCellEditor + fr.reseaumexico.editor.ui.editor.factorValue.FactorValueCellRenderer + fr.reseaumexico.editor.ui.decorator.MexicoDecoratorProvider + jaxx.runtime.swing.renderer.DecoratorProviderTableCellRenderer </import> <InputDesignEditorHandler id='handler'/> <InputDesignTableModel id='model' javaBean="new InputDesignTableModel()"/> - <FactorValueCellEditor id='cellEditor'/> + <FactorValueCellEditor id='cellEditor' constructorParams='this'/> + <FactorValueCellRenderer id='cellRenderer' constructorParams='this'/> + <script> <![CDATA[ @@ -50,6 +53,7 @@ // TODO sletellier 20111221 : do on time for all, put in context ? MexicoDecoratorProvider mexicoDecoratorProvider = new MexicoDecoratorProvider(); inputDesignTable.setDefaultRenderer(Factor.class, new DecoratorProviderTableCellRenderer(mexicoDecoratorProvider)); + inputDesignTable.setDefaultRenderer(Object.class, getCellRenderer()); // cell editor inputDesignTable.setDefaultEditor(Object.class, getCellEditor()); Modified: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditorHandler.java =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditorHandler.java 2012-01-04 13:58:53 UTC (rev 18) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignEditorHandler.java 2012-01-06 14:39:48 UTC (rev 19) @@ -32,6 +32,7 @@ import java.util.Collection; import java.util.Map; import javax.swing.JOptionPane; +import org.apache.commons.lang3.StringUtils; import static org.nuiton.i18n.I18n._; @@ -49,45 +50,49 @@ _("jmexico.scenario.inputName.title"), JOptionPane.QUESTION_MESSAGE); - // check that name is never used - Collection<Scenario> scenarios = inputDesign.getScenario(); + // if user enter name + if (StringUtils.isNotBlank(selectedName)) { - int maxOrder = 0; - for (Scenario scenario : scenarios) { - if (selectedName.equalsIgnoreCase(scenario.getName())) { + // check that name is never used + Collection<Scenario> scenarios = inputDesign.getScenario(); - JOptionPane.showMessageDialog(editor, - _("jmexico.error.title"), - _("jmexico.error.scenario.name.used"), - JOptionPane.ERROR_MESSAGE); + int maxOrder = 0; + for (Scenario scenario : scenarios) { + if (selectedName.equalsIgnoreCase(scenario.getName())) { - return; + JOptionPane.showMessageDialog(editor, + _("jmexico.error.title"), + _("jmexico.error.scenario.name.used"), + JOptionPane.ERROR_MESSAGE); + + return; + } + int orderNumber = scenario.getOrderNumber(); + if (maxOrder < orderNumber) { + maxOrder = orderNumber; + } } - int orderNumber = scenario.getOrderNumber(); - if (maxOrder < orderNumber) { - maxOrder = orderNumber; - } - } - // create new scenario - Scenario scenarioToAdd = new ScenarioImpl(); + // create new scenario + Scenario scenarioToAdd = new ScenarioImpl(); - // set last order - // TODO sletellier 20111219 : ask user order number - scenarioToAdd.setOrderNumber(maxOrder + 1); + // set last order + // TODO sletellier 20111219 : ask user order number + scenarioToAdd.setOrderNumber(maxOrder + 1); - // set name - scenarioToAdd.setName(selectedName); + // set name + scenarioToAdd.setName(selectedName); - // create all empty factor values - Collection<Factor> factors = inputDesign.getExperimentDesign().getFactor(); - Map<Factor, Object> factorValues = Maps.newHashMap(); - for (Factor factor : factors) { - factorValues.put(factor, null); + // create all empty factor values + Collection<Factor> factors = inputDesign.getExperimentDesign().getFactor(); + Map<Factor, Object> factorValues = Maps.newHashMap(); + for (Factor factor : factors) { + factorValues.put(factor, null); + } + scenarioToAdd.setFactorValues(factorValues); + + inputDesign.addScenario(scenarioToAdd); } - scenarioToAdd.setFactorValues(factorValues); - - inputDesign.addScenario(scenarioToAdd); } public void removeScenario(InputDesignEditor editor) { Copied: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignTableModel.java (from rev 16, 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/editor/InputDesignTableModel.java (rev 0) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/InputDesignTableModel.java 2012-01-06 14:39:48 UTC (rev 19) @@ -0,0 +1,205 @@ +/* + * #%L + * JMexico :: Swing Editor + * + * $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.editor.ui.editor; + +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 java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableModel; + +import static org.nuiton.i18n.I18n._; + +/** + * @author sletellier <letellier@codelutin.com> + * @since 0.1 + */ +public class InputDesignTableModel extends AbstractTableModel implements InputDesignScenarioListener { + + private static final long serialVersionUID = 1L; + + protected TableModel delegate; + protected InputDesign inputDesign; + protected List<Factor> factors; + + public InputDesignTableModel() { + delegate = new DefaultTableModel(); + } + + public void setInputDesign(InputDesign inputDesign) { + this.inputDesign = inputDesign; + inputDesign.addInputDesignScenarioListener(this); + + // extract factors + factors = new LinkedList<Factor>(inputDesign.getExperimentDesign().getFactor()); + + // sort on id + Collections.sort(factors, new Comparator<Factor>() { + + @Override + public int compare(Factor o1, Factor o2) { + return o1.getId().compareTo(o2.getId()); + } + }); + + fireTableStructureChanged(); + } + + @Override + public void onScenarioAdded(InputDesignScenarioEvent event) { + fireTableStructureChanged(); + } + + @Override + public void onScenarioRemoved(InputDesignScenarioEvent event) { + fireTableStructureChanged(); + } + + public InputDesign getInputDesign() { + return inputDesign; + } + + public Collection<Scenario> getScenarios() { + return inputDesign.getScenario(); + } + + public Scenario getScenario(int i) { + return inputDesign.getScenario(i); + } + + public Factor getFactor(int i) { + return factors.get(i); + } + + @Override + public int getRowCount() { + if (inputDesign == null) { + return delegate.getRowCount(); + } + return factors == null ? 0 : factors.size(); + } + + @Override + public int getColumnCount() { + if (inputDesign == null) { + return delegate.getColumnCount(); + } + Collection<Scenario> scenarios = getScenarios(); + if (scenarios == null) { + return 1; + } + return scenarios.size() + 1; + } + + @Override + public String getColumnName(int rowIndex) { + if (inputDesign == null) { + return delegate.getColumnName(rowIndex); + } + if (rowIndex == 0) { + return _("jmexico.factor.name"); + } + Scenario scenario = getScenario(rowIndex - 1); + return scenario == null ? "" : scenario.getName(); + } + + @Override + public Class<?> getColumnClass(int rowIndex) { + if (inputDesign == null) { + return delegate.getColumnClass(rowIndex); + } + if (rowIndex == 0) { + return Factor.class; + } + return Object.class; + } + + @Override + public boolean isCellEditable(int rowIndex, int columnIndex) { + if (inputDesign == null) { + return delegate.isCellEditable(rowIndex, columnIndex); + } + return columnIndex > 0; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + if (inputDesign == null) { + return delegate.getValueAt(rowIndex, columnIndex); + } + + // get key on rowIndex + Factor factor = getFactor(rowIndex); + + // first column is factors + if (columnIndex == 0) { + return factor; + } + + // others are scenarios + columnIndex = columnIndex - 1; + Scenario scenario = getScenario(columnIndex); + if (scenario == null) { + return null; + } + Map<Factor,Object> factorValues = scenario.getFactorValues(); + + Object result = factorValues.get(factor); + return result; + } + + @Override + public void setValueAt(Object o, int rowIndex, int columnIndex) { + if (inputDesign == null) { + delegate.setValueAt(o, rowIndex, columnIndex); + return; + } + if (columnIndex == 0) { + + // must never append + return; + } + int scenarioIndex = columnIndex - 1; + Scenario scenario = getScenario(scenarioIndex); + Map<Factor,Object> factorValues = scenario.getFactorValues(); + + // get key if rowIndex + Factor factor = getFactor(rowIndex); + + factorValues.put(factor, o); + scenario.setFactorValues(factorValues); + + fireTableCellUpdated(rowIndex, columnIndex); + } +} Copied: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellEditor.java (from rev 16, trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueCellEditor.java) =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellEditor.java (rev 0) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellEditor.java 2012-01-06 14:39:48 UTC (rev 19) @@ -0,0 +1,119 @@ +/* + * #%L + * JMexico :: Swing Editor + * + * $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.editor.ui.editor.factorValue; + +import fr.reseaumexico.model.Factor; +import java.awt.Component; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import javax.swing.AbstractCellEditor; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.table.TableCellEditor; +import jaxx.runtime.JAXXContext; + +/** + * Cell editor for factor values + * + * @author sletellier <letellier@codelutin.com> + * @since 0.1 + * @see TableCellEditor + */ +public class FactorValueCellEditor extends AbstractCellEditor + implements TableCellEditor, FocusListener { + + private static final long serialVersionUID = 1L; + + protected JTable table; + protected Factor factor; + protected Object currentValue; + protected JTextField defaultInlineEditor; + protected FactorValueEditorFactory.FactorValueCellEditor specificEditor; + protected boolean isEditing = false; + protected FactorValueEditorFactory factory; + + public FactorValueCellEditor(JAXXContext context) { + defaultInlineEditor = new JTextField(); + defaultInlineEditor.addFocusListener(this); + + // search specific factory in context + factory = context.getContextValue(FactorValueEditorFactory.class); + + // if not found, use mexico one + if (factory == null) { + factory = new FactorValueEditorFactory(); + } + } + + @Override + public Object getCellEditorValue() { + if (isEditing) { + currentValue = defaultInlineEditor.getText(); + isEditing = false; + } + if (specificEditor != null) { + currentValue = specificEditor.getValue(); + } + return currentValue; + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + // get factor + // TODO sletellier 20111221 : Take care of type + factor = (Factor) table.getValueAt(row, 0); + + currentValue = value; + + specificEditor = factory.getCellEditor(factor, currentValue); + + if (specificEditor != null) { + return specificEditor.getComponent(); + } + defaultInlineEditor.setText(currentValue == null ? "" : String.valueOf(currentValue)); + return defaultInlineEditor; + } + + @Override + public void focusGained(FocusEvent e) { + + // get factor + // TODO sletellier 20111221 : Take care of type + FactorValueEditor editor = factory.getDialogEditor(factor, currentValue); + if (editor == null) { + isEditing = true; + } else { + // display ui + editor.setVisible(true); + currentValue = editor.getValue(); + defaultInlineEditor.setText(currentValue == null ? "" : String.valueOf(currentValue)); + fireEditingStopped(); + } + } + + @Override + public void focusLost(FocusEvent e) { + } +} Added: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellRenderer.java =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellRenderer.java (rev 0) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueCellRenderer.java 2012-01-06 14:39:48 UTC (rev 19) @@ -0,0 +1,69 @@ +/* + * #%L + * JMexico :: Swing Editor + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 - 2012 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.editor.ui.editor.factorValue; + +import fr.reseaumexico.model.Factor; +import java.awt.Component; +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.TableCellRenderer; +import jaxx.runtime.JAXXContext; + +/** + * @author sletellier <letellier@codelutin.com> + */ +public class FactorValueCellRenderer implements TableCellRenderer { + + protected FactorValueEditorFactory factory; + protected TableCellRenderer delegate; + + public FactorValueCellRenderer(JAXXContext context) { + // search specific factory in context + factory = context.getContextValue(FactorValueEditorFactory.class); + + // if not found, use mexico one + if (factory == null) { + factory = new FactorValueEditorFactory(); + } + + this.delegate = new DefaultTableCellRenderer(); + + } + + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + Factor factor = (Factor) table.getValueAt(row, 0); + + FactorValueEditorFactory.FactorValueCellRenderer specificEditor = factory.getRenderedComponent(factor, value); + + Component result; + if (specificEditor == null) { + result = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + } else { + result = specificEditor.getComponent(); + } + return result; + } +} Added: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditor.jaxx =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditor.jaxx (rev 0) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditor.jaxx 2012-01-06 14:39:48 UTC (rev 19) @@ -0,0 +1,45 @@ +<!-- + #%L + JMexico :: Swing Editor + + $Id$ + $HeadURL$ + %% + Copyright (C) 2011 - 2012 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% + --> +<JDialog abstract='true' + genericType='B' + modal='true' + layout='{new BorderLayout()}'> + + <import> +java.awt.BorderLayout +fr.reseaumexico.model.Factor + </import> + + <Factor id='factor' initializer='getContextValue(Factor.class)'/> + + <script><![CDATA[ + +public abstract B getValue(); + +public abstract void setValue(B data); + ]]> + </script> + +</JDialog> \ No newline at end of file Copied: trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditorFactory.java (from rev 15, trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/FactorValueEditorFactory.java) =================================================================== --- trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditorFactory.java (rev 0) +++ trunk/jmexico-swing-editor/src/main/java/fr/reseaumexico/editor/ui/editor/factorValue/FactorValueEditorFactory.java 2012-01-06 14:39:48 UTC (rev 19) @@ -0,0 +1,200 @@ +/* + * #%L + * JMexico :: Swing Editor + * + * $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.editor.ui.editor.factorValue; + +import fr.reseaumexico.model.Domain; +import fr.reseaumexico.model.Factor; +import fr.reseaumexico.model.ValueType; +import java.awt.Component; +import javax.swing.JCheckBox; +import javax.swing.JTextField; +import jaxx.runtime.swing.editor.NumberEditor; + +/** + * Factory used to open specific editor to input factor value + * + * @author sletellier <letellier@codelutin.com> + * @since 0.1 + */ +public class FactorValueEditorFactory { + + /** + * Provide specific {@link javax.swing.JDialog} editor for factor value + * + * @param factor concerned + * @param value value to edit + * @return {@link FactorValueEditor} contain editor to open as {@link javax.swing.JDialog}, + * null if cell editor must be used + */ + public FactorValueEditor getDialogEditor(Factor factor, Object value) { + + // TODO sletellier 20121221 : open specific editor + +// if (oldValue == null) { +// +// JAXXInitialContext context = new JAXXInitialContext(); +// context.add(factor); +// FactorValueEditor<B> editor = (FactorValueEditor<B>) new FactorValueEditorImpl(context); +// editor.setValue(oldValue); +// return editor; +// } + + return null; + } + + /** + * Provide specific cell editor for factor value + * + * @param factor concerned + * @param value value to edit + * @return {@link FactorValueCellEditor} contain editor to display + */ + public FactorValueCellEditor getCellEditor(Factor factor, Object value) { + + // get type + Domain domain = factor.getDomain(); + if (domain != null) { + ValueType valueType = domain.getValueType(); + switch (valueType) { + case INTEGER: return new IntegerFactorValueInlineEditor((Integer)value); + case DECIMAL: return new DecimalFactorValueInlineEditor((Double)value); + case BOOLEAN: return new BooleanFactorValueInlineEditor((Boolean)value); + } + } + return new DefaultFactorValueInlineEditor(value); + } + + /** + * Provide a specific cell renderer for factor value + * + * @param factor concerned + * @param value value to edit + * @return {@link FactorValueCellRenderer} contain renderer to display + */ + public FactorValueCellRenderer getRenderedComponent(Factor factor, Object value) { + return getCellEditor(factor, value); + } + + protected static interface FactorValueCellRenderer { + + public abstract Component getComponent(); + } + + protected static interface FactorValueCellEditor<B> extends FactorValueCellRenderer { + + public abstract B getValue(); + public abstract Component getComponent(); + } + + protected static class DefaultFactorValueInlineEditor implements FactorValueCellEditor<String> { + + protected JTextField component; + + public DefaultFactorValueInlineEditor(Object value) { + this.component = new JTextField(String.valueOf(value)); + } + + @Override + public String getValue() { + return component.getText(); + } + + @Override + public Component getComponent() { + return component; + } + } + + protected static class BooleanFactorValueInlineEditor implements FactorValueCellEditor<Boolean> { + + protected JCheckBox checkBox; + + public BooleanFactorValueInlineEditor(Boolean value) { + this.checkBox = new JCheckBox(); + checkBox.setSelected(value); + } + + @Override + public Boolean getValue() { + return checkBox.isSelected(); + } + + @Override + public Component getComponent() { + return checkBox; + } + } + + protected static class IntegerFactorValueInlineEditor implements FactorValueCellEditor<Integer> { + + protected NumberEditor numberEditor; + + public IntegerFactorValueInlineEditor(Integer value) { + this.numberEditor = new NumberEditor(); + numberEditor.setModel(value); + + // FIXME sletellier 20120105 : we dont need to set modelText + numberEditor.setModelText(String.valueOf(value)); + numberEditor.setUseFloat(false); + numberEditor.setUseSign(true); + } + + @Override + public Integer getValue() { + return (Integer) numberEditor.getModel(); + } + + @Override + public Component getComponent() { + return numberEditor; + } + } + + protected static class DecimalFactorValueInlineEditor implements FactorValueCellEditor<Double> { + + protected NumberEditor numberEditor; + + public DecimalFactorValueInlineEditor(Double value) { + this.numberEditor = new NumberEditor(); + numberEditor.setModel(value); + + // FIXME sletellier 20120105 : we dont need to set modelText + numberEditor.setModelText(String.valueOf(value)); + numberEditor.setModelType(Double.class); + numberEditor.setUseFloat(true); + numberEditor.setUseSign(true); + } + + @Override + public Double getValue() { + return (Double) numberEditor.getModel(); + } + + @Override + public Component getComponent() { + return numberEditor; + } + } +}