1 package fr.ifremer.quadrige3.ui.swing.table.comment;
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 fr.ifremer.quadrige3.core.dao.technical.Assert;
26 import fr.ifremer.quadrige3.ui.core.dto.CommentAware;
27 import fr.ifremer.quadrige3.ui.swing.ApplicationUI;
28 import fr.ifremer.quadrige3.ui.swing.table.AbstractRowUIModel;
29 import fr.ifremer.quadrige3.ui.swing.table.AbstractTableModel;
30 import jaxx.runtime.SwingUtil;
31 import org.apache.commons.lang3.StringUtils;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.jaxx.application.bean.JavaBeanObjectUtil;
35
36 import javax.swing.AbstractCellEditor;
37 import javax.swing.JTable;
38 import javax.swing.border.LineBorder;
39 import javax.swing.event.CellEditorListener;
40 import javax.swing.event.ChangeEvent;
41 import javax.swing.table.TableCellEditor;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.util.EventObject;
45
46
47
48
49 public class CommentCellEditor extends AbstractCellEditor implements TableCellEditor {
50
51 private static final long serialVersionUID = 1L;
52
53
54
55
56 private static final Log LOG = LogFactory.getLog(CommentCellEditor.class);
57
58
59
60
61
62
63
64
65 public static TableCellEditor newEditor(ApplicationUI ui, String property, String titleI18n, boolean editable) {
66 return new CommentCellEditor(ui, property, titleI18n, editable);
67 }
68
69
70
71
72
73
74
75 public static TableCellEditor newEditor(ApplicationUI ui) {
76 return new CommentCellEditor(ui);
77 }
78
79 protected JTable table;
80
81 protected AbstractTableModel<AbstractRowUIModel<?, ?>> tableModel;
82
83 protected Integer rowIndex;
84
85 protected Integer columnIndex;
86
87 private final ButtonComment editorButton;
88
89 private final boolean editable;
90
91
92
93
94
95
96 public CommentCellEditor(ApplicationUI ui) {
97 this(ui, null, null, true);
98 }
99
100
101
102
103
104
105
106 public CommentCellEditor(ApplicationUI ui, String property, String titleI18n, boolean editable) {
107
108 this.editorButton = new ButtonComment(ui, null, property, titleI18n);
109 this.editorButton.setBorder(new LineBorder(Color.BLACK));
110 addCellEditorListener(new CellEditorListener() {
111 @Override
112 public void editingStopped(ChangeEvent e) {
113 editorButton.setSelected(false);
114 }
115
116 @Override
117 public void editingCanceled(ChangeEvent e) {
118 editorButton.setSelected(false);
119 }
120 });
121 this.editable = editable;
122 }
123
124
125
126
127 @Override
128 @SuppressWarnings("unchecked")
129 public Component getTableCellEditorComponent(JTable table,
130 Object value,
131 boolean isSelected,
132 int row,
133 int column) {
134 this.table = table;
135 this.tableModel = (AbstractTableModel<AbstractRowUIModel<?, ?>>) table.getModel();
136
137 rowIndex = row;
138 columnIndex = column;
139 String comment = (String) value;
140
141 AbstractRowUIModel rowModel = tableModel.getEntry(table.convertRowIndexToModel(row));
142 boolean isEditable = editable && rowModel.isEditable();
143 CommentAware commentAwareModel = null;
144 if (rowModel instanceof CommentAware) {
145 commentAwareModel = (CommentAware) rowModel;
146 }
147
148 editorButton.init(commentAwareModel, isEditable);
149
150 if (StringUtils.isEmpty(StringUtils.trim(comment))) {
151 this.editorButton.setIcon(SwingUtil.createActionIcon("edit-no-comment"));
152 } else {
153 this.editorButton.setIcon(SwingUtil.createActionIcon("edit-comment"));
154 }
155
156 return editorButton;
157 }
158
159
160
161
162 @Override
163 public boolean shouldSelectCell(EventObject anEvent) {
164 return true;
165 }
166
167
168
169
170 @Override
171 public Object getCellEditorValue() {
172
173 CommentAware model = editorButton.getBean();
174 Assert.notNull(model, "No model found in editor.");
175
176 Object result = editorButton.getProperty() == null ? model.getComment() : JavaBeanObjectUtil.getProperty(model, editorButton.getProperty());
177 if (LOG.isDebugEnabled()) {
178 LOG.debug("editor value: " + result);
179 }
180
181 return result;
182 }
183
184
185
186
187 @Override
188 public boolean stopCellEditing() {
189 boolean b = super.stopCellEditing();
190 if (b) {
191 editorButton.setBean(null);
192 }
193 editorButton.setSelected(false);
194 return b;
195 }
196
197
198
199
200 @Override
201 public void cancelCellEditing() {
202 editorButton.setBean(null);
203 editorButton.setSelected(false);
204 super.cancelCellEditing();
205 }
206 }