1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.customizer;
21
22 import com.cosylab.gui.components.introspection.BeanPropertiesTableModel;
23 import com.cosylab.gui.components.introspection.DefaultPropertiesTable;
24
25 import com.cosylab.util.Debug;
26 import com.cosylab.util.ObjectList;
27
28 import java.awt.Color;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.Iterator;
34
35 import javax.swing.JComponent;
36 import javax.swing.border.LineBorder;
37 import javax.swing.table.TableModel;
38
39
40
41
42
43
44
45
46
47
48 public class DefaultEditor implements Editor
49 {
50 private DefaultPropertiesTable defaultPropertiesTable = null;
51 private HashMap<String, ArrayList<Editor>> customEditors = null;
52 private HashMap<String, BeanPropertiesTableModel> defaultPropertiesTableModels = null;
53 private Object displayer = null;
54 private ObjectList activeEditors = null;
55
56
57
58
59 public DefaultEditor()
60 {
61 super();
62 defaultPropertiesTableModels = new HashMap<String, BeanPropertiesTableModel>();
63 customEditors = new HashMap<String, ArrayList<Editor>>();
64 activeEditors = new ObjectList(Editor.class);
65 defaultPropertiesTable = new DefaultPropertiesTable();
66 defaultPropertiesTable.setBorder(new LineBorder(Color.BLACK));
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public String[] getDefaultProperties(String aspect)
100 {
101 Debug.out(aspect);
102
103 return null;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public JComponent getEditorComponent(Object displayer, String aspect)
127 throws IllegalArgumentException
128 {
129 Editor ed;
130 Iterator it = activeEditors.iterator();
131
132 while (it.hasNext()) {
133 if ((ed = (Editor)it.next()).canEdit(displayer, aspect)) {
134 return ed.getEditorComponent(displayer, aspect);
135 }
136 }
137
138 ArrayList l = customEditors.get(aspect);
139
140 if (l != null) {
141 it = l.iterator();
142
143 while (it.hasNext()) {
144 if ((ed = (Editor)it.next()).canEdit(displayer, aspect)) {
145 activeEditors.add(ed);
146
147 return ed.getEditorComponent(displayer, aspect);
148 }
149 }
150 }
151
152 initializeDefaultPropertiesTable(displayer, aspect);
153
154 return defaultPropertiesTable;
155 }
156
157
158
159
160
161
162
163
164
165
166 public void addCustomEditor(String aspect, Editor editor)
167 {
168 ArrayList<Editor> l = customEditors.get(aspect);
169
170 if (l == null) {
171 l = new ArrayList<Editor>();
172 customEditors.put(aspect, l);
173 }
174
175 l.add(editor);
176 }
177
178
179
180
181
182
183
184 public void applySettings()
185 {
186 applyTableProperties();
187
188 Editor[] eds = getActiveEditors();
189
190 for (int i = 0; i < eds.length; i++) {
191 eds[i].applySettings();
192 }
193 }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public boolean canEdit(Object displayer, String aspect)
211 {
212 ArrayList<Editor> l = customEditors.get(aspect);
213
214 if (l != null) {
215 Iterator<Editor> it = l.iterator();
216
217 while (it.hasNext()) {
218 if (it.next().canEdit(displayer, aspect)) {
219 return true;
220 }
221 }
222 }
223
224 return (Arrays.binarySearch(ASPECTS, aspect) >= 0
225 && getDefaultProperties(aspect) != null);
226 }
227
228
229
230
231
232
233
234 public void revertSettings()
235 {
236 Editor[] eds = getActiveEditors();
237
238 for (int i = 0; i < eds.length; i++) {
239 eds[i].revertSettings();
240 }
241 }
242
243
244
245
246
247
248 public void stopEditing()
249 {
250 defaultPropertiesTableModels.clear();
251
252 Editor[] eds = getActiveEditors();
253
254 for (int i = 0; i < eds.length; i++) {
255 eds[i].stopEditing();
256 }
257
258 activeEditors.clear();
259 }
260
261
262
263
264 protected void applyTableProperties()
265 {
266 if (displayer == null) {
267 return;
268 }
269
270 Object[] modelList = defaultPropertiesTableModels.values().toArray();
271
272
273 for (int i = 0; i < modelList.length; i++) {
274 BeanPropertiesTableModel model = (BeanPropertiesTableModel)modelList[i];
275
276 try {
277 model.applyProperties();
278 } catch (Exception e) {
279 e.printStackTrace();
280 }
281 }
282 }
283
284
285
286
287 protected void initializeDefaultPropertiesTable(Object displayer,
288 String aspect) throws IllegalArgumentException
289 {
290 this.displayer = displayer;
291
292 if (defaultPropertiesTableModels.containsKey(aspect)) {
293 defaultPropertiesTable.setModel((TableModel)defaultPropertiesTableModels
294 .get(aspect));
295 } else {
296 String[] names = getDefaultProperties(aspect);
297
298 if (names != null) {
299 BeanPropertiesTableModel model = new BeanPropertiesTableModel(displayer,
300 names);
301 defaultPropertiesTableModels.put(aspect, model);
302 defaultPropertiesTable.setModel(model);
303 }
304 }
305 }
306
307
308
309
310 private Editor[] getActiveEditors()
311 {
312 return (Editor[])activeEditors.toArray();
313 }
314 }
315
316