1 package fr.ifremer.tutti.ui.swing.util.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 com.google.common.base.Preconditions;
26 import fr.ifremer.tutti.persistence.entities.CommentAware;
27 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
28 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
29 import fr.ifremer.tutti.ui.swing.util.TuttiUI;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
33
34 import javax.swing.AbstractCellEditor;
35 import javax.swing.JTable;
36 import javax.swing.border.LineBorder;
37 import javax.swing.event.CellEditorListener;
38 import javax.swing.event.ChangeEvent;
39 import javax.swing.table.TableCellEditor;
40 import java.awt.Color;
41 import java.awt.Component;
42 import java.util.EventObject;
43
44
45
46
47
48
49
50 public class CommentCellEditor extends AbstractCellEditor implements TableCellEditor {
51
52 private static final long serialVersionUID = 1L;
53
54
55 private static final Log log = LogFactory.getLog(CommentCellEditor.class);
56
57 public static TableCellEditor newEditor(TuttiUI ui) {
58
59 return new CommentCellEditor(ui.getHandler().getContext());
60 }
61
62 protected JTable table;
63
64 protected AbstractApplicationTableModel<AbstractTuttiBeanUIModel> tableModel;
65
66 protected Integer rowIndex;
67
68 protected Integer columnIndex;
69
70 protected final ButtonComment editorButton;
71
72 public CommentCellEditor(TuttiUIContext context) {
73
74 this.editorButton = new ButtonComment(context, null);
75 this.editorButton.setBorder(new LineBorder(Color.BLACK));
76 addCellEditorListener(new CellEditorListener() {
77 @Override
78 public void editingStopped(ChangeEvent e) {
79 editorButton.setSelected(false);
80 }
81
82 @Override
83 public void editingCanceled(ChangeEvent e) {
84 editorButton.setSelected(false);
85 }
86 });
87 }
88
89 @Override
90 public Component getTableCellEditorComponent(JTable table,
91 Object value,
92 boolean isSelected,
93 int row,
94 int column) {
95 this.table = table;
96 this.tableModel = (AbstractApplicationTableModel<AbstractTuttiBeanUIModel>) table.getModel();
97
98 rowIndex = row;
99 columnIndex = column;
100
101 CommentAware model = (CommentAware) tableModel.getEntry(row);
102
103 editorButton.init(model);
104
105 return editorButton;
106 }
107
108 @Override
109 public boolean shouldSelectCell(EventObject anEvent) {
110 return true;
111 }
112
113 @Override
114 public Object getCellEditorValue() {
115
116 CommentAware model = editorButton.getBean();
117 Preconditions.checkNotNull(model, "No model found in editor.");
118
119 Object result = model.getComment();
120 if (log.isDebugEnabled()) {
121 log.debug("editor value: " + result);
122 }
123
124 return result;
125 }
126
127 @Override
128 public boolean stopCellEditing() {
129 boolean b = super.stopCellEditing();
130 if (b) {
131 editorButton.setBean(null);
132 }
133 return b;
134 }
135
136 @Override
137 public void cancelCellEditing() {
138 editorButton.setBean(null);
139 super.cancelCellEditing();
140 }
141 }