1 13 package org.eclipse.ui.dialogs; 14 15 import java.util.ArrayList ; 16 import java.util.Map ; 17 18 import org.eclipse.core.resources.IContainer; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IWorkspace; 22 import org.eclipse.core.resources.IWorkspaceRoot; 23 import org.eclipse.core.runtime.IAdaptable; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.jface.viewers.CheckStateChangedEvent; 28 import org.eclipse.jface.viewers.ICheckStateListener; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.ITreeContentProvider; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.BusyIndicator; 33 import org.eclipse.swt.layout.GridData; 34 import org.eclipse.swt.layout.GridLayout; 35 import org.eclipse.swt.widgets.Button; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Event; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Text; 40 import org.eclipse.swt.widgets.Widget; 41 import org.eclipse.ui.internal.ide.DialogUtil; 42 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 43 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 44 import org.eclipse.ui.internal.ide.dialogs.IElementFilter; 45 import org.eclipse.ui.internal.ide.dialogs.ResourceTreeAndListGroup; 46 import org.eclipse.ui.model.WorkbenchLabelProvider; 47 import org.eclipse.ui.model.WorkbenchViewerComparator; 48 49 74 public abstract class WizardResourceImportPage extends WizardDataTransferPage { 75 private IResource currentResourceSelection; 76 77 private String initialContainerFieldValue; 79 80 protected java.util.List selectedTypes = new ArrayList (); 81 82 private Text containerNameField; 84 85 private Button containerBrowseButton; 86 87 91 protected ResourceTreeAndListGroup selectionGroup; 92 93 private static final String EMPTY_FOLDER_MESSAGE = IDEWorkbenchMessages.WizardImportPage_specifyFolder; 95 96 private static final String EMPTY_PROJECT_MESSAGE = IDEWorkbenchMessages.WizardImportPage_specifyProject; 97 98 private static final String INACCESSABLE_FOLDER_MESSAGE = IDEWorkbenchMessages.WizardImportPage_folderMustExist; 99 100 108 protected WizardResourceImportPage(String name, 109 IStructuredSelection selection) { 110 super(name); 111 112 currentResourceSelection = null; 114 if (selection.size() == 1) { 115 Object firstElement = selection.getFirstElement(); 116 if (firstElement instanceof IAdaptable) { 117 Object resource = ((IAdaptable) firstElement) 118 .getAdapter(IResource.class); 119 if (resource != null) { 120 currentResourceSelection = (IResource) resource; 121 } 122 } 123 } 124 125 if (currentResourceSelection != null) { 126 if (currentResourceSelection.getType() == IResource.FILE) { 127 currentResourceSelection = currentResourceSelection.getParent(); 128 } 129 130 if (!currentResourceSelection.isAccessible()) { 131 currentResourceSelection = null; 132 } 133 } 134 135 } 136 137 142 protected boolean allowNewContainerName() { 143 return true; 144 } 145 146 149 public void createControl(Composite parent) { 150 151 initializeDialogUnits(parent); 152 153 Composite composite = new Composite(parent, SWT.NULL); 154 composite.setLayout(new GridLayout()); 155 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 156 | GridData.HORIZONTAL_ALIGN_FILL)); 157 composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 158 composite.setFont(parent.getFont()); 159 160 createSourceGroup(composite); 161 162 createDestinationGroup(composite); 163 164 createOptionsGroup(composite); 165 166 restoreWidgetValues(); 167 updateWidgetEnablements(); 168 setPageComplete(determinePageCompletion()); 169 setErrorMessage(null); 171 setControl(composite); 172 } 173 174 179 protected final void createDestinationGroup(Composite parent) { 180 Composite containerGroup = new Composite(parent, SWT.NONE); 182 GridLayout layout = new GridLayout(); 183 layout.numColumns = 3; 184 containerGroup.setLayout(layout); 185 containerGroup.setLayoutData(new GridData( 186 GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 187 containerGroup.setFont(parent.getFont()); 188 189 Label resourcesLabel = new Label(containerGroup, SWT.NONE); 191 resourcesLabel.setText(IDEWorkbenchMessages.WizardImportPage_folder); 192 resourcesLabel.setFont(parent.getFont()); 193 194 containerNameField = new Text(containerGroup, SWT.SINGLE | SWT.BORDER); 196 containerNameField.addListener(SWT.Modify, this); 197 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 198 | GridData.GRAB_HORIZONTAL); 199 data.widthHint = SIZING_TEXT_FIELD_WIDTH; 200 containerNameField.setLayoutData(data); 201 containerNameField.setFont(parent.getFont()); 202 203 containerBrowseButton = new Button(containerGroup, SWT.PUSH); 205 containerBrowseButton.setText(IDEWorkbenchMessages.WizardImportPage_browse2); 206 containerBrowseButton.setLayoutData(new GridData( 207 GridData.HORIZONTAL_ALIGN_FILL)); 208 containerBrowseButton.addListener(SWT.Selection, this); 209 containerBrowseButton.setFont(parent.getFont()); 210 setButtonLayoutData(containerBrowseButton); 211 212 initialPopulateContainerField(); 213 } 214 215 218 protected void createFileSelectionGroup(Composite parent) { 219 220 this.selectionGroup = new ResourceTreeAndListGroup(parent, 222 new FileSystemElement("Dummy", null, true), getFolderProvider(), new WorkbenchLabelProvider(), 224 getFileProvider(), new WorkbenchLabelProvider(), SWT.NONE, 225 DialogUtil.inRegularFontMode(parent)); 226 227 ICheckStateListener listener = new ICheckStateListener() { 228 public void checkStateChanged(CheckStateChangedEvent event) { 229 updateWidgetEnablements(); 230 } 231 }; 232 233 WorkbenchViewerComparator comparator = new WorkbenchViewerComparator(); 234 this.selectionGroup.setTreeComparator(comparator); 235 this.selectionGroup.setListComparator(comparator); 236 this.selectionGroup.addCheckStateListener(listener); 237 238 } 239 240 248 protected abstract void createSourceGroup(Composite parent); 249 250 253 protected String getErrorDialogTitle() { 254 return IDEWorkbenchMessages.WizardImportPage_errorDialogTitle; 255 } 256 257 268 protected IPath getContainerFullPath() { 269 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 270 271 IPath testPath = getResourcePath(); 273 274 if (testPath.equals(workspace.getRoot().getFullPath())) { 275 return testPath; 276 } 277 278 IStatus result = workspace.validatePath(testPath.toString(), 279 IResource.PROJECT | IResource.FOLDER | IResource.ROOT); 280 if (result.isOK()) { 281 return testPath; 282 } 283 284 return null; 285 } 286 287 291 protected abstract ITreeContentProvider getFileProvider(); 292 293 297 protected abstract ITreeContentProvider getFolderProvider(); 298 299 303 protected IPath getResourcePath() { 304 return getPathFromText(this.containerNameField); 305 } 306 307 315 protected java.util.List getSelectedResources() { 316 return this.selectionGroup.getAllCheckedListItems(); 317 } 318 319 324 protected void getSelectedResources(IElementFilter filter, 325 IProgressMonitor monitor) throws InterruptedException { 326 this.selectionGroup.getAllCheckedListItems(filter, monitor); 327 } 328 329 336 protected IContainer getSpecifiedContainer() { 337 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 338 IPath path = getContainerFullPath(); 339 if (workspace.getRoot().exists(path)){ 340 IResource resource = workspace.getRoot().findMember(path); 341 if(resource.getType() == IResource.FILE) { 342 return null; 343 } 344 return (IContainer) resource; 345 346 } 347 348 349 return null; 350 } 351 352 356 protected java.util.List getTypesToImport() { 357 358 return selectedTypes; 359 } 360 361 365 protected void handleContainerBrowseButtonPressed() { 366 IPath containerPath = queryForContainer(getSpecifiedContainer(), 368 IDEWorkbenchMessages.WizardImportPage_selectFolderLabel, 369 IDEWorkbenchMessages.WizardImportPage_selectFolderTitle); 370 371 if (containerPath != null) { setErrorMessage(null); 374 containerNameField.setText(containerPath.makeRelative().toString()); 375 } 376 } 377 378 384 public void handleEvent(Event event) { 385 Widget source = event.widget; 386 387 if (source == containerBrowseButton) { 388 handleContainerBrowseButtonPressed(); 389 } 390 391 updateWidgetEnablements(); 392 } 393 394 398 protected void handleTypesEditButtonPressed() { 399 400 TypeFilteringDialog dialog = new TypeFilteringDialog(getContainer() 401 .getShell(), getTypesToImport()); 402 403 dialog.open(); 404 405 Object [] newSelectedTypes = dialog.getResult(); 406 if (newSelectedTypes != null) { this.selectedTypes = new ArrayList (newSelectedTypes.length); 408 for (int i = 0; i < newSelectedTypes.length; i++) { 409 this.selectedTypes.add(newSelectedTypes[i]); 410 } 411 412 setupSelectionsBasedOnSelectedTypes(); 413 } 414 415 } 416 417 420 protected final void initialPopulateContainerField() { 421 if (initialContainerFieldValue != null) { 422 containerNameField.setText(initialContainerFieldValue); 423 } else if (currentResourceSelection != null) { 424 containerNameField.setText(currentResourceSelection.getFullPath() 425 .makeRelative().toString()); 426 } 427 } 428 429 433 protected void setAllSelections(boolean value) { 434 selectionGroup.setAllSelections(value); 435 } 436 437 443 public void setContainerFieldValue(String value) { 444 if (containerNameField == null) { 445 initialContainerFieldValue = value; 446 } else { 447 containerNameField.setText(value); 448 } 449 } 450 451 455 protected void setupSelectionsBasedOnSelectedTypes() { 456 } 457 458 462 protected void updateSelections(final Map map) { 463 464 Runnable runnable = new Runnable () { 465 public void run() { 466 selectionGroup.updateSelections(map); 467 } 468 }; 469 470 BusyIndicator.showWhile(getShell().getDisplay(), runnable); 471 } 472 473 476 protected void updateWidgetEnablements() { 477 478 boolean pageComplete = determinePageCompletion(); 479 setPageComplete(pageComplete); 480 if (pageComplete) { 481 setMessage(null); 482 } 483 super.updateWidgetEnablements(); 484 } 485 486 489 protected final boolean validateDestinationGroup() { 490 491 IPath containerPath = getContainerFullPath(); 492 if (containerPath == null) { 493 setMessage(EMPTY_FOLDER_MESSAGE); 494 return false; 495 } 496 497 IContainer container = getSpecifiedContainer(); 499 if (container == null) { 500 if(IDEWorkbenchPlugin.getPluginWorkspace().getRoot().exists(getContainerFullPath())) { 502 return false; 503 } 504 505 IWorkspace workspace = IDEWorkbenchPlugin.getPluginWorkspace(); 507 IPath projectPath = containerPath.removeLastSegments(containerPath 508 .segmentCount() - 1); 509 510 if (workspace.getRoot().exists(projectPath)) { 511 return true; 512 } 513 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_projectNotExist); 514 return false; 515 } 516 if (!container.isAccessible()) { 517 setErrorMessage(INACCESSABLE_FOLDER_MESSAGE); 518 return false; 519 } 520 if (container.getLocationURI() == null) { 521 if (container.isLinked()) { 522 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_undefinedPathVariable); 523 } else { 524 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_containerNotExist); 525 } 526 return false; 527 } 528 529 530 if (sourceConflictsWithDestination(containerPath)) { 531 setErrorMessage(getSourceConflictMessage()); 532 return false; 533 } 534 535 if (container instanceof IWorkspaceRoot){ 536 setErrorMessage(EMPTY_PROJECT_MESSAGE); 537 return false; 538 } 539 return true; 540 541 } 542 543 547 protected final String getSourceConflictMessage() { 548 return (IDEWorkbenchMessages.WizardImportPage_importOnReceiver); 549 } 550 551 560 protected boolean sourceConflictsWithDestination(IPath sourcePath) { 561 return false; 562 } 563 564 567 protected boolean determinePageCompletion() { 568 if (noOpenProjects()) { 570 setErrorMessage(IDEWorkbenchMessages.WizardImportPage_noOpenProjects); 571 return false; 572 } 573 return super.determinePageCompletion(); 574 } 575 576 581 private boolean noOpenProjects() { 582 IProject[] projects = IDEWorkbenchPlugin.getPluginWorkspace().getRoot() 583 .getProjects(); 584 for (int i = 0; i < projects.length; i++) { 585 if (projects[i].isOpen()) { 586 return false; 587 } 588 } 589 return true; 590 } 591 } 592 | Popular Tags |