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.ui;
26
27
28 import java.lang.reflect.*;
29 import java.util.*;
30
31 import org.eclipse.jface.dialogs.MessageDialog;
32 import org.eclipse.jface.preference.IPreferenceStore;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.*;
35 import org.omwg.ontology.*;
36 import org.omwg.ontology.Type;
37 import org.wsmo.common.*;
38 import org.wsmo.mediator.*;
39 import org.wsmo.service.*;
40 import org.wsmostudio.preferences.WSMOMainPage;
41 import org.wsmostudio.runtime.*;
42 import org.wsmostudio.runtime.cache.WSMOTopEntity;
43 import org.wsmostudio.ui.editors.model.*;
44
45 import com.ontotext.wsmo4j.factory.IDReference;
46
47 /***
48 * Simple UI utility class.
49 *
50 * @author not attributable
51 * @version $Revision: 1420 $ $Date: 2008-01-29 19:46:27 +0200 $
52 */
53
54 public class Utils {
55
56 public static String normalizeSpaces(String input) {
57 StringBuffer result = new StringBuffer(input.length());
58 char ch = 'a';
59 for (int i = 0; i < input.length(); i++) {
60 if (Character.isWhitespace(input.charAt(i))) {
61 if (ch != ' ') {
62 ch = ' ';
63 result.append(ch);
64 }
65 continue;
66 }
67 result.append(ch = input.charAt(i));
68 }
69 return result.toString();
70 }
71
72
73
74 public static String getEntityLocalName(String labText) {
75 IPreferenceStore store = RuntimePlugin.getDefault().getPreferenceStore();
76 return getEntityLocalName(labText,
77 store.getBoolean(WSMOMainPage.PREF_USE_IRI_FULL_NAME),
78 store.getBoolean(WSMOMainPage.PREF_USE_SLASH_SEP),
79 null);
80 }
81
82 public static String getEntityLocalName(String labText, TopEntity nsHolder) {
83 IPreferenceStore store = RuntimePlugin.getDefault().getPreferenceStore();
84
85 if (labText.equals("_#")) {
86 return labText;
87 }
88 if (store.getBoolean(WSMOMainPage.PREF_USE_IRI_FULL_NAME)) {
89 return labText;
90 }
91 return getEntityLocalName(labText,
92 store.getBoolean(WSMOMainPage.PREF_USE_SLASH_SEP),
93 nsHolder);
94 }
95
96 public static String getEntityLocalNameForNavigator(String labText, TopEntity nsHolder) {
97 IPreferenceStore store = RuntimePlugin.getDefault().getPreferenceStore();
98 return getEntityLocalName(labText,
99 store.getBoolean(WSMOMainPage.PREF_USE_IRI_FULL_NAME)
100 && false == store.getBoolean(WSMOMainPage.PREF_USE_IRI_LOCAL_NAME_IN_NAVI),
101 store.getBoolean(WSMOMainPage.PREF_USE_SLASH_SEP),
102 nsHolder);
103 }
104
105 public static String getEntityLocalName(String labText,
106 boolean showFullName,
107 boolean useSlashSep,
108 TopEntity nsHolder) {
109 if (labText.equals("_#")) {
110 return labText;
111 }
112 if (showFullName) {
113 return labText;
114 }
115 return getEntityLocalName(labText, useSlashSep, nsHolder);
116 }
117
118 private static String getEntityLocalName(String labText,
119 boolean useSlashSep,
120 TopEntity nsHolder) {
121
122 int delimiterIndex = labText.lastIndexOf('#');
123 if (useSlashSep) {
124 delimiterIndex = Math.max(delimiterIndex, labText.lastIndexOf('/'));
125 }
126
127 if (delimiterIndex != -1
128 && delimiterIndex < labText.length() - 1) {
129 String localName = labText.substring(delimiterIndex+1);
130 if (nsHolder != null) {
131 String prefix = labText.substring(0, delimiterIndex + 1);
132 for(Namespace ns : nsHolder.listNamespaces()) {
133 if (ns.getIRI().toString().equals(prefix)) {
134 return ns.getPrefix() + ":" + localName;
135 }
136 }
137 }
138 return localName;
139 }
140 return labText;
141 }
142
143 public static String getMediatorType(Mediator medi) {
144 if (medi instanceof OOMediator) {
145 return WsmoImageRegistry.OOMEDIATOR_ICON;
146 }
147 if (medi instanceof WWMediator) {
148 return WsmoImageRegistry.WWMEDIATOR_ICON;
149 }
150 if (medi instanceof WGMediator) {
151 return WsmoImageRegistry.WGMEDIATOR_ICON;
152 }
153 return WsmoImageRegistry.GGMEDIATOR_ICON;
154 }
155
156 public static byte getPreferredMediatorType(Entity owner) {
157 if (owner instanceof Ontology) {
158 return WSMOTopEntity.OO_MEDIATOR;
159 }
160 if (owner instanceof Goal) {
161 return WSMOTopEntity.GG_MEDIATOR;
162 }
163 if (owner instanceof WebService) {
164 return WSMOTopEntity.WW_MEDIATOR;
165 }
166 if (owner instanceof OOMediator) {
167 return WSMOTopEntity.OO_MEDIATOR;
168 }
169 if (owner instanceof GGMediator) {
170 return WSMOTopEntity.GG_MEDIATOR;
171 }
172 if (owner instanceof WWMediator) {
173 return WSMOTopEntity.WW_MEDIATOR;
174 }
175 if (owner instanceof WGMediator) {
176 return WSMOTopEntity.WG_MEDIATOR;
177 }
178 return -1;
179 }
180
181 public static boolean isAProxy(Object testEntity) {
182 if (testEntity instanceof Proxy) {
183 InvocationHandler proxyHandler = Proxy.getInvocationHandler(testEntity);
184 if (proxyHandler instanceof IDReference) {
185 return false == ((IDReference)proxyHandler).isResolved();
186 }
187 }
188 return false;
189 }
190
191 public static IEditorInput findInputForModel(ObservableModel model) {
192 IWorkbenchPage page = PlatformUI.getWorkbench()
193 .getActiveWorkbenchWindow()
194 .getActivePage();
195 if (page == null) {
196 return null;
197 }
198 IEditorReference[] editors = page.getEditorReferences();
199 if (editors == null) {
200 return null;
201 }
202 for(int i = 0; i < editors.length; i++) {
203 try {
204 if (model.equals(editors[i].getEditorInput().getAdapter(ObservableModel.class))) {
205 return editors[i].getEditorInput();
206 }
207 }
208 catch(PartInitException pie) {
209 continue;
210 }
211 }
212 return null;
213 }
214
215 public static Set<IEditorInput> findInputsForEntity(Entity entity) {
216 IWorkbenchPage page = PlatformUI.getWorkbench()
217 .getActiveWorkbenchWindow()
218 .getActivePage();
219 Set<IEditorInput> results = new HashSet<IEditorInput>();
220 if (page == null) {
221 return results;
222 }
223 IEditorReference[] editors = page.getEditorReferences();
224 if (editors == null) {
225 return results;
226 }
227 for(int i = 0; i < editors.length; i++) {
228 try {
229 if (entity.equals(editors[i].getEditorInput().getAdapter(Entity.class))) {
230 results.add(editors[i].getEditorInput());
231 }
232 }
233 catch(PartInitException pie) {
234 continue;
235 }
236 }
237 return results;
238 }
239 public static Set<ObservableModel> findModelsForEntity(Entity entity) {
240 Set<IEditorInput> inputs = findInputsForEntity(entity);
241 Set<ObservableModel> results = new HashSet<ObservableModel>();
242 if (inputs == null) {
243 return results;
244 }
245 for(IEditorInput input : inputs) {
246 ObservableModel uiModel =
247 (ObservableModel)input.getAdapter(ObservableModel.class);
248 if (uiModel != null) {
249 results.add(uiModel);
250 }
251 }
252 return results;
253 }
254
255 public static Set<String> getTypesAsString(Set<Type> types) {
256 Set<String> result = new HashSet<String>();
257 for(Type testType : types) {
258 if (testType instanceof WsmlDataType) {
259 result.add(testType.toString());
260 }
261 }
262 if (result.size() == 0) {
263 return null;
264 }
265 return result;
266 }
267
268 public static Set<String> collectAllTypesAsString(Set<Attribute> attrs) {
269 Set<Type> allRanges = new HashSet<Type>();
270 for(Attribute attr : attrs) {
271 allRanges.addAll(attr.listTypes());
272 }
273 return getTypesAsString(allRanges);
274 }
275
276 public static TopEntity findTopContainer(ObservableModel model) {
277 TopEntity owner = (TopEntity)model.getAdapter(TopEntity.class);
278 while (model.getMasterModel() != null) {
279 model = model.getMasterModel();
280 if (model.getAdapter(TopEntity.class) != null) {
281 owner = (TopEntity)model.getAdapter(TopEntity.class);
282 }
283 }
284 return owner;
285 }
286
287 public static void handleImportedOntologies(Ontology onto) {
288 byte importsMode = (byte)
289 RuntimePlugin.getDefault().getPreferenceStore()
290 .getInt(WSMOMainPage.PREF_LOAD_IMPORTS);
291 switch (importsMode) {
292 case WorkspaceLocator.PROMPT_FOR_LOADING_IMPORTS:
293 if (hasNotLoadedImports(onto)
294 && MessageDialog.openConfirm(
295 Display.getCurrent().getActiveShell(),
296 "Load Imported Ontologies",
297 "Ontology '" + onto.getIdentifier().toString()
298 + "' imports ontologies which are currently not loaded. \nLoad them now as well?\n\n"
299 + "Note: this functionality can be controlled by a preferences page option.")) {
300 loadImportedOntologies(onto);
301 }
302 break;
303 case WorkspaceLocator.ALWAYS_LOAD_IMPORTS:
304 loadImportedOntologies(onto);
305 break;
306 default:
307 break;
308 }
309 }
310
311 public static boolean hasNotLoadedImports(TopEntity entity) {
312 if (isAProxy(entity)) {
313 return false;
314 }
315 for (Ontology onto : entity.listOntologies()) {
316 if (isAProxy(onto)) {
317 if (WSMORuntime.getCache()
318 .getFileLocationForId((IRI)onto.getIdentifier(), WSMOTopEntity.ONTOLOGY)
319 != null) {
320 return true;
321 }
322 }
323 else {
324 if (hasNotLoadedImports(onto)) {
325 return true;
326 }
327 }
328 }
329 return false;
330 }
331
332 public static void loadImportedOntologies(TopEntity entity) {
333 doLoadImportedOntologies(entity.listOntologies(), new HashSet<Ontology>());
334 }
335
336 private static void doLoadImportedOntologies(Set<Ontology> ontos, Set<Ontology> visited) {
337 for(Ontology iOnto : ontos) {
338 if (visited.contains(iOnto)) {
339 continue;
340 }
341 else {
342 if (Utils.isAProxy(iOnto)) {
343 WSMORuntime.getIOManager().doLoadOntology(iOnto.getIdentifier(), true);
344 }
345 if (Utils.isAProxy(iOnto) == false) {
346 visited.add(iOnto);
347 doLoadImportedOntologies(iOnto.listOntologies(), visited);
348 }
349 }
350 }
351 }
352
353 }
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449