1 11 package org.eclipse.jface.preference; 12 13 import java.io.File ; 14 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.DirectoryDialog; 19 20 24 public class DirectoryFieldEditor extends StringButtonFieldEditor { 25 28 protected DirectoryFieldEditor() { 29 } 30 31 38 public DirectoryFieldEditor(String name, String labelText, Composite parent) { 39 init(name, labelText); 40 setErrorMessage(JFaceResources 41 .getString("DirectoryFieldEditor.errorMessage")); setChangeButtonText(JFaceResources.getString("openBrowse")); setValidateStrategy(VALIDATE_ON_FOCUS_LOST); 44 createControl(parent); 45 } 46 47 51 protected String changePressed() { 52 File f = new File (getTextControl().getText()); 53 if (!f.exists()) { 54 f = null; 55 } 56 File d = getDirectory(f); 57 if (d == null) { 58 return null; 59 } 60 61 return d.getAbsolutePath(); 62 } 63 64 68 protected boolean doCheckState() { 69 String fileName = getTextControl().getText(); 70 fileName = fileName.trim(); 71 if (fileName.length() == 0 && isEmptyStringAllowed()) { 72 return true; 73 } 74 File file = new File (fileName); 75 return file.isDirectory(); 76 } 77 78 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 |