1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.repository.ui.actions;
26
27 import java.util.*;
28
29 import org.eclipse.core.runtime.*;
30 import org.eclipse.jface.action.*;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.osgi.framework.Bundle;
33 import org.wsmo.datastore.WsmoRepository;
34 import org.wsmostudio.runtime.LogManager;
35 import org.wsmostudio.runtime.extension.Initialisable;
36
37 public class ActionsManager {
38
39 public static final String ACTIONS_EXT_ID = "org.wsmostudio.repository.actions";
40 public static final String INSTANCE_REF = "RepoInstance";
41
42 public static final String SELECTION_REF = "IRI";
43 public static final String SELECTION_TYPE_REF = "IRIType";
44
45 public static final String ACTION_CONFIG_ELEMENT = "action";
46 public static final String CONF_ATTR_NAME = "name";
47 public static final String CONF_ATTR_HANDLER = "handler";
48 public static final String CONF_ATTR_R_TYPE = "repositoryType";
49 public static final String CONF_ATTR_ICON = "icon";
50
51 public static final String ANY_REPOSITORY_TYPE = "";
52
53 public static Comparator<IAction> comparator = new Comparator<IAction>() {
54 public int compare(IAction o1, IAction o2) {
55 return o1.getText().compareToIgnoreCase(o2.getText());
56 }
57 };
58
59 private Map<String, IAction[]> actionsMap;
60
61 public ActionsManager() {
62 actionsMap = new HashMap<String, IAction[]>();
63 loadActionsData();
64 }
65
66 public void initActions(MenuManager menuMgr,
67 String repositoryType,
68 WsmoRepository repo) {
69 IAction[] actions = actionsMap.get(repositoryType);
70 Map<String, WsmoRepository> params = new HashMap<String, WsmoRepository>();
71 params.put(INSTANCE_REF, repo);
72 boolean flagPutMenuSeparator = false;
73 if (actions != null) {
74 for (int i = 0; i < actions.length; i++) {
75 if (actions[i] instanceof Initialisable) {
76 try {
77 ((Initialisable)actions[i]).initialise(params);
78 }
79 catch(Exception ex) {
80 LogManager.logError(ex);
81 }
82 }
83 menuMgr.add(actions[i]);
84 flagPutMenuSeparator = true;
85 }
86 }
87
88
89 actions = actionsMap.get(ANY_REPOSITORY_TYPE);
90 if (actions != null) {
91 for (int i = 0; i < actions.length; i++) {
92 if (actions[i] instanceof Initialisable) {
93 try {
94 ((Initialisable)actions[i]).initialise(params);
95 }
96 catch(Exception ex) {
97 LogManager.logError(ex);
98 }
99 }
100 menuMgr.add(actions[i]);
101 flagPutMenuSeparator = true;
102 }
103 }
104
105 if (flagPutMenuSeparator) {
106 menuMgr.add(new Separator());
107 }
108 }
109
110
111 @SuppressWarnings("unchecked")
112 private void loadActionsData() {
113 Map<String, Set<IAction>> actionsBuffer = new HashMap<String, Set<IAction>>();
114
115 IExtensionPoint iExtPoint = Platform
116 .getExtensionRegistry()
117 .getExtensionPoint(ACTIONS_EXT_ID);
118 IExtension[] exts = iExtPoint.getExtensions();
119 for (int i = 0; i < exts.length; i++) {
120 IConfigurationElement[] confs = exts[i].getConfigurationElements();
121 for (int j = 0; j < confs.length; j++) {
122 if (false == confs[j].getName().equals(ACTION_CONFIG_ELEMENT)) {
123 continue;
124 }
125
126 String name = confs[j].getAttribute(CONF_ATTR_NAME);
127 if (name == null || name.trim().length() == 0) {
128 continue;
129 }
130 String repoType = confs[j].getAttribute(CONF_ATTR_R_TYPE);
131 if (repoType == null || repoType.trim().length() == 0) {
132 repoType = ANY_REPOSITORY_TYPE;
133 }
134 String managerClass = confs[j].getAttribute(CONF_ATTR_HANDLER);
135 IAction actionHandler = null;
136 try {
137 if (managerClass != null
138 && managerClass.trim().length() > 0) {
139 actionHandler = (IAction)confs[j].createExecutableExtension(
140 CONF_ATTR_HANDLER);
141 }
142 }
143 catch(CoreException coreEx) {
144 continue;
145 }
146 if (actionHandler == null) {
147 continue;
148 }
149 String iconPath = confs[j].getAttribute(CONF_ATTR_ICON);
150 if (iconPath != null) {
151 Bundle bundle = Platform.getBundle(confs[j].getNamespaceIdentifier());
152 actionHandler.setImageDescriptor(
153 ImageDescriptor.createFromURL(bundle.getEntry(iconPath)));
154 }
155
156 if (actionHandler instanceof Initialisable) {
157 Map params = new HashMap();
158 params.put(CONF_ATTR_NAME, name);
159 try {
160 ((Initialisable)actionHandler).initialise(params);
161 }
162 catch(Exception ex) {
163 LogManager.logError(ex);
164 }
165 }
166 addAction(repoType, actionHandler, actionsBuffer);
167 }
168 }
169 prepareData(actionsBuffer);
170 }
171
172 private void addAction(String repoType, IAction action, Map<String, Set<IAction>> container) {
173 Set<IAction> actions = container.get(repoType);
174 if (actions == null) {
175 actions = new HashSet<IAction>();
176 container.put(repoType, actions);
177 }
178 actions.add(action);
179 }
180
181 private void prepareData(Map<String, Set<IAction>> actionsBuffer) {
182 Comparator<IAction> comparator = new Comparator<IAction>() {
183 public int compare(IAction o1, IAction o2) {
184 return o1.getText().compareToIgnoreCase(o2.getText());
185 }
186 };
187 for(String repoType : actionsBuffer.keySet()) {
188 Set<IAction> actionsSet = actionsBuffer.get(repoType);
189 IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
190 Arrays.sort(actions, comparator);
191 actionsMap.put(repoType, actions);
192 }
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222