1 11 package org.eclipse.ui.dialogs; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IResource; 15 import org.eclipse.core.resources.IWorkspace; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.swt.widgets.Label; 27 import org.eclipse.swt.widgets.Text; 28 import org.eclipse.swt.widgets.Widget; 29 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 30 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 31 32 58 public abstract class WizardImportPage extends WizardDataTransferPage { 59 private IResource currentResourceSelection; 60 61 private String initialContainerFieldValue; 63 64 private Text containerNameField; 66 67 private Button containerBrowseButton; 68 69 77 protected WizardImportPage(String name, IStructuredSelection selection) { 78 super(name); 79 80 if (selection.size() == 1) { 81 currentResourceSelection = (IResource) selection.getFirstElement(); 82 } else { 83 currentResourceSelection = null; 84 } 85 86 if (currentResourceSelection != null) { 87 if (currentResourceSelection.getType() == IResource.FILE) { 88 currentResourceSelection = currentResourceSelection.getParent(); 89 } 90 91 if (!currentResourceSelection.isAccessible()) { 92 currentResourceSelection = null; 93 } 94 } 95 96 } 97 98 103 protected boolean allowNewContainerName() { 104 return true; 105 } 106 107 110 public void createControl(Composite parent) { 111 Composite composite = new Composite(parent, SWT.NULL); 112 composite.setLayout(new GridLayout()); 113 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 114 | GridData.HORIZONTAL_ALIGN_FILL)); 115 composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 116 117 createSourceGroup(composite); 118 119 createSpacer(composite); 120 121 createBoldLabel(composite, IDEWorkbenchMessages.WizardImportPage_destinationLabel); 122 createDestinationGroup(composite); 123 124 createSpacer(composite); 125 126 createBoldLabel(composite, IDEWorkbenchMessages.WizardImportPage_options); 127 createOptionsGroup(composite); 128 129 restoreWidgetValues(); 130 updateWidgetEnablements(); 131 setPageComplete(determinePageCompletion()); 132 133 setControl(composite); 134 } 135 136 141 protected final void createDestinationGroup(Composite parent) { 142 Composite containerGroup = new Composite(parent, SWT.NONE); 144 GridLayout layout = new GridLayout(); 145 layout.numColumns = 3; 146 containerGroup.setLayout(layout); 147 containerGroup.setLayoutData(new GridData( 148 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 149 150 Label resourcesLabel = new Label(containerGroup, SWT.NONE); 152 resourcesLabel.setText(IDEWorkbenchMessages.WizardImportPage_folder); 153 154 containerNameField = new Text(containerGroup, SWT.SINGLE | SWT.BORDER); 156 containerNameField.addListener(SWT.Modify, this); 157 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 158 | GridData.GRAB_HORIZONTAL); 159 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 160 containerNameField.setLayoutData(data); 161 162 containerBrowseButton = new Button(containerGroup, SWT.PUSH); 164 containerBrowseButton.setText(IDEWorkbenchMessages.WizardImportPage_browseLabel); 165 containerBrowseButton.setLayoutData(new GridData( 166 GridData.HORIZONTAL_ALIGN_FILL)); 167 containerBrowseButton.addListener(SWT.Selection, this); 168 169 initialPopulateContainerField(); 170 } 171 172 180 protected abstract void createSourceGroup(Composite parent); 181 182 187 protected void displayErrorDialog(String message) { 188 MessageDialog.openError(getContainer().getShell(), IDEWorkbenchMessages.WizardImportPage_errorDialogTitle, message); 189 } 190 191 202 protected IPath getContainerFullPath() { 203 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 204 205 IPath testPath = getResourcePath(); 207 208 IStatus result = workspace.validatePath(testPath.toString(), 209 IResource.PROJECT | IResource.FOLDER); 210 if (result.isOK()) { 211 return testPath; 212 } 213 214 return null; 215 } 216 217 221 protected IPath getResourcePath() { 222 return getPathFromText(this.containerNameField); 223 } 224 225 232 protected IContainer getSpecifiedContainer() { 233 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 234 IPath path = getContainerFullPath(); 235 if (workspace.getRoot().exists(path)) { 236 return (IContainer) workspace.getRoot().findMember(path); 237 } 238 239 return null; 240 } 241 242 246 protected void handleContainerBrowseButtonPressed() { 247 IPath containerPath = queryForContainer(getSpecifiedContainer(), 249 IDEWorkbenchMessages.WizardImportPage_selectFolderLabel); 250 251 if (containerPath != null) { 253 containerNameField.setText(containerPath.makeRelative().toString()); 254 } 255 } 256 257 262 public void handleEvent(Event event) { 263 Widget source = event.widget; 264 265 if (source == containerBrowseButton) { 266 handleContainerBrowseButtonPressed(); 267 } 268 269 setPageComplete(determinePageCompletion()); 270 updateWidgetEnablements(); 271 } 272 273 276 protected final void initialPopulateContainerField() { 277 if (initialContainerFieldValue != null) { 278 containerNameField.setText(initialContainerFieldValue); 279 } else if (currentResourceSelection != null) { 280 containerNameField.setText(currentResourceSelection.getFullPath() 281 .toString()); 282 } 283 } 284 285 291 public void setContainerFieldValue(String value) { 292 if (containerNameField == null) { 293 initialContainerFieldValue = value; 294 } else { 295 containerNameField.setText(value); 296 } 297 } 298 299 302 protected final boolean validateDestinationGroup() { 303 if (getContainerFullPath() == null) { 304 return false; 305 } 306 307 IContainer container = getSpecifiedContainer(); 309 if (container != null) { 310 if (!container.isAccessible()) { 311 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_folderMustExist); 312 return false; 313 } 314 } 315 316 return true; 317 318 } 319 } 320 | Popular Tags |