601847e640683910a6522636389fd46bfe7d07dc
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialogs / UriDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.dialogs;
12
13 import java.net.URI;
14 import java.net.URISyntaxException;
15
16 import org.eclipse.jface.dialogs.IInputValidator;
17 import org.eclipse.jface.dialogs.InputDialog;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.swt.widgets.Shell;
20
21 import eu.etaxonomy.taxeditor.store.StoreUtil;
22
23 /**
24 * <p>UriDialog class.</p>
25 *
26 * @author p.ciardelli
27 * @created 31.03.2009
28 * @version 1.0
29 *
30 * TODO make this extend TitleAreaDialog
31 */
32 public class UriDialog extends InputDialog {
33
34 /**
35 * @param parentShell
36 * @param dialogTitle
37 * @param dialogMessage
38 * @param initialValue
39 * @param validator
40 */
41 private UriDialog(Shell parentShell, String dialogTitle,
42 String dialogMessage) {
43 super(parentShell, dialogTitle,
44 dialogMessage, "http://", new IInputValidator() {
45
46 public String isValid(String text) {
47
48 try {
49 new URI(text);
50 } catch (URISyntaxException e) {
51 return "URL not correctly formed.";
52 }
53
54
55 if (text.length() <= "http://".length()) {
56 return "";
57 }
58 return null;
59 }
60
61 });
62 }
63
64 private URI getUri() {
65 URI uri = null;
66 try {
67 uri = new URI(getValue());
68 } catch (URISyntaxException e) {
69 StoreUtil.error(getClass(), e);
70 }
71 return uri;
72 }
73
74 /**
75 * <p>getUri</p>
76 *
77 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
78 * @param dialogTitle a {@link java.lang.String} object.
79 * @param dialogMessage a {@link java.lang.String} object.
80 * @return a {@link java.net.URI} object.
81 */
82 public static URI getUri(Shell shell, String dialogTitle,
83 String dialogMessage) {
84 UriDialog dialog = new UriDialog(shell,
85 dialogTitle,
86 dialogMessage);
87 if (dialog.open() == Window.CANCEL) {
88 return null;
89 }
90 return dialog.getUri();
91 }
92 }