1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths; 12 13 import java.io.File ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.Path; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.layout.GridLayout; 21 import org.eclipse.swt.widgets.Composite; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.DirectoryDialog; 24 import org.eclipse.swt.widgets.FileDialog; 25 import org.eclipse.swt.widgets.Shell; 26 27 import org.eclipse.jface.dialogs.IDialogSettings; 28 import org.eclipse.jface.dialogs.StatusDialog; 29 30 import org.eclipse.ui.PlatformUI; 31 32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 33 import org.eclipse.jdt.internal.ui.IUIConstants; 34 import org.eclipse.jdt.internal.ui.JavaPlugin; 35 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 36 import org.eclipse.jdt.internal.ui.dialogs.StatusUtil; 37 import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; 38 import org.eclipse.jdt.internal.ui.wizards.dialogfields.DialogField; 39 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IDialogFieldListener; 40 import org.eclipse.jdt.internal.ui.wizards.dialogfields.IStringButtonAdapter; 41 import org.eclipse.jdt.internal.ui.wizards.dialogfields.LayoutUtil; 42 import org.eclipse.jdt.internal.ui.wizards.dialogfields.SelectionButtonDialogField; 43 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringButtonDialogField; 44 import org.eclipse.jdt.internal.ui.wizards.dialogfields.StringDialogField; 45 46 public class VariableCreationDialog extends StatusDialog { 47 48 private IDialogSettings fDialogSettings; 49 50 private StringDialogField fNameField; 51 private StatusInfo fNameStatus; 52 53 private StringButtonDialogField fPathField; 54 private StatusInfo fPathStatus; 55 private SelectionButtonDialogField fDirButton; 56 57 private CPVariableElement fElement; 58 59 private List fExistingNames; 60 61 public VariableCreationDialog(Shell parent, CPVariableElement element, List existingNames) { 62 super(parent); 63 if (element == null) { 64 setTitle(NewWizardMessages.VariableCreationDialog_titlenew); 65 } else { 66 setTitle(NewWizardMessages.VariableCreationDialog_titleedit); 67 } 68 69 fDialogSettings= JavaPlugin.getDefault().getDialogSettings(); 70 71 fElement= element; 72 73 fNameStatus= new StatusInfo(); 74 fPathStatus= new StatusInfo(); 75 76 NewVariableAdapter adapter= new NewVariableAdapter(); 77 fNameField= new StringDialogField(); 78 fNameField.setDialogFieldListener(adapter); 79 fNameField.setLabelText(NewWizardMessages.VariableCreationDialog_name_label); 80 81 fPathField= new StringButtonDialogField(adapter); 82 fPathField.setDialogFieldListener(adapter); 83 fPathField.setLabelText(NewWizardMessages.VariableCreationDialog_path_label); 84 fPathField.setButtonLabel(NewWizardMessages.VariableCreationDialog_path_file_button); 85 86 fDirButton= new SelectionButtonDialogField(SWT.PUSH); 87 fDirButton.setDialogFieldListener(adapter); 88 fDirButton.setLabelText(NewWizardMessages.VariableCreationDialog_path_dir_button); 89 90 fExistingNames= existingNames; 91 92 if (element != null) { 93 fNameField.setText(element.getName()); 94 fPathField.setText(element.getPath().toString()); 95 fExistingNames.remove(element.getName()); 96 } else { 97 fNameField.setText(""); fPathField.setText(""); } 100 } 101 102 105 protected void configureShell(Shell newShell) { 106 super.configureShell(newShell); 107 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, IJavaHelpContextIds.VARIABLE_CREATION_DIALOG); 108 } 109 110 111 public CPVariableElement getClasspathElement() { 112 return new CPVariableElement(fNameField.getText(), new Path(fPathField.getText())); 113 } 114 115 118 protected Control createDialogArea(Composite parent) { 119 Composite composite= (Composite) super.createDialogArea(parent); 120 121 Composite inner= new Composite(composite, SWT.NONE); 122 inner.setFont(composite.getFont()); 123 124 GridLayout layout= new GridLayout(); 125 layout.marginWidth= 0; 126 layout.marginHeight= 0; 127 layout.numColumns= 3; 128 inner.setLayout(layout); 129 130 int fieldWidthHint= convertWidthInCharsToPixels(50); 131 132 fNameField.doFillIntoGrid(inner, 2); 133 LayoutUtil.setWidthHint(fNameField.getTextControl(null), fieldWidthHint); 134 LayoutUtil.setHorizontalGrabbing(fNameField.getTextControl(null)); 135 136 DialogField.createEmptySpace(inner, 1); 137 138 fPathField.doFillIntoGrid(inner, 3); 139 LayoutUtil.setWidthHint(fPathField.getTextControl(null), fieldWidthHint); 140 141 DialogField.createEmptySpace(inner, 2); 142 fDirButton.doFillIntoGrid(inner, 1); 143 144 DialogField focusField= (fElement == null) ? fNameField : fPathField; 145 focusField.postSetFocusOnDialogField(parent.getDisplay()); 146 applyDialogFont(composite); 147 return composite; 148 } 149 150 151 153 private class NewVariableAdapter implements IDialogFieldListener, IStringButtonAdapter { 154 155 public void dialogFieldChanged(DialogField field) { 157 doFieldUpdated(field); 158 } 159 160 public void changeControlPressed(DialogField field) { 162 doChangeControlPressed(field); 163 } 164 } 165 166 private void doChangeControlPressed(DialogField field) { 167 if (field == fPathField) { 168 IPath path= chooseExtJarFile(); 169 if (path != null) { 170 fPathField.setText(path.toString()); 171 } 172 } 173 } 174 175 private void doFieldUpdated(DialogField field) { 176 if (field == fNameField) { 177 fNameStatus= nameUpdated(); 178 } else if (field == fPathField) { 179 fPathStatus= pathUpdated(); 180 } else if (field == fDirButton) { 181 IPath path= chooseExtDirectory(); 182 if (path != null) { 183 fPathField.setText(path.toString()); 184 } 185 } 186 updateStatus(StatusUtil.getMoreSevere(fPathStatus, fNameStatus)); 187 } 188 189 private StatusInfo nameUpdated() { 190 StatusInfo status= new StatusInfo(); 191 String name= fNameField.getText(); 192 if (name.length() == 0) { 193 status.setError(NewWizardMessages.VariableCreationDialog_error_entername); 194 return status; 195 } 196 if (name.trim().length() != name.length()) { 197 status.setError(NewWizardMessages.VariableCreationDialog_error_whitespace); 198 } else if (!Path.ROOT.isValidSegment(name)) { 199 status.setError(NewWizardMessages.VariableCreationDialog_error_invalidname); 200 } else if (nameConflict(name)) { 201 status.setError(NewWizardMessages.VariableCreationDialog_error_nameexists); 202 } 203 return status; 204 } 205 206 private boolean nameConflict(String name) { 207 if (fElement != null && fElement.getName().equals(name)) { 208 return false; 209 } 210 for (int i= 0; i < fExistingNames.size(); i++) { 211 CPVariableElement elem= (CPVariableElement)fExistingNames.get(i); 212 if (name.equals(elem.getName())){ 213 return true; 214 } 215 } 216 return false; 217 } 218 219 220 private StatusInfo pathUpdated() { 221 StatusInfo status= new StatusInfo(); 222 223 String path= fPathField.getText(); 224 if (path.length() > 0) { if (!Path.ROOT.isValidPath(path)) { 226 status.setError(NewWizardMessages.VariableCreationDialog_error_invalidpath); 227 } else if (!new File (path).exists()) { 228 status.setWarning(NewWizardMessages.VariableCreationDialog_warning_pathnotexists); 229 } 230 } 231 return status; 232 } 233 234 235 private String getInitPath() { 236 String initPath= fPathField.getText(); 237 if (initPath.length() == 0) { 238 initPath= fDialogSettings.get(IUIConstants.DIALOGSTORE_LASTEXTJAR); 239 if (initPath == null) { 240 initPath= ""; } 242 } else { 243 IPath entryPath= new Path(initPath); 244 if (ArchiveFileFilter.isArchivePath(entryPath)) { 245 entryPath.removeLastSegments(1); 246 } 247 initPath= entryPath.toOSString(); 248 } 249 return initPath; 250 } 251 252 253 256 private IPath chooseExtJarFile() { 257 String initPath= getInitPath(); 258 259 FileDialog dialog= new FileDialog(getShell()); 260 dialog.setText(NewWizardMessages.VariableCreationDialog_extjardialog_text); 261 dialog.setFilterExtensions(new String [] {"*.jar;*.zip"}); dialog.setFilterPath(initPath); 263 String res= dialog.open(); 264 if (res != null) { 265 fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath()); 266 return Path.fromOSString(res).makeAbsolute(); 267 } 268 return null; 269 } 270 271 private IPath chooseExtDirectory() { 272 String initPath= getInitPath(); 273 274 DirectoryDialog dialog= new DirectoryDialog(getShell()); 275 dialog.setText(NewWizardMessages.VariableCreationDialog_extdirdialog_text); 276 dialog.setMessage(NewWizardMessages.VariableCreationDialog_extdirdialog_message); 277 dialog.setFilterPath(initPath); 278 String res= dialog.open(); 279 if (res != null) { 280 fDialogSettings.put(IUIConstants.DIALOGSTORE_LASTEXTJAR, dialog.getFilterPath()); 281 return Path.fromOSString(res); 282 } 283 return null; 284 } 285 286 287 288 } 289 | Popular Tags |