Author: fdesbois Date: 2012-08-20 18:13:19 +0200 (Mon, 20 Aug 2012) New Revision: 451 Url: http://forge.codelutin.com/repositories/revision/sammoa/451 Log: refs #1204 : remove TransectTable and move behavior in FlightUIHandler Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/ButtonActionTableCellEditorRenderer.java Removed: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectBooleanTableCell.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTable.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableHandler.java Modified: trunk/sammoa-persistence/src/main/java/fr/ulr/sammoa/persistence/ObservationDAOImpl.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/action/NextTransectAction.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.css trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.jaxx trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUIHandler.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectFlightModel.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableModel.java 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/util/SammoaUtil.java Modified: trunk/sammoa-persistence/src/main/java/fr/ulr/sammoa/persistence/ObservationDAOImpl.java =================================================================== --- trunk/sammoa-persistence/src/main/java/fr/ulr/sammoa/persistence/ObservationDAOImpl.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-persistence/src/main/java/fr/ulr/sammoa/persistence/ObservationDAOImpl.java 2012-08-20 16:13:19 UTC (rev 451) @@ -81,11 +81,7 @@ public List<E> findAllByFlightOrderedByObservationTime(Flight flight) { - String ql = String.format("FROM %1$s WHERE %2$s = :flight ORDER BY %3$s", - ObservationImpl.class.getSimpleName(), - Observation.PROPERTY_FLIGHT, - Observation.PROPERTY_OBSERVATION_TIME - ); + String ql = "FROM ObservationImpl WHERE flight = :flight ORDER BY observationTime"; try { List<E> result = findAllByQuery(ql, "flight", flight); @@ -97,11 +93,7 @@ public int findLastObservationNumber(Flight flight) { - String ql = String.format("SELECT max(%1$s) FROM %2$s WHERE %3$s = :flight", - Observation.PROPERTY_OBSERVATION_NUMBER, - ObservationImpl.class.getSimpleName(), - Observation.PROPERTY_FLIGHT - ); + String ql = "SELECT max(observationNumber) FROM ObservationImpl WHERE flight = :flight"; try { Integer queryResult = findByQuery(Integer.class, ql, "flight", flight); Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/action/NextTransectAction.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/action/NextTransectAction.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/action/NextTransectAction.java 2012-08-20 16:13:19 UTC (rev 451) @@ -23,12 +23,11 @@ */ package fr.ulr.sammoa.ui.swing.action; -import fr.ulr.sammoa.persistence.TransectFlight; +import fr.ulr.sammoa.ui.swing.flight.TransectFlightModel; import jaxx.runtime.JAXXContext; import org.nuiton.util.Resource; -import javax.swing.Action; -import javax.swing.JComponent; +import javax.swing.*; import java.awt.event.ActionEvent; import static org.nuiton.i18n.I18n._; @@ -42,7 +41,7 @@ private static final long serialVersionUID = 1L; - public static final String CLIENT_PROPERTY_INDEX = "index"; + public static final String CLIENT_PROPERTY_TRANSECT_FLIGHT = "transectFlight"; public NextTransectAction(JAXXContext context) { super(Resource.getIcon("/icons/action-next-transect.png"), context); @@ -52,15 +51,13 @@ @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); - if (source instanceof JComponent) { + TransectFlightModel transectFlight = null; + if (source instanceof JComponent) { JComponent component = (JComponent) e.getSource(); - Integer index = (Integer) component.getClientProperty(CLIENT_PROPERTY_INDEX); + transectFlight = (TransectFlightModel) component.getClientProperty(CLIENT_PROPERTY_TRANSECT_FLIGHT); - if (index != null) { - TransectFlight transectFlight = getFlight().getTransectFlight().get(index); - getFlightController().setNextTransect(transectFlight); - } + getFlightController().setNextTransect(transectFlight.getSource()); } } Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.css =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.css 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.css 2012-08-20 16:13:19 UTC (rev 451) @@ -142,6 +142,7 @@ } #transectTable { + model:{transectTableModel}; selectionModel:{transectFlightSelectionModel}; } Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.jaxx =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.jaxx 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUI.jaxx 2012-08-20 16:13:19 UTC (rev 451) @@ -31,6 +31,8 @@ javax.swing.ListSelectionModel javax.swing.DefaultListSelectionModel + org.jdesktop.swingx.JXTable + com.bbn.openmap.gui.OverlayMapPanel fr.ulr.sammoa.persistence.Flight @@ -174,8 +176,7 @@ </Table> <JScrollPane> - <TransectTable id='transectTable' - constructorParams='transectTableModel'/> + <JXTable id='transectTable'/> </JScrollPane> </JSplitPane> Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUIHandler.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUIHandler.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/FlightUIHandler.java 2012-08-20 16:13:19 UTC (rev 451) @@ -42,7 +42,9 @@ import com.bbn.openmap.util.PropUtils; import com.google.common.base.Objects; import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; import com.google.common.base.Strings; +import com.google.common.base.Supplier; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -95,27 +97,20 @@ import fr.ulr.sammoa.ui.swing.flight.layer.SimpleGeoPointLayer; import fr.ulr.sammoa.ui.swing.flight.layer.TransectLayer; import fr.ulr.sammoa.ui.swing.transect.TransectUI; +import fr.ulr.sammoa.ui.swing.util.ButtonActionTableCellEditorRenderer; import fr.ulr.sammoa.ui.swing.util.ColorTableCellRenderer; import fr.ulr.sammoa.ui.swing.util.SammoaUtil; import jaxx.runtime.JAXXObject; import jaxx.runtime.SwingUtil; import jaxx.runtime.swing.JAXXWidgetUtil; import jaxx.runtime.swing.editor.cell.NumberCellEditor; +import org.jdesktop.swingx.JXTable; import org.nuiton.util.ApplicationConfig; import org.nuiton.util.TimeLog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.swing.AbstractButton; -import javax.swing.Action; -import javax.swing.ActionMap; -import javax.swing.InputMap; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JComponent; -import javax.swing.KeyStroke; -import javax.swing.ListCellRenderer; -import javax.swing.ListSelectionModel; +import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; @@ -123,6 +118,7 @@ import java.awt.event.ComponentListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.beans.IndexedPropertyChangeEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.File; @@ -357,113 +353,137 @@ timeLog.log(uiStartTime, "initUI"); + long startTime = TimeLog.getTime(); + + // Strate ComboBox + { + JComboBox comboBox = ui.getStrateCombobox(); + + comboBox.setRenderer(decoratorService.newListCellRender(StrateModel.class)); + + StrateModel strateAll = getModel().getStrateAll(); + SwingUtil.fillComboBox(comboBox, getModel().getStrates(), strateAll); + + selectStrate(strateAll); + } + ListCellRenderer observerListCellRenderer = decoratorService.newListCellRender(Observer.class); + // Observer List { - // init transect table + ui.getObserverSelector().setRenderer( + new ObserverListCellRenderer(observerListCellRenderer) + ); + } - TransectTable transectTable = ui.getTransectTable(); + // Transect List + { + ui.getTransectList().setCellRenderer( + new TransectListCellRenderer(decoratorService.newListCellRender((TransectModel.class)))); + } - TransectTableModel transectTableModel = ui.getTransectTableModel(); + // Transect Table + { + JXTable table = ui.getTransectTable(); - // for boolean - TableCellRenderer booleanCellRenderer = transectTable.getDefaultRenderer(Boolean.class); - TransectBooleanTableCell booleanTableCell = new TransectBooleanTableCell(booleanCellRenderer, transectTable.getActionButtons()); - transectTable.setDefaultRenderer(Boolean.class, booleanTableCell); + TransectTableModel tableModel = ui.getTransectTableModel(); - transectTable.setDefaultEditor( - Observer.class, - new ObserverTableCellEditor(transectTableModel.getReference(), - observerListCellRenderer)); + NumberCellEditor<Integer> numberCellEditor = JAXXWidgetUtil.newNumberTableCellEditor(Integer.class, false); + table.setDefaultEditor(Integer.class, numberCellEditor); + table.setDefaultEditor(int.class, numberCellEditor); - transectTable.setDefaultRenderer(Observer.class, - decoratorService.newTableCellRender(Observer.class)); + TableCellRenderer stringRenderer = table.getDefaultRenderer(String.class); - // Set specific renderer for NbTimes column - TableCellRenderer defaultRenderer = transectTable.getDefaultRenderer(String.class); - TransectCrossingNumberCellRenderer cellRenderer = new TransectCrossingNumberCellRenderer(defaultRenderer, transectTableModel); - transectTable.getColumnModel().getColumn(TransectTableModel.TransectColumn.CROSSING_NUMBER.ordinal()).setCellRenderer(cellRenderer); + // OBSERVER columns (Left, Right, Nav, Co-Nav positions) + { + table.setDefaultEditor( + Observer.class, + new ObserverTableCellEditor(tableModel.getReference(), observerListCellRenderer) + ); - transectTable.setDefaultEditor(Boolean.class, booleanTableCell); + table.setDefaultRenderer(Observer.class, + decoratorService.newTableCellRender(Observer.class)); + } - NumberCellEditor<Integer> numberCellEditor = JAXXWidgetUtil.newNumberTableCellEditor(Integer.class, false); - transectTable.setDefaultEditor(Integer.class, numberCellEditor); - transectTable.setDefaultEditor(int.class, numberCellEditor); + // CROSSING_NUMBER column + { + TableColumn column = table.getColumn(TransectTableModel.TransectColumn.CROSSING_NUMBER.ordinal()); - // Position left column + TransectCrossingNumberCellRenderer cellRenderer = + new TransectCrossingNumberCellRenderer(stringRenderer, tableModel); + + column.setCellRenderer(cellRenderer); + } + + // LEFT position column { - TableColumn column = transectTable.getColumn(TransectTableModel.TransectColumn.POSITION_LEFT.ordinal()); - column.setHeaderRenderer(new ColorTableCellRenderer(defaultRenderer, SammoaColors.POSITION_LEFT_COLOR)); + TableColumn column = table.getColumn(TransectTableModel.TransectColumn.POSITION_LEFT.ordinal()); + column.setHeaderRenderer(new ColorTableCellRenderer(stringRenderer, SammoaColors.POSITION_LEFT_COLOR)); } - // Position right column + // RIGHT position column { - TableColumn column = transectTable.getColumn(TransectTableModel.TransectColumn.POSITION_RIGHT.ordinal()); - column.setHeaderRenderer(new ColorTableCellRenderer(defaultRenderer, SammoaColors.POSITION_RIGHT_COLOR)); + TableColumn column = table.getColumn(TransectTableModel.TransectColumn.POSITION_RIGHT.ordinal()); + column.setHeaderRenderer(new ColorTableCellRenderer(stringRenderer, SammoaColors.POSITION_RIGHT_COLOR)); } - transectTable.addHighlighter( + // ACTION column (Next Transect) + { + int columnIndex = TransectTableModel.TransectColumn.ACTION.ordinal(); + TableColumn column = table.getColumnModel().getColumn(columnIndex); + ButtonActionTableCellEditorRenderer editorRenderer = new ButtonActionTableCellEditorRenderer( + NextTransectAction.CLIENT_PROPERTY_TRANSECT_FLIGHT, + new Supplier<Action>() { + + @Override + public Action get() { + Action result = getActionMap().get("nextTransect"); + return result; + } + }, + new Predicate<Object>() { + + @Override + public boolean apply(Object input) { + return !((TransectFlightModel) input).isDeleted(); + } + }); + + column.setCellEditor(editorRenderer); + column.setCellRenderer(editorRenderer); + } + + table.addHighlighter( SammoaUtil.newColorHighlighter( - new CurrentTransectHighlightPredicate(transectTableModel), + new CurrentTransectHighlightPredicate(tableModel), SammoaColors.CURRENT_TRANSECT_ROW_COLOR) ); - transectTable.addHighlighter( + table.addHighlighter( SammoaUtil.newColorHighlighter( - new NextTransectHighlightPredicate(transectTableModel), + new NextTransectHighlightPredicate(tableModel), SammoaColors.NEXT_TRANSECT_ROW_COLOR) ); - transectTable.setSortable(false); + table.setSortable(false); } - ui.getStrateCombobox().setRenderer( - decoratorService.newListCellRender(StrateModel.class)); - ui.getObserverSelector().setRenderer( - new ObserverListCellRenderer(observerListCellRenderer) - ); - ui.getTransectList().setCellRenderer( - new TransectListCellRenderer(decoratorService.newListCellRender((TransectModel.class)))); + startTime = timeLog.log(startTime, "afterInitUI", "decoration done (editor/renderer)"); - long startTime = TimeLog.getTime(); - - // Init the actions initActions(); startTime = timeLog.log(startTime, "afterInitUI", "actions are created"); - // Add binding for Flight observers to update comboboxes references - // based on flightObserverForPositions getModel().getFlight().addPropertyChangeListener( - Flight.PROPERTY_OBSERVER, new PropertyChangeListener() { + Flight.PROPERTY_OBSERVER, flightObserversListener); - @Override - public void propertyChange(PropertyChangeEvent evt) { - Flight flight = (Flight) evt.getSource(); - List<Observer> observers = flightService.getFlightObserverForPositions(flight); - getModel().setFlightObserverForPositions(observers); - } - }); + getModel().addPropertyChangeListener( + FlightUIModel.PROPERTY_CURRENT_ROUTE, currentRouteListener); - // Strate ComboBox - StrateModel strateAll = getModel().getStrateAll(); - SwingUtil.fillComboBox(ui.getStrateCombobox(), getModel().getStrates(), strateAll); - selectStrate(strateAll); + getModel().addPropertyChangeListener( + FlightUIModel.PROPERTY_NEXT_TRANSECT, nextTransectListener); -// // Update current Transect depends on current Route -// getModel().addPropertyChangeListener( -// FlightUIModel.PROPERTY_CURRENT_ROUTE, new PropertyChangeListener() { -// -// @Override -// public void propertyChange(PropertyChangeEvent evt) { -// -// setCurrentTransect((Route) evt.getOldValue(), false); -// -// setCurrentTransect((Route) evt.getNewValue(), true); -// } -// }); - transectUi = new TransectUI(context); transectUi.addComponentListener(transectUIListener); @@ -500,10 +520,11 @@ @Override public void onRouteAdded(Route route) { + getModel().setCurrentRoute(route); + if (route != null) { getModel().addRoute(route); } - getModel().setCurrentRoute(route); } @Override @@ -591,38 +612,9 @@ // Add elements to the flight if (!Iterables.isEmpty(transects)) { -// Flight flight = getModel().getFlight(); -// -// // Ensure transect instances for all existing transectFlight -// // FIXME-fdesbois-2012-07-18 : to avoid this manual binding we -// // need to load all entities at startup in the same topiaContext -// // for the moment, it's not possible because of the transect management, -// // but it should be after cleaning import transect -// for (TransectFlight transectFlight : flight.getTransectFlight()) { -// Transect transect = transectFlight.getTransect(); -// -// for (Transect newTransect : transects) { -// -// // Check if it's equal but not the same instance -// if (transect.equals(newTransect) && transect != newTransect) { -// -// // Update the transect instance -// transectFlight.setTransect(newTransect); -// -// // Fire the table for change -// int rowIndex = ui.getTransectTableModel().getRowIndex(transectFlight); -// ui.getTransectTableModel().fireTableRowsUpdated(rowIndex, rowIndex); -// -// if (logger.isDebugEnabled()) { -// logger.debug(String.format("Transect instance %s change for previous TransectFlight %d", transect.getName(), rowIndex)); -// } -// } -// } -// } - // Retrieve the last selected row from the table - TransectTable table = ui.getTransectTable(); - int fromIndex = table.getLastSelectedRow(); + JTable table = ui.getTransectTable(); + int fromIndex = SammoaUtil.getLastSelectedRow(table); if (fromIndex == -1) { // Add after the last row fromIndex = table.getRowCount(); @@ -647,17 +639,6 @@ int toIndex = fromIndex + newTransectFlights.size() - 1; ui.getTransectTableModel().fireTableRowsInserted(fromIndex, toIndex); - - -// // Retrieve real nbTimes for each new transect -// Map<Transect, Long> transectRealNbTimes = -// flightService.getTransectRealNbTimes(Lists.newArrayList(transects)); -// getModel().putAllTransectRealNbTimes(transectRealNbTimes); - - // Display the transects if flight is started -// if (getFlightController().isRunning()) { -// showOnlyFlightTransects(listModel.getSelectedReferences()); -// } } } @@ -716,10 +697,10 @@ getModel().setActionMap(getActionMap()); initActions(getActionMap(), ui, getInputMap()); - List<JButton> actionButtons = ui.getTransectTable().getActionButtons(); - for (JButton actionButton : actionButtons) { - initAction(getActionMap(), actionButton, getInputMap()); - } +// List<JButton> actionButtons = ui.getTransectTable().getActionButtons(); +// for (JButton actionButton : actionButtons) { +// initAction(getActionMap(), actionButton, getInputMap()); +// } } protected void putAction(String actionName, Action action) { @@ -922,6 +903,54 @@ } } + protected PropertyChangeListener flightObserversListener = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + + // Add binding for Flight observers to update comboboxes references + // based on flightObserverForPositions + Flight flight = (Flight) evt.getSource(); + List<Observer> observers = flightService.getFlightObserverForPositions(flight); + getModel().setFlightObserverForPositions(observers); + + // Refresh all the table if some observer is removed (index = old collection size) + if (evt instanceof IndexedPropertyChangeEvent) { + int oldSize = ((IndexedPropertyChangeEvent) evt).getIndex(); + int currentSize = getModel().getFlight().sizeObserver(); + + if (currentSize < oldSize) { + ui.getTransectTableModel().fireTableDataChanged(); + } + } + } + }; + + protected PropertyChangeListener currentRouteListener = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + + Route oldValue = (Route) evt.getOldValue(); + if (oldValue != null) { + ui.getTransectTableModel().fireTableRowUpdated(oldValue.getTransectFlight()); + } + Route newValue = (Route) evt.getNewValue(); + if (newValue != null) { + ui.getTransectTableModel().fireTableRowUpdated(newValue.getTransectFlight()); + } + } + }; + + protected PropertyChangeListener nextTransectListener = new PropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent evt) { + ui.getTransectTableModel().fireTableRowUpdated((TransectFlight) evt.getOldValue()); + ui.getTransectTableModel().fireTableRowUpdated((TransectFlight) evt.getNewValue()); + } + }; + protected PropertyChangeListener transectFlightListener = new PropertyChangeListener() { @Override @@ -951,11 +980,15 @@ @Override public void stateChanged(DeviceStateEvent event) { + DeviceTechnicalException error = event.getError(); if (event.getOldValue() != DeviceState.ERROR && event.getNewValue() == DeviceState.ERROR && - event.getError() != null) { + error != null) { - context.setStatusMessage(event.getError().getMessageWithCause()); + if (logger.isErrorEnabled()) { + logger.error(error.getMessageWithCause(), error); + } + context.setStatusMessage(error.getMessageWithCause()); } } }; Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectBooleanTableCell.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectBooleanTableCell.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectBooleanTableCell.java 2012-08-20 16:13:19 UTC (rev 451) @@ -1,117 +0,0 @@ -package fr.ulr.sammoa.ui.swing.flight; -/* - * #%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 jaxx.runtime.swing.JAXXWidgetUtil; -import jaxx.runtime.swing.editor.BooleanCellEditor; - -import javax.swing.AbstractCellEditor; -import javax.swing.JButton; -import javax.swing.JComponent; -import javax.swing.JTable; -import javax.swing.table.TableCellEditor; -import javax.swing.table.TableCellRenderer; -import java.awt.Component; -import java.util.List; - -/** - * TODO - * - * @author tchemit <chemit@codelutin.com> - * @since TODO - */ -public class TransectBooleanTableCell extends AbstractCellEditor - implements TableCellRenderer, TableCellEditor { - - private static final long serialVersionUID = 1L; - - protected final BooleanCellEditor delegateEditor; - - protected final List<JButton> actionButtons; - - public TransectBooleanTableCell(TableCellRenderer cellRenderer, - List<JButton> actionButtons) { - delegateEditor = JAXXWidgetUtil.newBooleanTableCellEditor(cellRenderer); - this.actionButtons = actionButtons; - } - - @Override - public Component getTableCellRendererComponent(JTable table, - Object value, - boolean isSelected, - boolean hasFocus, - int row, - int column) { - - TransectTableModel.TransectColumn transectColumn = - TransectTableModel.TransectColumn.valueOf(column); - - Component result = null; - switch (transectColumn) { - - case DELETED: - JComponent render = (JComponent) delegateEditor.getTableCellRendererComponent( - table, value, isSelected, hasFocus, row, column); - - result = render; - break; - case ACTION: - JButton button = actionButtons.get(row); - button.setEnabled((Boolean) value); - result = button; - - default: - } - return result; - } - - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - - TransectTableModel.TransectColumn transectColumn = - TransectTableModel.TransectColumn.valueOf(column); - - Component result = null; - switch (transectColumn) { - - case DELETED: - result = delegateEditor.getTableCellEditorComponent( - table, value, isSelected, row, column); - break; - case ACTION: - JButton button = actionButtons.get(row); - button.setEnabled((Boolean) value); - result = button; - - default: - } - return result; - } - - @Override - public Object getCellEditorValue() { - return delegateEditor.getCellEditorValue(); - } -} Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectFlightModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectFlightModel.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectFlightModel.java 2012-08-20 16:13:19 UTC (rev 451) @@ -24,6 +24,7 @@ */ import com.google.common.base.Preconditions; +import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Iterables; import fr.ulr.sammoa.persistence.Flight; @@ -159,4 +160,22 @@ protected Flight getFlight() { return flightModel.getFlight(); } + + public static Predicate<TransectFlightModel> withTransectFlight(TransectFlight transectFlight) { + return new WithTransectFlightPredicate(transectFlight); + } + + protected static class WithTransectFlightPredicate implements Predicate<TransectFlightModel> { + + protected TransectFlight transectFlight; + + public WithTransectFlightPredicate(TransectFlight transectFlight) { + this.transectFlight = transectFlight; + } + + @Override + public boolean apply(TransectFlightModel input) { + return transectFlight.equals(input.getSource()); + } + } } Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTable.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTable.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTable.java 2012-08-20 16:13:19 UTC (rev 451) @@ -1,260 +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.flight; - -import com.google.common.collect.Lists; -import fr.ulr.sammoa.persistence.Flight; -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.persistence.TransectFlight; -import fr.ulr.sammoa.ui.swing.SammoaColors; -import fr.ulr.sammoa.ui.swing.SammoaUIContext; -import fr.ulr.sammoa.ui.swing.action.NextTransectAction; -import fr.ulr.sammoa.ui.swing.util.ColorTableCellRenderer; -import org.jdesktop.swingx.JXTable; - -import javax.swing.Action; -import javax.swing.ActionMap; -import javax.swing.JButton; -import javax.swing.event.TableModelEvent; -import javax.swing.table.TableCellRenderer; -import javax.swing.table.TableColumn; -import java.beans.IndexedPropertyChangeEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.List; - -/** - * Created: 15/06/12. - * <p/> - * TODO tchemit-2012-08-14 Remove this object, use a simple JTXTable and put - * TODO tchemit-2012-08-14 all the init code in handler. - * - * @author fdesbois <desbois@codelutin.com> - */ -public class TransectTable extends JXTable { - - private static final long serialVersionUID = 1L; - -// private static final Logger logger = LoggerFactory.getLogger(TransectTable.class); - -// protected static final ImageIcon ACTION_ICON = Resource.getIcon("/icons/action-next-transect.png"); - -// protected TransectTableHandler handler; - - protected List<JButton> actionButtons; - - public TransectTable(TransectTableModel model) { - super(model); - - for (int i = 0; i < getModel().getRowCount(); i++) { - addActionButton(i); - } - - PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { - @Override - public void propertyChange(PropertyChangeEvent evt) { - String propertyName = evt.getPropertyName(); - - if (FlightUIModel.PROPERTY_CURRENT_ROUTE.equals(propertyName)) { - - onCurrentRouteChanged( - (Route) evt.getOldValue(), (Route) evt.getNewValue()); - - } else if (FlightUIModel.PROPERTY_NEXT_TRANSECT.equals(propertyName)) { - - onNextTransectChanged( - (TransectFlight) evt.getOldValue(), (TransectFlight) evt.getNewValue()); - -// } else if (Flight.PROPERTY_TRANSECT_FLIGHT.equals(propertyName)) { -// -// IndexedPropertyChangeEvent indexedEvent = (IndexedPropertyChangeEvent) evt; -// onFlightTransectsAdded(indexedEvent); - - } else if (Flight.PROPERTY_OBSERVER.equals(propertyName)) { - - IndexedPropertyChangeEvent indexedEvent = (IndexedPropertyChangeEvent) evt; - onFlightObserversChanged(indexedEvent); - } - } - }; - - Flight flight = getModel().getFlight(); - if (flight != null) { - - flight.addPropertyChangeListener(Flight.PROPERTY_TRANSECT_FLIGHT, propertyChangeListener); - flight.addPropertyChangeListener(Flight.PROPERTY_OBSERVER, propertyChangeListener); - } - - SammoaUIContext.getUIContext().getFlightUIHandler().getModel().addPropertyChangeListener(propertyChangeListener); - } - -// public TransectTableHandler getHandler() { -// return handler; -// } - - public List<JButton> getActionButtons() { - if (actionButtons == null) { - actionButtons = Lists.newArrayList(); - } - return actionButtons; - } - - public int getLastSelectedRow() { - int result; - int nbRows = getSelectedRows().length; - if (nbRows > 0) { - result = getSelectedRows()[nbRows - 1]; - - } else { - // No line selected - result = -1; - } - return result; - } - - @Override - public TransectTableModel getModel() { - return (TransectTableModel) super.getModel(); - } - - @Override - public void tableChanged(TableModelEvent e) { - - // On fire INSERT, create buttons associated with each new rows - if (e.getType() == TableModelEvent.INSERT) { - - for (int i = e.getFirstRow(); i <= e.getLastRow(); i++) { - addActionButton(i); - } - } - - super.tableChanged(e); - } - - public JButton addActionButton(int index) { - - JButton result = new JButton(); - - // use client property used in Action to retrieve the correct transect - result.putClientProperty(NextTransectAction.CLIENT_PROPERTY_INDEX, index); - result.putClientProperty("actionName", "nextTransect"); - - ActionMap actionMap = getModel().getReference().getActionMap(); - if (actionMap != null) { - Action nextTransectAction = FlightUIHandler.getAction(actionMap, "nextTransect"); - result.setAction(nextTransectAction); - } - -// result.setIcon(ACTION_ICON); -// result.addActionListener(new ActionListener() { -// -// @Override -// public void actionPerformed(ActionEvent e) { -// JButton button = (JButton) e.getSource(); -// int index = (Integer) button.getClientProperty("index"); -// TransectFlight transectFlight = getModel().getRow(index); -// getHandler().setNextTransect(transectFlight); -// } -// }); - - getActionButtons().add(index, result); - - return result; - } - - /** - * Initialize column header colors for position left and position right - * - * @see ColorTableCellRenderer - */ - protected void initPositionColumnHeaderColors() { - - TableCellRenderer defaultRenderer = getDefaultRenderer(String.class); - - // Position left column - { - TableColumn column = getColumn(TransectTableModel.TransectColumn.POSITION_LEFT.ordinal()); - column.setHeaderRenderer(new ColorTableCellRenderer(defaultRenderer, SammoaColors.POSITION_LEFT_COLOR)); - } - - // Position right column - { - TableColumn column = getColumn(TransectTableModel.TransectColumn.POSITION_RIGHT.ordinal()); - column.setHeaderRenderer(new ColorTableCellRenderer(defaultRenderer, SammoaColors.POSITION_RIGHT_COLOR)); - } - } - - public void onCurrentRouteChanged(Route oldValue, Route newValue) { - - // Change of flight route status : begin, add, cb, end - - if (oldValue != null) { - TransectFlight transect = oldValue.getTransectFlight(); - if (transect != null) { - updateTransectRow(transect); - } - } - if (newValue != null) { - TransectFlight transect = newValue.getTransectFlight(); - if (transect != null) { - updateTransectRow(transect); - } - } - } - - public void onNextTransectChanged(TransectFlight oldValue, TransectFlight newValue) { - - if (oldValue != null) { - updateTransectRow(oldValue); - } - if (newValue != null) { - updateTransectRow(newValue); - } - } - -// public void onFlightTransectsAdded(IndexedPropertyChangeEvent evt) { -// -// int index = evt.getIndex(); -// -// getModel().fireTableRowsInserted(index, index); -// } - - public void onFlightObserversChanged(IndexedPropertyChangeEvent evt) { - - // Refresh all the table if some observer is removed (index = old collection size) - int oldSize = evt.getIndex(); - int currentSize = getModel().getFlight().sizeObserver(); - - if (currentSize < oldSize) { - getModel().fireTableDataChanged(); - } - } - - protected void updateTransectRow(TransectFlight transectFlight) { - - int index = getModel().getTransectFlightModel(transectFlight).getIndex(); - - getModel().fireTableRowsUpdated(index, index); - } -} Deleted: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableHandler.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableHandler.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableHandler.java 2012-08-20 16:13:19 UTC (rev 451) @@ -1,160 +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.flight; - -import fr.ulr.sammoa.persistence.Flight; -import fr.ulr.sammoa.persistence.Route; -import fr.ulr.sammoa.persistence.TransectFlight; -import fr.ulr.sammoa.ui.swing.SammoaUIContext; - -import java.beans.IndexedPropertyChangeEvent; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; - -/** - * Created: 18/06/12 - * FIXME tchemit-2012-08-14 Remove this handler - * - * @author fdesbois <desbois@codelutin.com> - * @deprecated I will remove this before release 0.6 - */ -@Deprecated -public class TransectTableHandler implements PropertyChangeListener { - - protected TransectTable view; - - public TransectTableHandler(TransectTable view) { - this.view = view; - } - - @Deprecated - public FlightUIModel getFlightUIModel() { - return SammoaUIContext.getUIContext().getFlightUIHandler().getModel(); - } - - public void init() { - getFlightUIModel().addPropertyChangeListener(this); - - Flight flight = getModel().getFlight(); - if (flight != null) { - flight.addPropertyChangeListener(Flight.PROPERTY_TRANSECT_FLIGHT, this); - flight.addPropertyChangeListener(Flight.PROPERTY_OBSERVER, this); - } - } - - public TransectTableModel getModel() { - return view.getModel(); - } - - @Override - public void propertyChange(PropertyChangeEvent evt) { - - String propertyName = evt.getPropertyName(); - - if (FlightUIModel.PROPERTY_CURRENT_ROUTE.equals(propertyName)) { - - onCurrentRouteChanged( - (Route) evt.getOldValue(), (Route) evt.getNewValue()); - - } else if (FlightUIModel.PROPERTY_NEXT_TRANSECT.equals(propertyName)) { - - onNextTransectChanged( - (TransectFlight) evt.getOldValue(), (TransectFlight) evt.getNewValue()); - -// } else if (Flight.PROPERTY_TRANSECT_FLIGHT.equals(propertyName)) { -// -// IndexedPropertyChangeEvent indexedEvent = (IndexedPropertyChangeEvent) evt; -// onFlightTransectsAdded(indexedEvent); - - } else if (Flight.PROPERTY_OBSERVER.equals(propertyName)) { - - IndexedPropertyChangeEvent indexedEvent = (IndexedPropertyChangeEvent) evt; - onFlightObserversChanged(indexedEvent); - } - } -// -// public void onFlightChanged(Flight oldValue, Flight newValue) { -// -// if (oldValue != null) { -// oldValue.removePropertyChangeListener(this); -// } -// newValue.addPropertyChangeListener(Flight.PROPERTY_TRANSECT_FLIGHT, this); -// newValue.addPropertyChangeListener(Flight.PROPERTY_OBSERVER, this); -// -// getModel().fireTableDataChanged(); -// } - - public void onCurrentRouteChanged(Route oldValue, Route newValue) { - - // Change of flight route status : begin, add, cb, end - - if (oldValue != null) { - TransectFlight transect = oldValue.getTransectFlight(); - if (transect != null) { - updateTransectRow(transect); - } - } - if (newValue != null) { - TransectFlight transect = newValue.getTransectFlight(); - if (transect != null) { - updateTransectRow(transect); - } - } - } - - public void onNextTransectChanged(TransectFlight oldValue, TransectFlight newValue) { - - if (oldValue != null) { - updateTransectRow(oldValue); - } - if (newValue != null) { - updateTransectRow(newValue); - } - } - -// public void onFlightTransectsAdded(IndexedPropertyChangeEvent evt) { -// -// int index = evt.getIndex(); -// -// getModel().fireTableRowsInserted(index, index); -// } - - public void onFlightObserversChanged(IndexedPropertyChangeEvent evt) { - - // Refresh all the table if some observer is removed (index = old collection size) - int oldSize = evt.getIndex(); - int currentSize = getModel().getFlight().sizeObserver(); - - if (currentSize < oldSize) { - getModel().fireTableDataChanged(); - } - } - - protected void updateTransectRow(TransectFlight transectFlight) { - - int index = getModel().getTransectFlightModel(transectFlight).getIndex(); - - getModel().fireTableRowsUpdated(index, index); - } -} Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableModel.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/TransectTableModel.java 2012-08-20 16:13:19 UTC (rev 451) @@ -24,7 +24,6 @@ */ package fr.ulr.sammoa.ui.swing.flight; -import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import fr.ulr.sammoa.persistence.Flight; @@ -36,7 +35,7 @@ import fr.ulr.sammoa.persistence.TransectFlight; import fr.ulr.sammoa.ui.swing.util.SammoaUtil; -import javax.swing.SwingUtilities; +import javax.swing.*; import javax.swing.table.AbstractTableModel; import java.util.List; @@ -70,18 +69,6 @@ return reference.getNextTransect(); } - @Deprecated - public TransectFlightModel getTransectFlightModel(final TransectFlight transectFlight) { - TransectFlightModel result = Iterables.find(getTransectFlights(), new Predicate<TransectFlightModel>() { - - @Override - public boolean apply(TransectFlightModel input) { - return input.getSource().equals(transectFlight); - } - }); - return result; - } - public List<TransectFlightModel> getTransectFlights() { return reference.getTransectFlights(); } @@ -90,10 +77,21 @@ return getTransectFlights().get(index); } -// public int getRowIndex(TransectFlightModel transectFlight) { -// return Iterables.indexOf(getTransectFlights(), Predicates.equalTo(transectFlight)); -// } + public int getRowIndex(final TransectFlight transectFlight) { + int result = Iterables.indexOf( + getTransectFlights(), + TransectFlightModel.withTransectFlight(transectFlight) + ); + return result; + } + public void fireTableRowUpdated(TransectFlight transectFlight) { + if (transectFlight != null) { + int index = getRowIndex(transectFlight); + fireTableRowsUpdated(index, index); + } + } + @Override public String getColumnName(int column) { List<String> labels = TransectColumn.getLabels(); @@ -281,12 +279,11 @@ ACTION( _("sammoa.flightPanel.table.column.action"), true, - Boolean.class + TransectFlightModel.class ) { @Override public Object getValue(int index, TransectFlightModel bean) { - // Action is available if bean is not deleted - return !bean.isDeleted(); + return bean; } @Override 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-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/observations/EffortPanelHandler.java 2012-08-20 16:13:19 UTC (rev 451) @@ -23,6 +23,7 @@ * #L% */ +import com.google.common.base.Supplier; import fr.ulr.sammoa.application.FlightService; import fr.ulr.sammoa.application.device.DeviceState; import fr.ulr.sammoa.application.device.DeviceStateEvent; @@ -47,6 +48,7 @@ import fr.ulr.sammoa.ui.swing.observations.action.MoveToPreviousEditableCellAction; import fr.ulr.sammoa.ui.swing.observations.action.MoveToPreviousRowEditableAction; import fr.ulr.sammoa.ui.swing.util.AbstractRowHighlightPredicate; +import fr.ulr.sammoa.ui.swing.util.ButtonActionTableCellEditorRenderer; import fr.ulr.sammoa.ui.swing.util.SammoaUtil; import fr.ulr.sammoa.ui.swing.util.TableDataChangeListener; import fr.ulr.sammoa.ui.swing.util.TextCellEditor; @@ -66,16 +68,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.swing.AbstractCellEditor; -import javax.swing.Action; -import javax.swing.DefaultCellEditor; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JTable; -import javax.swing.JTextField; -import javax.swing.ListCellRenderer; -import javax.swing.ListSelectionModel; -import javax.swing.SwingUtilities; +import javax.swing.*; import javax.swing.border.LineBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @@ -85,9 +78,7 @@ 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.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -441,7 +432,17 @@ // set renderer and editor as button for circle back action int circleBack = ObservationTableModel.ObservationColumn.CIRCLE_BACK.ordinal(); - CircleBackEditorRenderer editorRenderer = new CircleBackEditorRenderer(getFlightUIModel()); + ButtonActionTableCellEditorRenderer editorRenderer = new ButtonActionTableCellEditorRenderer( + CircleBackAction.CLIENT_PROPERTY_OBSERVATION, + new Supplier<Action>() { + + @Override + public Action get() { + return FlightUIHandler.getAction(getFlightUIModel().getActionMap(), "circleBack"); + } + }, + null + ); TableColumn column = dataTable.getColumnModel().getColumn(circleBack); column.setCellEditor(editorRenderer); @@ -751,50 +752,50 @@ return model.getBean(rowIndex); } } +// +// /** +// * Editeur/renderer des observations sous forme de bouton permettant +// * de rentrer en mode circle back sur l'observation choisie. +// */ +// public static class CircleBackEditorRenderer extends AbstractCellEditor +// implements TableCellRenderer, TableCellEditor { +// +// private static final long serialVersionUID = 1L; +// +// +// private final FlightUIModel fligthModel; +// +// public CircleBackEditorRenderer(FlightUIModel fligthModel) { +// this.fligthModel = fligthModel; +// } +// +// @Override +// public Object getCellEditorValue() { +// return null; +// } +// +// @Override +// public Component getTableCellEditorComponent(JTable table, +// Object value, boolean isSelected, int row, int column) { +// final Observation observation = (Observation) value; +// final JButton result = new JButton(); +// // FIXME-fdesbois-2012-07-08 : improve that, can't do any init... on new button, action is retrieve from actionMap +// if (fligthModel.getActionMap() != null) { +// Action circleBackAction = FlightUIHandler.getAction(fligthModel.getActionMap(), "circleBack"); +// result.setAction(circleBackAction); +// } +// result.putClientProperty(CircleBackAction.CLIENT_PROPERTY_OBSERVATION, observation); +// return result; +// } +// +// @Override +// public Component getTableCellRendererComponent(JTable table, +// Object value, boolean isSelected, boolean hasFocus, int row, +// int column) { +// return getTableCellEditorComponent(table, value, isSelected, row, column); +// } +// } - /** - * Editeur/renderer des observations sous forme de bouton permettant - * de rentrer en mode circle back sur l'observation choisie. - */ - public static class CircleBackEditorRenderer extends AbstractCellEditor - implements TableCellRenderer, TableCellEditor { - - private static final long serialVersionUID = 1L; - - - private final FlightUIModel fligthModel; - - public CircleBackEditorRenderer(FlightUIModel fligthModel) { - this.fligthModel = fligthModel; - } - - @Override - public Object getCellEditorValue() { - return null; - } - - @Override - public Component getTableCellEditorComponent(JTable table, - Object value, boolean isSelected, int row, int column) { - final Observation observation = (Observation) value; - final JButton result = new JButton(); - // FIXME-fdesbois-2012-07-08 : improve that, can't do any init... on new button, action is retrieve from actionMap - if (fligthModel.getActionMap() != null) { - Action circleBackAction = FlightUIHandler.getAction(fligthModel.getActionMap(), "circleBack"); - result.setAction(circleBackAction); - } - result.putClientProperty(CircleBackAction.CLIENT_PROPERTY_OBSERVATION, observation); - return result; - } - - @Override - public Component getTableCellRendererComponent(JTable table, - Object value, boolean isSelected, boolean hasFocus, int row, - int column) { - return getTableCellEditorComponent(table, value, isSelected, row, column); - } - } - public static class ObservationForSelectedRouteHighlightPredicate extends AbstractRowHighlightPredicate { protected FlightUIModel UIModel; Added: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/ButtonActionTableCellEditorRenderer.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/ButtonActionTableCellEditorRenderer.java (rev 0) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/ButtonActionTableCellEditorRenderer.java 2012-08-20 16:13:19 UTC (rev 451) @@ -0,0 +1,75 @@ +package fr.ulr.sammoa.ui.swing.util; + +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.collect.Maps; + +import javax.swing.*; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableCellRenderer; +import java.awt.*; +import java.util.Map; + +/** + * Created: 20/08/12 + * + * @author fdesbois <florian.desbois@codelutin.com> + */ +public class ButtonActionTableCellEditorRenderer extends AbstractCellEditor + implements TableCellRenderer, TableCellEditor { + + protected String actionPropertyName; + + protected Supplier<Action> actionSupplier; + + protected Predicate<Object> enabledPredicate; + + protected Map<Object, JButton> cache; + + public ButtonActionTableCellEditorRenderer(String actionPropertyName, + Supplier<Action> actionSupplier, + Predicate<Object> enabledPredicate) { + + this.actionPropertyName = actionPropertyName; + this.actionSupplier = actionSupplier; + this.enabledPredicate = enabledPredicate; + this.cache = Maps.newHashMap(); + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + Component result = getButton(value); + if (enabledPredicate != null) { + result.setEnabled(enabledPredicate.apply(value)); + } + return result; + } + + @Override + public Object getCellEditorValue() { + return null; + } + + @Override + public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { + Component result = getButton(value); + if (enabledPredicate != null) { + result.setEnabled(enabledPredicate.apply(value)); + } + return result; + } + + protected JButton getButton(Object bean) { + JButton result = cache.get(bean); + if (result == null) { + + result = new JButton(); + + result.putClientProperty(actionPropertyName, bean); + result.setAction(actionSupplier.get()); + + cache.put(bean, result); + } + return result; + } +} Property changes on: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/ButtonActionTableCellEditorRenderer.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/SammoaUtil.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/SammoaUtil.java 2012-08-20 15:11:50 UTC (rev 450) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/util/SammoaUtil.java 2012-08-20 16:13:19 UTC (rev 451) @@ -36,18 +36,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.swing.Action; -import javax.swing.JComponent; -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JOptionPane; -import javax.swing.JRootPane; -import javax.swing.KeyStroke; -import javax.swing.UIManager; -import java.awt.Color; -import java.awt.Component; -import java.awt.Cursor; -import java.awt.Dimension; +import javax.swing.*; +import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; @@ -261,4 +251,17 @@ JOptionPane.YES_NO_OPTION); return i == JOptionPane.YES_OPTION; } + + public static int getLastSelectedRow(JTable table) { + int result; + int nbRows = table.getSelectedRows().length; + if (nbRows > 0) { + result = table.getSelectedRows()[nbRows - 1]; + + } else { + // No line selected + result = -1; + } + return result; + } }