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.FileDialog; 19 20 24 public class FileFieldEditor extends StringButtonFieldEditor { 25 26 30 private String [] extensions = null; 31 32 36 private boolean enforceAbsolute = false; 37 38 41 protected FileFieldEditor() { 42 } 43 44 51 public FileFieldEditor(String name, String labelText, Composite parent) { 52 this(name, labelText, false, parent); 53 } 54 55 64 public FileFieldEditor(String name, String labelText, 65 boolean enforceAbsolute, Composite parent) { 66 init(name, labelText); 67 this.enforceAbsolute = enforceAbsolute; 68 setErrorMessage(JFaceResources 69 .getString("FileFieldEditor.errorMessage")); setChangeButtonText(JFaceResources.getString("openBrowse")); setValidateStrategy(VALIDATE_ON_FOCUS_LOST); 72 createControl(parent); 73 } 74 75 79 protected String changePressed() { 80 File f = new File (getTextControl().getText()); 81 if (!f.exists()) { 82 f = null; 83 } 84 File d = getFile(f); 85 if (d == null) { 86 return null; 87 } 88 89 return d.getAbsolutePath(); 90 } 91 92 96 protected boolean checkState() { 97 98 String msg = null; 99 100 String path = getTextControl().getText(); 101 if (path != null) { 102 path = path.trim(); 103 } else { 104 path = ""; } 106 if (path.length() == 0) { 107 if (!isEmptyStringAllowed()) { 108 msg = getErrorMessage(); 109 } 110 } else { 111 File file = new File (path); 112 if (file.isFile()) { 113 if (enforceAbsolute && !file.isAbsolute()) { 114 msg = JFaceResources 115 .getString("FileFieldEditor.errorMessage2"); } 117 } else { 118 msg = getErrorMessage(); 119 } 120 } 121 122 if (msg != null) { showErrorMessage(msg); 124 return false; 125 } 126 127 clearErrorMessage(); 129 return true; 130 } 131 132 138 private File getFile(File startingDirectory) { 139 140 FileDialog dialog = new FileDialog(getShell(), SWT.OPEN); 141 if (startingDirectory != null) { 142 dialog.setFileName(startingDirectory.getPath()); 143 } 144 if (extensions != null) { 145 dialog.setFilterExtensions(extensions); 146 } 147 String file = dialog.open(); 148 if (file != null) { 149 file = file.trim(); 150 if (file.length() > 0) { 151 return new File (file); 152 } 153 } 154 155 return null; 156 } 157 158 164 public void setFileExtensions(String [] extensions) { 165 this.extensions = extensions; 166 } 167 } 168 | Popular Tags |