1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260