Author: chatellier Date: 2009-06-09 11:53:17 +0000 (Tue, 09 Jun 2009) New Revision: 2351 Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/GearPopulationSelectivityModel.java Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/SelectivityEditorUI.jaxx Log: Replace ui table model Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/SelectivityEditorUI.jaxx =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/SelectivityEditorUI.jaxx 2009-06-09 11:52:58 UTC (rev 2350) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/SelectivityEditorUI.jaxx 2009-06-09 11:53:17 UTC (rev 2351) @@ -35,27 +35,17 @@ import fr.ifremer.isisfish.entities.PopulationImpl; import fr.ifremer.isisfish.entities.Selectivity; import fr.ifremer.isisfish.entities.Species; +import fr.ifremer.isisfish.ui.input.gear.GearPopulationSelectivityModel; import fr.ifremer.isisfish.ui.widget.editor.EquationTableEditor; import fr.ifremer.isisfish.ui.widget.editor.GenericCell; -// TODO why it's commented ? to remove ? -addPropertyChangeListener("bean", new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) { - if (evt.getOldValue() != null || evt.getNewValue() == null) { - // remove previous binding on getBean() - //jaxx.runtime.SwingUtil.removeDataBinding(get$Table0(), "fieldCellName.text", "fieldCellLongitude.text", "fieldCellLatitude.text", "fieldCellLand.selected", "fieldCellComment.text"); - } - if (evt.getNewValue() != null) { - // add binding on getBean() - //jaxx.runtime.SwingUtil.applyDataBinding(get$Table0(), "fieldCellName.text", "fieldCellLongitude.text", "fieldCellLatitude.text", "fieldCellLand.selected", "fieldCellComment.text"); - } - } -}); - selectivityTable.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { - removeSelectivityButton.setEnabled(selectivityTable.getSelectedRow() != -1); + // le bouton ne doit pas s'activer dans l'analyse de sensibilite + if (isActif()) { + removeSelectivityButton.setEnabled(selectivityTable.getSelectedRow() != -1); + } } }); @@ -70,11 +60,16 @@ setBean((GearImpl) gear); if (getBean() != null) { + setSelectivityPopulationSelected(false); setSelectivityTableModel(); fieldSelectivityPopulation.setModel(getSelectivityPopulationModel()); } getVerifier().addCurrentPanel(selectivityEquation); + // reset to false after addCurrentPanel refresh + selectivityEquation.setActif(false); + + setSelectivityPopulationSelected(false); removeSelectivityButton.setEnabled(false); } @@ -84,34 +79,19 @@ } protected void setSelectivityTableModel() { - DefaultTableModel model = new DefaultTableModel() { - @Override - public boolean isCellEditable(int row, int column) { - boolean result = false; - if (column > 0) { - result = true; - } - return result; - } - }; - Collection<Selectivity> selectivity = getBean().getPopulationSelectivity(); - java.util.List<Population> populations = new ArrayList<Population>(); - java.util.List<Equation> equations = new ArrayList<Equation>(); - if (selectivity != null) { - for (Selectivity s : selectivity) { - populations.add(s.getPopulation()); - - Equation equation = s.getEquation(); - getVerifier().addCurrentEntity(equation); - equations.add(equation); - } + java.util.List<Selectivity> selectivitiesList = new ArrayList<Selectivity>(); + // move collection to list + // and add all entity to verifier + for (Selectivity oneSelectivity : getBean().getPopulationSelectivity()) { + getVerifier().addCurrentEntity(oneSelectivity); + selectivitiesList.add(oneSelectivity); } - model.addColumn(_("isisfish.common.population"), populations.toArray()); - model.addColumn(_("isisfish.common.equation"), equations.toArray()); - + + // set table model + GearPopulationSelectivityModel model = new GearPopulationSelectivityModel(selectivitiesList); selectivityTable.setModel(model); - EquationTableEditor cellEditor = new EquationTableEditor(); - selectivityTable.getColumnModel().getColumn(1).setCellEditor(cellEditor); + selectivityTable.setDefaultRenderer(Equation.class, model); + selectivityTable.setDefaultEditor(Equation.class, new EquationTableEditor()); } protected void addSelectivity() { @@ -120,8 +100,8 @@ } protected void removeSelectivity() { - Population selectedPopulation = (Population)selectivityTable.getValueAt(selectivityTable.getSelectedRow(), 0); - Selectivity selectedSelectivity = (Selectivity)getBean().getPopulationSelectivity(selectedPopulation); + GearPopulationSelectivityModel model = (GearPopulationSelectivityModel)selectivityTable.getModel(); + Selectivity selectedSelectivity = model.getSelectivities().get(selectivityTable.getSelectedRow()); getAction().removeSelectivity(getBean(), selectedSelectivity); setSelectivityTableModel(); removeSelectivityButton.setEnabled(false); @@ -165,7 +145,7 @@ <cell columns='2' fill='both' weightx='1.0' weighty='1.0'> <InputOneEquationUI id='selectivityEquation' constructorParams='this' lblText='{_("isisfish.selectivity.equation")}' - bean='{getBean()}' nameEquation='Selectivity' actif='{isActif()}' + bean='{getBean()}' nameEquation='Selectivity' actif='{isSelectivityPopulationSelected()}' clazz='{fr.ifremer.isisfish.equation.SelectivityEquation.class}'/> </cell> </row> @@ -177,7 +157,7 @@ <Table> <row> <cell columns='2' fill='horizontal' weightx='1.0'> - <JButton id="addSelectivityButton" text="isisfish.common.add" onActionPerformed='addSelectivity()' enabled='{isActif()}'/> + <JButton id="addSelectivityButton" text="isisfish.common.add" onActionPerformed='addSelectivity()' enabled='{isSelectivityPopulationSelected()}'/> </cell> </row> <row> @@ -189,7 +169,7 @@ </row> <row> <cell columns='2' fill='horizontal' weightx='1.0'> - <JButton id="removeSelectivityButton" text="isisfish.common.remove" onActionPerformed='removeSelectivity()' enabled='{isActif()}'/> + <JButton id="removeSelectivityButton" text="isisfish.common.remove" onActionPerformed='removeSelectivity()' enabled='false'/> </cell> </row> </Table> Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/GearPopulationSelectivityModel.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/GearPopulationSelectivityModel.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/GearPopulationSelectivityModel.java 2009-06-09 11:53:17 UTC (rev 2351) @@ -0,0 +1,231 @@ +/* *##% + * 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.input.gear; + +import static org.codelutin.i18n.I18n._; + +import java.awt.Component; +import java.util.List; + +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JTable; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.TableCellRenderer; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import fr.ifremer.isisfish.entities.Equation; +import fr.ifremer.isisfish.entities.Gear; +import fr.ifremer.isisfish.entities.Selectivity; + +/** + * Table model for {@link Gear}#{@link Selectivity}. + * + * Columns : + * <li>selectivity population name</li> + * <li>selectivity equation</li> + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class GearPopulationSelectivityModel extends AbstractTableModel implements TableCellRenderer { + + /** Log. */ + private static Log log = LogFactory.getLog(GearPopulationSelectivityModel.class); + + /** serialVersionUID. */ + private static final long serialVersionUID = 3169786638868209920L; + + /** Columns names. */ + public final static String[] COLUMN_NAMES = { + _("isisfish.common.population"), + _("isisfish.common.equation") + }; + + protected List<Selectivity> selectivities; + + /** + * Empty constructor. + */ + public GearPopulationSelectivityModel() { + this(null); + } + + /** + * Constructor with data. + * + * @param selectivities initial selectivities + */ + public GearPopulationSelectivityModel( + List<Selectivity> selectivities) { + super(); + this.selectivities = selectivities; + } + + /** + * Set target species list. + * + * @param selectivities the selectivities to set + */ + public void setSelectivities(List<Selectivity> selectivities) { + this.selectivities = selectivities; + } + + /** + * Get selectivity list. + * + * @return selectivity list + */ + public List<Selectivity> getSelectivities() { + return this.selectivities; + } + + /* + * @see javax.swing.table.TableModel#getColumnCount() + */ + @Override + public int getColumnCount() { + return COLUMN_NAMES.length; + } + + /* + * @see javax.swing.table.TableModel#getRowCount() + */ + @Override + public int getRowCount() { + int rows = 0; + if (selectivities != null) { + rows = selectivities.size(); + } + return rows; + } + + /* + * @see javax.swing.table.TableModel#getValueAt(int, int) + */ + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + + Object result = null; + + Selectivity selectivity = selectivities.get(rowIndex); + switch (columnIndex) { + case 0: + result = selectivity.getPopulation().getName(); + break; + case 1: + result = selectivity.getEquation(); + 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 = Equation.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); + } + + Selectivity selectivity = selectivities.get(rowIndex); + switch (columnIndex) { + case 1: + Equation eq = (Equation)value; + // two events for event to be fired + selectivity.setEquation(null); + selectivity.setEquation(eq); + break; + default: + throw new IndexOutOfBoundsException("Can't edit column " + columnIndex); + } + + } + + /* + * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int) + */ + @Override + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) { + + Component c = null; + switch (column) { + case 0: + c = new JLabel(value.toString()); + break; + case 1: + Equation equation = (Equation)value; + c = new JButton(equation.getName() + "(" + equation.getCategory() + ")"); + break; + default: + throw new IndexOutOfBoundsException("No such column " + column); + } + return c; + } + +} Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/ui/input/gear/GearPopulationSelectivityModel.java ___________________________________________________________________ Name: svn:keywords + Date Author Revision