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