1 package fr.ifremer.tutti.ui.swing.content.operation.catches.individualobservation;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.base.Preconditions;
26 import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.IndividualObservationBatchRowModel;
27 import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.IndividualObservationBatchTableModel;
28 import fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency.SpeciesFrequencyUIModel;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import javax.swing.AbstractCellEditor;
33 import javax.swing.JButton;
34 import javax.swing.JTable;
35 import javax.swing.border.LineBorder;
36 import javax.swing.table.TableCellEditor;
37 import java.awt.Color;
38 import java.awt.Component;
39 import java.util.EventObject;
40 import java.util.Objects;
41
42 import static org.nuiton.i18n.I18n.t;
43
44
45
46
47
48 public class SamplingCodeCellEditor extends AbstractCellEditor implements TableCellEditor {
49
50 private static final long serialVersionUID = 1L;
51
52
53 private static final Log log = LogFactory.getLog(SamplingCodeCellEditor.class);
54
55 public static TableCellEditor newEditor(SpeciesFrequencyUIModel model) {
56
57 return new SamplingCodeCellEditor(model);
58 }
59
60 protected JTable table;
61
62 protected IndividualObservationBatchTableModel tableModel;
63
64 protected Integer rowIndex;
65
66 protected Integer columnIndex;
67
68 protected final SampleCodeGenerator editorButton;
69
70 public SamplingCodeCellEditor(SpeciesFrequencyUIModel model) {
71 this.editorButton = new SampleCodeGenerator(model);
72 }
73
74 @Override
75 public Component getTableCellEditorComponent(JTable table,
76 Object value,
77 boolean isSelected,
78 int row,
79 int column) {
80 this.table = table;
81 this.tableModel = (IndividualObservationBatchTableModel) table.getModel();
82
83 rowIndex = row;
84 columnIndex = column;
85
86 editorButton.setRow(tableModel.getEntry(row));
87
88 return editorButton;
89 }
90
91 @Override
92 public boolean shouldSelectCell(EventObject anEvent) {
93 return true;
94 }
95
96 @Override
97 public Object getCellEditorValue() {
98
99 IndividualObservationBatchRowModel model = editorButton.getRow();
100 Preconditions.checkNotNull(model, "No model found in editor.");
101
102 Object result = model.getSamplingCode();
103 if (log.isDebugEnabled()) {
104 log.debug("editor value: " + result);
105 }
106
107 return result;
108 }
109
110 @Override
111 public boolean stopCellEditing() {
112 boolean b = super.stopCellEditing();
113 if (b) {
114 editorButton.setRow(null);
115 }
116 return b;
117 }
118
119 @Override
120 public void cancelCellEditing() {
121 editorButton.setRow(null);
122 super.cancelCellEditing();
123 }
124
125 public class SampleCodeGenerator extends JButton {
126
127 private final SpeciesFrequencyUIModel model;
128
129 private IndividualObservationBatchRowModel row;
130
131 SampleCodeGenerator(SpeciesFrequencyUIModel model) {
132
133 Objects.requireNonNull(model);
134
135 this.model = model;
136 setBorder(new LineBorder(Color.BLACK));
137 setText(t("tutti.editIndividualObservationBatch.table.editor.codeSampleGenerator.label"));
138 addActionListener(evt -> generateCode());
139 }
140
141 public IndividualObservationBatchRowModel getRow() {
142 return row;
143 }
144
145 public void setRow(IndividualObservationBatchRowModel row) {
146 this.row = row;
147 }
148
149 protected void generateCode() {
150
151 int samplingCodeId = model.getSamplingCodeUICache().getNextSamplingCodeId();
152 String samplingCode = model.getIndividualObservationModel().getSamplingCodePrefix().toSamplingCode(samplingCodeId);
153
154 if (log.isInfoEnabled()) {
155 log.info("Generated sampling code: " + samplingCode);
156 }
157 row.setSamplingCode(samplingCode);
158
159 stopCellEditing();
160 }
161
162 }
163
164
165 }