Author: fdesbois Date: 2012-09-11 12:08:46 +0200 (Tue, 11 Sep 2012) New Revision: 569 Url: http://forge.codelutin.com/repositories/revision/sammoa/569 Log: fixes #1469 : allow LEG transect edition Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/EffortPanelHandler.java trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/RouteTableModel.java trunk/sammoa-ui-swing/src/main/resources/i18n/sammoa-ui-swing_en_GB.properties Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/EffortPanelHandler.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/EffortPanelHandler.java 2012-09-10 15:55:43 UTC (rev 568) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/EffortPanelHandler.java 2012-09-11 10:08:46 UTC (rev 569) @@ -24,10 +24,10 @@ */ import com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport; +import com.google.common.base.Objects; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; -import fr.ulr.sammoa.application.DecoratorService; import fr.ulr.sammoa.application.FlightService; import fr.ulr.sammoa.application.device.DeviceState; import fr.ulr.sammoa.application.device.DeviceStateEvent; @@ -415,14 +415,22 @@ UIDecoratorService decoratorService = context.getService(UIDecoratorService.class); - TableCellRenderer stringRenderer = table.getDefaultRenderer(String.class); - // TransectFlight { + TableCellRenderer tableRenderer = + decoratorService.newTableCellRender(TransectFlightModel.class); TableCellRenderer renderer = new TransectFlightCellRenderer( - stringRenderer, decoratorService, getModel()); + tableRenderer, getModel()); + JComboBox comboBox = new JComboBox(); + ListCellRenderer listRenderer = + decoratorService.newListCellRender(TransectFlightModel.class); + comboBox.setRenderer(listRenderer); + TableCellEditor editor = new TransectFlightCellEditor( + comboBox, getModel()); + table.setDefaultRenderer(TransectFlight.class, renderer); + table.setDefaultEditor(TransectFlight.class, editor); } table.addHighlighter(SammoaUtil.newBackgroundColorHighlighter( @@ -878,19 +886,92 @@ } }; + /** + * Editor for type {@link TransectFlight}. This will use the + * {@link DefaultCellEditor} with {@link JComboBox} as editor component. + * <p/> + * The model used is the list of {@link TransectFlightModel} from + * {@link FlightUIModel}. So we need to convert {@link TransectFlightModel} + * to {@link TransectFlight} and listen the model change on property + * {@link FlightUIModel#PROPERTY_TRANSECT_FLIGHTS}. + * <p/> + * We also want to ensure the change by asking a user question to confirm it. + */ + private static class TransectFlightCellEditor extends DefaultCellEditor + implements PropertyChangeListener { + + protected FlightUIModel model; + + protected TransectFlight initValue; + + public TransectFlightCellEditor(JComboBox comboBox, + FlightUIModel model) { + super(comboBox); + this.model = model; + SwingUtil.fillComboBox(comboBox, model.getTransectFlights(), null); + model.addPropertyChangeListener(FlightUIModel.PROPERTY_TRANSECT_FLIGHTS, this); + } + + @Override + public Component getTableCellEditorComponent(JTable table, + Object value, + boolean isSelected, + int row, + int column) { + + initValue = (TransectFlight) value; + TransectFlightModel modelValue = TransectFlightModel.findTransectFlightModel( + model.getTransectFlights(), initValue); + + return super.getTableCellEditorComponent(table, modelValue, isSelected, row, column); + } + + @Override + public TransectFlight getCellEditorValue() { + TransectFlightModel modelValue = (TransectFlightModel) super.getCellEditorValue(); + return modelValue == null ? null : modelValue.getSource(); + } + + @Override + public boolean stopCellEditing() { + boolean result; + + TransectFlight value = getCellEditorValue(); + if (Objects.equal(value, initValue) + || SammoaUtil.askQuestion(editorComponent, _( + "sammoa.confirmDialog.changeRouteTransect.message", + value.getTransect().getName()))) { + + result = super.stopCellEditing(); + + } else { + cancelCellEditing(); + result = true; + } + return result; + } + + @Override + public void propertyChange(PropertyChangeEvent evt) { + + List<TransectFlightModel> newValue = + (List<TransectFlightModel>) evt.getNewValue(); + + if (newValue != null) { + SwingUtil.fillComboBox((JComboBox) editorComponent, newValue, null); + } + } + } + private static class TransectFlightCellRenderer implements TableCellRenderer { protected TableCellRenderer delegate; - protected Decorator<TransectFlightModel> decorator; - protected FlightUIModel model; public TransectFlightCellRenderer(TableCellRenderer delegate, - DecoratorService decoratorService, FlightUIModel model) { this.delegate = delegate; - this.decorator = decoratorService.getDecoratorByType(TransectFlightModel.class); this.model = model; } @@ -907,14 +988,11 @@ if (value != null) { TransectFlight transectFlight = (TransectFlight) value; - // Retrieve the transect params from matching TransectFlightModel - TransectFlightModel transectFlightModel = - TransectFlightModel.findTransectFlightModel(model.getTransectFlights(), - transectFlight); - - newValue = decorator.toString(transectFlightModel); + newValue = TransectFlightModel.findTransectFlightModel( + model.getTransectFlights(), transectFlight); } - return delegate.getTableCellRendererComponent(table, newValue, isSelected, hasFocus, row, column); + return delegate.getTableCellRendererComponent( + table, newValue, isSelected, hasFocus, row, column); } } Modified: trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/RouteTableModel.java =================================================================== --- trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/RouteTableModel.java 2012-09-10 15:55:43 UTC (rev 568) +++ trunk/sammoa-ui-swing/src/main/java/fr/ulr/sammoa/ui/swing/flight/effort/RouteTableModel.java 2012-09-11 10:08:46 UTC (rev 569) @@ -136,7 +136,15 @@ } }, ROUTE_TYPE(false, RouteType.class, Route.PROPERTY_ROUTE_TYPE), - TRANSECT(false, TransectFlight.class, Route.PROPERTY_TRANSECT_FLIGHT), + TRANSECT(true, TransectFlight.class, Route.PROPERTY_TRANSECT_FLIGHT) { + + @Override + public boolean isEditable(Route bean, boolean validationMode) { + return super.isEditable(bean, validationMode) + && bean.getRouteType() == RouteType.LEG + && validationMode; + } + }, SEA_STATE(true, int.class, Route.PROPERTY_SEA_STATE), SWELL(true, int.class, Route.PROPERTY_SWELL), TURBIDITY(true, int.class, Route.PROPERTY_TURBIDITY), Modified: trunk/sammoa-ui-swing/src/main/resources/i18n/sammoa-ui-swing_en_GB.properties =================================================================== --- trunk/sammoa-ui-swing/src/main/resources/i18n/sammoa-ui-swing_en_GB.properties 2012-09-10 15:55:43 UTC (rev 568) +++ trunk/sammoa-ui-swing/src/main/resources/i18n/sammoa-ui-swing_en_GB.properties 2012-09-11 10:08:46 UTC (rev 569) @@ -72,6 +72,7 @@ sammoa.config.category.other.description=Other sammoa.config.category.shortcuts=Shortcuts sammoa.config.category.shortcuts.description=List of all the shortcuts +sammoa.confirmDialog.changeRouteTransect.message=Are you sure you want to change the LEG transect by %s ? sammoa.confirmDialog.deleteTransect.message=The flight is not started, do you want to definitely delete this flight's transect ? sammoa.confirmDialog.flightInProgress.message.exit=A flight is in progress, are you sure you want to quit ? sammoa.confirmDialog.flightInProgress.message.showHome=A flight is in progress, are you sure you want to go back to the home screen ?