1 package fr.ifremer.tutti.ui.swing.util.attachment;
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.TuttiUIContext;
27 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
28 import fr.ifremer.tutti.ui.swing.util.TuttiUI;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
32
33 import javax.swing.AbstractCellEditor;
34 import javax.swing.JTable;
35 import javax.swing.border.LineBorder;
36 import javax.swing.event.CellEditorListener;
37 import javax.swing.event.ChangeEvent;
38 import javax.swing.table.TableCellEditor;
39 import java.awt.Color;
40 import java.awt.Component;
41 import java.util.EventObject;
42
43
44
45
46
47
48
49
50 public class AttachmentCellEditor extends AbstractCellEditor implements TableCellEditor {
51
52 private static final long serialVersionUID = 1L;
53
54
55 private static final Log log = LogFactory.getLog(AttachmentCellEditor.class);
56
57 public static TableCellEditor newEditor(TuttiUI ui) {
58
59 return new AttachmentCellEditor(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 ButtonAttachment editorButton;
71
72 public AttachmentCellEditor(TuttiUIContext context) {
73
74 this.editorButton = new ButtonAttachment(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 AttachmentModelAware model =
102 (AttachmentModelAware) tableModel.getEntry(row);
103
104 editorButton.init(model);
105
106 return editorButton;
107 }
108
109 @Override
110 public boolean shouldSelectCell(EventObject anEvent) {
111 return false;
112 }
113
114 @Override
115 public Object getCellEditorValue() {
116
117 AttachmentModelAware model = editorButton.getBean();
118 Preconditions.checkNotNull(model, "No model found in editor.");
119
120 Object result = model.getAttachment();
121 if (log.isDebugEnabled()) {
122 log.debug("editor value: " + result);
123 }
124
125 return result;
126 }
127
128 @Override
129 public boolean stopCellEditing() {
130 boolean b = super.stopCellEditing();
131 if (b) {
132 editorButton.setBean(null);
133 }
134 return b;
135 }
136
137 @Override
138 public void cancelCellEditing() {
139 editorButton.setBean(null);
140 super.cancelCellEditing();
141 }
142 }