switched plugin version to 2.1
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / editor / images / UrlDialog.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.editor.images;
12
13 import java.net.MalformedURLException;
14 import java.net.URL;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.jface.dialogs.IInputValidator;
18 import org.eclipse.jface.dialogs.InputDialog;
19 import org.eclipse.jface.window.Window;
20 import org.eclipse.swt.widgets.Shell;
21
22 import eu.etaxonomy.taxeditor.controller.GlobalController;
23
24 /**
25 * @author p.ciardelli
26 * @created 31.03.2009
27 * @version 1.0
28 */
29 public class UrlDialog extends InputDialog {
30 private static final Logger logger = Logger.getLogger(UrlDialog.class);
31 private URL url;
32
33 /**
34 * @param parentShell
35 * @param dialogTitle
36 * @param dialogMessage
37 * @param initialValue
38 * @param validator
39 */
40 private UrlDialog(Shell parentShell, String dialogTitle,
41 String dialogMessage) {
42 super(parentShell, dialogTitle,
43 dialogMessage, "http://", new IInputValidator() {
44
45 public String isValid(String text) {
46 URL url = null;
47 try {
48 url = new URL(text);
49 } catch (MalformedURLException e) {
50 return "URL not correctly formed.";
51 }
52
53 if (text.length() <= "http://".length()) {
54 return "";
55 }
56 return null;
57 }
58
59 });
60 }
61
62 private URL getUrl() {
63 URL url = null;
64 try {
65 url = new URL(getValue());
66 } catch (MalformedURLException e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }
70 return url;
71 }
72
73 public static URL getUrl(String dialogTitle,
74 String dialogMessage) {
75 UrlDialog dialog = new UrlDialog(GlobalController.getShell(),
76 dialogTitle,
77 dialogMessage);
78 if (dialog.open() == Window.CANCEL) {
79 return null;
80 }
81 return dialog.getUrl();
82 }
83 }