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
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
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
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;
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) {
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) {
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347