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.collect.Lists;
26  import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
27  import fr.ifremer.tutti.persistence.entities.referential.CaracteristicQualitativeValue;
28  import fr.ifremer.tutti.service.DecoratorService;
29  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
30  import jaxx.runtime.SwingUtil;
31  import jaxx.runtime.swing.JAXXWidgetUtil;
32  import jaxx.runtime.swing.editor.bean.BeanUIUtil;
33  import jaxx.runtime.swing.editor.cell.NumberCellEditor;
34  import jaxx.runtime.swing.renderer.DecoratorListCellRenderer;
35  import org.jdesktop.swingx.autocomplete.ComboBoxCellEditor;
36  import org.jdesktop.swingx.autocomplete.ObjectToStringConverter;
37  import org.nuiton.decorator.Decorator;
38  
39  import javax.swing.AbstractCellEditor;
40  import javax.swing.JComboBox;
41  import javax.swing.JTable;
42  import javax.swing.border.LineBorder;
43  import javax.swing.table.TableCellEditor;
44  import java.awt.Color;
45  import java.awt.Component;
46  import java.util.List;
47  
48  /**
49   * Editor for the values of the caracteristics of the fishing operations.
50   * The editor depends on the caracteristic value type.
51   *
52   * @author Kevin Morin - kmorin@codelutin.com
53   * @since 0.3
54   */
55  public class CaracteristicValueEditor extends AbstractCellEditor implements TableCellEditor {
56  
57      private static final long serialVersionUID = 1L;
58  
59      protected int caracteristicColumn;
60  
61      protected TableCellEditor editor;
62  
63      protected Decorator<CaracteristicQualitativeValue> decorator;
64  
65      public CaracteristicValueEditor(TuttiUIContext context) {
66          this(0, context);
67      }
68  
69      public CaracteristicValueEditor(int caracteristicColumn, TuttiUIContext context) {
70          super();
71          this.caracteristicColumn = caracteristicColumn;
72          DecoratorService decoratorService = context.getDecoratorService();
73          decorator = decoratorService.getDecoratorByType(CaracteristicQualitativeValue.class);
74      }
75  
76      @Override
77      public Object getCellEditorValue() {
78          return editor.getCellEditorValue();
79      }
80  
81      @Override
82      public Component getTableCellEditorComponent(JTable table,
83                                                   Object value,
84                                                   boolean isSelected,
85                                                   int row,
86                                                   int column) {
87  
88          Caracteristic caracteristic = (Caracteristic)
89                  table.getModel().getValueAt(row, caracteristicColumn);
90          if (caracteristic == null) {
91  
92              // can't edit a null value ?
93  
94          } else {
95              switch (caracteristic.getCaracteristicType()) {
96  
97                  case NUMBER:
98                      // by default this is a number
99                      NumberCellEditor<Float> editor =
100                             JAXXWidgetUtil.newNumberTableCellEditor(Float.class, false);
101                     editor.getNumberEditor().setSelectAllTextOnError(true);
102                     editor.getNumberEditor().getTextField().setBorder(new LineBorder(Color.GRAY, 2));
103                     this.editor = editor;
104                     break;
105                 case QUALITATIVE:
106                     JComboBox comboBox = new JComboBox();
107                     comboBox.setRenderer(new DecoratorListCellRenderer(decorator));
108 
109                     // always use a copy of data
110                     List<CaracteristicQualitativeValue> data =
111                             Lists.newArrayList(caracteristic.getQualitativeValue());
112                     // add a null value at first position
113                     if (!data.isEmpty() && data.get(0) != null) {
114                         data.add(0, null);
115                     }
116                     SwingUtil.fillComboBox(comboBox, data, null);
117 
118                     ObjectToStringConverter converter =
119                             BeanUIUtil.newDecoratedObjectToStringConverter(decorator);
120                     BeanUIUtil.decorate(comboBox, converter);
121                     this.editor = new ComboBoxCellEditor(comboBox);
122                     break;
123                 case TEXT:
124                     // use default editor
125 
126                     this.editor = table.getDefaultEditor(Object.class);
127                     break;
128             }
129         }
130 
131         return editor.getTableCellEditorComponent(
132                 table, value, isSelected, row, column);
133     }
134 
135 }