View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.caracteristics;
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.Functions;
26  import com.google.common.base.Preconditions;
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Maps;
29  import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
30  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31  import fr.ifremer.tutti.service.DecoratorService;
32  import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor;
33  import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIHandler;
34  import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
35  import jaxx.runtime.validator.swing.SwingValidator;
36  import org.apache.commons.collections4.MapUtils;
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.io.Serializable;
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Set;
47  
48  import static fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapCellComponent.CaracteristicMapCellEditor;
49  
50  /**
51   * @author Kevin Morin - kmorin@codelutin.com
52   * @since 1.4
53   */
54  public class CaracteristicMapEditorUIHandler
55          extends AbstractTuttiTableUIHandler<CaracteristicMapEditorRowModel, CaracteristicMapEditorUIModel, CaracteristicMapEditorUI> {
56  
57      private final static Log log =
58              LogFactory.getLog(CaracteristicMapEditorUIHandler.class);
59  
60      protected CaracteristicMapCellEditor caracteristicMapCellEditor;
61  
62      public CaracteristicMapEditorUIHandler() {
63          super(CaracteristicMapEditorRowModel.PROPERTY_VALUE);
64      }
65  
66      //------------------------------------------------------------------------//
67      //-- AbstractTuttiTableUIHandler methods                                --//
68      //------------------------------------------------------------------------//
69  
70      @Override
71      public CaracteristicMapEditorTableModel getTableModel() {
72          return (CaracteristicMapEditorTableModel) getTable().getModel();
73      }
74  
75      @Override
76      public JXTable getTable() {
77          return ui.getCaracteristicMapEditorTable();
78      }
79  
80      @Override
81      protected boolean isRowValid(CaracteristicMapEditorRowModel row) {
82          return row.getKey() != null && row.getValue() != null;
83      }
84  
85      @Override
86      protected void onRowModified(int rowIndex, CaracteristicMapEditorRowModel row,
87                                   String propertyName, Object oldValue, Object newValue) {
88          recomputeRowValidState(row);
89          super.onRowModified(rowIndex, row, propertyName, oldValue, newValue);
90          saveSelectedRowIfNeeded();
91      }
92  
93      @Override
94      protected void saveSelectedRowIfRequired(TuttiBeanMonitor<CaracteristicMapEditorRowModel> rowMonitor,
95                                               CaracteristicMapEditorRowModel row) {
96          if (row.isValid()) {
97              // there is a valid bean attached to the monitor
98  
99              if (rowMonitor.wasModified()) {
100 
101                 // monitored bean was modified, save it
102                 if (log.isInfoEnabled()) {
103                     log.info("Row " + row + " was modified, will save it");
104                 }
105 
106                 saveRow(row);
107 
108             }
109         }
110     }
111 
112     @Override
113     protected void beforeOpenPopup(int rowIndex, int columnIndex) {
114         super.beforeOpenPopup(rowIndex, columnIndex);
115 
116         getModel().setRemoveCaracteristicEnabled(rowIndex >= 0);
117     }
118 
119     //------------------------------------------------------------------------//
120     //-- AbstractTuttiUIHandler methods                                     --//
121     //------------------------------------------------------------------------//
122 
123     @Override
124     public void beforeInit(CaracteristicMapEditorUI ui) {
125         super.beforeInit(ui);
126         CaracteristicMapEditorUIModel model = new CaracteristicMapEditorUIModel();
127         ui.setContextValue(model);
128     }
129 
130     @Override
131     public void afterInit(CaracteristicMapEditorUI ui) {
132         initUI(ui);
133 
134         initBeanFilterableComboBox(getKeyCombo(), new ArrayList<>(), null);
135         getModel().setAllAvailableCaracteristics(new ArrayList<>(getDataContext().getCaracteristicWithProtected()));
136 
137         JXTable table = getTable();
138 
139         // create table column model
140         DefaultTableColumnModelExt columnModel =
141                 new DefaultTableColumnModelExt();
142 
143         {
144 
145             addColumnToModel(columnModel,
146                              null,
147                              newTableCellRender(Caracteristic.class, DecoratorService.CARACTERISTIC_WITH_UNIT),
148                              CaracteristicMapEditorTableModel.KEY);
149         }
150 
151         {
152 
153             addColumnToModel(columnModel,
154                              new CaracteristicValueEditor(getContext()),
155                              new CaracteristicValueRenderer(getContext()),
156                              CaracteristicMapEditorTableModel.VALUE);
157         }
158 
159         // create table model
160         CaracteristicMapEditorTableModel tableModel =
161                 new CaracteristicMapEditorTableModel(columnModel);
162 
163         table.setModel(tableModel);
164         table.setColumnModel(columnModel);
165 
166         initTable(table);
167     }
168 
169     @Override
170     protected JComponent getComponentToFocus() {
171         return getUI().getNewRowKey();
172     }
173 
174     @Override
175     public void onCloseUI() {
176         if (log.isDebugEnabled()) {
177             log.debug("closing: " + ui);
178         }
179 
180         caracteristicMapCellEditor.closeEditor();
181     }
182 
183     @Override
184     public SwingValidator<CaracteristicMapEditorUIModel> getValidator() {
185         return null;
186     }
187 
188     public CaracteristicMapCellEditor getCaracteristicMapCellEditor() {
189         return caracteristicMapCellEditor;
190     }
191 
192     //------------------------------------------------------------------------//
193     //-- Cancelable methods                                                 --//
194     //------------------------------------------------------------------------//
195 
196 //    @Override
197 //    public void cancel() {
198 //
199 //        if (log.isDebugEnabled()) {
200 //            log.debug("Cancel UI " + ui);
201 //        }
202 //
203 //        // close dialog
204 //        closeUI(ui);
205 //    }
206 
207     //------------------------------------------------------------------------//
208     //-- Public methods                                                     --//
209     //------------------------------------------------------------------------//
210 
211 //    /** Adds a row with the parameter selected in the combo box */
212 //    public void addRow() {
213 //        BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
214 //        Caracteristic selectedItem = (Caracteristic) keyCombo.getSelectedItem();
215 //        CaracteristicMapEditorTableModel tableModel = getTableModel();
216 //
217 //        CaracteristicMapEditorRowModel row = tableModel.createNewRow();
218 //        row.setKey(selectedItem);
219 //        getModel().getRows().add(row);
220 //
221 //        int rowIndex = tableModel.getRowCount() - 1;
222 //        tableModel.fireTableRowsInserted(rowIndex, rowIndex);
223 //
224 //        keyCombo.getHandler().removeItem(selectedItem);
225 //
226 //        CaracteristicMapEditorUIModel model = getModel();
227 //        model.setModify(true);
228 //        recomputeRowValidState(row);
229 //    }
230 
231     /**
232      * Edit the batch caracteristics
233      *
234      * @param caracteristicMapColumnRowModel the row to edit
235      * @param caracteristicMapCellEditor     the editor
236      * @param caracteristicsUsed             the set of the caracteristics used in the other rows.
237      *                                       If no caracteristic is set on this row, add automatically these caracteristics with a null value
238      */
239     public void editBatch(CaracteristicMapColumnRowModel caracteristicMapColumnRowModel,
240                           CaracteristicMapCellEditor caracteristicMapCellEditor,
241                           Set<Caracteristic> caracteristicsUsed) {
242 
243         this.caracteristicMapCellEditor = caracteristicMapCellEditor;
244 
245         CaracteristicMapEditorTableModel tableModel = getTableModel();
246         CaracteristicMapEditorUIModel model = getModel();
247 
248         CaracteristicMap caracteristicMap = caracteristicMapColumnRowModel.getCaracteristics();
249         if (MapUtils.isEmpty(caracteristicMap)) {
250             caracteristicMap = new CaracteristicMap();
251             caracteristicMap.putAll(Maps.asMap(caracteristicsUsed, Functions.constant((Serializable) null)));
252         }
253         model.setCaracteristicMap(caracteristicMap);
254 
255         List<CaracteristicMapEditorRowModel> rows = Lists.newArrayList();
256         List<Caracteristic> caracteristics = Lists.newArrayList(caracteristicMap.keySet());
257 
258         List<Caracteristic> availableCaracteristics = model.getAvailableCaracteristics();
259 
260         for (Caracteristic key : caracteristics) {
261             CaracteristicMapEditorRowModel newRow = tableModel.createNewRow();
262             newRow.setKey(key);
263             newRow.setValue(caracteristicMap.get(key));
264             rows.add(newRow);
265         }
266 
267         model.setRows(rows);
268 
269         // add the lengthstep caracteristic in the caracteristics which cannot be added
270         caracteristics.add(caracteristicMapColumnRowModel.getLengthStepCaracteristic());
271 
272         List<Caracteristic> caracteristicList = Lists.newArrayList();
273 
274         for (Caracteristic caracteristic : availableCaracteristics) {
275             if (!caracteristics.contains(caracteristic)) {
276                 caracteristicList.add(caracteristic);
277             }
278         }
279 
280         BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
281         keyCombo.setData(caracteristicList);
282         keyCombo.getHandler().reset();
283         model.setModify(false);
284     }
285 
286 //    public void removeCaracteristic() {
287 //        int rowIndex = getTable().getSelectedRow();
288 //
289 //        Preconditions.checkState(
290 //                rowIndex != -1,
291 //                "Cant remove caracteristic if no caracteristic selected");
292 //
293 //        CaracteristicMapEditorRowModel row = getTableModel().getEntry(rowIndex);
294 //
295 //        CaracteristicMap caracteristicMap = getModel().getCaracteristicMap();
296 //        if (caracteristicMap != null) {
297 //            caracteristicMap.remove(row.getKey());
298 //        }
299 //
300 //        //add the row in the combo
301 //        BeanFilterableComboBox keyCombo = getKeyCombo();
302 //        keyCombo.addItem(row.getKey());
303 //        keyCombo.getHandler().reset();
304 //
305 //        // remove the row from the model
306 //        getModel().getRows().remove(rowIndex);
307 //
308 //        // refresh all the table
309 //        getTableModel().fireTableRowsDeleted(rowIndex, rowIndex);
310 //
311 //        getModel().removeRowInError(row);
312 //    }
313 
314 //    public void save() {
315 //
316 //        if (log.isDebugEnabled()) {
317 //            log.debug("Save UI " + ui);
318 //        }
319 //
320 //        caracteristicMapCellEditor.validateEdition(getModel());
321 //
322 //        closeUI(ui);
323 //    }
324 
325     //------------------------------------------------------------------------//
326     //-- Internal methods                                                   --//
327     //------------------------------------------------------------------------//
328 
329     protected BeanFilterableComboBox<Caracteristic> getKeyCombo() {
330         return ui.getNewRowKey();
331     }
332 
333     protected void saveRow(CaracteristicMapEditorRowModel row) {
334 
335         if (row.isValid()) {
336             CaracteristicMap caracteristics = getModel().getCaracteristicMap();
337             Preconditions.checkNotNull(caracteristics);
338 
339             caracteristics.put(row.getKey(), row.getValue());
340         }
341     }
342 }