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.ui.editors.common;
26
27
28 import java.util.Set;
29
30 import org.omwg.ontology.*;
31
32 import org.eclipse.swt.widgets.*;
33 import org.eclipse.swt.layout.*;
34 import org.eclipse.swt.SWT;
35
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.graphics.Point;
38 import org.eclipse.swt.events.*;
39 import org.eclipse.jface.dialogs.MessageDialog;
40 import org.eclipse.jface.resource.JFaceResources;
41
42 import org.wsmostudio.runtime.*;
43 import org.wsmostudio.ui.Utils;
44
45 /***
46 * An editor component specialized in creation and modification of DataValue
47 * objects (part of the WSMO API). A restricted functionality version of it
48 * can be used as a WsmlDataType chooser.
49 * This editor is designed as a common purpose component which appears as a
50 * standalone modal window on top of the main studio workbench.
51 *
52 * @author not attributable
53 * @version $Revision: 1.12 $ $Date: 2006/01/09 12:51:13 $
54 */
55
56 public class DataValueEditor {
57
58 private DataValue inputValue, resultValue;
59 private WsmlDataType resultType;
60 private Combo predefTypeSelector;
61
62 private Text valueField;
63
64
65 private boolean usedAsTypeSelector = false;
66
67 static String[] types = new String[] {
68 WsmlDataType.WSML_STRING,
69 WsmlDataType.WSML_DECIMAL,
70 WsmlDataType.WSML_INTEGER,
71 WsmlDataType.WSML_FLOAT,
72 WsmlDataType.WSML_DOUBLE,
73 WsmlDataType.WSML_IRI,
74 WsmlDataType.WSML_SQNAME,
75 WsmlDataType.WSML_BOOLEAN,
76 WsmlDataType.WSML_DURATION,
77 WsmlDataType.WSML_DATETIME,
78 WsmlDataType.WSML_TIME,
79 WsmlDataType.WSML_DATE,
80 WsmlDataType.WSML_GYEARMONTH,
81 WsmlDataType.WSML_GYEAR,
82 WsmlDataType.WSML_GMONTHDAY,
83 WsmlDataType.WSML_GDAY,
84 WsmlDataType.WSML_GMONTH,
85 WsmlDataType.WSML_HEXBINARY,
86 WsmlDataType.WSML_BASE64BINARY
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 };
107
108 private Shell shell;
109
110 private boolean dirty = false;
111
112 public static WsmlDataType createTypeSelector(Shell parent) {
113 DataValueEditor dve = new DataValueEditor(parent, null, true, null);
114 dve.open();
115 return dve.getResultType();
116 }
117
118 public static DataValue createEditor(Shell parent, Set<String> typeRestriction) {
119 return createEditor(parent, null, typeRestriction);
120 }
121 public static DataValue createEditor(Shell parent) {
122 return createEditor(parent, null, null);
123 }
124
125 public static DataValue createEditor(Shell parent, DataValue input, Set<String> typeRestriction) {
126 DataValueEditor dve = new DataValueEditor(parent, input, false, typeRestriction);
127 dve.open();
128 return dve.getResultValue();
129 }
130
131 private DataValueEditor(Shell parent,
132 DataValue value,
133 boolean isTypeSelector,
134 Set<String> allowedTypes) {
135 shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.SYSTEM_MODAL);
136 this.inputValue = value;
137 this.usedAsTypeSelector = isTypeSelector;
138 Image icon = JFaceResources.getImageRegistry().get(WsmoImageRegistry.DATA_VALUE_ICON);
139 shell.setImage(icon);
140 shell.setText("Data Value Editor");
141 shell.setLayout(new FillLayout(SWT.VERTICAL));
142
143 Composite mainContainer = new Composite(shell, SWT.EMBEDDED);
144 createDialogArea(mainContainer, allowedTypes);
145 createControlArea(mainContainer);
146
147 if (value != null) {
148 initEditorContent(value);
149 }
150 else {
151 dirty = true;
152 }
153 shell.pack();
154
155 Point pLocation = parent.getLocation();
156 Point pSize = parent.getSize();
157 shell.setLocation(pLocation.x + pSize.x / 2 - shell.getSize().x / 2,
158 pLocation.y + pSize.y / 2 - shell.getSize().y / 2);
159 }
160
161 private void createDialogArea(Composite comp, Set<String> typeRestr) {
162 comp.setLayout(new GridLayout(1, false));
163
164 Group typeSelectorContainer = new Group(comp, SWT.NONE);
165 typeSelectorContainer.setLayout(new GridLayout(2, false));
166 typeSelectorContainer.setText("Data Type");
167 typeSelectorContainer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
168
169 new Label(typeSelectorContainer, SWT.NONE).setText("Available types : ");
170
171
172
173
174
175
176
177
178
179
180
181 predefTypeSelector = new Combo(typeSelectorContainer, SWT.READ_ONLY);
182
183 String[] allowedTypes = (typeRestr != null) ?
184 typeRestr.toArray(new String[typeRestr.size()])
185 : types;
186
187 predefTypeSelector.setItems(allowedTypes);
188 predefTypeSelector.select(0);
189 if (allowedTypes.length < 2) {
190 predefTypeSelector.setEnabled(false);
191 }
192 predefTypeSelector.addSelectionListener(new SelectionAdapter() {
193 public void widgetSelected(SelectionEvent e) {
194 dirty = true;
195 }
196 });
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217 if (false == usedAsTypeSelector) {
218 Composite inputContainer = new Composite(comp, SWT.NONE);
219 inputContainer.setLayout(new GridLayout(2, false));
220 inputContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
221 new Label(inputContainer, SWT.NONE).setText("Value: ");
222
223 valueField = new Text(inputContainer, SWT.BORDER);
224 valueField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
225 valueField.addModifyListener(new ModifyListener() {
226 public void modifyText(ModifyEvent me) {
227 dirty = true;
228 }
229 });
230 }
231 }
232
233 private void createControlArea(Composite mainContainer) {
234 Composite buttons = new Composite(mainContainer, SWT.EMBEDDED);
235 buttons.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
236 FillLayout fLayout = new FillLayout(SWT.HORIZONTAL);
237 fLayout.spacing = 10;
238
239 buttons.setLayout(fLayout);
240
241 Button okButton = new Button(buttons, SWT.PUSH);
242 okButton.setText("OK");
243 okButton.addSelectionListener(new SelectionAdapter() {
244 public void widgetSelected(SelectionEvent e) {
245 if (dirty) {
246 try {
247 doUpdate();
248 }
249 catch(Exception ex) {
250 MessageDialog.openError(shell,
251 "Invalid Data",
252 ex.getClass().getName()
253 + " : "
254 + ex.getMessage());
255 return;
256 }
257 }
258 shell.dispose();
259 }
260 });
261 shell.setDefaultButton(okButton);
262
263 Button noButton = new Button(buttons, SWT.PUSH);
264 noButton.setText("Cancel");
265 noButton.addSelectionListener(new SelectionAdapter() {
266 public void widgetSelected(SelectionEvent e) {
267 shell.dispose();
268 }
269 });
270
271 }
272
273 public void open() {
274 shell.open();
275 Display display = shell.getDisplay();
276 while (!shell.isDisposed()) {
277 if (!display.readAndDispatch()) {
278 display.sleep();
279 }
280 }
281 if (!dirty) {
282 resultType = inputValue.getType();
283 resultValue = inputValue;
284 }
285 }
286
287 protected WsmlDataType getResultType() {
288 return resultType;
289 }
290
291 protected DataValue getResultValue() {
292 return resultValue;
293 }
294
295 public void dispose() {
296 }
297
298 private void doUpdate() throws Exception {
299 String resultDataType;
300
301 resultDataType = predefTypeSelector.getItem(
302 predefTypeSelector.getSelectionIndex());
303
304
305
306
307 if (resultDataType == null
308 || resultDataType.trim().length() == 0) {
309 throw new Exception("Data type is not specified!");
310 }
311
312 if (true == usedAsTypeSelector) {
313 resultType = WSMORuntime.getRuntime()
314 .getDataFactory()
315 .createWsmlDataType(resultDataType);
316 }
317 else {
318 String value = valueField.getText();
319 if (value == null
320 || value.trim().length() == 0) {
321 throw new Exception("No data value is specified!");
322 }
323 resultValue = Utils.createDataValue("_\"" + resultDataType + "\"("+value+")");
324 }
325 }
326
327 private void initEditorContent(DataValue input) {
328 String typeAsString = input.getType().toString();
329 boolean isPredefinedType = false;
330 String[] selectorTypes = predefTypeSelector.getItems();
331 for(int i = 0; i < types.length; i++) {
332 if (selectorTypes[i].equals(typeAsString)) {
333 predefTypeSelector.select(i);
334 isPredefinedType = true;
335 break;
336 }
337 }
338 if (false == isPredefinedType) {
339 predefTypeSelector.add(typeAsString);
340 predefTypeSelector.select(predefTypeSelector.getItemCount()-1);
341 }
342
343
344
345
346
347
348
349
350 String strValue = input.toString();
351 if (strValue.startsWith("_")) {
352 int valStart = strValue.indexOf("(");
353 int valEnd = strValue.lastIndexOf(")");
354 if (valStart > 0
355 && valEnd > 0
356 && valStart < valEnd) {
357 strValue = strValue.substring(valStart+1, valEnd);
358 }
359 }
360 valueField.setText(strValue);
361 dirty = false;
362 }
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397