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.grounding.sawsdl.ui.editor;
26  
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  import java.util.LinkedList;
30  import java.util.List;
31  
32  import org.eclipse.jface.dialogs.*;
33  import org.w3c.dom.*;
34  import org.wsmostudio.runtime.WSMORuntime;
35  import org.wsmostudio.ui.editors.common.WSMOChooser;
36  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
37  
38  import com.ontotext.wsmo4j.grounding.sawsdl.WSDLUtils;
39  
40  public class ViewActionHandler {
41  
42      public static final String ADD_REF_ACTION = "Add Reference";
43      public static final String REMOVE_REF_ACTION = "Remove Reference";
44      
45      /***
46       * Adds a new modelReference to a certain selected element. Only *NOT" presented 
47       * references are added.
48       * @param target - the selected element to be annotated
49       * @param viewer
50       */
51      public static void performAddReference(Element target, WSDLSEditor viewer) {
52  
53      	WSMOChooser chooser = new WSMOChooser(viewer.getSite().getShell(),
54                  WSMORuntime.getRuntime(),
55                  WSMOContentProvider.NO_FILTER);
56          org.wsmo.common.Entity c = (org.wsmo.common.Entity)chooser.open();
57          if (c == null) {
58              return;
59          }
60          performAddReference(target, c.getIdentifier().toString(), viewer);
61      }
62  
63      /***
64       * Adds a new modelReference to a certain selected element. Only *NOT" presented 
65       * references are added.
66       * @param target
67       * @param newRef
68       * @param viewer
69       */
70      public static void performAddReference(Element target, String newRef, WSDLSEditor viewer) {
71  
72          if (WSDLUtils.matchName(
73                          target.getTagName(), 
74                          viewer.getAnnotationManager().getWSDLPrefix(), 
75                          Utils.OPERATION_ELEMENT)
76                  && Utils.isWSDL11(target.getOwnerDocument())) {
77              target = Utils.getAttrExtensionsElement(
78                      target, viewer.getAnnotationManager().getSAWSDLPrefix());
79          }
80  
81          List<String> oldRefs = Utils.retrieveReferences(target, 
82                  viewer.getAnnotationManager().getSAWSDLPrefix());
83          if (oldRefs.contains(newRef)) {
84              return; // this reference is already presented here
85          }
86          if (oldRefs.size() > 0) {
87              newRef = Utils.getReferenceAttribute(target, 
88                      viewer.getAnnotationManager().getSAWSDLPrefix()).getValue()
89                      + ' '
90                      + newRef;
91          }
92          Utils.annotateWithModelRef(target, newRef, viewer);
93      }
94      
95      /***
96       * Updates a certain element's modelReferences annotaton.
97       * @param target - the element to be updated
98       * @param iris - a list of references (null elements indicate removed references)
99       * @param viewer
100      */
101     public static void performUpdateReferences(Element target,
102                                                List<String> iris,
103                                                WSDLSEditor viewer) {
104 
105         Attr targetAttr = Utils.getReferenceAttribute(target, 
106                 viewer.getAnnotationManager().getSAWSDLPrefix());
107         String refsData = WSDLUtils.constructFromIRIs(iris);
108         boolean dirty = false;
109         if (refsData.length() == 0) {
110             if (targetAttr != null) {
111                 target.removeAttributeNode(targetAttr);
112                 dirty = true;
113             }
114         }
115         else {
116             Utils.annotateWithModelRef(target, refsData, viewer);
117             dirty = true;
118         }
119         
120         if (dirty) {
121             viewer.setDirty(dirty);
122             viewer.getUITree().refresh(target, true);
123         }
124     }
125     
126     public static void performEditSchemaRef(Element el, 
127                                             String schemaURI,                                
128                                             boolean isLifting, 
129                                             WSDLSEditor viewer) {
130 
131         String sawsdlPrefix = viewer.getAnnotationManager().getSAWSDLPrefix();
132         InputDialog iDialog = new InputDialog(
133                 viewer.getSite().getShell(), 
134                 ((isLifting) ? "Lifting" : "Lowering") + " Schema Mapping Reference", 
135                 "URI reference :", 
136                 schemaURI, 
137                 new IInputValidator() {
138                     public String isValid(String newText) {
139                         if (newText.trim().length() == 0) {
140                             return "";
141                         }
142                         try {
143                             new URI(newText.trim());
144                         }
145                         catch(URISyntaxException uriError) {
146                             return uriError.getMessage();
147                         }
148                         return null;
149                     }
150             
151         });
152         if (iDialog.open() != Dialog.OK) {
153             return;
154         }
155         String newValue = iDialog.getValue().trim();
156         if (newValue.equals(schemaURI)) {
157             return;
158         }
159         
160         Attr oldAttr = (isLifting) ? Utils.getLiftingAttribute(el, sawsdlPrefix)
161                 : Utils.getLoweringAttribute(el, sawsdlPrefix);
162         
163         List<String> oldValues = (oldAttr != null) ? 
164                 WSDLUtils.splitIRIsFromString(oldAttr.getValue())
165                 : new LinkedList<String>();
166         int insertionPos = oldValues.indexOf(schemaURI); 
167         if (false == oldValues.contains(newValue)) {
168             if (insertionPos != -1) {
169                 oldValues.add(insertionPos, newValue);
170             }
171             else {
172                 oldValues.add(newValue);
173             }
174         }
175         oldValues.remove(schemaURI);
176 
177         String newMappingAll = WSDLUtils.constructFromIRIs(oldValues);
178         if (isLifting) {
179             Utils.annotateWithLiftingRef(el, newMappingAll, viewer);
180         }
181         else {
182             Utils.annotateWithLoweringRef(el, newMappingAll, viewer);
183         }
184         viewer.setDirty(true);
185         viewer.getUITree().refresh(el, true);
186     }
187     
188     public static void performRemoveSchemaRef(Element el, 
189             String schemaURI,                                
190             boolean isLifting, 
191             WSDLSEditor viewer) {
192 
193         String sawsdlPrefix = viewer.getAnnotationManager().getSAWSDLPrefix();
194 
195         Attr oldAttr = (isLifting) ? Utils.getLiftingAttribute(el, sawsdlPrefix)
196                 : Utils.getLoweringAttribute(el, sawsdlPrefix);
197 
198         List<String> oldValues = WSDLUtils.splitIRIsFromString(oldAttr.getValue());
199         oldValues.remove(schemaURI);
200         
201         if (oldValues.size() == 0) {
202             el.removeAttribute(oldAttr.getName());
203         }
204         else {
205             String newMappingAll = WSDLUtils.constructFromIRIs(oldValues);
206             if (isLifting) {
207                 Utils.annotateWithLiftingRef(el, newMappingAll, viewer);
208             }
209             else {
210                 Utils.annotateWithLoweringRef(el, newMappingAll, viewer);
211             }
212         }
213         viewer.setDirty(true);
214         viewer.getUITree().refresh(el, true);
215     }
216 
217 }
218 
219 /*
220  * $Log$
221  * Revision 1.9  2007/05/03 09:42:00  alex_simov
222  * WSDL 11 support improved with attrExtensions for operations
223  *
224  * Revision 1.8  2007/05/02 17:05:58  alex_simov
225  * update (muitliple lifting/lowering schema refs)
226  *
227  * Revision 1.7  2007/04/25 16:53:38  alex_simov
228  * no message
229  *
230  * Revision 1.6  2007/04/17 14:29:15  alex_simov
231  * no message
232  *
233  * Revision 1.5  2006/11/21 09:43:58  alex_simov
234  * The restriction for using only WSMO concepts in the annotation is removed.
235  *
236  * Revision 1.4  2006/10/12 16:25:27  alex_simov
237  * 1) multiple URI references support added
238  * 2) basic lowering/lifting schema support added
239  *
240  * Revision 1.3  2006/09/04 15:23:33  alex_simov
241  * bugfix[1531709]: Context menus relied on right button mouse click instead of
242  * being registered as dedicated context menus for the corresponding UI
243  * controls
244  *
245  * Revision 1.2  2006/07/07 13:02:25  alex_simov
246  * commented code removed
247  *
248  * Revision 1.1  2006/07/05 15:37:29  alex_simov
249  * no message
250  *
251  * Revision 1.3  2006/07/03 15:43:16  alex_simov
252  * no message
253  *
254  * Revision 1.2  2006/06/22 11:49:06  alex_simov
255  * category support added
256  *
257  * Revision 1.1  2006/06/21 12:55:13  alex_simov
258  * no message
259  *
260  */