1 package fr.ifremer.tutti.ui.swing.util.editor;
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.collect.Lists;
26 import fr.ifremer.tutti.persistence.entities.TuttiEntities;
27 import fr.ifremer.tutti.persistence.entities.referential.Vessel;
28 import fr.ifremer.tutti.service.DecoratorService;
29 import fr.ifremer.tutti.ui.swing.TuttiUIContext;
30 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
31 import org.jdesktop.swingx.renderer.DefaultTableRenderer;
32 import org.nuiton.decorator.Decorator;
33 import org.nuiton.decorator.JXPathDecorator;
34
35 import javax.swing.DefaultCellEditor;
36 import javax.swing.DefaultListCellRenderer;
37 import javax.swing.JComboBox;
38 import javax.swing.JList;
39 import javax.swing.JTable;
40 import javax.swing.table.TableCellEditor;
41 import javax.swing.table.TableCellRenderer;
42 import java.awt.Component;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.MouseEvent;
45 import java.util.EventObject;
46 import java.util.List;
47 import java.util.Map;
48
49
50
51
52
53 public class VesselTableCell {
54
55 protected Decorator<Vessel> decorator;
56
57 protected List<Vessel> entities;
58
59 protected Map<String, Vessel> entityMap;
60
61 public VesselTableCell(TuttiUIContext context) {
62 DecoratorService decoratorService = context.getDecoratorService();
63 decorator = decoratorService.getDecoratorByType(Vessel.class);
64
65 entities = Lists.newArrayList(context.getDataContext().getFishingVessels());
66 entities.addAll(context.getDataContext().getScientificVessels());
67 entityMap = TuttiEntities.splitById(entities);
68 }
69
70 public TableCellEditor getNewTableCellEditor() {
71 return new VesselTableCellEditor();
72 }
73
74 public TableCellRenderer getNewTableCellRenderer() {
75 return new VesselTableCellRenderer();
76 }
77
78 protected class VesselTableCellEditor extends DefaultCellEditor {
79
80 private static final long serialVersionUID = 1L;
81
82 protected VesselTableCellEditor() {
83 super(new JComboBox());
84
85 final BeanFilterableComboBox<Vessel> component = new BeanFilterableComboBox<>();
86 component.setI18nPrefix("tutti.property.");
87 component.setShowReset(true);
88 component.setBeanType(Vessel.class);
89 setClickCountToStart(1);
90
91 editorComponent = component;
92 delegate = new DefaultCellEditor.EditorDelegate() {
93 private static final long serialVersionUID = 1L;
94
95 @Override
96 public void setValue(Object value) {
97 if (value != null && String.class.isInstance(value)) {
98 value = entityMap.get(value);
99 }
100 component.setSelectedItem(value);
101 }
102
103 @Override
104 public Object getCellEditorValue() {
105 String result = null;
106 Object selectedItem = component.getSelectedItem();
107 if (Vessel.class.isInstance(selectedItem)) {
108 Vessel vessel = (Vessel) component.getSelectedItem();
109 if (vessel != null) {
110 result = vessel.getId();
111 }
112 }
113 return result;
114 }
115
116 @Override
117 public boolean shouldSelectCell(EventObject anEvent) {
118 if (anEvent instanceof MouseEvent) {
119 MouseEvent e = (MouseEvent) anEvent;
120 return e.getID() != MouseEvent.MOUSE_DRAGGED;
121 }
122 return true;
123 }
124
125 @Override
126 public boolean stopCellEditing() {
127 if (component.isEditable()) {
128
129 component.getCombobox().actionPerformed(
130 new ActionEvent(VesselTableCellEditor.this, 0, ""));
131 }
132 return super.stopCellEditing();
133 }
134 };
135
136 component.init((JXPathDecorator<Vessel>) decorator, entities);
137 }
138
139 class VesselListCellRenderer extends DefaultListCellRenderer {
140
141 private static final long serialVersionUID = 1L;
142
143 @Override
144 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
145 return super.getListCellRendererComponent(list, decorator.toString(value), index, isSelected, cellHasFocus);
146 }
147 }
148 }
149
150 protected class VesselTableCellRenderer extends DefaultTableRenderer {
151
152 private static final long serialVersionUID = 1L;
153
154 @Override
155 public Component getTableCellRendererComponent(JTable table, Object value,
156 boolean isSelected, boolean hasFocus,
157 int row, int column) {
158
159 String vesselId = String.valueOf(value);
160 Vessel vessel = entityMap.get(vesselId);
161 return super.getTableCellRendererComponent(table, decorator.toString(vessel), isSelected, hasFocus, row, column);
162 }
163 }
164 }