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  
26  package org.wsmostudio.ui;
27  
28  import java.util.*;
29  
30  import org.eclipse.jface.dialogs.IInputValidator;
31  import org.eclipse.jface.dialogs.InputDialog;
32  import org.eclipse.swt.SWT;
33  import org.eclipse.swt.events.SelectionAdapter;
34  import org.eclipse.swt.events.SelectionEvent;
35  import org.eclipse.swt.layout.*;
36  import org.eclipse.swt.widgets.*;
37  import org.wsmo.common.*;
38  import org.wsmo.factory.WsmoFactory;
39  import org.wsmostudio.runtime.*;
40  
41  /***
42   * A specialized InputDialog intended to take wsmo identifiers user input.
43   *
44   * @author not attributable
45   * @version $Revision: 1453 $ $Date: 2008-03-04 13:34:35 +0200 $
46   */
47  
48  public class IdentifierInputDialog extends InputDialog {
49  
50      private WsmoFactory factory;
51      private TopEntity nsHolder;
52      private static String selectedNSName = null;
53      private Combo nsChooser;
54      private Button useNS;
55      private static final String DEF_NS_LABEL = "<default>";
56      
57      private static IInputValidator customInputValidator = null;
58      private String originalInitValue;
59      
60      /*
61       * Constructs IdentifierInputDialog without initial value and without custom input validator
62       */
63      public IdentifierInputDialog(Shell parentShell, 
64              String dialogTitle,
65              String dialogMessage,
66              TopEntity namespacesContainer,
67              final WsmoFactory factory,
68              final boolean allowAnonymous) {
69          this(parentShell, dialogTitle, dialogMessage, null, namespacesContainer, factory, allowAnonymous, null);
70      }
71  
72      /*
73       * Constructs IdentifierInputDialog without without custom input validator
74       */
75      public IdentifierInputDialog(Shell parentShell, 
76              String dialogTitle,
77              String dialogMessage,
78              String initValue,
79              final TopEntity namespacesContainer,
80              final WsmoFactory factory,
81              final boolean allowAnonymous) {
82          this(parentShell, dialogTitle, dialogMessage, initValue, namespacesContainer, factory, allowAnonymous, null);
83      }
84  
85      public IdentifierInputDialog(Shell parentShell, 
86                                   String dialogTitle,
87                                   String dialogMessage,
88                                   String initValue,
89                                   final TopEntity namespacesContainer,
90                                   final WsmoFactory factory,
91                                   final boolean allowAnonymous,
92                                   final IInputValidator customValidator) {
93          super(parentShell, 
94                dialogTitle,
95                dialogMessage, 
96                getNamespacedLocalName(initValue,namespacesContainer),  
97                new IInputValidator() {
98                    public String isValid(String newText) {
99                        if (newText.trim().length() == 0) {
100                           return (allowAnonymous) ? null : "";
101                       }
102                       if (newText.trim().equals("_#")
103                               && allowAnonymous == false) {
104                           return "Anonymous ID is not allowed here";
105                       }
106                       int errorPos = IRIUtils.validateIRI(newText);
107                       if (errorPos != -1) {
108                           return "Invalid character '"
109                                   + newText.charAt(errorPos) 
110                                   + "' at position "
111                                   + errorPos
112                                   + ".";
113                       }
114                       try {
115                           Namespace ns = null;
116                           if (selectedNSName != null) {
117                               ns = (selectedNSName.equals(DEF_NS_LABEL)) ?
118                                       namespacesContainer.getDefaultNamespace()
119                                       : namespacesContainer.findNamespace(selectedNSName);
120                           }
121                           Identifier testID = makeIDFromString(namespacesContainer, ns, newText, factory);
122                           if (testID == null) {
123                               return "Invalid identifier";
124                           }
125                       }
126                       catch(Exception ex) {
127                           return ex.getMessage();
128                       }
129                       if (customInputValidator != null) {
130                            return customInputValidator.isValid(newText);
131                       }
132                       return null;
133                   }
134               });
135         customInputValidator = customValidator;
136         this.nsHolder = namespacesContainer;
137         this.factory = factory;
138         selectedNSName = null; //reset value
139         originalInitValue = initValue;
140     }
141     
142     public void setInputValidator(IInputValidator validator) {
143         customInputValidator = validator;
144     }
145     
146     protected Control createDialogArea(Composite parent) {
147         Control control = super.createDialogArea(parent);
148         if (nsHolder == null) { // no NS section
149             return control;
150         }
151         boolean hasDefNS = nsHolder.getDefaultNamespace() != null;
152         Set<Namespace> namespaces = nsHolder.listNamespaces();
153         Namespace initNamespace = null;
154         if (this.originalInitValue != null 
155                 && false == this.originalInitValue.equals(super.getValue())) {
156             initNamespace = findNamespaceFor(
157                     this.originalInitValue.substring(0, 
158                             this.originalInitValue.length() - super.getValue().length()), 
159                     nsHolder);
160         }
161         if (initNamespace != null) {
162             selectedNSName = initNamespace.getPrefix();
163         }
164         
165         Composite nsComp = new Composite((Composite)control, SWT.NONE);
166         nsComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
167         nsComp.setLayout(new GridLayout(2, false));
168         useNS = new Button(nsComp, SWT.CHECK);
169         useNS.setText("Use Namespace ");
170         useNS.addSelectionListener(new SelectionAdapter() {
171             public void widgetSelected(SelectionEvent se) {
172                 nsChooser.setEnabled(useNS.getSelection());
173                 if (useNS.getSelection() == true) {
174                     selectedNSName = nsChooser.getItem(nsChooser.getSelectionIndex());
175                 }
176                 else {
177                     selectedNSName = null;
178                 }
179                 IdentifierInputDialog.this.validateInput();
180             }
181         });
182         
183         nsChooser = new Combo(nsComp, SWT.READ_ONLY);
184         nsChooser.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
185         nsChooser.setEnabled(initNamespace != null);
186         if (false == hasDefNS 
187                 && namespaces.size() == 0) {
188             nsChooser.add("<no namespaces defined>");
189             nsChooser.select(0);
190             useNS.setEnabled(false);
191             return control;
192         }
193         
194         if (hasDefNS == true) {
195             nsChooser.add(DEF_NS_LABEL);
196         }
197         int selectionIndex = 0;
198         for (Namespace ns : namespaces) {
199             if (ns.equals(initNamespace)) {
200                 selectionIndex = nsChooser.getItemCount();
201             }
202             nsChooser.add(ns.getPrefix());
203         }
204         nsChooser.select(selectionIndex);
205         nsChooser.addSelectionListener(new SelectionAdapter() {
206             public void widgetSelected(SelectionEvent se) {
207                 selectedNSName = nsChooser.getItem(nsChooser.getSelectionIndex());
208                 IdentifierInputDialog.this.validateInput();
209             }
210         });
211         return control;
212     }
213     
214     public String getValue() {
215         throw new RuntimeException("IdentifierInputDialog#getValue(): Unsupported method, use getIdentifier() instead.");
216     }
217     
218     public Identifier getIdentifier() {
219         Namespace ns = null;
220         if (selectedNSName != null) {
221             ns = (selectedNSName.equals(DEF_NS_LABEL)) ?
222                     nsHolder.getDefaultNamespace()
223                     : nsHolder.findNamespace(selectedNSName);
224         }
225         try {
226             return makeIDFromString(nsHolder, ns, (super.getText().isDisposed()) 
227                                                   ? super.getValue() 
228                                                   : super.getText().getText(), 
229                                                   this.factory);
230         }
231         catch(Exception ex) {
232             LogManager.logError(ex);
233             return null;
234         }
235     }
236     
237     public static Identifier makeIDFromString(TopEntity nsHolder, Namespace ns, String idString, WsmoFactory factory) 
238     throws Exception {
239         if (idString == null
240                 || idString.trim().length() == 0
241                 || idString.trim().equals("_#")) {
242             return factory.createAnonymousID();
243         }
244         if (nsHolder != null) {
245             if (idString.lastIndexOf("#") != -1) {
246                 String prefix = idString.substring(0, idString.lastIndexOf("#"));
247                 Namespace prefNS = nsHolder.findNamespace(prefix);
248                 if (prefNS != null) { // the input contains known NS prefix
249                     ns = prefNS;
250                     idString = idString.substring(idString.lastIndexOf("#") + 1);
251                 }
252             }
253             else {
254                 if (idString.indexOf(":") == -1 
255                         && ns == null) {
256                     ns = nsHolder.getDefaultNamespace();
257                 }
258             }
259         }
260         
261         if (ns == null) {
262             return factory.createIRI(idString);
263         }
264         else {
265             return factory.createIRI(ns, idString);
266         }
267     }
268     
269     public static String getNamespacedLocalName(String id, TopEntity nsHolder) {
270         if (id == null) {
271             return null;
272         }
273         if (nsHolder == null) {
274             return id;
275         }
276         if (nsHolder.getDefaultNamespace() != null) {
277             String nsIRI = nsHolder.getDefaultNamespace().getIRI().toString();
278             if (id.startsWith(nsIRI) 
279                     && id.length() > nsIRI.length()) {
280                 return id.substring(nsIRI.length());
281             }
282             for(Namespace ns : nsHolder.listNamespaces()) {
283                 nsIRI = ns.getIRI().toString();
284                 if (id.startsWith(nsIRI) 
285                         && id.length() > nsIRI.length()) {
286                     return id.substring(nsIRI.length());
287                 }
288             }
289         }
290         return id;
291     }
292     
293     private Namespace findNamespaceFor(String nsIRI, TopEntity nsHolder) {
294         if (nsHolder == null) {
295             return null;
296         }
297         if (nsHolder.getDefaultNamespace() != null) {
298             if (nsHolder.getDefaultNamespace().getIRI().toString().equals(nsIRI)) {
299                 return nsHolder.getDefaultNamespace();
300             }
301         }
302         for(Namespace ns : nsHolder.listNamespaces()) {
303             if (ns.getIRI().toString().equals(nsIRI)) {
304                 return ns;
305             }
306         }
307         return null;
308     }
309     
310 }
311 
312 /*
313  * $Log$
314  * Revision 1.10  2007/04/17 14:35:32  alex_simov
315  * migration to the latest wsmo4j (java 5)
316  *
317  * Revision 1.9  2007/02/19 14:45:30  alex_simov
318  * bugfix [1663285]: NFP editor shows property names as :
319  * <namespace_prefix>:<local name> (if a suitable namespece is defined).
320  * System NFPs (having wsmostudio prefix) are read-only and coloured differently
321  *
322  * Revision 1.8  2006/04/11 12:21:41  alex_simov
323  * bugfix[1466316]: Additional checks added to reject SOME of the invalid
324  *  symbols for IRIs
325  *
326  * Revision 1.7  2006/01/31 14:34:54  alex_simov
327  * bugfix[1418557]: user input not checked correctly and as a result correct inputs are rejected
328  *
329  * Revision 1.6  2006/01/23 14:32:37  alex_simov
330  * bugfix: absolute IRIs not handled correctly
331  *
332  * Revision 1.5  2006/01/20 16:34:48  alex_simov
333  * The dialog component allows plugging custom content validators
334  *
335  * Revision 1.4  2006/01/09 12:51:13  alex_simov
336  * Copyright message in header updated
337  *
338  * Revision 1.3  2005/11/28 16:01:47  alex_simov
339  * improved namespaces management for input strings
340  *
341  * Revision 1.2  2005/09/20 14:40:23  alex_simov
342  * small updates
343  *
344  * Revision 1.1  2005/09/14 10:02:49  alex_simov
345  * new Identifier input UI component introduced
346  *
347  */