1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 /***
36
37 * <p>Title: WSMO Studio</p>
38
39 * <p>Description: Semantic Web Service Editor</p>
40
41 * <p>Copyright: Copyright (c) 2004-2006</p>
42
43 * <p>Company: OntoText Lab. / SIRMA </p>
44
45 */
46
47
48
49 package org.wsmostudio.ui.views.navigator.actions;
50
51
52
53 import java.util.*;
54
55
56
57 import org.eclipse.core.runtime.*;
58
59 import org.eclipse.jface.action.*;
60
61 import org.wsmostudio.runtime.LogManager;
62
63 import org.wsmostudio.runtime.extension.Initialisable;
64
65 import org.wsmostudio.ui.views.navigator.*;
66
67
68
69 public class ActionRegistry {
70
71
72
73 private static String ACTION_CONFIG_ELEMENT = "action";
74
75 private static String CONF_ATTR_HANDLER = "handler";
76
77 private static String CONF_ATTR_CATEGORY = "category";
78
79
80
81 private static String ADD_CATEGORY = "add";
82
83 private static String REMOVE_CATEGORY = "remove";
84
85
86
87 private Map<String, IAction[]> addActionsMap,
88
89 removeActionsMap,
90
91 miscActionsMap;
92
93 public Comparator<IAction> comparator = new Comparator<IAction>() {
94
95 public int compare(IAction o1, IAction o2) {
96
97 return o1.getText().compareToIgnoreCase(o2.getText());
98
99 }
100
101 };
102
103
104
105
106
107 public ActionRegistry(WSMONavigator navi) {
108
109 addActionsMap = new HashMap<String, IAction[]>();
110
111 removeActionsMap = new HashMap<String, IAction[]>();
112
113 miscActionsMap = new HashMap<String, IAction[]>();
114
115 loadActionsData(navi);
116
117 }
118
119
120
121 public void initAddActions(IMenuManager mgr, Object target) {
122
123 IAction[] actions = collectRelevantActions(target.getClass(), addActionsMap);
124
125 if (actions == null) {
126
127 return;
128
129 }
130
131 for (int i = 0; i < actions.length; i++) {
132
133 mgr.add(actions[i]);
134
135 }
136
137 }
138
139
140
141 public void initRemoveActions(IMenuManager mgr, Object target) {
142
143 IAction[] actions = collectRelevantActions(target.getClass(), removeActionsMap);
144
145 if (actions == null) {
146
147 return;
148
149 }
150
151 for (int i = 0; i < actions.length; i++) {
152
153 mgr.add(actions[i]);
154
155 }
156
157 }
158
159
160
161 public void initMiscActions(IMenuManager mgr, Object target) {
162
163 IAction[] actions = collectRelevantActions(target.getClass(), miscActionsMap);
164
165 if (actions == null) {
166
167 return;
168
169 }
170
171 for (int i = 0; i < actions.length; i++) {
172
173 mgr.add(actions[i]);
174
175 }
176
177 }
178
179
180
181 public IAction getCustomEditAction(Object target) {
182
183 IAction result = getCustomEditAction(target, addActionsMap);
184
185 if (result == null) {
186
187 result = getCustomEditAction(target, miscActionsMap);
188
189 }
190
191 if (result == null) {
192
193 result = getCustomEditAction(target, removeActionsMap);
194
195 }
196
197 return result;
198
199 }
200
201
202
203 public IAction[] collectRelevantActions(Class target, Map<String, IAction[]> map) {
204
205 List<IAction> buffer = new LinkedList<IAction>();
206
207 collectRelevantActionsUnordered(target, map, buffer);
208
209 if (buffer.size() == 0) {
210
211 return null;
212
213 }
214
215 Set<String> tempSet = new HashSet<String>();
216
217 for(Iterator it = buffer.listIterator(); it.hasNext();) {
218
219 IAction action = (IAction)it.next();
220
221 if (tempSet.contains(action.getText())) {
222
223 it.remove();
224
225 continue;
226
227 }
228
229 tempSet.add(action.getText());
230
231 }
232
233 IAction[] actions = buffer.toArray(new IAction[buffer.size()]);
234
235 Arrays.sort(actions, comparator);
236
237 return actions;
238
239 }
240
241
242
243 private void collectRelevantActionsUnordered(Class target, Map<String, IAction[]> map, List<IAction> result) {
244
245 if (map.containsKey(target.getName())) {
246
247 IAction[] actions = map.get(target.getName());
248
249 for(int i = 0; i < actions.length; i++) {
250
251 result.add(actions[i]);
252
253 }
254
255 }
256
257 if (target.getSuperclass() != null
258
259 && false == target.getSuperclass().equals(Object.class)) {
260
261 collectRelevantActionsUnordered(target.getSuperclass(), map, result);
262
263 }
264
265 Class[] ifaces = target.getInterfaces();
266
267 if (ifaces != null) {
268
269 for(int i = 0; i < ifaces.length; i++) {
270
271 collectRelevantActionsUnordered(ifaces[i], map, result);
272
273 }
274
275 }
276
277 }
278
279
280
281 private IAction getCustomEditAction(Object target, Map<String, IAction[]> map) {
282
283 String table_key = ContentExtensionManager.getRelatedKeyForTable(
284
285 target, map, true);
286
287 if (table_key == null) {
288
289 return null;
290
291 }
292
293 IAction[] actions = map.get(table_key);
294
295 for (int i = 0; i < actions.length; i++) {
296
297 if (actions[i].getText().toLowerCase().indexOf("edit") != -1) {
298
299 return actions[i];
300
301 }
302
303 }
304
305 return null;
306
307 }
308
309
310
311 @SuppressWarnings("unchecked")
312
313 private void loadActionsData(WSMONavigator navi) {
314
315 Map<String, Set<IAction>> addActionsBuffer = new HashMap<String, Set<IAction>>();
316
317 Map<String, Set<IAction>> removeActionsBuffer = new HashMap<String, Set<IAction>>();
318
319 Map<String, Set<IAction>> miscActionsBuffer = new HashMap<String, Set<IAction>>();
320
321
322
323 IExtensionPoint iExtPoint = Platform
324
325 .getExtensionRegistry()
326
327 .getExtensionPoint(
328
329 ContentExtensionManager.CONTENT_EXT_ID);
330
331 IExtension[] exts = iExtPoint.getExtensions();
332
333 for (int i = 0; i < exts.length; i++) {
334
335 IConfigurationElement[] confs = exts[i].getConfigurationElements();
336
337 for (int j = 0; j < confs.length; j++) {
338
339 if (false == confs[j].getName().equals(ACTION_CONFIG_ELEMENT)) {
340
341 continue;
342
343 }
344
345
346
347 String className = confs[j].getAttribute(
348
349 ContentExtensionManager.CONF_ATTR_CLASS);
350
351 if (className == null || className.trim().length() == 0) {
352
353 continue;
354
355 }
356
357 String actionName = confs[j].getAttribute(
358
359 ContentExtensionManager.CONF_ATTR_NAME);
360
361 if (actionName == null || actionName.trim().length() == 0) {
362
363 continue;
364
365 }
366
367 String managerClass = confs[j].getAttribute(CONF_ATTR_HANDLER);
368
369 IAction actionHandler = null;
370
371 try {
372
373 if (managerClass != null
374
375 && managerClass.trim().length() > 0) {
376
377 actionHandler = (IAction)confs[j].createExecutableExtension(
378
379 CONF_ATTR_HANDLER);
380
381 }
382
383 }
384
385 catch(CoreException coreEx) {
386
387 continue;
388
389 }
390
391 if (actionHandler == null) {
392
393 continue;
394
395 }
396
397 if (actionHandler instanceof Initialisable) {
398
399 Map params = new HashMap();
400
401 params.put(AbstractAction.NAME_KEY, actionName);
402
403 params.put(AbstractAction.NAVIGATOR_KEY, navi);
404
405 try {
406
407 ((Initialisable)actionHandler).initialise(params);
408
409 }
410
411 catch(Exception ex) {
412
413 LogManager.logError(ex);
414
415 }
416
417 }
418
419
420
421 String actionCategory = confs[j].getAttribute(CONF_ATTR_CATEGORY);
422
423 Map<String, Set<IAction>> container;
424
425 if (actionCategory.equalsIgnoreCase(ADD_CATEGORY)) {
426
427 container = addActionsBuffer;
428
429 }
430
431 else if (actionCategory.equalsIgnoreCase(REMOVE_CATEGORY)) {
432
433 container = removeActionsBuffer;
434
435 }
436
437 else {
438
439 container = miscActionsBuffer;
440
441 }
442
443 addAction(className, actionHandler, container);
444
445 }
446
447 }
448
449 prepareData(addActionsBuffer, removeActionsBuffer, miscActionsBuffer);
450
451 }
452
453
454
455 private void addAction(String className, IAction action, Map<String, Set<IAction>> container) {
456
457 Set<IAction> actions = container.get(className);
458
459 if (actions == null) {
460
461 actions = new HashSet<IAction>();
462
463 container.put(className, actions);
464
465 }
466
467 actions.add(action);
468
469 }
470
471
472
473 private void prepareData(Map<String, Set<IAction>> addActionsBuffer,
474
475 Map<String, Set<IAction>> removeActionsBuffer,
476
477 Map<String, Set<IAction>> miscActionsBuffer) {
478
479
480
481 for(String className : addActionsBuffer.keySet()) {
482
483 Set<IAction> actionsSet = addActionsBuffer.get(className);
484
485 IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
486
487 Arrays.sort(actions, comparator);
488
489 addActionsMap.put(className, actions);
490
491 }
492
493
494
495 for(String className : removeActionsBuffer.keySet()) {
496
497 Set<IAction> actionsSet = removeActionsBuffer.get(className);
498
499 IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
500
501 Arrays.sort(actions, comparator);
502
503 removeActionsMap.put(className, actions);
504
505 }
506
507
508
509 for(String className : miscActionsBuffer.keySet()) {
510
511 Set<IAction> actionsSet = miscActionsBuffer.get(className);
512
513 IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
514
515 Arrays.sort(actions, comparator);
516
517 miscActionsMap.put(className, actions);
518
519 }
520
521 }
522
523 }
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551