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.semanticgov.ui.editor;
26  
27  import java.util.*;
28  
29  import org.eclipse.jface.dialogs.MessageDialog;
30  import org.eclipse.jface.window.Window;
31  import org.eclipse.swt.SWT;
32  import org.eclipse.swt.widgets.*;
33  import org.eclipse.ui.PartInitException;
34  import org.eclipse.ui.PlatformUI;
35  import org.omwg.ontology.*;
36  import org.semanticgov.ui.Constants;
37  import org.wsmo.common.IRI;
38  import org.wsmo.common.Identifier;
39  import org.wsmo.factory.WsmoFactory;
40  import org.wsmostudio.runtime.*;
41  import org.wsmostudio.ui.*;
42  import org.wsmostudio.ui.editors.WSMOEditorInput;
43  import org.wsmostudio.ui.editors.common.IWSMOSelectionValidator;
44  import org.wsmostudio.ui.editors.common.WSMOChooser;
45  import org.wsmostudio.ui.editors.model.AxiomModel;
46  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
47  
48  public class ActionHandler {
49  
50      public static final String ADD_SOCIETAL_ENTITY = "Add Client Type";
51      public static final String REMOVE_CLIENT = "Remove Client Type";
52  
53      public static final String ADD_SERVICE_PROCESS = "Add Process";
54      public static final String REMOVE_SERVICE_PROCESS = "Remove Process";
55  
56      public static final String ADD_SERVICE_PROVIDER = "Add Provider";
57      public static final String REMOVE_SERVICE_PROVIDER = "Remove Provider";
58  
59      public static final String ADD_INPUT = "Add Input";
60      public static final String REMOVE_INPUT = "Remove Input";
61      public static final String ADD_OUTPUT = "Add Output";
62      public static final String REMOVE_OUTPUT = "Remove Output";
63  
64      public static final String ADD_PRECOND = "Add Precondition";
65      public static final String EDIT_PRECOND = "Edit Precondition";
66      public static final String REMOVE_PRECOND = "Remove Precondition";
67  
68      public static final String ADD_EFFECT = "Add Effect";
69      public static final String EDIT_EFFECT = "Edit Effect";
70      public static final String REMOVE_EFFECT = "Remove Effect";
71  
72      private PAServiceModel model;
73      
74      ActionHandler(PAServiceModel uiModel) {
75          this.model = uiModel;
76      }
77      
78      public void performAction(String action, Table receiver) {
79          if (action.equals(ADD_SOCIETAL_ENTITY)) {
80              IRI instID = selectInstance(Constants.SOCIETAL_ENTITY, 
81                      "Select Client Type");
82              if (instID != null) {
83                  this.model.addClientType(instID);
84              }
85              return;
86          }
87          if (action.equals(REMOVE_CLIENT)) {
88              TableItem[] items = receiver.getSelection();
89              if (items != null && items.length > 0) {
90                  this.model.removeClientType((IRI)items[0].getData());
91              }
92              return;
93          }
94  
95          if (action.equals(ADD_SERVICE_PROVIDER)) {
96              IRI instID = selectInstance(Constants.PROVIDER, 
97                      "Select Provider");
98              if (instID != null) {
99                  this.model.addServiceProvider(instID);
100             }
101             return;
102         }
103         if (action.equals(REMOVE_SERVICE_PROVIDER)) {
104             TableItem[] items = receiver.getSelection();
105             if (items != null && items.length > 0) {
106                 this.model.removeServiceProvider((IRI)items[0].getData());
107             }
108             return;
109         }
110 
111         if (action.equals(ADD_SERVICE_PROCESS)) {
112             IRI instID = selectInstance(Constants.PROCESS, 
113                     "Select Process Instance");
114             if (instID != null) {
115                 this.model.addServiceProcess(instID);
116             }
117             return;
118         }
119         if (action.equals(REMOVE_SERVICE_PROCESS)) {
120             TableItem[] items = receiver.getSelection();
121             if (items != null && items.length > 0) {
122                 this.model.removeServiceProcess((IRI)items[0].getData());
123             }
124             return;
125         }
126 
127         if (action.equals(ADD_INPUT)) {
128             IRI instID = selectInstance(Constants.EVIDENCE_PLACEHOLDER, 
129                     "Select Input");
130             if (instID != null) { 
131                 this.model.addInput(instID);
132             }
133             return;
134         }
135         if (action.equals(ADD_OUTPUT)) {
136             IRI instID = selectInstance(Constants.EVIDENCE_PLACEHOLDER, 
137                     "Select Output");
138             if (instID != null) { 
139                 this.model.addOutput(instID);
140             }
141             return;
142         }
143         if (action.equals(REMOVE_INPUT)) {
144             TableItem[] items = receiver.getSelection();
145             if (items != null && items.length > 0) {
146                 this.model.removeInput((IRI)items[0].getData());
147             }
148             return;
149         }
150         if (action.equals(REMOVE_OUTPUT)) {
151             TableItem[] items = receiver.getSelection();
152             if (items != null && items.length > 0) {
153                 this.model.removeOutput((IRI)items[0].getData());
154             }
155             return;
156         }
157         if(action.equals(ADD_PRECOND)) {
158             WsmoFactory wsmoFactory = WSMORuntime.getRuntime().getWsmoFactory();
159             IdentifierInputDialog iDialog = new IdentifierInputDialog(
160                     Display.getCurrent().getActiveShell(), 
161                     "New Precondition Axiom", 
162                     "Name (leave blank for anonymous): ",
163                     model.getTopEntity(),
164                     wsmoFactory, 
165                     true);
166             if (iDialog.open() != Window.OK) {
167                 return;
168             }
169             Identifier axiomRef = iDialog.getIdentifier();
170             Axiom newAxiom = wsmoFactory.createAxiom(axiomRef);
171             model.addPrecondition(newAxiom);
172             return;
173         }
174         if(action.equals(ADD_EFFECT)) {
175             WsmoFactory wsmoFactory = WSMORuntime.getRuntime().getWsmoFactory();
176             IdentifierInputDialog iDialog = new IdentifierInputDialog(
177                     Display.getCurrent().getActiveShell(), 
178                     "New Effect Axiom", 
179                     "Name (leave blank for anonymous): ",
180                     model.getTopEntity(),
181                     wsmoFactory, 
182                     true);
183             if (iDialog.open() != Window.OK) {
184                 return;
185             }
186             Identifier axiomRef = iDialog.getIdentifier();
187             Axiom newAxiom = wsmoFactory.createAxiom(axiomRef);
188             model.addEffect(newAxiom);
189             return;
190         }
191         if (action.equals(REMOVE_PRECOND)) {
192             TableItem[] items = receiver.getSelection();
193             if (items != null && items.length > 0) {
194                 Axiom precond = (Axiom)items[0].getData();
195                 model.removePrecondition(precond);
196             }
197             return;
198         }
199         if (action.equals(REMOVE_EFFECT)) {
200             TableItem[] items = receiver.getSelection();
201             if (items != null && items.length > 0) {
202                 Axiom effect = (Axiom)items[0].getData();
203                 model.removeEffect(effect);
204             }
205             return;
206         }
207         if (action.equals(EDIT_EFFECT) 
208                 || action.equals(EDIT_PRECOND)) {
209             doEditAxiom(receiver);
210             return;
211         }
212 
213 
214     MessageDialog.openWarning(Display.getCurrent().getActiveShell(), 
215             "Error","Action Not Implemented - " + action);
216     }
217     
218     public void doEditAxiom(Table receiver) {
219         TableItem[] sel = receiver.getSelection();
220         if (sel == null || sel.length == 0) {
221             return;
222         }
223         Axiom axiom = (Axiom)sel[0].getData();
224         String targetEditorID = WsmoUIPlugin.getDefault()
225                                     .getExtensionManager()
226                                         .locateEditorForEntity(axiom);
227         AxiomModel axiomUIModel = (AxiomModel)
228                 GUIHelper.createEditorModel(axiom, 
229                         model, 
230                         targetEditorID);
231 
232         WSMOEditorInput input = new WSMOEditorInput(
233                                         axiom,
234                                         axiomUIModel);
235         try {
236             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
237                 .openEditor(input, targetEditorID);
238         }
239         catch(PartInitException pie) {
240             LogManager.logError(pie);
241         }
242         
243     }
244 
245     
246     private IRI selectInstance(IRI concept, String dialogTitle) {
247         
248         if (model.getServiceDescription().listOntologies().size() == 0) {
249             MessageDialog.openError(Display.getCurrent().getActiveShell(), 
250                                     "No Ontology", 
251                                     "No Reference imported ontology specified!");
252             return null;
253         }
254         
255         WSMOContentProvider provider = new WSMOContentProvider(WSMOContentProvider.INSTANCE_MASK 
256         		| WSMOContentProvider.ONTOLOGY_MASK 
257         		| WSMOContentProvider.CONCEPT_MASK) {
258    
259         	@SuppressWarnings("unchecked")
260 			protected Object[] createConceptContent(Concept superConcept) {
261         		Set<Concept> subConceptsSet = new HashSet<Concept>();
262         		for(Concept subConcept : superConcept.listSubConcepts()) {
263         			if (subConcept.getOntology() != null) {
264         				subConceptsSet.add(subConcept);
265         			}
266         		}
267         		Object[] subConcepts = subConceptsSet.toArray();
268         		int resultLength = subConcepts.length;
269         		Set<Instance> instancesSet = new HashSet<Instance>();
270         		for(Instance instance : superConcept.listInstances()) {
271         			if (instance.getOntology() != null) {
272         				instancesSet.add(instance);
273         			}
274         		}
275         		Object[] instances = instancesSet.toArray();
276         		resultLength += instances.length;
277 
278         		Object[] result = new Object[resultLength];
279         		if (subConcepts.length > 0) {
280         			Arrays.sort(subConcepts, entityComparator);
281         			System.arraycopy(subConcepts, 0, result, 0, subConcepts.length);
282         		}
283         		if (instances.length > 0) {
284         			Arrays.sort(instances, entityComparator);
285         			System.arraycopy(instances, 0, result, subConcepts.length, instances.length);
286         		}
287         		return result;
288         	}
289         };
290 
291         WSMOChooser chooser = new WSMOChooser(Display.getCurrent().getActiveShell(),
292         		SWT.SINGLE,
293         		WSMORuntime.getRuntime().getWsmoFactory().getConcept(concept), 
294         		provider);
295        
296         chooser.setFilter(new IWSMOSelectionValidator() {
297             public String isValid(Object selection) {
298                 if (false == selection instanceof Instance) {
299                     return "Please select a Instance";
300                 }
301                 return null; // i.e. no error
302             }
303         });
304         chooser.setDialogTitle(dialogTitle);
305         chooser.expandTree();
306 
307         Instance inst = (Instance)chooser.open();
308         if (inst == null) { // operation has been cancelled
309             return null;
310         }
311         return (inst.getIdentifier() instanceof IRI) ? 
312                 (IRI)inst.getIdentifier() : null;
313     }
314     
315 }
316 
317 /*
318  * $Log: ActionHandler.java,v $
319  * Revision 1.5  2007/07/19 12:50:20  alex_simov
320  * refactoring
321  *
322  * Revision 1.4  2007/01/26 14:11:29  alex_simov
323  * no message
324  *
325  * Revision 1.3  2007/01/23 16:23:34  alex_simov
326  * no message
327  *
328  * Revision 1.2  2007/01/22 17:25:35  alex_simov
329  * no message
330  *
331  * Revision 1.1  2007/01/12 16:30:28  alex_simov
332  * PA Service editor initial version
333  *
334  */