KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > URLFieldEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ant.internal.ui.preferences;
13
14 import java.io.File JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import org.eclipse.ant.internal.ui.AntUIPlugin;
18 import org.eclipse.jface.preference.StringButtonFieldEditor;
19 import org.eclipse.jface.resource.JFaceResources;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.DirectoryDialog;
23
24 public class URLFieldEditor extends StringButtonFieldEditor {
25     
26     public URLFieldEditor(String JavaDoc name, String JavaDoc labelText, Composite parent) {
27         super(name, labelText, parent);
28         setEmptyStringAllowed(false);
29         setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$
30
setErrorMessage(AntPreferencesMessages.URLFieldEditor_0);
31     }
32     
33     /* (non-Javadoc)
34      * @see org.eclipse.jface.preference.StringFieldEditor#doCheckState()
35      */

36     protected boolean doCheckState() {
37         String JavaDoc text= getTextControl().getText();
38         if (text != null && text.length() > 0) {
39             try {
40                 new URL JavaDoc(text);
41             } catch (MalformedURLException JavaDoc e) {
42                 return false;
43             }
44         }
45         return true;
46     }
47     
48     /* (non-Javadoc)
49      * Method declared on StringButtonFieldEditor.
50      * Opens the directory chooser dialog and returns the <code>URL</code> of the selected directory.
51      */

52     protected String JavaDoc changePressed() {
53         URL JavaDoc url= null;
54         try {
55             url = new URL JavaDoc(getTextControl().getText());
56         } catch (MalformedURLException JavaDoc e1) {
57         }
58         File JavaDoc f = null;
59         if (url != null) {
60             f= new File JavaDoc(url.getFile());
61             if (!f.exists()) {
62                 f = null;
63             }
64         }
65         
66         File JavaDoc d = getDirectory(f);
67         if (d == null) {
68             return null;
69         }
70
71         try {
72             return d.toURL().toExternalForm();
73         } catch (MalformedURLException JavaDoc e) {
74             AntUIPlugin.log("Internal error setting documentation location", e); //$NON-NLS-1$
75
return null;
76         }
77     }
78     
79     /**
80      * Helper that opens the directory chooser dialog.
81      * @param startingDirectory The directory the dialog will open in.
82      * @return File File or <code>null</code>.
83      */

84     private File JavaDoc getDirectory(File JavaDoc startingDirectory) {
85
86         DirectoryDialog fileDialog = new DirectoryDialog(getShell(), SWT.OPEN);
87         if (startingDirectory != null) {
88             fileDialog.setFilterPath(startingDirectory.getPath());
89         }
90         String JavaDoc dir = fileDialog.open();
91         if (dir != null) {
92             dir = dir.trim();
93             if (dir.length() > 0) {
94                 return new File JavaDoc(dir);
95             }
96         }
97
98         return null;
99     }
100 }
101
Popular Tags