1 11 12 package org.eclipse.ant.internal.ui.preferences; 13 14 import java.io.File ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 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 name, String labelText, Composite parent) { 27 super(name, labelText, parent); 28 setEmptyStringAllowed(false); 29 setChangeButtonText(JFaceResources.getString("openBrowse")); setErrorMessage(AntPreferencesMessages.URLFieldEditor_0); 31 } 32 33 36 protected boolean doCheckState() { 37 String text= getTextControl().getText(); 38 if (text != null && text.length() > 0) { 39 try { 40 new URL (text); 41 } catch (MalformedURLException e) { 42 return false; 43 } 44 } 45 return true; 46 } 47 48 52 protected String changePressed() { 53 URL url= null; 54 try { 55 url = new URL (getTextControl().getText()); 56 } catch (MalformedURLException e1) { 57 } 58 File f = null; 59 if (url != null) { 60 f= new File (url.getFile()); 61 if (!f.exists()) { 62 f = null; 63 } 64 } 65 66 File d = getDirectory(f); 67 if (d == null) { 68 return null; 69 } 70 71 try { 72 return d.toURL().toExternalForm(); 73 } catch (MalformedURLException e) { 74 AntUIPlugin.log("Internal error setting documentation location", e); return null; 76 } 77 } 78 79 84 private File getDirectory(File startingDirectory) { 85 86 DirectoryDialog fileDialog = new DirectoryDialog(getShell(), SWT.OPEN); 87 if (startingDirectory != null) { 88 fileDialog.setFilterPath(startingDirectory.getPath()); 89 } 90 String dir = fileDialog.open(); 91 if (dir != null) { 92 dir = dir.trim(); 93 if (dir.length() > 0) { 94 return new File (dir); 95 } 96 } 97 98 return null; 99 } 100 } 101 | Popular Tags |