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.wizards;
26  
27  
28  import org.eclipse.core.resources.*;
29  import org.eclipse.core.runtime.*;
30  import org.eclipse.jface.resource.JFaceResources;
31  import org.eclipse.jface.viewers.IStructuredSelection;
32  import org.eclipse.jface.wizard.*;
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.events.*;
35  import org.eclipse.swt.graphics.Image;
36  import org.eclipse.swt.layout.*;
37  import org.eclipse.swt.widgets.*;
38  import org.wsmo.common.IRI;
39  import org.wsmostudio.runtime.*;
40  import org.wsmostudio.runtime.cache.WSMOTopEntity;
41  import org.wsmostudio.ui.*;
42  
43  /***
44   * This page represents an essential part of the new WSMO object wizards.
45   * It is the UI component in which the user supplies an identifier for the
46   * object to be created. The input is validated runtime for duplication 
47   * and syntax errors.
48   *
49   * @author not attributable
50   * @version $Revision: 1453 $ $Date: 2008-03-04 13:34:35 +0200 $
51   */
52  
53  public class NewWsmoIDPage extends WizardPage {
54  
55      IWizardPage previousPage;
56      Composite mainPanel;
57      protected Text idField, defNSField;
58      protected Combo wsmlVariantChooser;
59      private String errorMsg = null;
60      private String wizardTargetType;
61      
62      private Label fnLab1, fnLab2;
63      private String previewString = null; 
64      
65      public NewWsmoIDPage(String wsmoName, IStructuredSelection sel) {
66          super("Create a new "+wsmoName);
67          wizardTargetType = wsmoName;
68      }
69      
70      public boolean canFlipToNextPage() {
71          return false;
72      }
73  
74      public String getName() {
75          return "Create a new "+wizardTargetType;
76      }
77  
78      public IWizardPage getNextPage() {
79          return null;
80      }
81  
82      public IWizardPage getPreviousPage() {
83          return previousPage;
84      }
85  
86      public boolean isPageComplete() {
87          return errorMsg == null 
88                  && idField.getText().trim().length() > 0
89                  && super.isPageComplete();
90      }
91  
92      public void setPreviousPage(IWizardPage page) {
93          previousPage = page;
94      }
95  
96      public void createControl(Composite parent) {
97          mainPanel = new Composite(parent, SWT.NONE);
98          mainPanel.setLayout(new GridLayout(1, false));
99          
100         new Label(mainPanel, SWT.NONE).setText("New "+wizardTargetType+" Identifier :");
101         
102         idField = new Text(mainPanel, SWT.BORDER);
103         idField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
104         idField.addModifyListener(new ModifyListener() {
105             public void modifyText(ModifyEvent e) {
106                 validateInput(idField.getText());
107             }
108         });
109         
110         new Label(mainPanel, SWT.NONE).setText("Default Namespace :");
111         
112         defNSField = new Text(mainPanel, SWT.BORDER);
113         defNSField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
114         defNSField.addModifyListener(new ModifyListener() {
115             public void modifyText(ModifyEvent e) {
116                 validateInput(idField.getText());
117             }
118         });
119 
120         Composite wsmlVarPanel = new Composite(mainPanel, SWT.NONE);
121         wsmlVarPanel.setLayout(new GridLayout(2, false));
122         wsmlVarPanel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
123         new Label(wsmlVarPanel, SWT.NONE).setText("WSML Variant : ");
124         wsmlVariantChooser = GUIHelper.createWsmlVariantChooser(wsmlVarPanel, null);
125         wsmlVariantChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126         
127         new Label(mainPanel, SWT.NONE);// separator
128         fnLab1 = new Label(mainPanel, SWT.NONE);
129         fnLab1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
130         fnLab2 = new Label(mainPanel, SWT.NONE);
131         fnLab2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
132         fnLab2.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
133         
134         setControl(mainPanel);
135     }
136 
137     public void dispose() {
138         mainPanel.dispose();
139         wsmlVariantChooser.dispose();
140         super.dispose();
141     }
142 
143     public String getErrorMessage() {
144         return null;//errorMsg;
145     }
146 
147     public Image getImage() {
148         return JFaceResources.getImageRegistry().get(WsmoImageRegistry.WSMO_LOGO_SMALL); 	
149     }
150 
151     public String getMessage() {
152         if (errorMsg != null) {
153             return errorMsg;
154         }
155         return "Create a new " + wizardTargetType.toLowerCase();
156     }
157 
158     public String getTitle() {
159         return "New " + wizardTargetType;
160     }
161     
162     private void validateInput(String id) {
163 
164         if (id.trim().length() == 0) {
165             errorMsg = "ID field is empty.";
166         }
167         else if (defNSField.getText().trim().length() == 0) {
168             errorMsg = "No default namespace defined.";
169         }
170         else {
171             int errPos = IRIUtils.validateIRI(id);
172             if (errPos != -1) {
173                 errorMsg = "Invalid character in ID ('" + id.charAt(errPos) + "') at position " + errPos;
174             }
175             else {
176                 try {
177                     if (id.indexOf(":") == -1) {
178                         String nsText = defNSField.getText().trim();
179                         if (false == nsText.endsWith("#")
180                                 && false == nsText.endsWith("/")) {
181                             nsText += "#";
182                         }
183                         id = nsText + id;
184                     }
185                     IRI uri = WSMORuntime.getRuntime().getWsmoFactory().createIRI(id);
186                     byte oldType = WSMORuntime.getCache().getTypeByIRI(uri);
187                     if (oldType != -1) {
188                         errorMsg = "Duplicate identifier '" 
189                             + id 
190                             + "' - used for " 
191                             + WSMOTopEntity.getTypeAsString(oldType);
192                     }
193                     else {
194                         errorMsg = null; // no errors
195                     }
196                     
197                     try {
198                         WSMORuntime.getRuntime().getWsmoFactory().createIRI(defNSField.getText().trim());
199                     }
200                     catch(Exception ex) {
201                         errorMsg = "Invalid namespace:" + ex.getMessage();
202                     }
203                 }
204                 catch(Exception e) {
205                     errorMsg = e.getMessage();
206                 }
207             }
208         }
209         if (getPreviousPage() instanceof WizardNewWSMLFileCreationPage) {
210             WizardNewWSMLFileCreationPage filenamePage = 
211                 (WizardNewWSMLFileCreationPage)getPreviousPage();
212             if (filenamePage.useIRIasFilename()) {
213                 fnLab1.setText("Generated filename preview : ");
214                 if (errorMsg == null) {
215                     String fnTemp = 
216                         IRIUtils.createFilenameFromIRI(getDefaultNamespace(), getInputIdentifier());
217                     
218                     if (fnTemp != null) {
219                         previewString = fnTemp;
220                         fnLab2.setText(fnTemp);
221                         fnLab2.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
222                     }
223                     IPath testPath = filenamePage.getContainerFullPath().append(previewString);
224                     if (ResourcesPlugin.getWorkspace().getRoot().getFile(testPath).exists()) {
225                         errorMsg = "File '" + previewString + "' already exists";
226                     }
227                 }
228                 else {
229                     previewString = null;
230                     fnLab2.setText(" < no preview > ");
231                     fnLab2.setForeground(Display.getCurrent().getSystemColor(SWT.DEFAULT));
232                 }
233             }
234         }
235         getWizard().getContainer().updateMessage();
236         getWizard().getContainer().updateButtons();
237     }
238     
239     public String getInputIdentifier() {
240         if (errorMsg != null) {
241             return null;
242         }
243         return idField.getText().trim();
244     }
245 
246     public String getDefaultNamespace() {
247         if (errorMsg != null) {
248             return null;
249         }
250         String result = defNSField.getText().trim();
251         if (false == result.endsWith("#")
252                 && false == result.endsWith("/")) {
253             result += "#";
254         }
255         return result;
256     }
257     
258     public String getWsmlVariant() {
259         if (wsmlVariantChooser.getSelectionIndex() > 0) {
260             return wsmlVariantChooser.getText();
261         }
262         return null;
263     }
264     
265     public String getGeneratedFilename() {
266         return this.previewString;
267     }
268     
269     public void setVisible(boolean visible) {
270         if (visible == true 
271                 && getPreviousPage() != null
272                 && getPreviousPage() instanceof WizardNewWSMLFileCreationPage) {
273             IPath selectedPath = ((WizardNewWSMLFileCreationPage)getPreviousPage()).getContainerFullPath();
274             IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(selectedPath);
275             String nsValue = "";
276             if (res != null) {
277                 IProject containerProject = res.getProject();
278                 if (containerProject != null) {
279                     try {
280                         String nsVal = containerProject.getPersistentProperty(
281                                 ProjectNamespaceAction.PROJECT_NAMESPACE_NAME);
282                         if (nsVal != null) {
283                             nsValue = nsVal;
284                         }
285                     }
286                     catch(CoreException coreEx) {}
287                 }
288             }
289             defNSField.setText(nsValue);
290             validateInput(idField.getText());
291         }
292         else {
293             fnLab1.setText("");
294             fnLab2.setText("");
295         }
296         super.setVisible(visible);
297     }
298 
299 }
300 
301 /*
302  * $Log$
303  * Revision 1.12  2007/07/19 13:08:32  alex_simov
304  * - rfe [1751326] Navigation through the editor tab
305  * - concepts/attributes which are from a different namespace are prefixed
306  *
307  * Revision 1.11  2006/04/13 13:27:20  alex_simov
308  * RFE[1418614]: new WSMO wizards support filename generation from
309  * supplied entity identifier (IRI)
310  *
311  * Revision 1.10  2006/02/20 12:50:00  alex_simov
312  * ui update
313  *
314  * Revision 1.9  2006/02/07 11:40:22  alex_simov
315  * rfe[1423241]: WSMO projects have a default namespace property which
316  * facilitates new entities creation
317  *
318  * Revision 1.8  2006/01/09 12:51:14  alex_simov
319  * Copyright message in header updated
320  *
321  * Revision 1.7  2005/12/21 15:30:27  alex_simov
322  * default namespace support fix
323  *
324  * Revision 1.6  2005/09/14 14:48:21  alex_simov
325  * wsml variants UI support added
326  *
327  * Revision 1.5  2005/07/29 15:08:04  alex_simov
328  * added javadoc: class description, footer
329  *
330  *
331  */