Author: tchemit Date: 2012-08-01 18:32:14 +0200 (Wed, 01 Aug 2012) New Revision: 339 Url: http://forge.codelutin.com/repositories/revision/sammoa/339 Log: refacotr and clean observatiosn package Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextEditableCellAction.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextRowEditableAction.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousEditableCellAction.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousRowEditableAction.java Removed: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationForSelectedRouteHighlightPredicate.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationRouteTable.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableListSelectionListener.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteNoModificationHighlightPredicate.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableListSelectionListener.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanel.jaxx trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanelHandler.java Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanel.jaxx =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanel.jaxx 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanel.jaxx 2012-08-01 16:32:14 UTC (rev 339) @@ -36,6 +36,7 @@ fr.ulr.sammoa.ui.swing.flight.FlightUIModel org.nuiton.validator.bean.list.BeanListValidator + org.jdesktop.swingx.JXTable jaxx.runtime.swing.renderer.DecoratorProviderListCellRenderer jaxx.runtime.swing.model.GenericListModel @@ -63,14 +64,14 @@ <RouteTableModel id='routeTableModel' initializer='new RouteTableModel(flightUIModel)'/> - <RouteTableListSelectionListener id='routeSelectionModel' - constructorParams='flightUIModel'/> + <!--RouteTableListSelectionListener id='routeSelectionModel' + constructorParams='flightUIModel'/--> <ObservationTableModel id='observationTableModel' constructorParams='flightUIModel'/> - <ObservationTableListSelectionListener id='observationSelectionModel' - constructorParams='flightUIModel'/> + <!--ObservationTableListSelectionListener id='observationSelectionModel' + constructorParams='flightUIModel'/--> <!-- validator --> <BeanListValidator id='routeValidator' genericType='Route' @@ -131,8 +132,7 @@ <row> <cell fill='both' weightx='1.0' weighty='1.0' columns='10'> <JScrollPane id='routeTableScroll'> - <ObservationRouteTable id='routeTable' - constructorParams='_("sammoa.timePattern"), routeSelectionModel'/> + <JXTable id='routeTable'/> </JScrollPane> </cell> </row> @@ -161,8 +161,7 @@ <row> <cell fill='both' weightx='1.0' weighty='1.0' columns='4'> <JScrollPane id='observationTableScroll'> - <ObservationRouteTable id='observationTable' - constructorParams='_("sammoa.timePattern"), observationSelectionModel'/> + <JXTable id='observationTable' /> </JScrollPane> </cell> </row> Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanelHandler.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanelHandler.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanelHandler.java 2012-08-01 16:32:14 UTC (rev 339) @@ -31,19 +31,29 @@ import fr.ulr.sammoa.persistence.GeoPoint; import fr.ulr.sammoa.persistence.Observation; import fr.ulr.sammoa.persistence.ObservationStatus; +import fr.ulr.sammoa.persistence.Observations; import fr.ulr.sammoa.persistence.Observer; import fr.ulr.sammoa.persistence.ObserverPosition; import fr.ulr.sammoa.persistence.Position; import fr.ulr.sammoa.persistence.Route; +import fr.ulr.sammoa.persistence.Routes; import fr.ulr.sammoa.ui.swing.SammoaColors; import fr.ulr.sammoa.ui.swing.SammoaDecoratorProvider; import fr.ulr.sammoa.ui.swing.SammoaUIContext; import fr.ulr.sammoa.ui.swing.action.CircleBackAction; +import fr.ulr.sammoa.ui.swing.observations.action.MoveToNextEditableCellAction; +import fr.ulr.sammoa.ui.swing.observations.action.MoveToNextRowEditableAction; +import fr.ulr.sammoa.ui.swing.observations.action.MoveToPreviousEditableCellAction; +import fr.ulr.sammoa.ui.swing.observations.action.MoveToPreviousRowEditableAction; import fr.ulr.sammoa.ui.swing.flight.FlightUIHandler; import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; +import fr.ulr.sammoa.ui.swing.util.AbstractRowHighlightPredicate; import fr.ulr.sammoa.ui.swing.util.SammoaUtil; import fr.ulr.sammoa.ui.swing.util.TableDataChangeListener; +import fr.ulr.sammoa.ui.swing.util.TextCellEditor; import jaxx.runtime.SwingUtil; +import jaxx.runtime.swing.JAXXWidgetUtil; +import jaxx.runtime.swing.editor.cell.NumberCellEditor; import jaxx.runtime.validator.swing.SwingListValidatorDataLocator; import jaxx.runtime.validator.swing.SwingListValidatorHighlightPredicate; import jaxx.runtime.validator.swing.SwingListValidatorMessageTableModel; @@ -64,16 +74,29 @@ import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.border.LineBorder; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableModel; import java.awt.Color; import java.awt.Component; +import java.awt.Rectangle; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.util.Date; import java.util.List; +import static org.nuiton.i18n.I18n._; import static org.nuiton.i18n.I18n.n_; /** @@ -136,6 +159,53 @@ JXTable dataTable = ui.getRouteTable(); + init(dataTable, new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + + if (!e.getValueIsAdjusting()) { + + ListSelectionModel listSelectionModel = + (ListSelectionModel) e.getSource(); + int firstIndex = e.getFirstIndex(); + int lastIndex = e.getLastIndex(); + Integer newSelectedRow = null; + + if (listSelectionModel.isSelectionEmpty()) { + + // no selection + } else if (listSelectionModel.isSelectedIndex(firstIndex)) { + + // use first index + newSelectedRow = firstIndex; + } else if (listSelectionModel.isSelectedIndex(lastIndex)) { + + // use last index + newSelectedRow = lastIndex; + } + FlightUIModel flightUIModel = ui.getFlightUIModel(); + List<Route> routes = flightUIModel.getRoutes(); + Route route = null; + + if (newSelectedRow != null && + newSelectedRow < routes.size()) { + route = routes.get(newSelectedRow); + + if (logger.isInfoEnabled()) { + logger.info("Select route from index {}", newSelectedRow); + } + } else { + if (logger.isInfoEnabled()) { + logger.info("No route selected"); + } + } + + // set new route in model to validate + flightUIModel.setRouteEditBean(route); + } + } + }); + SwingUtil.setI18nTableHeaderRenderer( dataTable, n_("sammoa.observations.routeTable.column.effortNumber"), @@ -307,6 +377,55 @@ n_("sammoa.observations.observationTable.column.circleback.tip") ); +// init(dataTable, new ObservationTableListSelectionListener(ui.getFlightUIModel())); + init(dataTable, new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + + FlightUIModel flightUIModel = ui.getFlightUIModel(); + + if (!e.getValueIsAdjusting()) { + + ListSelectionModel listSelectionModel = + (ListSelectionModel) e.getSource(); + int firstIndex = e.getFirstIndex(); + int lastIndex = e.getLastIndex(); + Integer newSelectedRow = null; + + if (listSelectionModel.isSelectionEmpty()) { + + // no selection + } else if (listSelectionModel.isSelectedIndex(firstIndex)) { + + // use first index + newSelectedRow = firstIndex; + } else if (listSelectionModel.isSelectedIndex(lastIndex)) { + + // use last index + newSelectedRow = lastIndex; + } + List<Observation> observations = flightUIModel.getObservations(); + Observation observation = null; + + if (newSelectedRow != null && + newSelectedRow < observations.size()) { + observation = observations.get(newSelectedRow); + + if (logger.isInfoEnabled()) { + logger.info("Select observation from index {}", newSelectedRow); + } + } else { + if (logger.isInfoEnabled()) { + logger.info("No observation selected"); + } + } + + // set new observation in model to validate + flightUIModel.setObservationEditBean(observation); + } + } + }); + // set renderer and editor as button for circle back action int circleBack = ObservationTableModel.ObservationColumn.CIRCLE_BACK.ordinal(); CircleBackEditorRenderer editorRenderer = new CircleBackEditorRenderer(getFlightUIModel()); @@ -451,6 +570,112 @@ return SammoaUIContext.getUIContext().getDecoratorProvider(); } + + public static void init(final JXTable table, + ListSelectionListener selectionListener) { + + // make tab key to focus only next editable cell + table.setSelectionModel(new ForceSelectionSelectionModel()); + + table.setSortable(false); + + table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); + + final Action moveToNextEditableCell = new MoveToNextEditableCellAction(table); + final Action moveToPreviousEditableCell = new MoveToPreviousEditableCellAction(table); + final Action moveToPreviousRowEditableCell = new MoveToPreviousRowEditableAction(table); + final Action moveToNextRowEditableCell = new MoveToNextRowEditableAction(table); + + // redéfini les comportements par defaut de la table par les notres + table.getActionMap().put("selectNextRowCell", moveToNextEditableCell); + table.getActionMap().put("selectNextColumnCell", moveToNextEditableCell); + table.getActionMap().put("selectNextColumn", moveToNextEditableCell); + table.getActionMap().put("selectPreviousColumn", moveToPreviousEditableCell); + + // Key adapter à ajouter sur les éditeurs où l'on souhaite gérer les + // touches "entrer", "gauche", "doite" de facon personnalisée. + KeyListener goNextCellAdapter = new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER || + e.getKeyCode() == KeyEvent.VK_RIGHT) { + e.consume(); + moveToNextEditableCell.actionPerformed(null); + } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { + e.consume(); + moveToPreviousEditableCell.actionPerformed(null); + } else if (e.getKeyCode() == KeyEvent.VK_UP) { + e.consume(); + moveToPreviousRowEditableCell.actionPerformed(null); + } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { + e.consume(); + moveToNextRowEditableCell.actionPerformed(null); + } + } + }; + + // TextCellEditor + { + TextCellEditor editor = new TextCellEditor(); + JTextField textField = editor.getComponent(); + textField.addKeyListener(goNextCellAdapter); + textField.setBorder(new LineBorder(Color.GRAY, 2)); +// textField.setBorder(BasicBorders.getTextFieldBorder()); + table.setDefaultEditor(String.class, editor); + } + // NumberCellEditor + { + NumberCellEditor<Integer> editor = JAXXWidgetUtil.newNumberTableCellEditor(Integer.class, false); + editor.getNumberEditor().setSelectAllTextOnError(true); + JTextField textField = editor.getNumberEditor().getTextField(); + textField.addKeyListener(goNextCellAdapter); + textField.setBorder(new LineBorder(Color.GRAY, 2)); +// textField.setBorder(BasicBorders.getTextFieldBorder()); + table.setDefaultEditor(int.class, editor); + table.setDefaultEditor(Integer.class, editor); + } + + // Boolean editor + { + TableCellEditor editor = table.getDefaultEditor(Boolean.class); + table.setDefaultEditor(boolean.class, editor); + TableCellRenderer renderer = table.getDefaultRenderer(Boolean.class); + table.setDefaultRenderer(boolean.class, renderer); + } + + // Renderers + TableCellRenderer defaultDateCellRenderer = table.getDefaultRenderer(Date.class); + table.setDefaultRenderer(Date.class, JAXXWidgetUtil.newDateTableCellRenderer(defaultDateCellRenderer, _("sammoa.timePattern"))); + + table.getSelectionModel().addListSelectionListener(selectionListener); + + SwingUtil.scrollToTableSelection(table); + + table.getModel().addTableModelListener(new TableModelListener() { + @Override + public void tableChanged(TableModelEvent e) { + switch (e.getType()) { + case TableModelEvent.INSERT: + + // auto select new added line + table.getSelectionModel().setSelectionInterval(e.getFirstRow(), e.getFirstRow()); + Rectangle rect = table.getCellRect(e.getFirstRow(), 0, true); + table.scrollRectToVisible(rect); + break; + + case TableModelEvent.DELETE: + case TableModelEvent.UPDATE: + + // auto select first line ? + if (table.getSelectedRow() < 0 && table.getRowCount() > 0) { + table.getSelectionModel().setSelectionInterval(0, 0); + } + break; + } + } + }); + } + private static class RouteValidatorDataLocator implements SwingListValidatorDataLocator<Route> { @Override @@ -558,4 +783,74 @@ } } + public static class ObservationForSelectedRouteHighlightPredicate extends AbstractRowHighlightPredicate { + + protected FlightUIModel UIModel; + + public ObservationForSelectedRouteHighlightPredicate(FlightUIModel UIModel) { + this.UIModel = UIModel; + } + + @Override + protected boolean isHighlighted(int rowIndex) { + + Route selectedRoute = UIModel.getRouteEditBean(); + + boolean result; + if (selectedRoute != null) { + + Route nextRoute = UIModel.getNextRoute(selectedRoute); + + Observation observation = getValueAt(rowIndex); + + result = Observations.inRoute(observation, selectedRoute, nextRoute); + + } else { + result = false; + } + return result; + } + + @Override + protected Observation getValueAt(int rowIndex) { + return UIModel.getObservations().get(rowIndex); + } + } + + public class RouteNoModificationHighlightPredicate extends AbstractRowHighlightPredicate { + + protected RouteTableModel model; + + public RouteNoModificationHighlightPredicate(RouteTableModel model) { + this.model = model; + } + + @Override + protected boolean isHighlighted(int rowIndex) { + + Route route = getValueAt(rowIndex); + + boolean result = false; + + if (route.isDeleted()) { + // do nothing + + } else { + + int previousRouteIndex = rowIndex - 1; + if (previousRouteIndex >= 0) { + + Route previousRoute = getValueAt(previousRouteIndex); + + result = Routes.equal(route, previousRoute); + } + } + return result; + } + + @Override + protected Route getValueAt(int rowIndex) { + return model.getRow(rowIndex); + } + } } Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,59 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -package fr.ulr.sammoa.ui.swing.observations; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.DefaultListSelectionModel; - -/** - * Surcharge de modele de selection par default pour empecher la deselection - * de la dernière ligne et pour qu'il y est toujours une ligne selectionnée. - * - * @author echatellier - */ -public class ForceSelectionSelectionModel extends DefaultListSelectionModel { - - private static final Logger logger = - LoggerFactory.getLogger(ForceSelectionSelectionModel.class); - - private static final long serialVersionUID = 1L; - - @Override - public int getSelectionMode() { - return SINGLE_SELECTION; - } - - @Override - public void removeSelectionInterval(int index0, int index1) { - // don't do anything - if (logger.isDebugEnabled()) { - logger.debug("don't remove selection interval ({}, {})", - index0, index1); - } - } -} Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,59 @@ +/* + * #%L + * SAMMOA :: UI Swing + * + * $Id$ + * $HeadURL: http://svn.forge.codelutin.com/svn/sammoa/trunk/sammoa-ui-swing/src/main/jav... $ + * %% + * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ + +package fr.ulr.sammoa.ui.swing.observations; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.DefaultListSelectionModel; + +/** + * Surcharge de modele de selection par default pour empecher la deselection + * de la dernière ligne et pour qu'il y est toujours une ligne selectionnée. + * + * @author echatellier + */ +public class ForceSelectionSelectionModel extends DefaultListSelectionModel { + + private static final Logger logger = + LoggerFactory.getLogger(ForceSelectionSelectionModel.class); + + private static final long serialVersionUID = 1L; + + @Override + public int getSelectionMode() { + return SINGLE_SELECTION; + } + + @Override + public void removeSelectionInterval(int index0, int index1) { + // don't do anything + if (logger.isDebugEnabled()) { + logger.debug("don't remove selection interval ({}, {})", + index0, index1); + } + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ForceSelectionSelectionModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationForSelectedRouteHighlightPredicate.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationForSelectedRouteHighlightPredicate.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationForSelectedRouteHighlightPredicate.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,69 +0,0 @@ -package fr.ulr.sammoa.ui.swing.observations; -/* - * #%L - * SAMMOA :: UI Swing - * $Id:$ - * $HeadURL:$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -import fr.ulr.sammoa.persistence.Observation; -import fr.ulr.sammoa.persistence.Observations; -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; -import fr.ulr.sammoa.ui.swing.util.AbstractRowHighlightPredicate; - -/** - * Created: 16/07/12 - * - * @author fdesbois <desbois@codelutin.com> - */ -public class ObservationForSelectedRouteHighlightPredicate extends AbstractRowHighlightPredicate { - - protected FlightUIModel UIModel; - - public ObservationForSelectedRouteHighlightPredicate(FlightUIModel UIModel) { - this.UIModel = UIModel; - } - - @Override - protected boolean isHighlighted(int rowIndex) { - - Route selectedRoute = UIModel.getRouteEditBean(); - - boolean result; - if (selectedRoute != null) { - - Route nextRoute = UIModel.getNextRoute(selectedRoute); - - Observation observation = getValueAt(rowIndex); - - result = Observations.inRoute(observation, selectedRoute, nextRoute); - - } else { - result = false; - } - return result; - } - - @Override - protected Observation getValueAt(int rowIndex) { - return UIModel.getObservations().get(rowIndex); - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationRouteTable.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationRouteTable.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationRouteTable.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,321 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ - -package fr.ulr.sammoa.ui.swing.observations; - -import fr.ulr.sammoa.ui.swing.util.TextCellEditor; -import jaxx.runtime.SwingUtil; -import jaxx.runtime.swing.JAXXWidgetUtil; -import jaxx.runtime.swing.editor.cell.NumberCellEditor; -import org.jdesktop.swingx.JXTable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.AbstractAction; -import javax.swing.JTextField; -import javax.swing.border.LineBorder; -import javax.swing.event.ListSelectionListener; -import javax.swing.event.TableModelEvent; -import javax.swing.table.TableCellEditor; -import javax.swing.table.TableCellRenderer; -import java.awt.Color; -import java.awt.Rectangle; -import java.awt.event.ActionEvent; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.util.Date; - -/** - * Redefinition d'une table pour initialiser la selection par defaut - * et forcer toujours une selection. - * <p/> - * http://www.jroller.com/santhosh/entry/tweaking_jtable_editing - * - * @author echatellier - */ -public class ObservationRouteTable extends JXTable { - - private static final Logger logger = LoggerFactory.getLogger(ObservationRouteTable.class); - - private static final long serialVersionUID = 1L; - - /** Action qui se déplace à la prochaine cellule editable. */ - protected AbstractAction moveToNextEditableCell = new AbstractAction() { - - private static final long serialVersionUID = 1L; - - @Override - public void actionPerformed(ActionEvent evt) { - - int currentRow = getSelectedRow(); - int currentColumn = getSelectedColumn(); - - logger.debug("Move to next editable cell, {}, {}", currentRow, currentColumn); - - while (currentRow <= getRowCount() || currentColumn <= getColumnCount()) { - - // go to next cell - currentColumn++; - - // select next cell - if (currentColumn >= getColumnCount()) { - currentColumn = 0; - currentRow++; - } - - if (isCellEditable(currentRow, currentColumn)) { - doSelectCell(currentRow, currentColumn); -// // select cell -// setColumnSelectionInterval(currentColumn, currentColumn); -// setRowSelectionInterval(currentRow, currentRow); -// editCellAt(currentRow, currentColumn); -// logger.debug("While select cell at {}, {}", currentRow, currentColumn); - break; - } else { - logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); - } - } - } - }; - - /** Action qui se déplace à la cellule editable précédente. */ - protected AbstractAction moveToPreviousEditableCell = new AbstractAction() { - - private static final long serialVersionUID = 1L; - - @Override - public void actionPerformed(ActionEvent evt) { - - int currentRow = getSelectedRow(); - int currentColumn = getSelectedColumn(); - - logger.debug("Move to previous editable cell, {}, {}", currentRow, currentColumn); - - - while (currentRow > 0 || currentColumn > 0) { - - // go to next cell - currentColumn--; - - // select next cell - if (currentColumn < 0) { - currentColumn = getColumnCount() - 1; - currentRow--; - } - - if (isCellEditable(currentRow, currentColumn)) { - doSelectCell(currentRow, currentColumn); -// // select cell -// setColumnSelectionInterval(currentColumn, currentColumn); -// setRowSelectionInterval(currentRow, currentRow); -// logger.debug("While select cell at {}, {}", currentRow, currentColumn); -// editCellAt(currentRow, currentColumn); - break; - } else { - logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); - } - } - } - }; - - /** Action qui se déplace à la cellule editable de la ligne du dessus (il faut que la cellule courant soit aussi éditable). */ - protected AbstractAction moveToPreviousRowEditableCell = new AbstractAction() { - - private static final long serialVersionUID = 1L; - - @Override - public void actionPerformed(ActionEvent evt) { - - int currentRow = getSelectedRow(); - int currentColumn = getSelectedColumn(); - - if (isCellEditable(currentRow, currentColumn)) { - logger.debug("Move to previous row editable cell, {}, {}", currentRow, currentColumn); - currentRow--; - - if (currentRow < 0) { - logger.debug("No previous row"); - } else { - doSelectCell(currentRow, currentColumn); - } - } else { - logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); - } - } - }; - - /** Action qui se déplace à la cellule editable de la ligne du dessus (il faut que la cellule courant soit aussi éditable). */ - protected AbstractAction moveToNextRowEditableCell = new AbstractAction() { - - private static final long serialVersionUID = 1L; - - @Override - public void actionPerformed(ActionEvent evt) { - - int currentRow = getSelectedRow(); - int currentColumn = getSelectedColumn(); - - if (isCellEditable(currentRow, currentColumn)) { - logger.debug("Move to next row editable cell, {}, {}", currentRow, currentColumn); - currentRow++; - - if (currentRow >= getRowCount()) { - logger.debug("No next row"); - } else { - doSelectCell(currentRow, currentColumn); - } - } else { - logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); - } - } - }; - - protected void doSelectCell(int currentRow, int currentColumn) { - setColumnSelectionInterval(currentColumn, currentColumn); - setRowSelectionInterval(currentRow, currentRow); - logger.debug("While select cell at {}, {}", currentRow, currentColumn); - editCellAt(currentRow, currentColumn); - } - - /** - * Key adapter à ajouter sur les éditeurs où l'on souhaite gérer les - * touches "entrer", "gauche", "doite" de facon personnalisée. - */ -// protected KeyListener goNextCellAdapter = new KeyAdapter() { -// @Override -// public void keyPressed(KeyEvent e) { -// if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_RIGHT) { -// e.consume(); -// moveToNextEditableCell.actionPerformed(null); -// } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { -// e.consume(); -// moveToPreviousEditableCell.actionPerformed(null); -// } -// } -// }; - public ObservationRouteTable(String datePattern, - ListSelectionListener selectionListener) { - - // make tab key to focus only next editable cell - setSelectionModel(new ForceSelectionSelectionModel()); - - setSortable(false); - - putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); - - // redéfini les comportements par defaut de la table par les notres - getActionMap().put("selectNextRowCell", moveToNextEditableCell); - getActionMap().put("selectNextColumnCell", moveToNextEditableCell); - getActionMap().put("selectNextColumn", moveToNextEditableCell); - getActionMap().put("selectPreviousColumn", moveToPreviousEditableCell); - - // Key adapter à ajouter sur les éditeurs où l'on souhaite gérer les - // touches "entrer", "gauche", "doite" de facon personnalisée. - KeyListener goNextCellAdapter = new KeyAdapter() { - @Override - public void keyPressed(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER || - e.getKeyCode() == KeyEvent.VK_RIGHT) { - e.consume(); - moveToNextEditableCell.actionPerformed(null); - } else if (e.getKeyCode() == KeyEvent.VK_LEFT) { - e.consume(); - moveToPreviousEditableCell.actionPerformed(null); - } else if (e.getKeyCode() == KeyEvent.VK_UP) { - e.consume(); - moveToPreviousRowEditableCell.actionPerformed(null); - } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { - e.consume(); - moveToNextRowEditableCell.actionPerformed(null); - } - } - }; - - // TextCellEditor - { - TextCellEditor editor = new TextCellEditor(); - JTextField textField = editor.getComponent(); - textField.addKeyListener(goNextCellAdapter); - textField.setBorder(new LineBorder(Color.GRAY, 2)); -// textField.setBorder(BasicBorders.getTextFieldBorder()); - setDefaultEditor(String.class, editor); - } - // NumberCellEditor - { - NumberCellEditor<Integer> editor = JAXXWidgetUtil.newNumberTableCellEditor(Integer.class, false); - editor.getNumberEditor().setSelectAllTextOnError(true); - JTextField textField = editor.getNumberEditor().getTextField(); - textField.addKeyListener(goNextCellAdapter); - textField.setBorder(new LineBorder(Color.GRAY, 2)); -// textField.setBorder(BasicBorders.getTextFieldBorder()); - setDefaultEditor(int.class, editor); - setDefaultEditor(Integer.class, editor); - } - - // Boolean editor - { - TableCellEditor editor = getDefaultEditor(Boolean.class); - setDefaultEditor(boolean.class, editor); - TableCellRenderer renderer = getDefaultRenderer(Boolean.class); - setDefaultRenderer(boolean.class, renderer); - } - - // Renderers - TableCellRenderer defaultDateCellRenderer = getDefaultRenderer(Date.class); - setDefaultRenderer(Date.class, JAXXWidgetUtil.newDateTableCellRenderer(defaultDateCellRenderer, datePattern)); - - if (selectionListener != null) { - getSelectionModel().addListSelectionListener(selectionListener); - } - - SwingUtil.scrollToTableSelection(this); - } - - /** Appellé lorsque les données changent. */ - @Override - public void tableChanged(TableModelEvent e) { - super.tableChanged(e); - - switch (e.getType()) { - case TableModelEvent.INSERT: - - // auto select new added line - getSelectionModel().setSelectionInterval(e.getFirstRow(), e.getFirstRow()); - Rectangle rect = getCellRect(e.getFirstRow(), 0, true); - scrollRectToVisible(rect); - break; - - case TableModelEvent.DELETE: - case TableModelEvent.UPDATE: - - // auto select first line ? - if (getSelectedRow() < 0 && getRowCount() > 0) { - getSelectionModel().setSelectionInterval(0, 0); - } - break; - } - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableListSelectionListener.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableListSelectionListener.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableListSelectionListener.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,99 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ulr.sammoa.ui.swing.observations; - -import com.google.common.base.Preconditions; -import fr.ulr.sammoa.persistence.Observation; -import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.ListSelectionModel; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import java.util.List; - -/** - * Listen the observation table selection model to set the - * {@link FlightUIModel#observationEditBean} which isten the validator. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class ObservationTableListSelectionListener implements ListSelectionListener { - - private static final Logger logger = - LoggerFactory.getLogger(ObservationTableListSelectionListener.class); - - protected final FlightUIModel flightUIModel; - - public ObservationTableListSelectionListener(FlightUIModel flightUIModel) { - Preconditions.checkNotNull(flightUIModel); - this.flightUIModel = flightUIModel; - } - - @Override - public void valueChanged(ListSelectionEvent e) { - - if (!e.getValueIsAdjusting()) { - - ListSelectionModel listSelectionModel = - (ListSelectionModel) e.getSource(); - int firstIndex = e.getFirstIndex(); - int lastIndex = e.getLastIndex(); - Integer newSelectedRow = null; - - if (listSelectionModel.isSelectionEmpty()) { - - // no selection - } else if (listSelectionModel.isSelectedIndex(firstIndex)) { - - // use first index - newSelectedRow = firstIndex; - } else if (listSelectionModel.isSelectedIndex(lastIndex)) { - - // use last index - newSelectedRow = lastIndex; - } - List<Observation> observations = flightUIModel.getObservations(); - Observation observation = null; - - if (newSelectedRow != null && - newSelectedRow < observations.size()) { - observation = observations.get(newSelectedRow); - - if (logger.isInfoEnabled()) { - logger.info("Select observation from index {}", newSelectedRow); - } - } else { - if (logger.isInfoEnabled()) { - logger.info("No observation selected"); - } - } - - // set new observation in model to validate - flightUIModel.setObservationEditBean(observation); - } - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,265 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ulr.sammoa.ui.swing.observations; - -import com.google.common.base.Strings; -import fr.ulr.sammoa.persistence.Campaign; -import fr.ulr.sammoa.persistence.Flight; -import fr.ulr.sammoa.persistence.Observation; -import fr.ulr.sammoa.persistence.ObservationStatus; -import fr.ulr.sammoa.persistence.Observer; -import fr.ulr.sammoa.persistence.ObserverPosition; -import fr.ulr.sammoa.persistence.Region; -import fr.ulr.sammoa.persistence.Species; -import fr.ulr.sammoa.persistence.SpeciesImpl; -import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; -import fr.ulr.sammoa.ui.swing.util.SammoaUtil; -import jaxx.runtime.SwingUtil; -import org.apache.commons.lang3.tuple.Pair; - -import javax.swing.table.AbstractTableModel; -import java.util.Date; -import java.util.List; - -/** @author sletellier <letellier@codelutin.com> */ -public class ObservationTableModel extends AbstractTableModel { - - private static final long serialVersionUID = 1L; - - protected FlightUIModel flightUIModel; - - public ObservationTableModel(FlightUIModel flightUIModel) { - this.flightUIModel = flightUIModel; - } - - @Override - public int getRowCount() { - return getBean().size(); - } - - @Override - public int getColumnCount() { - return ObservationColumn.values().length; - } - - @Override - public String getColumnName(int column) { - ObservationColumn observationColumn = ObservationColumn.valueOf(column); - return observationColumn.getColumnName(); - } - - @Override - public Class<?> getColumnClass(int column) { - ObservationColumn observationColumn = ObservationColumn.valueOf(column); - return observationColumn.getType(); - } - - @Override - public boolean isCellEditable(int row, int column) { - ObservationColumn observationColumn = ObservationColumn.valueOf(column); - boolean result = observationColumn.isEditable(); - return result; - } - - @Override - public Object getValueAt(int row, int column) { - Observation observation = getBean(row); - ObservationColumn observationColumn = ObservationColumn.valueOf(column); - Object result = observationColumn.getValue(observation); - return result; - } - - @Override - public void setValueAt(Object aValue, int row, int column) { - Observation observation = getBean(row); - ObservationColumn observationColumn = ObservationColumn.valueOf(column); - Flight flight = observation.getFlight(); - Campaign campaign = flight.getCampaign(); - Region region = campaign.getRegion(); - observationColumn.setValue(observation, aValue, region); - fireTableRowsUpdated(row, row); - } - - public int getBeanIndex(Observation bean) { - int row = getBean().indexOf(bean); - return row; - } - - public List<Observation> getBean() { - return flightUIModel.getObservations(); - } - - public Observation getBean(int row) { - SwingUtil.ensureRowIndex(this, row); - Observation bean = getBean().get(row); - return bean; - } - - public Pair<Integer, Integer> getCell(Observation bean, String fieldName) { - - int row = getBeanIndex(bean); - int col = ObservationColumn.getValueFromFieldName(fieldName).ordinal(); - - Pair<Integer, Integer> cell = Pair.of(row, col); - return cell; - } - - protected enum ObservationColumn { - - OBSERVATION_NUMBER(false, Observation.PROPERTY_OBSERVATION_NUMBER), - OBSERVATION_TIME(false, Date.class, Observation.PROPERTY_OBSERVATION_TIME), - OBSERVER(false, - Observation.PROPERTY_OBSERVER_POSITION, - ObserverPosition.PROPERTY_OBSERVER, - Observer.PROPERTY_INITIALS - ), - POD_SIZE(true, int.class, Observation.PROPERTY_POD_SIZE), - SPECIES(true, Observation.PROPERTY_SPECIES, Species.PROPERTY_CODE) { - @Override - public void setValue(Observation bean, Object value, Region region) { - String newValue = (String) value; - - if (!Strings.isNullOrEmpty(newValue)) { - - String oldValue = bean.getSpecies() == null - ? null : bean.getSpecies().getCode(); - - if (!newValue.equals(oldValue)) { - - // Always use a new instance for different value to - // fire change on Observation and not on Species - Species species = new SpeciesImpl((String) value, region); - bean.setSpecies(species); - } - - } else { - bean.setSpecies(null); - } - } - }, - AGE(true, Observation.PROPERTY_AGE), - DEC_ANGLE(true, int.class, Observation.PROPERTY_DEC_ANGLE), - CUE(true, Observation.PROPERTY_CUE), - BEHAVIOUR(true, Observation.PROPERTY_BEHAVIOUR) { - @Override - public void setValue(Observation bean, Object value, Region region) { - String newValue = (String) value; - if (Strings.isNullOrEmpty(newValue)) { - newValue = null; - } - bean.setBehaviour(newValue); - } - }, - SWIM_DIR(true, int.class, Observation.PROPERTY_SWIM_DIR), - CALVES(true, Observation.PROPERTY_CALVES), - PHOTO(true, boolean.class, Observation.PROPERTY_PHOTO), - COMMENT(true, Observation.PROPERTY_COMMENT), - OBSERVATION_STATUS(true, ObservationStatus.class, Observation.PROPERTY_OBSERVATION_STATUS), - DELETED(true, boolean.class, Observation.PROPERTY_DELETED), - CIRCLE_BACK(true, Observation.class, "circleBack") { - @Override - public Object getValue(Observation bean) { - return bean; - } - - @Override - public void setValue(Observation bean, Object value, Region region) { - } - }; - - private boolean editable; - - private String[] beanProperties; - - private Class<?> type; - - private final String columnName; - - private ObservationColumn(boolean editable, - String... beanProperties) { - this(editable, String.class, beanProperties); - } - - private ObservationColumn(boolean editable, - Class<?> type, - String... beanProperties) { - this.editable = editable; - this.type = type; - this.beanProperties = beanProperties; - this.columnName = beanProperties[0]; - } - - public Class<?> getType() { - return type; - } - - public String getColumnName() { - return columnName; - } - - public int getColumnIndex() { - return ordinal(); - } - - public Object getValue(Observation bean) { - Object result = SammoaUtil.getPropertyValue(bean, beanProperties); - return result; - } - - public void setValue(Observation bean, Object value, Region region) { - if (type.isPrimitive() && value == null) { - // can not set a null value to a primitive field - } else { - SammoaUtil.setPropertyValue(bean, value, beanProperties); - } - } - - public boolean isEditable() { - boolean result = editable; - return result; - } - - public static ObservationColumn valueOf(int ordinal) { - for (ObservationColumn value : values()) { - if (ordinal == value.ordinal()) { - return value; - } - } - throw new EnumConstantNotPresentException(ObservationColumn.class, - "ordinal=" + ordinal); - } - - public static ObservationColumn getValueFromFieldName(String fieldName) { - ObservationColumn result = null; - for (ObservationColumn value : values()) { - if (fieldName.equals(value.columnName)) { - result = value; - break; - } - } - return result; - } - } -} Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,265 @@ +/* + * #%L + * SAMMOA :: UI Swing + * * + * $Id$ + * $HeadURL: http://svn.forge.codelutin.com/svn/sammoa/trunk/sammoa-ui-swing/src/main/jav... $ + * %% + * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ +package fr.ulr.sammoa.ui.swing.observations; + +import com.google.common.base.Strings; +import fr.ulr.sammoa.persistence.Campaign; +import fr.ulr.sammoa.persistence.Flight; +import fr.ulr.sammoa.persistence.Observation; +import fr.ulr.sammoa.persistence.ObservationStatus; +import fr.ulr.sammoa.persistence.Observer; +import fr.ulr.sammoa.persistence.ObserverPosition; +import fr.ulr.sammoa.persistence.Region; +import fr.ulr.sammoa.persistence.Species; +import fr.ulr.sammoa.persistence.SpeciesImpl; +import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; +import fr.ulr.sammoa.ui.swing.util.SammoaUtil; +import jaxx.runtime.SwingUtil; +import org.apache.commons.lang3.tuple.Pair; + +import javax.swing.table.AbstractTableModel; +import java.util.Date; +import java.util.List; + +/** @author sletellier <letellier@codelutin.com> */ +public class ObservationTableModel extends AbstractTableModel { + + private static final long serialVersionUID = 1L; + + protected FlightUIModel flightUIModel; + + public ObservationTableModel(FlightUIModel flightUIModel) { + this.flightUIModel = flightUIModel; + } + + @Override + public int getRowCount() { + return getBean().size(); + } + + @Override + public int getColumnCount() { + return ObservationColumn.values().length; + } + + @Override + public String getColumnName(int column) { + ObservationColumn observationColumn = ObservationColumn.valueOf(column); + return observationColumn.getColumnName(); + } + + @Override + public Class<?> getColumnClass(int column) { + ObservationColumn observationColumn = ObservationColumn.valueOf(column); + return observationColumn.getType(); + } + + @Override + public boolean isCellEditable(int row, int column) { + ObservationColumn observationColumn = ObservationColumn.valueOf(column); + boolean result = observationColumn.isEditable(); + return result; + } + + @Override + public Object getValueAt(int row, int column) { + Observation observation = getBean(row); + ObservationColumn observationColumn = ObservationColumn.valueOf(column); + Object result = observationColumn.getValue(observation); + return result; + } + + @Override + public void setValueAt(Object aValue, int row, int column) { + Observation observation = getBean(row); + ObservationColumn observationColumn = ObservationColumn.valueOf(column); + Flight flight = observation.getFlight(); + Campaign campaign = flight.getCampaign(); + Region region = campaign.getRegion(); + observationColumn.setValue(observation, aValue, region); + fireTableRowsUpdated(row, row); + } + + public int getBeanIndex(Observation bean) { + int row = getBean().indexOf(bean); + return row; + } + + public List<Observation> getBean() { + return flightUIModel.getObservations(); + } + + public Observation getBean(int row) { + SwingUtil.ensureRowIndex(this, row); + Observation bean = getBean().get(row); + return bean; + } + + public Pair<Integer, Integer> getCell(Observation bean, String fieldName) { + + int row = getBeanIndex(bean); + int col = ObservationColumn.getValueFromFieldName(fieldName).ordinal(); + + Pair<Integer, Integer> cell = Pair.of(row, col); + return cell; + } + + public enum ObservationColumn { + + OBSERVATION_NUMBER(false, Observation.PROPERTY_OBSERVATION_NUMBER), + OBSERVATION_TIME(false, Date.class, Observation.PROPERTY_OBSERVATION_TIME), + OBSERVER(false, + Observation.PROPERTY_OBSERVER_POSITION, + ObserverPosition.PROPERTY_OBSERVER, + Observer.PROPERTY_INITIALS + ), + POD_SIZE(true, int.class, Observation.PROPERTY_POD_SIZE), + SPECIES(true, Observation.PROPERTY_SPECIES, Species.PROPERTY_CODE) { + @Override + public void setValue(Observation bean, Object value, Region region) { + String newValue = (String) value; + + if (!Strings.isNullOrEmpty(newValue)) { + + String oldValue = bean.getSpecies() == null + ? null : bean.getSpecies().getCode(); + + if (!newValue.equals(oldValue)) { + + // Always use a new instance for different value to + // fire change on Observation and not on Species + Species species = new SpeciesImpl((String) value, region); + bean.setSpecies(species); + } + + } else { + bean.setSpecies(null); + } + } + }, + AGE(true, Observation.PROPERTY_AGE), + DEC_ANGLE(true, int.class, Observation.PROPERTY_DEC_ANGLE), + CUE(true, Observation.PROPERTY_CUE), + BEHAVIOUR(true, Observation.PROPERTY_BEHAVIOUR) { + @Override + public void setValue(Observation bean, Object value, Region region) { + String newValue = (String) value; + if (Strings.isNullOrEmpty(newValue)) { + newValue = null; + } + bean.setBehaviour(newValue); + } + }, + SWIM_DIR(true, int.class, Observation.PROPERTY_SWIM_DIR), + CALVES(true, Observation.PROPERTY_CALVES), + PHOTO(true, boolean.class, Observation.PROPERTY_PHOTO), + COMMENT(true, Observation.PROPERTY_COMMENT), + OBSERVATION_STATUS(true, ObservationStatus.class, Observation.PROPERTY_OBSERVATION_STATUS), + DELETED(true, boolean.class, Observation.PROPERTY_DELETED), + CIRCLE_BACK(true, Observation.class, "circleBack") { + @Override + public Object getValue(Observation bean) { + return bean; + } + + @Override + public void setValue(Observation bean, Object value, Region region) { + } + }; + + private boolean editable; + + private String[] beanProperties; + + private Class<?> type; + + private final String columnName; + + private ObservationColumn(boolean editable, + String... beanProperties) { + this(editable, String.class, beanProperties); + } + + private ObservationColumn(boolean editable, + Class<?> type, + String... beanProperties) { + this.editable = editable; + this.type = type; + this.beanProperties = beanProperties; + this.columnName = beanProperties[0]; + } + + public Class<?> getType() { + return type; + } + + public String getColumnName() { + return columnName; + } + + public int getColumnIndex() { + return ordinal(); + } + + public Object getValue(Observation bean) { + Object result = SammoaUtil.getPropertyValue(bean, beanProperties); + return result; + } + + public void setValue(Observation bean, Object value, Region region) { + if (type.isPrimitive() && value == null) { + // can not set a null value to a primitive field + } else { + SammoaUtil.setPropertyValue(bean, value, beanProperties); + } + } + + public boolean isEditable() { + boolean result = editable; + return result; + } + + public static ObservationColumn valueOf(int ordinal) { + for (ObservationColumn value : values()) { + if (ordinal == value.ordinal()) { + return value; + } + } + throw new EnumConstantNotPresentException(ObservationColumn.class, + "ordinal=" + ordinal); + } + + public static ObservationColumn getValueFromFieldName(String fieldName) { + ObservationColumn result = null; + for (ObservationColumn value : values()) { + if (fieldName.equals(value.columnName)) { + result = value; + break; + } + } + return result; + } + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/ObservationTableModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteNoModificationHighlightPredicate.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteNoModificationHighlightPredicate.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteNoModificationHighlightPredicate.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,65 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ulr.sammoa.ui.swing.observations; - -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.persistence.Routes; -import fr.ulr.sammoa.ui.swing.util.AbstractRowHighlightPredicate; - -public class RouteNoModificationHighlightPredicate extends AbstractRowHighlightPredicate { - - protected RouteTableModel model; - - public RouteNoModificationHighlightPredicate(RouteTableModel model) { - this.model = model; - } - - @Override - protected boolean isHighlighted(int rowIndex) { - - Route route = getValueAt(rowIndex); - - boolean result = false; - - if (route.isDeleted()) { - // do nothing - - } else { - - int previousRouteIndex = rowIndex - 1; - if (previousRouteIndex >= 0) { - - Route previousRoute = getValueAt(previousRouteIndex); - - result = Routes.equal(route, previousRoute); - } - } - return result; - } - - @Override - protected Route getValueAt(int rowIndex) { - return model.getRow(rowIndex); - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableListSelectionListener.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableListSelectionListener.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableListSelectionListener.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,99 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ulr.sammoa.ui.swing.observations; - -import com.google.common.base.Preconditions; -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.swing.ListSelectionModel; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import java.util.List; - -/** - * Listen the route table selection model to set the - * {@link FlightUIModel#routeEditBean} which isten the validator. - * - * @author tchemit <chemit@codelutin.com> - * @since 0.2 - */ -public class RouteTableListSelectionListener implements ListSelectionListener { - - private static final Logger logger = - LoggerFactory.getLogger(RouteTableListSelectionListener.class); - - protected final FlightUIModel flightUIModel; - - public RouteTableListSelectionListener(FlightUIModel flightUIModel) { - Preconditions.checkNotNull(flightUIModel); - this.flightUIModel = flightUIModel; - } - - @Override - public void valueChanged(ListSelectionEvent e) { - - if (!e.getValueIsAdjusting()) { - - ListSelectionModel listSelectionModel = - (ListSelectionModel) e.getSource(); - int firstIndex = e.getFirstIndex(); - int lastIndex = e.getLastIndex(); - Integer newSelectedRow = null; - - if (listSelectionModel.isSelectionEmpty()) { - - // no selection - } else if (listSelectionModel.isSelectedIndex(firstIndex)) { - - // use first index - newSelectedRow = firstIndex; - } else if (listSelectionModel.isSelectedIndex(lastIndex)) { - - // use last index - newSelectedRow = lastIndex; - } - List<Route> routes = flightUIModel.getRoutes(); - Route route = null; - - if (newSelectedRow != null && - newSelectedRow < routes.size()) { - route = routes.get(newSelectedRow); - - if (logger.isInfoEnabled()) { - logger.info("Select route from index {}", newSelectedRow); - } - } else { - if (logger.isInfoEnabled()) { - logger.info("No route selected"); - } - } - - // set new route in model to validate - flightUIModel.setRouteEditBean(route); - } - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java 2012-08-01 15:26:31 UTC (rev 338) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -1,242 +0,0 @@ -/* - * #%L - * SAMMOA :: UI Swing - * * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package fr.ulr.sammoa.ui.swing.observations; - -import fr.ulr.sammoa.persistence.Flight; -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.persistence.Transect; -import fr.ulr.sammoa.persistence.TransectFlight; -import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; -import fr.ulr.sammoa.ui.swing.util.SammoaUtil; -import jaxx.runtime.SwingUtil; -import org.apache.commons.lang3.tuple.Pair; - -import javax.swing.table.AbstractTableModel; -import java.util.Date; -import java.util.List; - -/** @author sletellier <letellier@codelutin.com> */ -public class RouteTableModel extends AbstractTableModel { - - private static final long serialVersionUID = 1L; - - protected FlightUIModel flightUIModel; - - public RouteTableModel(FlightUIModel flightUIModel) { - this.flightUIModel = flightUIModel; - } - - @Override - public Class<?> getColumnClass(int column) { - RouteColumn routeColumn = RouteColumn.valueOf(column); - return routeColumn.getType(); - } - - @Override - public int getRowCount() { - return getBean().size(); - } - - public Route getRow(int index) { - return getBean().get(index); - } - - public List<Route> getBean() { - return flightUIModel.getRoutes(); - } - - @Override - public int getColumnCount() { - return RouteColumn.values().length; - } - - @Override - public String getColumnName(int column) { - return RouteColumn.valueOf(column).getColumnName(); - } - - @Override - public boolean isCellEditable(int row, int column) { - Route route = getRow(row); - RouteColumn routeColumn = RouteColumn.valueOf(column); - boolean result = routeColumn.isEditable(route); - return result; - } - - @Override - public Object getValueAt(int row, int column) { - Route route = getRow(row); - RouteColumn routeColumn = RouteColumn.valueOf(column); - Object result = routeColumn.getValue(route, this); - return result; - } - - @Override - public void setValueAt(Object aValue, int row, int column) { - Route route = getRow(row); - RouteColumn routeColumn = RouteColumn.valueOf(column); - routeColumn.setValue(route, aValue); - fireTableRowsUpdated(row, row); - } - - public int getBeanIndex(Route bean) { - int row = getBean().indexOf(bean); - return row; - } - - public Route getBean(int row) { - SwingUtil.ensureRowIndex(this, row); - Route bean = getBean().get(row); - return bean; - } - - public Pair<Integer, Integer> getCell(Route bean, String fieldName) { - - int row = getBeanIndex(bean); - int col = RouteColumn.getValueFromFieldName(fieldName).ordinal(); - - Pair<Integer, Integer> cell = Pair.of(row, col); - return cell; - } - - protected Flight getFlight() { - return flightUIModel.getFlight(); - } - - protected enum RouteColumn { - - EFFORT_NUMBER(false, Route.PROPERTY_EFFORT_NUMBER), - BEGIN_TIME(false, Date.class, Route.PROPERTY_BEGIN_TIME), - ROUTE_TYPE(false, Route.PROPERTY_ROUTE_TYPE), - TRANSECT(false, - Route.PROPERTY_TRANSECT_FLIGHT, - TransectFlight.PROPERTY_TRANSECT, - Transect.PROPERTY_NAME - ) { - @Override - public Object getValue(Route bean, RouteTableModel model) { - Object result; - TransectFlight transectFlight = bean.getTransectFlight(); - if (transectFlight != null) { - - Transect transect = transectFlight.getTransect(); - - // retrieve the index of the transectFlight in the flight - int index = model.getFlight().getTransectFlightIndex(transectFlight); - - result = transect.getName() + " (" + index + ")"; - - } else { - result = null; - } - return result; - } - }, - SEA_STATE(true, int.class, Route.PROPERTY_SEA_STATE), - SWELL(true, int.class, Route.PROPERTY_SWELL), - TURBIDITY(true, int.class, Route.PROPERTY_TURBIDITY), - SKY_GLINT(true, int.class, Route.PROPERTY_SKY_GLINT), - GLARE_FROM(true, Integer.class, Route.PROPERTY_GLARE_FROM), - GLARE_TO(true, Integer.class, Route.PROPERTY_GLARE_TO), - GLARE_SEVERITY(true, int.class, Route.PROPERTY_GLARE_SEVERITY), - GLARE_UNDER(true, boolean.class, Route.PROPERTY_GLARE_UNDER), - CLOUD_COVER(true, int.class, Route.PROPERTY_CLOUD_COVER), - SUBJECTIVE_CONDITIONS(true, Route.PROPERTY_SUBJECTIVE_CONDITIONS), - UNEXPECTED_LEFT(true, Route.PROPERTY_UNEXPECTED_LEFT), - UNEXEPECTED_RIGHT(true, Route.PROPERTY_UNEXPECTED_RIGHT), - COMMENT(true, Route.PROPERTY_COMMENT), - DELETED(true, boolean.class, Route.PROPERTY_DELETED); - - private final boolean editable; - - private final String[] beanProperties; - - private final Class<?> type; - - private final String columnName; - - - private RouteColumn(boolean editable, - String... beanProperties) { - this(editable, null, beanProperties); - } - - private RouteColumn(boolean editable, - Class<?> type, - String... beanProperties) { - this.editable = editable; - this.type = type; - this.beanProperties = beanProperties; - this.columnName = beanProperties[0]; - } - - public Class<?> getType() { - return type; - } - - public String getColumnName() { - return columnName; - } - - public Object getValue(Route bean, RouteTableModel model) { - Object result = SammoaUtil.getPropertyValue(bean, beanProperties); - return result; - } - - public void setValue(Route bean, Object value) { - if (type.isPrimitive() && value==null) { - // can not set a null value to a primitive field - } else { - SammoaUtil.setPropertyValue(bean, value, beanProperties); - } - } - - public boolean isEditable(Route bean) { - boolean result = editable; - return result; - } - - public static RouteColumn valueOf(int ordinal) { - for (RouteColumn value : values()) { - if (ordinal == value.ordinal()) { - return value; - } - } - throw new EnumConstantNotPresentException(RouteColumn.class, - "ordinal=" + ordinal); - } - - public static RouteColumn getValueFromFieldName(String fieldName) { - RouteColumn result = null; - for (RouteColumn value : values()) { - if (fieldName.equals(value.columnName)) { - result = value; - break; - } - } - return result; - } - } -} Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,242 @@ +/* + * #%L + * SAMMOA :: UI Swing + * * + * $Id$ + * $HeadURL: http://svn.forge.codelutin.com/svn/sammoa/trunk/sammoa-ui-swing/src/main/jav... $ + * %% + * Copyright (C) 2012 UMS 3462, 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 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 Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/gpl-3.0.html>. + * #L% + */ +package fr.ulr.sammoa.ui.swing.observations; + +import fr.ulr.sammoa.persistence.Flight; +import fr.ulr.sammoa.persistence.Route; +import fr.ulr.sammoa.persistence.Transect; +import fr.ulr.sammoa.persistence.TransectFlight; +import fr.ulr.sammoa.ui.swing.flight.FlightUIModel; +import fr.ulr.sammoa.ui.swing.util.SammoaUtil; +import jaxx.runtime.SwingUtil; +import org.apache.commons.lang3.tuple.Pair; + +import javax.swing.table.AbstractTableModel; +import java.util.Date; +import java.util.List; + +/** @author sletellier <letellier@codelutin.com> */ +public class RouteTableModel extends AbstractTableModel { + + private static final long serialVersionUID = 1L; + + protected FlightUIModel flightUIModel; + + public RouteTableModel(FlightUIModel flightUIModel) { + this.flightUIModel = flightUIModel; + } + + @Override + public Class<?> getColumnClass(int column) { + RouteColumn routeColumn = RouteColumn.valueOf(column); + return routeColumn.getType(); + } + + @Override + public int getRowCount() { + return getBean().size(); + } + + public Route getRow(int index) { + return getBean().get(index); + } + + public List<Route> getBean() { + return flightUIModel.getRoutes(); + } + + @Override + public int getColumnCount() { + return RouteColumn.values().length; + } + + @Override + public String getColumnName(int column) { + return RouteColumn.valueOf(column).getColumnName(); + } + + @Override + public boolean isCellEditable(int row, int column) { + Route route = getRow(row); + RouteColumn routeColumn = RouteColumn.valueOf(column); + boolean result = routeColumn.isEditable(route); + return result; + } + + @Override + public Object getValueAt(int row, int column) { + Route route = getRow(row); + RouteColumn routeColumn = RouteColumn.valueOf(column); + Object result = routeColumn.getValue(route, this); + return result; + } + + @Override + public void setValueAt(Object aValue, int row, int column) { + Route route = getRow(row); + RouteColumn routeColumn = RouteColumn.valueOf(column); + routeColumn.setValue(route, aValue); + fireTableRowsUpdated(row, row); + } + + public int getBeanIndex(Route bean) { + int row = getBean().indexOf(bean); + return row; + } + + public Route getBean(int row) { + SwingUtil.ensureRowIndex(this, row); + Route bean = getBean().get(row); + return bean; + } + + public Pair<Integer, Integer> getCell(Route bean, String fieldName) { + + int row = getBeanIndex(bean); + int col = RouteColumn.getValueFromFieldName(fieldName).ordinal(); + + Pair<Integer, Integer> cell = Pair.of(row, col); + return cell; + } + + protected Flight getFlight() { + return flightUIModel.getFlight(); + } + + protected enum RouteColumn { + + EFFORT_NUMBER(false, Route.PROPERTY_EFFORT_NUMBER), + BEGIN_TIME(false, Date.class, Route.PROPERTY_BEGIN_TIME), + ROUTE_TYPE(false, Route.PROPERTY_ROUTE_TYPE), + TRANSECT(false, + Route.PROPERTY_TRANSECT_FLIGHT, + TransectFlight.PROPERTY_TRANSECT, + Transect.PROPERTY_NAME + ) { + @Override + public Object getValue(Route bean, RouteTableModel model) { + Object result; + TransectFlight transectFlight = bean.getTransectFlight(); + if (transectFlight != null) { + + Transect transect = transectFlight.getTransect(); + + // retrieve the index of the transectFlight in the flight + int index = model.getFlight().getTransectFlightIndex(transectFlight); + + result = transect.getName() + " (" + index + ")"; + + } else { + result = null; + } + return result; + } + }, + SEA_STATE(true, int.class, Route.PROPERTY_SEA_STATE), + SWELL(true, int.class, Route.PROPERTY_SWELL), + TURBIDITY(true, int.class, Route.PROPERTY_TURBIDITY), + SKY_GLINT(true, int.class, Route.PROPERTY_SKY_GLINT), + GLARE_FROM(true, Integer.class, Route.PROPERTY_GLARE_FROM), + GLARE_TO(true, Integer.class, Route.PROPERTY_GLARE_TO), + GLARE_SEVERITY(true, int.class, Route.PROPERTY_GLARE_SEVERITY), + GLARE_UNDER(true, boolean.class, Route.PROPERTY_GLARE_UNDER), + CLOUD_COVER(true, int.class, Route.PROPERTY_CLOUD_COVER), + SUBJECTIVE_CONDITIONS(true, Route.PROPERTY_SUBJECTIVE_CONDITIONS), + UNEXPECTED_LEFT(true, Route.PROPERTY_UNEXPECTED_LEFT), + UNEXEPECTED_RIGHT(true, Route.PROPERTY_UNEXPECTED_RIGHT), + COMMENT(true, Route.PROPERTY_COMMENT), + DELETED(true, boolean.class, Route.PROPERTY_DELETED); + + private final boolean editable; + + private final String[] beanProperties; + + private final Class<?> type; + + private final String columnName; + + + private RouteColumn(boolean editable, + String... beanProperties) { + this(editable, null, beanProperties); + } + + private RouteColumn(boolean editable, + Class<?> type, + String... beanProperties) { + this.editable = editable; + this.type = type; + this.beanProperties = beanProperties; + this.columnName = beanProperties[0]; + } + + public Class<?> getType() { + return type; + } + + public String getColumnName() { + return columnName; + } + + public Object getValue(Route bean, RouteTableModel model) { + Object result = SammoaUtil.getPropertyValue(bean, beanProperties); + return result; + } + + public void setValue(Route bean, Object value) { + if (type.isPrimitive() && value==null) { + // can not set a null value to a primitive field + } else { + SammoaUtil.setPropertyValue(bean, value, beanProperties); + } + } + + public boolean isEditable(Route bean) { + boolean result = editable; + return result; + } + + public static RouteColumn valueOf(int ordinal) { + for (RouteColumn value : values()) { + if (ordinal == value.ordinal()) { + return value; + } + } + throw new EnumConstantNotPresentException(RouteColumn.class, + "ordinal=" + ordinal); + } + + public static RouteColumn getValueFromFieldName(String fieldName) { + RouteColumn result = null; + for (RouteColumn value : values()) { + if (fieldName.equals(value.columnName)) { + result = value; + break; + } + } + return result; + } + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/RouteTableModel.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextEditableCellAction.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextEditableCellAction.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextEditableCellAction.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,67 @@ +package fr.ulr.sammoa.ui.swing.observations.action; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.AbstractAction; +import javax.swing.JTable; +import java.awt.event.ActionEvent; + +/** +* Action to edit next editable cell from selected cell. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.5 +*/ +public class MoveToNextEditableCellAction extends AbstractAction { + private static final long serialVersionUID = 1L; + + private static final Logger logger = + LoggerFactory.getLogger(MoveToNextEditableCellAction.class); + + protected final JTable table; + + public MoveToNextEditableCellAction(JTable table) { + this.table = table; + } + + + @Override + public void actionPerformed(ActionEvent e) { + int currentRow = table.getSelectedRow(); + int currentColumn = table.getSelectedColumn(); + + logger.debug("Move to next editable cell, {}, {}", currentRow, currentColumn); + + while (currentRow <= table.getRowCount() || currentColumn <= table.getColumnCount()) { + + // go to next cell + currentColumn++; + + // select next cell + if (currentColumn >= table.getColumnCount()) { + currentColumn = 0; + currentRow++; + } + + if (table.isCellEditable(currentRow, currentColumn)) { + doSelectCell(currentRow, currentColumn); +// // select cell +// setColumnSelectionInterval(currentColumn, currentColumn); +// setRowSelectionInterval(currentRow, currentRow); +// editCellAt(currentRow, currentColumn); +// logger.debug("While select cell at {}, {}", currentRow, currentColumn); + break; + } else { + logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); + } + } + } + + protected void doSelectCell(int currentRow, int currentColumn) { + table.setColumnSelectionInterval(currentColumn, currentColumn); + table.setRowSelectionInterval(currentRow, currentRow); + logger.debug("While select cell at {}, {}", currentRow, currentColumn); + table.editCellAt(currentRow, currentColumn); + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextEditableCellAction.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextRowEditableAction.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextRowEditableAction.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextRowEditableAction.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,53 @@ +package fr.ulr.sammoa.ui.swing.observations.action; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.AbstractAction; +import javax.swing.JTable; +import java.awt.event.ActionEvent; + +/** +* Action to edit next row only if selected cell is editable. +* +* @author tchemit <chemit@codelutin.com> +* @since 0.5 +*/ +public class MoveToNextRowEditableAction extends AbstractAction { + + private static final long serialVersionUID = 1L; + + private static final Logger logger = LoggerFactory.getLogger(MoveToNextRowEditableAction.class); + + protected final JTable table; + + public MoveToNextRowEditableAction(JTable table) { + this.table = table; + } + + @Override + public void actionPerformed(ActionEvent e) { + int currentRow = table.getSelectedRow(); + int currentColumn = table.getSelectedColumn(); + + if (table.isCellEditable(currentRow, currentColumn)) { + logger.debug("Move to next row editable cell, {}, {}", currentRow, currentColumn); + currentRow++; + + if (currentRow >= table.getRowCount()) { + logger.debug("No next row"); + } else { + doSelectCell(currentRow, currentColumn); + } + } else { + logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); + } + } + + protected void doSelectCell(int currentRow, int currentColumn) { + table.setColumnSelectionInterval(currentColumn, currentColumn); + table.setRowSelectionInterval(currentRow, currentRow); + logger.debug("While select cell at {}, {}", currentRow, currentColumn); + table.editCellAt(currentRow, currentColumn); + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToNextRowEditableAction.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousEditableCellAction.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousEditableCellAction.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousEditableCellAction.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,66 @@ +package fr.ulr.sammoa.ui.swing.observations.action; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.AbstractAction; +import javax.swing.JTable; +import java.awt.event.ActionEvent; + +/** + * Action to edit previous editable cell from selected cell. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.5 + */ +public class MoveToPreviousEditableCellAction extends AbstractAction { + private static final long serialVersionUID = 1L; + + private static final Logger logger = + LoggerFactory.getLogger(MoveToPreviousEditableCellAction.class); + + protected final JTable table; + + public MoveToPreviousEditableCellAction(JTable table) { + this.table = table; + } + + @Override + public void actionPerformed(ActionEvent e) { + int currentRow = table.getSelectedRow(); + int currentColumn = table.getSelectedColumn(); + + logger.debug("Move to previous editable cell, {}, {}", currentRow, currentColumn); + + while (currentRow > 0 || currentColumn > 0) { + + // go to next cell + currentColumn--; + + // select next cell + if (currentColumn < 0) { + currentColumn = table.getColumnCount() - 1; + currentRow--; + } + + if (table.isCellEditable(currentRow, currentColumn)) { + doSelectCell(currentRow, currentColumn); +// // select cell +// setColumnSelectionInterval(currentColumn, currentColumn); +// setRowSelectionInterval(currentRow, currentRow); +// logger.debug("While select cell at {}, {}", currentRow, currentColumn); +// editCellAt(currentRow, currentColumn); + break; + } else { + logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); + } + } + } + + protected void doSelectCell(int currentRow, int currentColumn) { + table.setColumnSelectionInterval(currentColumn, currentColumn); + table.setRowSelectionInterval(currentRow, currentRow); + logger.debug("While select cell at {}, {}", currentRow, currentColumn); + table.editCellAt(currentRow, currentColumn); + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousEditableCellAction.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousRowEditableAction.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousRowEditableAction.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousRowEditableAction.java 2012-08-01 16:32:14 UTC (rev 339) @@ -0,0 +1,53 @@ +package fr.ulr.sammoa.ui.swing.observations.action; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.swing.AbstractAction; +import javax.swing.JTable; +import java.awt.event.ActionEvent; + +/** +* Action to edit previous row only if selected cell is editable. + * + * @author tchemit <chemit@codelutin.com> + * @since 0.5 +*/ +public class MoveToPreviousRowEditableAction extends AbstractAction { + private static final long serialVersionUID = 1L; + + private static final Logger logger = LoggerFactory.getLogger(MoveToPreviousRowEditableAction.class); + + protected final JTable table; + + public MoveToPreviousRowEditableAction(JTable table) { + this.table = table; + } + + @Override + public void actionPerformed(ActionEvent e) { + + int currentRow = table.getSelectedRow(); + int currentColumn = table.getSelectedColumn(); + + if (table.isCellEditable(currentRow, currentColumn)) { + logger.debug("Move to previous row editable cell, {}, {}", currentRow, currentColumn); + currentRow--; + + if (currentRow < 0) { + logger.debug("No previous row"); + } else { + doSelectCell(currentRow, currentColumn); + } + } else { + logger.debug("Cell at {}, {} not editable", currentRow, currentColumn); + } + } + + protected void doSelectCell(int currentRow, int currentColumn) { + table.setColumnSelectionInterval(currentColumn, currentColumn); + table.setRowSelectionInterval(currentRow, currentRow); + logger.debug("While select cell at {}, {}", currentRow, currentColumn); + table.editCellAt(currentRow, currentColumn); + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/action/MoveToPreviousRowEditableAction.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native