including changes from cdmlib-print
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / 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.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 /**
22 * <p>UriDialog class.</p>
23 *
24 * @author p.ciardelli
25 * @created 31.03.2009
26 * @version 1.0
27 *
28 * TODO make this extend TitleAreaDialog
29 */
30 public class UriDialog extends InputDialog {
31
32 /**
33 * @param parentShell
34 * @param dialogTitle
35 * @param dialogMessage
36 * @param initialValue
37 * @param validator
38 */
39 private UriDialog(Shell parentShell, String dialogTitle,
40 String dialogMessage) {
41 super(parentShell, dialogTitle,
42 dialogMessage, "http://", new IInputValidator() {
43
44 public String isValid(String text) {
45
46 try {
47 new URI(text);
48 } catch (URISyntaxException e) {
49 return "URL not correctly formed.";
50 }
51
52
53 if (text.length() <= "http://".length()) {
54 return "";
55 }
56 return null;
57 }
58
59 });
60 }
61
62 private URI getUri() {
63 URI uri = null;
64 try {
65 uri = new URI(getValue());
66 } catch (URISyntaxException e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }
70 return uri;
71 }
72
73 /**
74 * <p>getUri</p>
75 *
76 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
77 * @param dialogTitle a {@link java.lang.String} object.
78 * @param dialogMessage a {@link java.lang.String} object.
79 * @return a {@link java.net.URI} object.
80 */
81 public static URI getUri(Shell shell, String dialogTitle,
82 String dialogMessage) {
83 UriDialog dialog = new UriDialog(shell,
84 dialogTitle,
85 dialogMessage);
86 if (dialog.open() == Window.CANCEL) {
87 return null;
88 }
89 return dialog.getUri();
90 }
91 }