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.manageractions;
26
27 import java.util.*;
28
29 import org.eclipse.core.runtime.*;
30 import org.eclipse.jface.action.*;
31 import org.wsmo.common.IRI;
32 import org.wsmo.datastore.WsmoRepository;
33 import org.wsmostudio.repository.ui.WSMOEntityContainer;
34 import org.wsmostudio.repository.ui.actions.ActionsManager;
35 import org.wsmostudio.runtime.LogManager;
36 import org.wsmostudio.runtime.extension.Initialisable;
37
38 public class ExtensionActionsManager {
39
40 public static final String ACTIONS_EXT_ID = "org.wsmostudio.repository.manager_actions";
41 private Map<String, IAction[]> ontoActions, wsActions, goalActions, mediatorActions;;
42
43 public ExtensionActionsManager() {
44 ontoActions = new HashMap<String, IAction[]>();
45 wsActions = new HashMap<String, IAction[]>();
46 goalActions = new HashMap<String, IAction[]>();
47 mediatorActions = new HashMap<String, IAction[]>();
48 loadActionsData();
49 }
50
51 public void initActions(MenuManager menuMgr,
52 String repositoryType,
53 byte contextType,
54 WsmoRepository repo,
55 IRI selectionIRI) {
56 IAction[] actions = null;
57 switch (contextType) {
58 case WSMOEntityContainer.ONTOLOGY_CONTENT:
59 actions = ontoActions.get(repositoryType);
60 break;
61 case WSMOEntityContainer.GOAL_CONTENT:
62 actions = goalActions.get(repositoryType);
63 break;
64 case WSMOEntityContainer.SERVICE_CONTENT:
65 actions = wsActions.get(repositoryType);
66 break;
67 default:
68 actions = mediatorActions.get(repositoryType);
69 }
70 if (actions == null) {
71 return;
72 }
73
74 if (actions.length > 0) {
75 menuMgr.add(new Separator());
76 }
77
78 for (int i = 0; i < actions.length; i++) {
79 final IAction action = actions[i];
80 if (action instanceof Initialisable) {
81 Map<String, Object> params = new HashMap<String, Object>();
82 params.put(ActionsManager.INSTANCE_REF, repo);
83 params.put(ActionsManager.SELECTION_REF, selectionIRI);
84 params.put(ActionsManager.SELECTION_TYPE_REF, Byte.valueOf(contextType));
85 try {
86 ((Initialisable)actions[i]).initialise(params);
87 }
88 catch(Exception ex) {
89 LogManager.logError(ex);
90 }
91 }
92 menuMgr.add(actions[i]);
93 }
94 }
95
96
97 @SuppressWarnings("unchecked")
98 private void loadActionsData() {
99 Map<String, Set<IAction>> ontoActionsBuffer = new HashMap<String, Set<IAction>>();
100 Map<String, Set<IAction>> wsActionsBuffer = new HashMap<String, Set<IAction>>();
101 Map<String, Set<IAction>> goalActionsBuffer = new HashMap<String, Set<IAction>>();
102 Map<String, Set<IAction>> mediActionsBuffer = new HashMap<String, Set<IAction>>();
103
104 IExtensionPoint iExtPoint = Platform
105 .getExtensionRegistry()
106 .getExtensionPoint(ACTIONS_EXT_ID);
107 IExtension[] exts = iExtPoint.getExtensions();
108 for (int i = 0; i < exts.length; i++) {
109 IConfigurationElement[] confs = exts[i].getConfigurationElements();
110 for (int j = 0; j < confs.length; j++) {
111 if (false == confs[j].getName().equals(ActionsManager.ACTION_CONFIG_ELEMENT)) {
112 continue;
113 }
114
115 String name = confs[j].getAttribute(ActionsManager.CONF_ATTR_NAME);
116 if (name == null || name.trim().length() == 0) {
117 continue;
118 }
119 String repoType = confs[j].getAttribute(ActionsManager.CONF_ATTR_R_TYPE);
120 if (repoType == null || repoType.trim().length() == 0) {
121 continue;
122 }
123 String managerClass = confs[j].getAttribute(ActionsManager.CONF_ATTR_HANDLER);
124 IAction actionHandler = null;
125 try {
126 if (managerClass != null
127 && managerClass.trim().length() > 0) {
128 actionHandler = (IAction)confs[j].createExecutableExtension(
129 ActionsManager.CONF_ATTR_HANDLER);
130 }
131 }
132 catch(CoreException coreEx) {
133 continue;
134 }
135 if (actionHandler == null) {
136 continue;
137 }
138 if (actionHandler instanceof Initialisable) {
139 Map params = new HashMap();
140 params.put(ActionsManager.CONF_ATTR_NAME, name);
141 try {
142 ((Initialisable)actionHandler).initialise(params);
143 }
144 catch(Exception ex) {
145 LogManager.logError(ex);
146 }
147 }
148 processUsageTypes(repoType,
149 actionHandler,
150 confs[j].getChildren(),
151 ontoActionsBuffer,
152 goalActionsBuffer,
153 wsActionsBuffer,
154 mediActionsBuffer);
155 }
156 }
157 prepareData(ontoActionsBuffer, ontoActions);
158 prepareData(goalActionsBuffer, goalActions);
159 prepareData(wsActionsBuffer, wsActions);
160 prepareData(mediActionsBuffer, mediatorActions);
161 }
162
163 private void processUsageTypes(String repoType,
164 IAction actionHandler,
165 IConfigurationElement[] usages,
166 Map<String, Set<IAction>> ontoActionsBuffer,
167 Map<String, Set<IAction>> goalActionsBuffer,
168 Map<String, Set<IAction>> wsActionsBuffer,
169 Map<String, Set<IAction>> mediActionsBuffer) {
170 if (usages == null) {
171 return;
172 }
173 for(int i = 0; i < usages.length; i++) {
174 if (false == usages[i].getName().equals("usage")) {
175 continue;
176 }
177 String usageType = usages[i].getAttribute("type");
178 if (usageType == null) {
179 continue;
180 }
181 if (usageType.equalsIgnoreCase("ontology")
182 || usageType.equalsIgnoreCase("any")) {
183 addAction(repoType, actionHandler, ontoActionsBuffer);
184 }
185 if (usageType.equalsIgnoreCase("goal")
186 || usageType.equalsIgnoreCase("any")) {
187 addAction(repoType, actionHandler, goalActionsBuffer);
188 }
189 if (usageType.equalsIgnoreCase("service")
190 || usageType.equalsIgnoreCase("any")) {
191 addAction(repoType, actionHandler, wsActionsBuffer);
192 }
193 if (usageType.equalsIgnoreCase("mediator")
194 || usageType.equalsIgnoreCase("any")) {
195 addAction(repoType, actionHandler, mediActionsBuffer);
196 }
197 }
198
199 }
200
201 private void addAction(String repoType, IAction action, Map<String, Set<IAction>> container) {
202 Set<IAction> actions = container.get(repoType);
203 if (actions == null) {
204 actions = new HashSet<IAction>();
205 container.put(repoType, actions);
206 }
207 actions.add(action);
208 }
209
210 private void prepareData(Map<String, Set<IAction>> actionsBuffer, Map<String, IAction[]> target) {
211 for(String repoType : actionsBuffer.keySet()) {
212 Set<IAction> actionsSet = actionsBuffer.get(repoType);
213 IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
214 Arrays.sort(actions, ActionsManager.comparator);
215 target.put(repoType, actions);
216 }
217 }
218 }
219
220
221
222
223
224
225
226
227
228
229
230