1 13 14 package org.eclipse.ui.dialogs; 15 16 import org.eclipse.core.filesystem.IFileStore; 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IPathVariableManager; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IWorkspace; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.wizard.WizardPage; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.events.ModifyEvent; 29 import org.eclipse.swt.events.ModifyListener; 30 import org.eclipse.swt.events.SelectionAdapter; 31 import org.eclipse.swt.events.SelectionEvent; 32 import org.eclipse.swt.events.SelectionListener; 33 import org.eclipse.swt.graphics.Font; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Composite; 38 import org.eclipse.swt.widgets.DirectoryDialog; 39 import org.eclipse.swt.widgets.FileDialog; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.ide.dialogs.PathVariableSelectionDialog; 44 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 45 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 46 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 47 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils; 48 49 57 public class WizardNewLinkPage extends WizardPage { 58 private String initialLinkTarget; 59 60 private int type; 61 62 private boolean createLink = false; 63 64 private IContainer container; 65 66 private Text linkTargetField; 68 69 private Button browseButton; 70 71 private Button variablesButton; 72 73 80 public WizardNewLinkPage(String pageName, int type) { 81 super(pageName); 82 this.type = type; 83 setPageComplete(true); 84 } 85 86 89 public void createControl(Composite parent) { 90 Font font = parent.getFont(); 91 initializeDialogUnits(parent); 92 Composite topLevel = new Composite(parent, SWT.NONE); 94 GridLayout layout = new GridLayout(); 95 layout.numColumns = 3; 96 topLevel.setLayout(layout); 97 topLevel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 98 | GridData.HORIZONTAL_ALIGN_FILL)); 99 topLevel.setFont(font); 100 PlatformUI.getWorkbench().getHelpSystem().setHelp(topLevel, 101 IIDEHelpContextIds.NEW_LINK_WIZARD_PAGE); 102 103 final Button createLinkButton = new Button(topLevel, SWT.CHECK); 104 if (type == IResource.FILE) { 105 createLinkButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_linkFileButton); 106 } else { 107 createLinkButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_linkFolderButton); 108 } 109 createLinkButton.setSelection(createLink); 110 GridData data = new GridData(); 111 data.horizontalSpan = 3; 112 createLinkButton.setLayoutData(data); 113 createLinkButton.setFont(font); 114 SelectionListener listener = new SelectionAdapter() { 115 public void widgetSelected(SelectionEvent e) { 116 createLink = createLinkButton.getSelection(); 117 browseButton.setEnabled(createLink); 118 variablesButton.setEnabled(createLink); 119 linkTargetField.setEnabled(createLink); 120 setPageComplete(validatePage()); 121 } 122 }; 123 createLinkButton.addSelectionListener(listener); 124 125 createLinkLocationGroup(topLevel, createLink); 126 validatePage(); 127 128 setErrorMessage(null); 129 setMessage(null); 130 setControl(topLevel); 131 } 132 133 139 private void createLinkLocationGroup(Composite locationGroup, 140 boolean enabled) { 141 Font font = locationGroup.getFont(); 142 Label fill = new Label(locationGroup, SWT.NONE); 143 GridData data = new GridData(); 144 Button button = new Button(locationGroup, SWT.CHECK); 145 data.widthHint = button.computeSize(SWT.DEFAULT, SWT.DEFAULT).x; 146 button.dispose(); 147 fill.setLayoutData(data); 148 149 linkTargetField = new Text(locationGroup, SWT.BORDER); 151 data = new GridData(GridData.FILL_HORIZONTAL); 152 linkTargetField.setLayoutData(data); 153 linkTargetField.setFont(font); 154 linkTargetField.setEnabled(enabled); 155 linkTargetField.addModifyListener(new ModifyListener() { 156 public void modifyText(ModifyEvent e) { 157 setPageComplete(validatePage()); 158 } 159 }); 160 if (initialLinkTarget != null) { 161 linkTargetField.setText(initialLinkTarget); 162 } 163 164 browseButton = new Button(locationGroup, SWT.PUSH); 166 setButtonLayoutData(browseButton); 167 browseButton.setFont(font); 168 browseButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_browseButton); 169 browseButton.addSelectionListener(new SelectionAdapter() { 170 public void widgetSelected(SelectionEvent event) { 171 handleLinkTargetBrowseButtonPressed(); 172 } 173 }); 174 browseButton.setEnabled(enabled); 175 176 fill = new Label(locationGroup, SWT.NONE); 177 data = new GridData(); 178 data.horizontalSpan = 2; 179 fill.setLayoutData(data); 180 181 variablesButton = new Button(locationGroup, SWT.PUSH); 183 setButtonLayoutData(variablesButton); 184 variablesButton.setFont(font); 185 variablesButton.setText(IDEWorkbenchMessages.WizardNewLinkPage_variablesButton); 186 variablesButton.addSelectionListener(new SelectionAdapter() { 187 public void widgetSelected(SelectionEvent event) { 188 handleVariablesButtonPressed(); 189 } 190 }); 191 variablesButton.setEnabled(enabled); 192 } 193 194 200 public String getLinkTarget() { 201 if (createLink && linkTargetField != null 202 && linkTargetField.isDisposed() == false) { 203 return linkTargetField.getText(); 204 } 205 return null; 206 } 207 208 211 private void handleLinkTargetBrowseButtonPressed() { 212 String linkTargetName = linkTargetField.getText(); 213 String selection = null; 214 IFileStore store = null; 215 if (linkTargetName.length() > 0) { 216 store = IDEResourceInfoUtils.getFileStore(linkTargetName); 217 if (store == null || !store.fetchInfo().exists()) { 218 store = null; 219 } 220 } 221 if (type == IResource.FILE) { 222 FileDialog dialog = new FileDialog(getShell()); 223 if (store != null) { 224 if (store.fetchInfo().isDirectory()) { 225 dialog.setFilterPath(linkTargetName); 226 } else { 227 dialog.setFileName(linkTargetName); 228 } 229 } 230 selection = dialog.open(); 231 } else { 232 DirectoryDialog dialog = new DirectoryDialog(getShell()); 233 if (store != null) { 234 if (!store.fetchInfo().isDirectory()) { 235 linkTargetName = store.getParent().getName(); 236 } 237 if (linkTargetName != null) { 238 dialog.setFilterPath(linkTargetName); 239 } 240 } 241 dialog.setMessage(IDEWorkbenchMessages.WizardNewLinkPage_targetSelectionLabel); 242 selection = dialog.open(); 243 } 244 if (selection != null) { 245 linkTargetField.setText(selection); 246 } 247 } 248 249 252 private void handleVariablesButtonPressed() { 253 PathVariableSelectionDialog dialog = new PathVariableSelectionDialog( 254 getShell(), type); 255 256 if (dialog.open() == IDialogConstants.OK_ID) { 257 String [] variableNames = (String []) dialog.getResult(); 258 259 if (variableNames != null) { 260 IPathVariableManager pathVariableManager = ResourcesPlugin 261 .getWorkspace().getPathVariableManager(); 262 IPath path = pathVariableManager.getValue(variableNames[0]); 263 264 if (path != null) { 265 linkTargetField.setText(path.toOSString()); 266 } 267 } 268 } 269 } 270 271 278 public void setContainer(IContainer container) { 279 this.container = container; 280 } 281 282 287 public void setLinkTarget(String target) { 288 initialLinkTarget = target; 289 if (linkTargetField != null && linkTargetField.isDisposed() == false) { 290 linkTargetField.setText(target); 291 } 292 } 293 294 302 private boolean validateFileType(IFileStore linkTargetStore) { 303 boolean valid = true; 304 305 if (type == IResource.FILE && linkTargetStore.fetchInfo().isDirectory()) { 306 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNotFile); 307 valid = false; 308 } else if (type == IResource.FOLDER 309 && !linkTargetStore.fetchInfo().isDirectory()) { 310 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNotFolder); 311 valid = false; 312 } 313 return valid; 314 } 315 316 323 private boolean validateLinkTargetName(String linkTargetName) { 324 boolean valid = true; 325 326 if ("".equals(linkTargetName)) { setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetEmpty); 328 valid = false; 329 } else { 330 IPath path = new Path(""); if (path.isValidPath(linkTargetName) == false) { 332 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetInvalid); 333 valid = false; 334 } 335 } 336 return valid; 337 } 338 339 346 private boolean validatePage() { 347 boolean valid = true; 348 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 349 350 if (createLink) { 351 String linkTargetName = linkTargetField.getText(); 352 353 valid = validateLinkTargetName(linkTargetName); 354 if (valid) { 355 IFileStore linkTargetFile = IDEResourceInfoUtils.getFileStore(linkTargetName); 356 if (linkTargetFile == null || !linkTargetFile.fetchInfo().exists()) { 357 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetNonExistent); 358 valid = false; 359 } else { 360 IStatus locationStatus = workspace.validateLinkLocation( 361 container, new Path(linkTargetName)); 362 363 if (locationStatus.isOK() == false) { 364 setErrorMessage(IDEWorkbenchMessages.WizardNewLinkPage_linkTargetLocationInvalid); 365 valid = false; 366 } else { 367 valid = validateFileType(linkTargetFile); 368 } 369 } 370 } 371 } 372 if (valid) { 375 setMessage(null); 376 setErrorMessage(null); 377 } 378 return valid; 379 } 380 } 381 | Popular Tags |