View Javadoc
1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, Ontotext Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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)) { //force loading
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  * $Log$
357  * Revision 1.28  2007/07/19 12:58:16  alex_simov
358  * refactoring
359  *
360  * Revision 1.27  2007/04/17 14:35:32  alex_simov
361  * migration to the latest wsmo4j (java 5)
362  *
363  * Revision 1.26  2007/02/19 14:45:30  alex_simov
364  * bugfix [1663285]: NFP editor shows property names as :
365  * <namespace_prefix>:<local name> (if a suitable namespece is defined).
366  * System NFPs (having wsmostudio prefix) are read-only and coloured differently
367  *
368  * Revision 1.25  2007/02/13 15:53:15  alex_simov
369  * bugfix[1312971]: The WSMO Text editor synchronizes the wsmo object
370  * model automatically with the other editors (if possible) or issues a warning
371  * message otherwise.
372  *
373  * Revision 1.24  2006/11/24 16:54:03  alex_simov
374  * ui improvements
375  *
376  * Revision 1.23  2006/11/20 17:15:02  alex_simov
377  * no message
378  *
379  * Revision 1.22  2006/11/02 17:19:11  alex_simov
380  * no message
381  *
382  * Revision 1.21  2006/10/12 16:28:10  alex_simov
383  * no message
384  *
385  * Revision 1.20  2006/08/01 11:21:12  alex_simov
386  * bugfix[1531709]: Context menus relied on right button mouse click instead of
387  * being registered as dedicated context menus for the corresponding UI
388  * controls
389  *
390  * Revision 1.19  2006/07/11 16:11:47  alex_simov
391  * no message
392  *
393  * Revision 1.18  2006/05/25 09:23:18  alex_simov
394  * New preferences options added allowing IRI vizualization customization
395  *  related to showing full/local names in the UI components
396  *
397  * Revision 1.17  2006/04/07 15:23:58  alex_simov
398  * rfe[1465647]: Ontology statistics action added to WSMO Navigator
399  * (available also in WSMO choosers showing ontologies)
400  *
401  * Revision 1.16  2006/04/06 12:56:05  alex_simov
402  * new utility method added getPreferredMediatorType()
403  *
404  * Revision 1.15  2006/02/20 12:42:13  alex_simov
405  * collectAllTypesAsString() added
406  *
407  * Revision 1.14  2006/01/25 12:59:57  alex_simov
408  * UI fix: identifier label set to empty string when full identifier ends with hash sign
409  *
410  * Revision 1.13  2006/01/09 12:51:13  alex_simov
411  * Copyright message in header updated
412  *
413  * Revision 1.12  2005/12/21 15:29:39  alex_simov
414  * new method added: findTopContainer()
415  *
416  * Revision 1.11  2005/12/06 09:39:24  alex_simov
417  * converter types-to-strings method added
418  *
419  * Revision 1.10  2005/12/01 13:51:07  alex_simov
420  * UI fix
421  *
422  * Revision 1.9  2005/11/25 14:25:07  alex_simov
423  * wsmo4j proxies support method added
424  *
425  * Revision 1.8  2005/11/10 13:42:07  alex_simov
426  * update
427  *
428  * Revision 1.7  2005/11/09 16:12:16  alex_simov
429  * UIModels support added
430  *
431  * Revision 1.6  2005/09/27 14:00:17  alex_simov
432  * reflecting wsmo4j refectoring
433  *
434  * Revision 1.5  2005/09/19 15:10:58  alex_simov
435  * createDataValue() moved from WSMLAnalizer
436  *
437  * Revision 1.4  2005/09/16 14:25:11  alex_simov
438  * Identifier.asString() removed, use Object.toString() instead
439  *
440  * Revision 1.3  2005/09/14 14:48:21  alex_simov
441  * wsml variants UI support added
442  *
443  * Revision 1.2  2005/09/10 09:17:21  alex_simov
444  * bugfix
445  *
446  * Revision 1.1  2005/08/08 12:20:54  alex_simov
447  * new utility class
448  *
449  */