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.wizards;
26  
27  import org.eclipse.jface.fieldassist.*;
28  import org.eclipse.jface.resource.JFaceResources;
29  import org.eclipse.jface.viewers.IStructuredSelection;
30  import org.eclipse.jface.wizard.IWizardPage;
31  import org.eclipse.jface.wizard.WizardPage;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.events.*;
34  import org.eclipse.swt.graphics.Image;
35  import org.eclipse.swt.layout.GridData;
36  import org.eclipse.swt.layout.GridLayout;
37  import org.eclipse.swt.widgets.*;
38  import org.omwg.ontology.Ontology;
39  import org.semanticgov.ui.Activator;
40  import org.wsmo.common.IRI;
41  import org.wsmostudio.runtime.*;
42  import org.wsmostudio.runtime.cache.WSMOTopEntity;
43  import org.wsmostudio.ui.editors.common.IWSMOSelectionValidator;
44  import org.wsmostudio.ui.editors.common.WSMOChooser;
45  import org.wsmostudio.ui.views.navigator.WSMOContentProvider;
46  
47  public class NewPAServiceIDPage extends WizardPage {
48  
49      IWizardPage previousPage;
50      Composite mainPanel;
51      DecoratedField decIdField, decDefNSField; 
52      FieldDecoration idFieldDecor, defNSFieldDecor; 
53      Text idField, defNSField, ontoIDField;
54      
55      private Ontology selectedReferenceOntology;
56  
57      private String errorMsg = null;
58      
59      
60      public NewPAServiceIDPage(IStructuredSelection sel) {
61          super("Create a new Public Administration service");
62      }
63      
64      public boolean canFlipToNextPage() {
65          return false;
66      }
67  
68      public String getName() {
69          return "Create a new Public Administration service";
70      }
71  
72      public IWizardPage getNextPage() {
73          return null;
74      }
75  
76      public IWizardPage getPreviousPage() {
77          return previousPage;
78      }
79  
80      public boolean isPageComplete() {
81          return errorMsg == null 
82                  && idField.getText().trim().length() > 0
83                  && super.isPageComplete();
84      }
85  
86      public void setPreviousPage(IWizardPage page) {
87          previousPage = page;
88      }
89  
90      public void createControl(Composite parent) {
91          mainPanel = new Composite(parent, SWT.NONE);
92          mainPanel.setLayout(new GridLayout(1, false));
93          
94          Group serviceNamePane = new Group(mainPanel, SWT.NONE);
95          serviceNamePane.setText("New Service");
96          serviceNamePane.setLayout(new GridLayout(1, false));
97          serviceNamePane.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
98          
99          new Label(serviceNamePane, SWT.NONE).setText("Service Identifier :");
100         
101         FieldDecoration standardError = FieldDecorationRegistry.getDefault()
102             .getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
103         
104         decIdField = new DecoratedField(serviceNamePane, SWT.BORDER, new TextControlCreator());
105         idFieldDecor = new FieldDecoration(standardError.getImage(), "");
106         decIdField.addFieldDecoration(idFieldDecor, SWT.LEFT, false);
107         decIdField.hideDecoration(idFieldDecor);
108         idField = (Text)decIdField.getControl(); 
109         
110         decIdField.getLayoutControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
111         idField.addModifyListener(new ModifyListener() {
112             public void modifyText(ModifyEvent e) {
113                 validateInput(idField.getText());
114             }
115         });
116         
117         new Label(serviceNamePane, SWT.NONE).setText("Default Namespace :");
118         
119         decDefNSField = new DecoratedField(serviceNamePane, SWT.BORDER, new TextControlCreator());
120         defNSFieldDecor = new FieldDecoration(standardError.getImage(), "");
121         decDefNSField.addFieldDecoration(defNSFieldDecor, SWT.LEFT, false);
122         decDefNSField.hideDecoration(defNSFieldDecor);
123         defNSField = (Text)decDefNSField.getControl();
124 
125         decDefNSField.getLayoutControl().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126         defNSField.addModifyListener(new ModifyListener() {
127             public void modifyText(ModifyEvent e) {
128                 validateInput(idField.getText());
129             }
130         });
131 
132         Group instOntoPanel = new Group(mainPanel, SWT.NONE);
133         instOntoPanel.setText("Uses Ontology");
134         instOntoPanel.setLayout(new GridLayout(2, false));
135         instOntoPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
136         
137         Label lab = new Label(instOntoPanel, SWT.NONE);
138         lab.setText("Public Service reference ontology : ");
139         GridData gd = new GridData();
140         gd.horizontalSpan = 2;
141         lab.setLayoutData(gd);
142         
143         ontoIDField = new Text(instOntoPanel, SWT.BORDER);
144         ontoIDField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
145         
146         
147         Button chooseOntoButton = new Button(instOntoPanel, SWT.NONE);
148         chooseOntoButton.setText("Select");
149         chooseOntoButton.setImage(JFaceResources.getImage(WsmoImageRegistry.ONTOLOGY_ICON));
150         chooseOntoButton.addSelectionListener(new SelectionAdapter() {
151             public void widgetSelected(SelectionEvent event) {
152                 doSelectOntology();
153             }
154         });
155         
156         setControl(mainPanel);
157     }
158     
159     private void doSelectOntology() {
160         WSMOChooser chooser = new WSMOChooser(getShell(),
161                 WSMORuntime.getRuntime(), 
162                 WSMOContentProvider.ONTOLOGY_MASK);
163         chooser.setFilter(new IWSMOSelectionValidator() {
164             public String isValid(Object tested) {
165                 if (tested instanceof Ontology) {
166                     return null;
167                 }
168                 return "An Ontology selection expected!";
169             }
170         });
171         Ontology newOntology = (Ontology)chooser.open();
172         if (newOntology != null) {
173             this.selectedReferenceOntology = newOntology;
174             ontoIDField.setText(newOntology.getIdentifier().toString());
175         }
176     }
177 
178     public void dispose() {
179         mainPanel.dispose();
180         super.dispose();
181     }
182 
183     public String getErrorMessage() {
184         return errorMsg;
185     }
186 
187     public Image getImage() {
188         return Activator.getSGLogo().createImage();    
189     }
190 
191     public String getMessage() {
192         return "Create a new Public Administration service";
193     }
194 
195     public String getTitle() {
196         return "New Public Administration Service";
197     }
198     
199     private void validateInput(String id) {
200 
201         String idError = null;
202         String nsError = null;
203         
204         if (id.trim().length() == 0) {
205             idError = "ID field is empty.";
206         }
207         else {
208             int errPos = IRIUtils.validateIRI(id);
209             if (errPos != -1) {
210                 idError = "Invalid character in ID ('" 
211                     + id.charAt(errPos) 
212                     + "') at position " 
213                     + errPos;
214             }
215         }
216 
217         if (defNSField.getText().trim().length() == 0) {
218             nsError = "No default namespace defined.";
219         }
220         else {
221             try {
222                 WSMORuntime.getRuntime().getWsmoFactory().createIRI(defNSField.getText().trim());
223             }
224             catch(Exception ex) {
225                 nsError = "Invalid namespace:" + ex.getMessage();
226             }
227 
228         }
229         
230         if (idError == null && nsError == null) {        
231             try {
232                 if (id.indexOf(":") == -1) {
233                     String nsText = defNSField.getText().trim();
234                     if (false == nsText.endsWith("#")
235                             && false == nsText.endsWith("/")) {
236                         nsText += "#";
237                     }
238                     id = nsText + id;
239                 }
240                 IRI uri = WSMORuntime.getRuntime().getWsmoFactory().createIRI(id);
241                 byte oldType = WSMORuntime.getCache().getTypeByIRI(uri);
242                 if (oldType != -1) {
243                     idError = "Duplicate identifier '" 
244                         + id 
245                         + "' - used for " 
246                         + WSMOTopEntity.getTypeAsString(oldType);
247                 }
248             }
249             catch(Exception e) {
250                 idError = e.getMessage();
251             }
252         }
253 
254         if (idError != null) {
255             errorMsg = idError;
256         }
257         else {
258             errorMsg = nsError;
259         }
260         
261         decorateField(idError, decIdField, idFieldDecor);
262         decorateField(nsError, decDefNSField, defNSFieldDecor);
263         
264         getWizard().getContainer().updateMessage();
265         getWizard().getContainer().updateButtons();
266     }
267     
268     private void decorateField(String error, DecoratedField field, FieldDecoration decor) {
269         if (error == null) {
270             field.hideDecoration(decor);
271         }
272         else {
273             decor.setDescription(error);
274             field.showDecoration(decor);
275         }
276     }
277     
278     public String getInputIdentifier() {
279         if (errorMsg != null) {
280             return null;
281         }
282         return idField.getText().trim();
283     }
284     
285     public Ontology getReferenceOntology() {
286         return this.selectedReferenceOntology;
287     }
288 
289     public String getDefaultNamespace() {
290         if (errorMsg != null) {
291             return null;
292         }
293         String result = defNSField.getText().trim();
294         if (false == result.endsWith("#")
295                 && false == result.endsWith("/")) {
296             result += "#";
297         }
298         return result;
299     }
300 
301 }
302 
303 /*
304  * $Log: NewPAServiceIDPage.java,v $
305  * Revision 1.3  2007/02/21 14:39:03  alex_simov
306  * instance ontology specification removed from the wizard page
307  *
308  * Revision 1.2  2007/01/22 17:25:35  alex_simov
309  * no message
310  *
311  * Revision 1.1  2007/01/12 16:30:29  alex_simov
312  * PA Service editor initial version
313  *
314  */