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.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  //    private Text dataTypeField;
62      private Text valueField;
63  //    private Button predefButton, otherTypeButton;
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          DataFactory.XSD_STRING,
89          DataFactory.XSD_DECIMAL,
90          DataFactory.XSD_FLOAT,
91          DataFactory.XSD_DOUBLE,
92          DataFactory.XSD_ANYURI,
93          DataFactory.XSD_QNAME,
94          DataFactory.XSD_BOOLEAN,
95          DataFactory.XSD_DURATION,
96          DataFactory.XSD_DATETIME,
97          DataFactory.XSD_TIME,
98          DataFactory.XSD_DATE,
99          DataFactory.XSD_GYEARMONTH,
100         DataFactory.XSD_GYEAR,
101         DataFactory.XSD_GMONTHDAY,
102         DataFactory.XSD_GDAY,
103         DataFactory.XSD_GMONTH,
104         DataFactory.XSD_HEXBINARY,
105         DataFactory.XSD_BASE64BINARY*/
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 /*        predefButton = new Button(typeSelectorContainer, SWT.RADIO);
171         predefButton.setText("Use predefined: ");
172         predefButton.setSelection(true);
173         predefButton.addSelectionListener(new SelectionAdapter() {
174             public void widgetSelected(SelectionEvent e) {
175                 predefTypeSelector.setEnabled(predefButton.getSelection());
176                 dataTypeField.setEnabled(!predefButton.getSelection());
177                 dirty = true;
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 /*        otherTypeButton = new Button(typeSelectorContainer, SWT.RADIO);
199         otherTypeButton.setText("Other type: ");
200         otherTypeButton.addSelectionListener(new SelectionAdapter() {
201             public void widgetSelected(SelectionEvent e) {
202                 dataTypeField.setEnabled(otherTypeButton.getSelection());
203                 predefTypeSelector.setEnabled(!otherTypeButton.getSelection());
204                 dirty = true;
205             }
206         });
207         
208         dataTypeField = new Text(typeSelectorContainer, SWT.SINGLE | SWT.BORDER);
209         dataTypeField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
210         dataTypeField.setEnabled(false);
211         dataTypeField.addModifyListener(new ModifyListener() {
212             public void modifyText(ModifyEvent me) {
213                 dirty = true;
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 //        if (predefTypeSelector.getEnabled()) {
301             resultDataType = predefTypeSelector.getItem(
302                     predefTypeSelector.getSelectionIndex());
303 //        }
304 //        else {
305 //            resultDataType = dataTypeField.getText();
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 //        if (false == isPredefinedType) {
343 //            dataTypeField.setText(typeAsString);
344 //        }
345 //        predefButton.setSelection(isPredefinedType);
346 //        otherTypeButton.setSelection(!isPredefinedType);
347 //        dataTypeField.setEnabled(!isPredefinedType);
348 //        predefTypeSelector.setEnabled(isPredefinedType);
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  * $Log: DataValueEditor.java,v $
367  * Revision 1.12  2006/01/09 12:51:13  alex_simov
368  * Copyright message in header updated
369  *
370  * Revision 1.11  2005/12/06 09:41:20  alex_simov
371  * 1) DataValue editor made aware of type restriction depending on usage
372  * 2) unsupported datatypes functionality disabled (commented)
373  *
374  * Revision 1.10  2005/12/02 15:55:56  alex_simov
375  * bigfix: string values not constructed correctly
376  *
377  * Revision 1.9  2005/09/27 14:00:18  alex_simov
378  * reflecting wsmo4j refectoring
379  *
380  * Revision 1.8  2005/09/21 15:09:11  alex_simov
381  * minor fixes
382  *
383  * Revision 1.7  2005/09/19 15:13:41  alex_simov
384  * UI fixes;
385  * DataFactory is taken from  WSMORuntime
386  *
387  * Revision 1.6  2005/09/10 09:17:21  alex_simov
388  * bugfix
389  *
390  * Revision 1.5  2005/09/09 13:52:20  alex_simov
391  * value creation bug fixed
392  *
393  * Revision 1.4  2005/07/29 15:08:02  alex_simov
394  * added javadoc: class description, footer
395  *
396  *
397  */