View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.cruise;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  import com.google.common.base.Preconditions;
26  import com.google.common.collect.Lists;
27  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
28  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
29  import fr.ifremer.tutti.persistence.entities.referential.Gear;
30  import fr.ifremer.tutti.service.DecoratorService;
31  import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor;
32  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicValueEditor;
33  import fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicValueRenderer;
34  import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIHandler;
35  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
36  import jaxx.runtime.validator.swing.SwingValidator;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  import org.jdesktop.swingx.JXTable;
40  import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
41  
42  import javax.swing.JComponent;
43  import java.util.List;
44  import java.util.stream.Collectors;
45  
46  /**
47   * @author Kevin Morin - kmorin@codelutin.com
48   * @since 2.1
49   */
50  public class GearCaracteristicsEditorUIHandler
51          extends AbstractTuttiTableUIHandler<GearCaracteristicsEditorRowModel, GearCaracteristicsEditorUIModel, GearCaracteristicsEditorUI> {
52  
53      private final static Log log =
54              LogFactory.getLog(GearCaracteristicsEditorUIHandler.class);
55  
56      public GearCaracteristicsEditorUIHandler() {
57          super(GearCaracteristicsEditorRowModel.PROPERTY_VALUE);
58      }
59  
60      @Override
61      public GearCaracteristicsEditorTableModel getTableModel() {
62          return (GearCaracteristicsEditorTableModel) getTable().getModel();
63      }
64  
65      @Override
66      public JXTable getTable() {
67          return ui.getGearCaracteristicsEditorTable();
68      }
69  
70      @Override
71      protected boolean isRowValid(GearCaracteristicsEditorRowModel row) {
72          return row.getKey() != null && row.getValue() != null;
73      }
74  
75      @Override
76      protected void onRowModified(int rowIndex, GearCaracteristicsEditorRowModel row,
77                                   String propertyName, Object oldValue, Object newValue) {
78          recomputeRowValidState(row);
79          super.onRowModified(rowIndex, row, propertyName, oldValue, newValue);
80          saveSelectedRowIfNeeded();
81      }
82  
83      @Override
84      protected void onAfterSelectedRowChanged(int oldRowIndex, GearCaracteristicsEditorRowModel oldRow, int newRowIndex, GearCaracteristicsEditorRowModel newRow) {
85          super.onAfterSelectedRowChanged(oldRowIndex, oldRow, newRowIndex, newRow);
86          if (newRow != null) {
87              recomputeRowValidState(newRow);
88          }
89  //        getModel().setRemoveCaracteristicEnabled(newRowIndex >= 0);
90      }
91  
92      @Override
93      protected void beforeOpenPopup(int rowIndex, int columnIndex) {
94          super.beforeOpenPopup(rowIndex, columnIndex);
95  
96          getModel().setRemoveCaracteristicEnabled(rowIndex != -1);
97      }
98  
99      @Override
100     protected void saveSelectedRowIfRequired(TuttiBeanMonitor<GearCaracteristicsEditorRowModel> rowMonitor,
101                                              GearCaracteristicsEditorRowModel row) {
102         if (row.isValid()) {
103             // there is a valid bean attached to the monitor
104 
105             if (rowMonitor.wasModified()) {
106 
107                 // monitored bean was modified, save it
108                 if (log.isDebugEnabled()) {
109                     log.debug("Row " + row + " was modified, will save it");
110                 }
111 
112                 saveRow(row);
113 
114             }
115         }
116     }
117 
118     @Override
119     public void beforeInit(GearCaracteristicsEditorUI ui) {
120         super.beforeInit(ui);
121         GearCaracteristicsEditorUIModel model =
122                 new GearCaracteristicsEditorUIModel();
123         ui.setContextValue(model);
124 
125         model.addPropertyChangeListener(GearCaracteristicsEditorUIModel.PROPERTY_GEAR, evt -> editGear((Gear) evt.getNewValue()));
126 
127     }
128 
129     @Override
130     public void afterInit(GearCaracteristicsEditorUI ui) {
131         initUI(ui);
132 
133         initBeanFilterableComboBox(getKeyCombo(), Lists.<Caracteristic>newArrayList(), null);
134         getModel().setAvailableCaracteristics(getDataContext().getCaracteristics());
135 
136         JXTable table = getTable();
137 
138         // create table column model
139         DefaultTableColumnModelExt columnModel =
140                 new DefaultTableColumnModelExt();
141 
142         {
143 
144             addColumnToModel(columnModel,
145                              null,
146                              newTableCellRender(Caracteristic.class, DecoratorService.CARACTERISTIC_WITH_UNIT),
147                              GearCaracteristicsEditorTableModel.KEY);
148         }
149 
150         {
151 
152             addColumnToModel(columnModel,
153                              new CaracteristicValueEditor(getContext()),
154                              new CaracteristicValueRenderer(getContext()),
155                              GearCaracteristicsEditorTableModel.VALUE);
156         }
157 
158         // create table model
159         GearCaracteristicsEditorTableModel tableModel =
160                 new GearCaracteristicsEditorTableModel(columnModel);
161 
162         table.setModel(tableModel);
163         table.setColumnModel(columnModel);
164 
165         initTable(table);
166     }
167 
168     @Override
169     protected JComponent getComponentToFocus() {
170         return getUI().getNewRowKey();
171     }
172 
173     @Override
174     public void onCloseUI() {
175         if (log.isDebugEnabled()) {
176             log.debug("closing: " + ui);
177         }
178 
179         EditCruiseUI ui = getUI().getParentContainer(EditCruiseUI.class);
180         ui.getMainPanelLayout().setSelected(EditCruiseUIHandler.CRUISE_CARD);
181 
182         // when canceling always invalid model (in that way)
183         getModel().setValid(false);
184         getModel().setGear(null);
185     }
186 
187     @Override
188     public SwingValidator<GearCaracteristicsEditorUIModel> getValidator() {
189         return null;
190     }
191 
192 //    @Override
193 //    public void cancel() {
194 //
195 //        if (log.isDebugEnabled()) {
196 //            log.debug("Cancel UI " + ui);
197 //        }
198 //
199 //        // close dialog
200 //        closeUI(ui);
201 //    }
202 
203     protected BeanFilterableComboBox<Caracteristic> getKeyCombo() {
204         return ui.getNewRowKey();
205     }
206 
207     protected void saveRow(GearCaracteristicsEditorRowModel row) {
208 
209         if (row.isValid()) {
210             CaracteristicMap caracteristics = getModel().getCaracteristicMap();
211             Preconditions.checkNotNull(caracteristics);
212 
213             caracteristics.put(row.getKey(), row.getValue());
214         }
215     }
216 
217     //------------------------------------------------------------------------//
218     //-- Public methods                                                     --//
219     //------------------------------------------------------------------------//
220 
221     /** Adds a row with the parameter selected in the combo box */
222 //    public void addRow() {
223 //        BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
224 //        Caracteristic selectedItem = (Caracteristic) keyCombo.getSelectedItem();
225 //        GearCaracteristicsEditorTableModel tableModel = getTableModel();
226 //
227 //        GearCaracteristicsEditorRowModel row = tableModel.createNewRow();
228 //        row.setKey(selectedItem);
229 ////        tableModel.addNewRow(row);
230 //        getModel().getRows().add(row);
231 //
232 //        int rowIndex = tableModel.getRowCount() - 1;
233 //        tableModel.fireTableRowsInserted(rowIndex, rowIndex);
234 //
235 //        keyCombo.getHandler().removeItem(selectedItem);
236 //
237 //        GearCaracteristicsEditorUIModel model = getModel();
238 //        model.setModify(true);
239 //        recomputeRowValidState(row);
240 //    }
241 
242 //    public void removeCaracteristic() {
243 //        int rowIndex = getTable().getSelectedRow();
244 //
245 //        Preconditions.checkState(
246 //                rowIndex != -1,
247 //                "Cant remove caracteristic if no caracteristic selected");
248 //
249 //        GearCaracteristicsEditorRowModel row = getTableModel().getEntry(rowIndex);
250 //
251 //        CaracteristicMap caracteristicMap = getModel().getCaracteristicMap();
252 //        if (caracteristicMap != null) {
253 //            caracteristicMap.remove(row.getKey());
254 //        }
255 //
256 //        //add the row in the combo
257 //        BeanFilterableComboBox keyCombo = getKeyCombo();
258 //        keyCombo.addItem(row.getKey());
259 ////        selectFirstInCombo(keyCombo);
260 //        keyCombo.getHandler().reset();
261 //
262 //        // remove the row from the model
263 //        getModel().getRows().remove(rowIndex);
264 //
265 //        // refresh all the table
266 //        getTableModel().fireTableRowsDeleted(rowIndex, rowIndex);
267 //
268 //        getModel().removeRowInError(row);
269 //    }
270 
271 //    public void save() {
272 //
273 //        if (log.isDebugEnabled()) {
274 //            log.debug("Save UI " + ui);
275 //        }
276 //
277 //        Gear gear = getModel().getGear();
278 //        gear.setCaracteristics((CaracteristicMap) getModel().getCaracteristicMap().clone());
279 //        getPersistenceService().saveGearCaracteristics(gear, getDataContext().getCruise());
280 //
281 //        closeUI(ui);
282 //    }
283     protected void editGear(Gear gear) {
284 
285         if (gear != null) {
286             GearCaracteristicsEditorTableModel tableModel = getTableModel();
287             GearCaracteristicsEditorUIModel model = getModel();
288 
289             CaracteristicMap caracteristicMap = gear.getCaracteristics();
290             if (caracteristicMap == null) {
291                 caracteristicMap = new CaracteristicMap();
292             }
293             // always clear caracteristic map before all
294             model.getCaracteristicMap().clear();
295             model.getCaracteristicMap().putAll(caracteristicMap);
296 
297             List<GearCaracteristicsEditorRowModel> rows = Lists.newArrayList();
298             List<Caracteristic> caracteristics = Lists.newArrayList(caracteristicMap.keySet());
299 
300             List<Caracteristic> availableCaracteristics = model.getAvailableCaracteristics();
301 
302             for (Caracteristic key : caracteristics) {
303                 GearCaracteristicsEditorRowModel newRow = tableModel.createNewRow();
304                 newRow.setKey(key);
305                 newRow.setValue(caracteristicMap.get(key));
306                 rows.add(newRow);
307             }
308 
309             model.setRows(rows);
310 
311             List<Caracteristic> caracteristicList = Lists.newArrayList();
312 
313             caracteristicList.addAll(availableCaracteristics
314                                              .stream()
315                                              .filter(caracteristic -> !caracteristics.contains(caracteristic))
316                                              .collect(Collectors.toList()));
317 
318             BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
319             keyCombo.setData(caracteristicList);
320             //        selectFirstInCombo(keyCombo);
321             keyCombo.getHandler().reset();
322             model.setModify(false);
323 
324         } else {
325             cleanrRowMonitor();
326             getModel().setRows(Lists.<GearCaracteristicsEditorRowModel>newArrayList());
327             getTable().clearSelection();
328         }
329     }
330 }