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.TuttiLocation;
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
54
55 public class TuttiLocationTableCell {
56
57 protected Decorator<TuttiLocation> decorator;
58
59 protected List<TuttiLocation> entities;
60
61 protected Map<String, TuttiLocation> entityMap;
62
63 public TuttiLocationTableCell(TuttiUIContext context) {
64 DecoratorService decoratorService = context.getDecoratorService();
65 decorator = decoratorService.getDecoratorByType(TuttiLocation.class);
66
67 entities = Lists.newArrayList(context.getPersistenceService().getAllCountry());
68 entityMap = TuttiEntities.splitById(entities);
69 }
70
71 public TableCellEditor getNewTableCellEditor() {
72 return new TuttiLocationTableCellEditor();
73 }
74
75 public TableCellRenderer getNewTableCellRenderer() {
76 return new TuttiLocationTableCellRenderer();
77 }
78
79 protected class TuttiLocationTableCellEditor extends DefaultCellEditor {
80
81 private static final long serialVersionUID = 1L;
82
83 protected TuttiLocationTableCellEditor() {
84 super(new JComboBox());
85
86 final BeanFilterableComboBox<TuttiLocation> component = new BeanFilterableComboBox<>();
87 component.setI18nPrefix("tutti.property.");
88 component.setShowReset(true);
89 component.setBeanType(TuttiLocation.class);
90 setClickCountToStart(1);
91
92 editorComponent = component;
93 delegate = new DefaultCellEditor.EditorDelegate() {
94 private static final long serialVersionUID = 1L;
95
96 @Override
97 public void setValue(Object value) {
98 if (value != null && String.class.isInstance(value)) {
99 value = entityMap.get(value);
100 }
101 component.setSelectedItem(value);
102 }
103
104 @Override
105 public Object getCellEditorValue() {
106 String result = null;
107 Object selectedItem = component.getSelectedItem();
108 if (TuttiLocation.class.isInstance(selectedItem)) {
109 TuttiLocation entity = (TuttiLocation) component.getSelectedItem();
110 if (entity != null) {
111 result = entity.getId();
112 }
113 }
114 return result;
115 }
116
117 @Override
118 public boolean shouldSelectCell(EventObject anEvent) {
119 if (anEvent instanceof MouseEvent) {
120 MouseEvent e = (MouseEvent) anEvent;
121 return e.getID() != MouseEvent.MOUSE_DRAGGED;
122 }
123 return true;
124 }
125
126 @Override
127 public boolean stopCellEditing() {
128 if (component.isEditable()) {
129
130 component.getCombobox().actionPerformed(
131 new ActionEvent(TuttiLocationTableCellEditor.this, 0, ""));
132 }
133 return super.stopCellEditing();
134 }
135 };
136
137 component.init((JXPathDecorator<TuttiLocation>) decorator, entities);
138 }
139
140 class TuttiLocationListCellRenderer extends DefaultListCellRenderer {
141
142 private static final long serialVersionUID = 1L;
143
144 @Override
145 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
146 return super.getListCellRendererComponent(list, decorator.toString(value), index, isSelected, cellHasFocus);
147 }
148 }
149 }
150
151 protected class TuttiLocationTableCellRenderer extends DefaultTableRenderer {
152
153 private static final long serialVersionUID = 1L;
154
155 @Override
156 public Component getTableCellRendererComponent(JTable table, Object value,
157 boolean isSelected, boolean hasFocus,
158 int row, int column) {
159
160 String entityId = String.valueOf(value);
161 TuttiLocation entity = entityMap.get(entityId);
162 return super.getTableCellRendererComponent(table, decorator.toString(entity), isSelected, hasFocus, row, column);
163 }
164 }
165 }