View Javadoc
1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, 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-2007</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.ui;
26  
27  import java.util.Set;
28  
29  import org.eclipse.core.runtime.Path;
30  import org.eclipse.jface.dialogs.MessageDialog;
31  import org.eclipse.jface.resource.JFaceResources;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.events.MouseAdapter;
34  import org.eclipse.swt.events.MouseEvent;
35  import org.eclipse.swt.graphics.Image;
36  import org.eclipse.swt.graphics.Rectangle;
37  import org.eclipse.swt.widgets.*;
38  import org.eclipse.ui.*;
39  import org.omwg.ontology.*;
40  import org.wsmo.common.*;
41  import org.wsmo.mediator.*;
42  import org.wsmo.service.*;
43  import org.wsmostudio.runtime.*;
44  import org.wsmostudio.runtime.cache.WSMOTopEntity;
45  import org.wsmostudio.ui.editors.WSMOEditorInput;
46  import org.wsmostudio.ui.editors.WSMOEditorLauncher;
47  import org.wsmostudio.ui.editors.model.*;
48  
49  public class GUIHelper {
50  
51      static TableItem doCreateTableItem(Table owner, Object target, Image icon, TopEntity nsHolder) {
52          TableItem item = new TableItem(owner, SWT.None);
53          item.setData(target);
54          item.setImage(icon);
55          String textLabel = null;
56          if (target instanceof Entity) {
57              if (nsHolder != null) {
58                  textLabel = Utils.getEntityLocalName(((Entity)target).getIdentifier().toString(), nsHolder);
59              }
60              else {
61                  textLabel = Utils.getEntityLocalName(((Entity)target).getIdentifier().toString());
62              }
63          }
64          else {
65              textLabel = target.toString();
66          }
67          item.setText(textLabel);
68          return item;
69      }
70  
71      public static TableItem createTableItem(Table owner, Object target, String iconID) {
72          if (iconID == null) {
73              return createTableItem(owner, target, (TopEntity)null);
74          }
75          return doCreateTableItem(owner, target, JFaceResources.getImageRegistry().get(iconID), null);
76      }
77  
78      public static TableItem createTableItem(Table owner, Object target, TopEntity nsHolder) {
79          return doCreateTableItem(owner, target, WsmoImageRegistry.getImageForObject(target), nsHolder);
80      }
81  
82      public static boolean containsCursor(Control owner) {
83      	return owner.getBounds().contains(
84      			owner.getParent().toControl(Display.getCurrent().getCursorLocation()));
85      }
86  
87      public static boolean containsCursor(Rectangle frame, Control owner) {
88      	return frame.contains(
89      			owner.toControl(Display.getCurrent().getCursorLocation()));
90      }
91  
92      public static Combo createWsmlVariantChooser(Composite parent,
93                                                   String selection) {
94          Combo wsmlVariantChooser = new Combo(parent, SWT.READ_ONLY);
95          wsmlVariantChooser.add("");
96          wsmlVariantChooser.add(WSML.WSML_CORE);
97          wsmlVariantChooser.add(WSML.WSML_DL);
98          wsmlVariantChooser.add(WSML.WSML_FLIGHT);
99          wsmlVariantChooser.add(WSML.WSML_FULL);
100         wsmlVariantChooser.add(WSML.WSML_RULE);
101         wsmlVariantChooser.setVisibleItemCount(6);
102         int selIndex = 0;
103         if (selection != null
104                 && wsmlVariantChooser.indexOf(selection) != -1) {
105             selIndex = wsmlVariantChooser.indexOf(selection);
106         }
107         wsmlVariantChooser.select(selIndex);
108         return wsmlVariantChooser;
109     }
110 
111     public static ObservableModel createDefaultModel(Object entity) {
112         if (entity instanceof Ontology) {
113             return new OntologyModel((Ontology)entity);
114         }
115         if (entity instanceof Mediator) {
116             return new MediatorModel((Mediator)entity);
117         }
118         if (entity instanceof ServiceDescription) {
119             return new ServiceDescriptionModel((ServiceDescription)entity);
120         }
121         if (entity instanceof Axiom) {
122             return new AxiomModel((Axiom)entity);
123         }
124         if (entity instanceof Capability) {
125             return new CapabilityModel((Capability)entity);
126         }
127         if (entity instanceof Concept) {
128             return new ConceptModel((Concept)entity);
129         }
130         if (entity instanceof Instance) {
131             return new InstanceModel((Instance)entity);
132         }
133         if (entity instanceof RelationInstance) {
134             return new RelationInstanceModel((RelationInstance)entity);
135         }
136         if (entity instanceof Relation) {
137             return new RelationModel((Relation)entity);
138         }
139         if (entity instanceof Attribute) {
140             return new AttributeModel((Attribute)entity);
141         }
142         if (entity instanceof Interface) {
143             return new InterfaceModel((Interface)entity);
144         }
145         if (entity instanceof TopEntity) {
146             return new TopEntityModel((TopEntity)entity);
147         }
148         if (entity instanceof Entity) {
149             return new EntityModel((Entity)entity);
150         }
151         return new ObservableModel(entity);
152     }
153 
154     public static ObservableModel createEditorModel(Object entity, 
155                                                     ObservableModel masterModel, 
156                                                     String editorID) {
157         ObservableModel result = WsmoUIPlugin
158                                      .getDefault()
159                                          .getExtensionManager()
160                                              .createModelForEditor(
161                                                      editorID, entity, masterModel); 
162         if (result == null) {
163             result = createDefaultModel(entity);
164         }
165         if (masterModel != null) {
166             masterModel.addSubModel((ObservableModel)result);
167         }
168         return result;
169     }
170 
171     public static ObservableModel createEditorModel(Object entity, String editorID) {
172         return createEditorModel(entity, null, editorID);
173     }
174 
175     public static void closeDependentEditors(IWorkbenchPart part) {
176         if (false == part instanceof IEditorPart) {
177             return;
178         }
179         ObservableModel model = 
180             (ObservableModel)((IEditorPart)part).getEditorInput().getAdapter(ObservableModel.class);
181         if (model == null 
182                 || false == model instanceof ObservableModel) {
183             return;
184         }
185         Set<ObservableModel> dependentModels = ((ObservableModel)model).listSubModels(); 
186         
187         IWorkbenchPage page = PlatformUI.getWorkbench()
188                                   .getActiveWorkbenchWindow()
189                                       .getActivePage();
190         if (page == null) {
191             return;
192         }
193         IEditorReference[] editors = page.getEditorReferences();
194         if (editors == null) {
195             return;
196         }
197         for(int i = 0; i < editors.length; i++) {
198             try {
199                 ObservableModel testModel = 
200                     (ObservableModel)editors[i].getEditorInput().getAdapter(ObservableModel.class);
201                 if (dependentModels.contains(testModel)) {
202                     page.closeEditor(editors[i].getEditor(false), false);
203                 }
204             }
205             catch(PartInitException pie) {
206                 continue;
207             }
208         }
209     }
210 
211     public static void showOntoStatistics(Ontology onto, Shell parentShell) {
212         if (Utils.isAProxy(onto)) {
213             WSMORuntime.getIOManager().doLoadOntology(onto.getIdentifier(), false);
214             if (Utils.isAProxy(onto)) {
215                 return;
216             }
217         }
218         StringBuffer content = new StringBuffer();
219         content.append("Ontology Identifier:\n");
220         content.append(onto.getIdentifier().toString());
221         content.append("\n\nAxioms :             \t\t").append(onto.listAxioms().size());
222         content.append("\nConcepts :           \t\t").append(onto.listConcepts().size());
223         content.append("\nInstances :          \t\t").append(onto.listInstances().size());
224         content.append("\nRelations :          \t\t").append(onto.listRelations().size());
225         content.append("\nRelation Instances : \t\t").append(onto.listRelationInstances().size());
226         
227         MessageDialog.openInformation(
228                 parentShell, 
229                 "Ontology Statistics",
230                 content.toString());
231     }
232     
233     public static void registerDoubleClickOpenAction(final Table table, final ObservableModel parentModel) {
234         table.addMouseListener(new MouseAdapter() {
235             public void mouseDoubleClick(MouseEvent e) {
236                 final TableItem[] sel = table.getSelection();
237                 if (sel != null 
238                         && sel.length > 0) {
239                     GUIHelper.openInEditor((Entity)sel[0].getData(), parentModel);
240                 }
241             }
242         });
243 
244     }
245     
246     public static void openInEditor(Entity target) {
247         openInEditor(target, null);
248     }
249     public static void openInEditor(Entity target, ObservableModel parentModel) {
250         if (Utils.isAProxy(target)) {
251             if (target instanceof TopEntity) {
252                 if (target instanceof Ontology) {
253                     WSMORuntime.getIOManager().doLoadOntology(target.getIdentifier());
254                 }
255                 else if (target instanceof Goal) {
256                     WSMORuntime.getIOManager().doLoadGoal(target.getIdentifier());
257                 } 
258                 else if (target instanceof WebService) {
259                     WSMORuntime.getIOManager().doLoadWebService(target.getIdentifier());
260                 } 
261                 else if (target instanceof OOMediator) {
262                     WSMORuntime.getIOManager().doLoadOOMediator(target.getIdentifier());
263                 } 
264                 else if (target instanceof WWMediator) {
265                     WSMORuntime.getIOManager().doLoadWWMediator(target.getIdentifier());
266                 } 
267                 else if (target instanceof WGMediator) {
268                     WSMORuntime.getIOManager().doLoadWGMediator(target.getIdentifier());
269                 } 
270                 else if (target instanceof GGMediator) {
271                     WSMORuntime.getIOManager().doLoadGGMediator(target.getIdentifier());
272                 }
273                 
274                 if (Utils.isAProxy(target)) {
275                     MessageDialog.openWarning(
276                             Display.getCurrent().getActiveShell(), 
277                             "External Definition", 
278                             "Unable to locate definition of '" + target.getIdentifier().toString() 
279                             + "' in workspace!");
280                     return;
281                 }
282             }
283             else {
284                 MessageDialog.openWarning(
285                         Display.getCurrent().getActiveShell(), 
286                         "External Definition", 
287                         "The selected entity (" + target.getIdentifier().toString() 
288                         + ") has no local definition and can not be editted!");
289                 return;
290             }
291         }
292 
293         if (target instanceof TopEntity
294                 && false == target instanceof Interface
295                 && false == target instanceof Capability) {
296 
297             String filePath = WSMORuntime.getCache().getFileLocationForId(
298                     (IRI)target.getIdentifier(), 
299                     WSMOTopEntity.getEntityType((TopEntity)target));
300             if (filePath == null) {
301                 MessageDialog.openWarning(
302                         Display.getCurrent().getActiveShell(), 
303                         "External Definition", 
304                         "Unable to locate definition of '" + target.getIdentifier().toString() 
305                         + "' in workspace!");
306                 return;
307             }
308             new WSMOEditorLauncher().open(new Path(filePath));
309             return;
310         }
311         try {
312             String targetEditorID = WsmoUIPlugin.getDefault()
313                 .getExtensionManager()
314                     .locateEditorForEntity(target);
315             if (targetEditorID == null) { // ignore unknown entities
316                 return;
317             }
318 
319             Set<IEditorInput> openedModels = Utils.findInputsForEntity(target);
320             IEditorInput input = null;
321             if (openedModels.size() > 0) {
322                 for(IEditorInput eInput : openedModels) {
323                     if (eInput.getAdapter(ObservableModel.class) != null) {
324                         ModelUtils.notifySubModels((ObservableModel)
325                                 eInput.getAdapter(ObservableModel.class));
326                         input = eInput;
327                     }
328                 }
329             }
330             if (input == null) {
331                 if (target instanceof OntologyElement) {
332                     Ontology onto = ((OntologyElement)target).getOntology();
333                     
334                     if (onto == null) {
335                         MessageDialog.openWarning(
336                                 Display.getCurrent().getActiveShell(), 
337                                 "External Definition", 
338                                 "The selected entity (" + target.getIdentifier().toString() 
339                                 + ") has no local definition and can not be editted!");
340                         return;
341                     }
342                     
343                     Set<IEditorInput> openedOntoModels = 
344                             Utils.findInputsForEntity(onto);
345                     boolean modelFound = false;
346                     for(IEditorInput eInput : openedOntoModels) {
347                         if (eInput.getAdapter(ObservableModel.class) != null) {
348                             parentModel = (ObservableModel)
349                                 eInput.getAdapter(ObservableModel.class);
350                             modelFound = true;
351                             break;
352                         }
353                     }
354                     if (false == modelFound) {
355                         openInEditor(onto);
356                     }
357                     openedOntoModels = Utils.findInputsForEntity(onto);
358                     for(IEditorInput eInput : openedOntoModels) {
359                         if (eInput.getAdapter(ObservableModel.class) != null) {
360                             parentModel = (ObservableModel)
361                                 eInput.getAdapter(ObservableModel.class);
362                             break;
363                         }
364                     }
365                 }
366                 input = new WSMOEditorInput(
367                         target,
368                         createEditorModel(target, parentModel, targetEditorID));
369             }
370 
371             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
372                 .openEditor(input, targetEditorID);
373         }
374         catch(PartInitException e) {
375             LogManager.logError(e);
376         }
377     }
378 
379 }
380 /*
381  * $Log$
382  * Revision 1.1  2007/07/19 12:58:16  alex_simov
383  * refactoring
384  *
385  */