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.choreography.editors;
26  
27  import java.util.Set;
28  
29  import org.deri.wsmo4j.choreography.signature.WSDLGroundingRI;
30  import org.eclipse.jface.action.*;
31  import org.eclipse.jface.dialogs.Dialog;
32  import org.eclipse.jface.resource.JFaceResources;
33  import org.eclipse.jface.viewers.*;
34  import org.eclipse.jface.window.Window;
35  import org.eclipse.swt.SWT;
36  import org.eclipse.swt.graphics.Image;
37  import org.eclipse.swt.layout.*;
38  import org.eclipse.swt.widgets.*;
39  import org.omwg.ontology.Concept;
40  import org.wsmo.common.*;
41  import org.wsmo.factory.ChoreographyFactory;
42  import org.wsmo.service.signature.*;
43  import org.wsmostudio.choreography.ChoreographyPlugin;
44  import org.wsmostudio.choreography.editors.model.ChoreographyModel;
45  import org.wsmostudio.runtime.*;
46  import org.wsmostudio.ui.*;
47  import org.wsmostudio.ui.editors.common.WSMOChooser;
48  
49  
50  
51  /***
52   * A common purpose GUI component, designed to maintain a set of WSMO-API Ontology
53   * references. It is a sub-component of all WSMO editors whose target input objects
54   * have an "importsOntology" property. The supported user operation are inclusion and
55   * exclusion of references. 
56   *
57   * @author not attributable
58   * @version $Revision: 1224 $ $Date: 2007-07-19 15:54:57 +0300 $
59   */
60  
61  public class SigModesContainer 
62  implements ITreeContentProvider, ILabelProvider {
63      
64      public static final int IN_MODES = 1;
65      public static final int OUT_MODES = 2;
66      public static final int SHARED_MODES = 3;
67      public static final int STATIC_MODES = 4;
68      public static final int CONTROLLED_MODES = 5;
69      
70      private TreeViewer modesTable;
71      
72      private ChoreographyModel uiModel;
73      private int modesType;
74      
75      public SigModesContainer(TabFolder parentFolder,
76              ChoreographyModel model,
77              int modesType) {
78          
79          this.modesType = modesType;
80          this.uiModel = model;
81          
82          TabItem mainItem = new TabItem(parentFolder, SWT.NONE);
83          
84          parentFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
85          mainItem.setText(labelForMode(modesType));
86          parentFolder.setLayout(new GridLayout(1, false));
87          
88          createTable(parentFolder);
89          createContextMenu();
90          
91          mainItem.setControl(modesTable.getControl());
92      }
93      
94      public void reloadModes() {
95          modesTable.setInput(uiModel.getStateSignature());
96          modesTable.getTree().setBackground(
97                  Display.getCurrent().getSystemColor(
98                          (uiModel.getStateSignature() == null) ?
99                                  SWT.COLOR_WIDGET_BACKGROUND
100                                 : SWT.COLOR_LIST_BACKGROUND));
101         modesTable.getTree().setToolTipText(
102                 (uiModel.getStateSignature() == null) ?
103                         "No StateSignature defined. Use context menu to create one!" : null);
104         this.modesTable.refresh(true);
105     }
106 
107     private void createTable(Composite parent) {
108         
109         modesTable = new TreeViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
110         modesTable.setContentProvider(this);
111         modesTable.setLabelProvider(this);
112         modesTable.setInput(uiModel.getStateSignature());
113         
114         modesTable.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
115         modesTable.expandAll();
116         modesTable.getTree().setBackground(
117                 Display.getCurrent().getSystemColor(
118                         (uiModel.getStateSignature() == null) ?
119                                 SWT.COLOR_WIDGET_BACKGROUND
120                                 : SWT.COLOR_LIST_BACKGROUND));
121         modesTable.getTree().setToolTipText(
122                 (uiModel.getStateSignature() == null) ?
123                         "No StateSignature defined. Use context menu to create one!" : null);
124     }
125 
126     private Set<?> listModesOfType(int modeType) {
127         switch(modeType) {
128             case IN_MODES:
129                 return uiModel.getStateSignature().listInModes();
130             case OUT_MODES:
131                 return uiModel.getStateSignature().listOutModes();
132             case SHARED_MODES:
133                 return uiModel.getStateSignature().listSharedModes();
134             case STATIC_MODES:
135                 return uiModel.getStateSignature().listStaticModes();
136             case CONTROLLED_MODES:
137                 return uiModel.getStateSignature().listControlledModes();
138             default:
139                 return null;
140         }
141     }
142        
143     private String labelForMode(int mode) {
144         switch(mode) {
145             case IN_MODES:
146                 return "IN modes";
147             case OUT_MODES:
148                 return "OUT modes";
149             case SHARED_MODES:
150                 return "SHARED modes";
151             case STATIC_MODES:
152                 return "STATIC modes";
153             case CONTROLLED_MODES:
154                 return "CONTROLLED modes";
155             default:
156                 return "";
157         }
158     }
159     
160     private void createContextMenu() {
161     	final MenuManager menuMgr = new MenuManager();
162     	menuMgr.setRemoveAllWhenShown(true);
163     	menuMgr.addMenuListener(new IMenuListener() {
164     		public void menuAboutToShow(IMenuManager mgr) {
165     			fillContextMenu(menuMgr);
166     		}
167     	});
168     	modesTable.getTree().setMenu(menuMgr.createContextMenu(modesTable.getTree()));
169     }
170 
171     private void fillContextMenu(MenuManager menuMgr) {
172 
173         if (false == GUIHelper.containsCursor(modesTable.getTree())) {
174             return;
175     	}
176         if (uiModel.getStateSignature() == null) {
177             menuMgr.add(new Action("Create State Signature") {
178     		    public void run() {
179     			    handleCreateStateSignature();
180     		    }
181     	    });
182             return;
183         }
184     	
185         TreeItem[] sel = modesTable.getTree().getSelection();
186         Object selection = null;
187         if (sel != null 
188                 && sel.length > 0 
189                 && GUIHelper.containsCursor(sel[0].getBounds(), modesTable.getTree())) {
190             selection = sel[0].getData();
191         }
192         final Object selectedObject = selection;
193         
194         if (selectedObject != null 
195         		&& selectedObject instanceof Grounding) {
196             menuMgr.add(new Action("Remove Grounding") {
197     		    public void run() {
198     			    handleRemoveGrounding((Grounding)selectedObject);
199     		    }
200     	    });
201             return;
202         }
203 
204         menuMgr.add(new Action("Add Mode") {
205 		    public void run() {
206 			    handleAddMode();
207 		    }
208 	    });
209         if (selectedObject == null) {
210             return; // no more actions needed
211         }
212         
213         if (selectedObject instanceof GroundedMode) {
214             menuMgr.add(new Action("Add Grounding") {
215     		    public void run() {
216     			    handleAddGrounding();
217     		    }
218     	    });
219         }
220 
221         menuMgr.add(new Separator());
222         menuMgr.add(new Action("Remove Mode") {
223 		    public void run() {
224 			    handleRemoveMode((Mode)selectedObject);
225 		    }
226 	    });
227 
228     }
229 
230     public void dispose() {
231     }
232     
233     private void handleAddMode() {
234         WSMOChooser chooser = WSMOChooser.createConceptChooser(
235                 modesTable.getTree().getShell(),
236                 WSMORuntime.getRuntime());
237         
238         Concept concept = (Concept)chooser.open();
239         if (concept == null) {
240             return;
241         }
242         Mode newMode = createNewMode(concept, modesType);
243         if (newMode == null) {
244             return;
245         }
246         uiModel.addMode(newMode);
247     }
248     private void handleAddGrounding() {
249         IdentifierInputDialog iDialog = new IdentifierInputDialog(
250                 modesTable.getTree().getShell(), 
251                 "New Grounding",
252                 "Grounding IRI :",
253                 null, // no namespaces
254                 WSMORuntime.getRuntime().getWsmoFactory(),
255                 false);
256         if (iDialog.open() != Window.OK) {
257             return;
258         }
259         IRI groundingID = (IRI)iDialog.getIdentifier();
260         GroundedMode gMode = (GroundedMode)((IStructuredSelection)modesTable.getSelection()).getFirstElement();
261         uiModel.addGrounding(gMode, new WSDLGroundingRI(groundingID));
262     }
263     private void handleRemoveMode(Mode selected) {
264         uiModel.removeMode(selected);
265     }
266     private void handleRemoveGrounding(Grounding selected) {
267         GroundedMode mode = (GroundedMode)modesTable.getTree().getSelection()[0].getParentItem().getData();
268         uiModel.removeGrounding(mode, selected);
269     }
270     private void handleCreateStateSignature() {
271         IdentifierInputDialog iriInputDialog = new IdentifierInputDialog(
272                 modesTable.getTree().getShell(),
273                 "New State Signature",
274                 "New State Signature Identifier :",
275                 Utils.findTopContainer(uiModel),
276                 WSMORuntime.getRuntime().getWsmoFactory(),
277                 true);
278         if (iriInputDialog.open() != Dialog.OK) {
279             return;
280         }
281         Identifier sSigID = iriInputDialog.getIdentifier();
282         ChoreographyFactory fact = ChoreographyPlugin.getFactory();
283         StateSignature sSig = fact.containers.createStateSignature(sSigID);
284 
285         uiModel.getChoreography().setStateSignature(sSig);
286         uiModel.setChanged();
287     }
288     
289     public static Mode createNewMode(Concept concept, int modesType) {
290         ChoreographyFactory fact = ChoreographyPlugin.getFactory();
291         switch(modesType) {
292             case IN_MODES:
293                 return fact.modes.createIn(concept);
294             case OUT_MODES:
295                 return fact.modes.createOut(concept);
296             case SHARED_MODES:
297                 return fact.modes.createShared(concept);
298             case STATIC_MODES:
299                 return fact.modes.createStatic(concept);
300             case CONTROLLED_MODES:
301                 return fact.modes.createControlled(concept);
302         }
303         return null;
304     }
305 
306 // ILabelProvider implementation
307     public Image getImage(Object object) {
308         if (object instanceof WSDLGrounding) {
309             return JFaceResources.getImageRegistry().get(WsmoImageRegistry.IRI_ICON);
310         }
311         if (object instanceof Mode) {
312             switch(modesType) {
313                 case IN_MODES:
314                     return JFaceResources.getImageRegistry().get(ChoreographyPlugin.IN_MODE_ICON);
315                 case OUT_MODES:
316                     return JFaceResources.getImageRegistry().get(ChoreographyPlugin.OUT_MODE_ICON);
317                 case SHARED_MODES:
318                     return JFaceResources.getImageRegistry().get(ChoreographyPlugin.SHARED_MODE_ICON);
319                 case STATIC_MODES:
320                     return JFaceResources.getImageRegistry().get(ChoreographyPlugin.STATIC_MODE_ICON);
321                 case CONTROLLED_MODES:
322                     return JFaceResources.getImageRegistry().get(ChoreographyPlugin.CONTROLLED_MODE_ICON);
323             }
324         }
325         return WsmoImageRegistry.getImageForObject(object);
326     }
327     
328     public String getText(Object object) {
329         if (object instanceof WSDLGrounding) {
330             return ((WSDLGrounding)object).getIRI().toString();
331         }
332         if (object instanceof Mode) {
333             return Utils.getEntityLocalName(
334                     ((Mode)object).getConcept().getIdentifier().toString(),
335                     Utils.findTopContainer(this.uiModel));
336         }
337         return object.toString();
338     }
339     
340     public void addListener(ILabelProviderListener l) {}
341     public void removeListener(ILabelProviderListener l) {}
342     public boolean isLabelProperty(Object o, String s) {
343         return false;
344     }
345     
346 //  ITreeContentProvider inplementation start
347     
348     public Object[] getChildren(Object parentElement) {
349         if (parentElement == null) {
350             return null;
351         }
352         if (parentElement instanceof StateSignature) {
353             return listModesOfType(modesType).toArray();
354         }
355         if (parentElement instanceof GroundedMode) {
356             try {
357                 return ((GroundedMode)parentElement).getGrounding().toArray();
358             }
359             catch(NotGroundedException nge) {
360                 return null;
361             }
362         }
363         return null;
364     }
365     
366     public Object getParent(Object element) {
367         return null;
368     }
369     
370     public boolean hasChildren(Object element) {
371         if (element == null) {
372             return false;
373         }
374         if (element instanceof StateSignature) {
375             return true;
376         }
377         if (element instanceof Mode) {
378             try {
379                 return element instanceof GroundedMode 
380                 && ((GroundedMode)element).getGrounding().size() > 0;
381             }
382             catch(NotGroundedException nge) {
383                 return false;// i.e. no children 
384             }
385         }
386         return false;
387     }
388     
389     public Object[] getElements(Object inputElement) {
390         return getChildren(inputElement);
391     }
392     
393     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
394     }
395 }
396 
397 /*
398  * $Log$
399  * Revision 1.16  2007/07/19 12:52:44  alex_simov
400  * refactoring in WSMO UI plug-in
401  *
402  * Revision 1.15  2007/02/22 16:29:11  alex_simov
403  * *** empty log message ***
404  *
405  * Revision 1.14  2006/11/22 14:11:06  alex_simov
406  * Code refactoring: the plug-in is synchronized with the latest wsmo4j changes
407  *
408  * Revision 1.13  2006/08/02 12:31:57  alex_simov
409  * bugfix[1531709]: Context menus relied on right button mouse click instead of
410  * being registered as dedicated context menus for the corresponding UI
411  * controls
412  *
413  * Revision 1.12  2006/04/19 13:31:14  alex_simov
414  * UI adjustments
415  *
416  * Revision 1.11  2006/04/18 07:17:20  alex_simov
417  * This component is now capable to handle choreographies without
418  * StateSignature
419  *
420  * Revision 1.10  2006/01/09 12:51:11  alex_simov
421  * Copyright message in header updated
422  *
423  * Revision 1.9  2005/12/19 15:35:54  alex_simov
424  * update
425  *
426  * Revision 1.8  2005/12/16 12:44:43  alex_simov
427  * Plugin activator contains a shared ChoreographyFactory instance
428  *
429  * Revision 1.7  2005/11/16 16:21:22  alex_simov
430  * choreography editor enriched with rules management support
431  *
432  * Revision 1.6  2005/11/16 09:49:03  alex_simov
433  * ui fix
434  *
435  * Revision 1.5  2005/11/10 13:33:24  alex_simov
436  * UIModels support added
437  *
438  * Revision 1.4  2005/11/03 13:39:31  alex_simov
439  * context menu actions addition
440  *
441  * Revision 1.3  2005/11/02 14:51:23  alex_simov
442  * moving to MVC architecture cont'd
443  *
444  * Revision 1.2  2005/11/02 08:43:11  alex_simov
445  * update
446  *
447  * Revision 1.1  2005/10/27 11:15:08  alex_simov
448  * initial plugin commit
449  *
450  *
451  */