1 11 package org.eclipse.ui.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Hashtable ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Vector ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IProject; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.ResourcesPlugin; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.viewers.CheckStateChangedEvent; 27 import org.eclipse.jface.viewers.ICheckStateListener; 28 import org.eclipse.jface.viewers.IStructuredSelection; 29 import org.eclipse.jface.viewers.ITreeContentProvider; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.custom.BusyIndicator; 32 import org.eclipse.swt.events.SelectionAdapter; 33 import org.eclipse.swt.events.SelectionEvent; 34 import org.eclipse.swt.events.SelectionListener; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Shell; 41 import org.eclipse.ui.ide.IDE; 42 import org.eclipse.ui.internal.ide.DialogUtil; 43 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 44 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 45 import org.eclipse.ui.internal.ide.dialogs.ResourceTreeAndListGroup; 46 import org.eclipse.ui.model.WorkbenchContentProvider; 47 import org.eclipse.ui.model.WorkbenchLabelProvider; 48 49 76 public abstract class WizardExportResourcesPage extends WizardDataTransferPage { 77 private IStructuredSelection initialResourceSelection; 78 79 private List selectedTypes = new ArrayList (); 80 81 private ResourceTreeAndListGroup resourceGroup; 83 84 private final static String SELECT_TYPES_TITLE = IDEWorkbenchMessages.WizardTransferPage_selectTypes; 85 86 private final static String SELECT_ALL_TITLE = IDEWorkbenchMessages.WizardTransferPage_selectAll; 87 88 private final static String DESELECT_ALL_TITLE = IDEWorkbenchMessages.WizardTransferPage_deselectAll; 89 90 99 protected WizardExportResourcesPage(String pageName, 100 IStructuredSelection selection) { 101 super(pageName); 102 this.initialResourceSelection = selection; 103 } 104 105 110 protected boolean allowNewContainerName() { 111 return false; 112 } 113 114 135 protected Button createButton(Composite parent, int id, String label, 136 boolean defaultButton) { 137 ((GridLayout) parent.getLayout()).numColumns++; 139 140 Button button = new Button(parent, SWT.PUSH); 141 142 GridData buttonData = new GridData(GridData.FILL_HORIZONTAL); 143 button.setLayoutData(buttonData); 144 145 button.setData(new Integer (id)); 146 button.setText(label); 147 button.setFont(parent.getFont()); 148 149 if (defaultButton) { 150 Shell shell = parent.getShell(); 151 if (shell != null) { 152 shell.setDefaultButton(button); 153 } 154 button.setFocus(); 155 } 156 button.setFont(parent.getFont()); 157 setButtonLayoutData(button); 158 return button; 159 } 160 161 167 protected final void createButtonsGroup(Composite parent) { 168 169 Font font = parent.getFont(); 170 171 Composite buttonComposite = new Composite(parent, SWT.NONE); 173 buttonComposite.setFont(parent.getFont()); 174 175 GridLayout layout = new GridLayout(); 176 layout.numColumns = 3; 177 layout.makeColumnsEqualWidth = true; 178 buttonComposite.setLayout(layout); 179 buttonComposite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 180 | GridData.HORIZONTAL_ALIGN_FILL)); 181 182 Button selectTypesButton = createButton(buttonComposite, 184 IDialogConstants.SELECT_TYPES_ID, SELECT_TYPES_TITLE, false); 185 186 SelectionListener listener = new SelectionAdapter() { 187 public void widgetSelected(SelectionEvent e) { 188 handleTypesEditButtonPressed(); 189 } 190 }; 191 selectTypesButton.addSelectionListener(listener); 192 selectTypesButton.setFont(font); 193 setButtonLayoutData(selectTypesButton); 194 195 Button selectButton = createButton(buttonComposite, 196 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false); 197 198 listener = new SelectionAdapter() { 199 public void widgetSelected(SelectionEvent e) { 200 resourceGroup.setAllSelections(true); 201 } 202 }; 203 selectButton.addSelectionListener(listener); 204 selectButton.setFont(font); 205 setButtonLayoutData(selectButton); 206 207 Button deselectButton = createButton(buttonComposite, 208 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false); 209 210 listener = new SelectionAdapter() { 211 public void widgetSelected(SelectionEvent e) { 212 resourceGroup.setAllSelections(false); 213 } 214 }; 215 deselectButton.addSelectionListener(listener); 216 deselectButton.setFont(font); 217 setButtonLayoutData(deselectButton); 218 219 } 220 221 224 public void createControl(Composite parent) { 225 226 initializeDialogUnits(parent); 227 228 Composite composite = new Composite(parent, SWT.NULL); 229 composite.setLayout(new GridLayout()); 230 composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL 231 | GridData.HORIZONTAL_ALIGN_FILL)); 232 composite.setFont(parent.getFont()); 233 234 createResourcesGroup(composite); 235 createButtonsGroup(composite); 236 237 createDestinationGroup(composite); 238 239 createOptionsGroup(composite); 240 241 restoreResourceSpecificationWidgetValues(); restoreWidgetValues(); if (initialResourceSelection != null) { 244 setupBasedOnInitialSelections(); 245 } 246 247 updateWidgetEnablements(); 248 setPageComplete(determinePageCompletion()); 249 setErrorMessage(null); 251 setControl(composite); 252 } 253 254 262 protected abstract void createDestinationGroup(Composite parent); 263 264 269 protected final void createResourcesGroup(Composite parent) { 270 271 List input = new ArrayList (); 274 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot() 275 .getProjects(); 276 for (int i = 0; i < projects.length; i++) { 277 if (projects[i].isOpen()) { 278 input.add(projects[i]); 279 } 280 } 281 282 this.resourceGroup = new ResourceTreeAndListGroup(parent, input, 283 getResourceProvider(IResource.FOLDER | IResource.PROJECT), 284 WorkbenchLabelProvider.getDecoratingWorkbenchLabelProvider(), 285 getResourceProvider(IResource.FILE), WorkbenchLabelProvider 286 .getDecoratingWorkbenchLabelProvider(), SWT.NONE, 287 DialogUtil.inRegularFontMode(parent)); 288 289 ICheckStateListener listener = new ICheckStateListener() { 290 public void checkStateChanged(CheckStateChangedEvent event) { 291 updateWidgetEnablements(); 292 } 293 }; 294 295 this.resourceGroup.addCheckStateListener(listener); 296 } 297 298 301 protected String getErrorDialogTitle() { 302 return IDEWorkbenchMessages.WizardExportPage_errorDialogTitle; 303 } 304 305 311 protected boolean ensureResourcesLocal(List resources) { 312 return true; 313 } 314 315 324 protected List extractNonLocalResources(List originalList) { 325 Vector result = new Vector (originalList.size()); 326 Iterator resourcesEnum = originalList.iterator(); 327 328 while (resourcesEnum.hasNext()) { 329 IResource currentResource = (IResource) resourcesEnum.next(); 330 if (!currentResource.isLocal(IResource.DEPTH_ZERO)) { 331 result.addElement(currentResource); 332 } 333 } 334 335 return result; 336 } 337 338 342 private ITreeContentProvider getResourceProvider(final int resourceType) { 343 return new WorkbenchContentProvider() { 344 public Object [] getChildren(Object o) { 345 if (o instanceof IContainer) { 346 IResource[] members = null; 347 try { 348 members = ((IContainer) o).members(); 349 } catch (CoreException e) { 350 return new Object [0]; 352 } 353 354 ArrayList results = new ArrayList (); 356 for (int i = 0; i < members.length; i++) { 357 if ((members[i].getType() & resourceType) > 0) { 359 results.add(members[i]); 360 } 361 } 362 return results.toArray(); 363 } 364 if (o instanceof ArrayList ) { 366 return ((ArrayList ) o).toArray(); 367 } 368 return new Object [0]; 369 } 370 }; 371 } 372 373 381 protected List getSelectedResources() { 382 Iterator resourcesToExportIterator = this 383 .getSelectedResourcesIterator(); 384 List resourcesToExport = new ArrayList (); 385 while (resourcesToExportIterator.hasNext()) { 386 resourcesToExport.add(resourcesToExportIterator.next()); 387 } 388 return resourcesToExport; 389 } 390 391 400 protected Iterator getSelectedResourcesIterator() { 401 return this.resourceGroup.getAllCheckedListItems().iterator(); 402 } 403 404 410 protected List getTypesToExport() { 411 412 return selectedTypes; 413 } 414 415 423 protected List getWhiteCheckedResources() { 424 425 return this.resourceGroup.getAllWhiteCheckedItems(); 426 } 427 428 432 protected void handleTypesEditButtonPressed() { 433 Object [] newSelectedTypes = queryResourceTypesToExport(); 434 435 if (newSelectedTypes != null) { this.selectedTypes = new ArrayList (newSelectedTypes.length); 437 for (int i = 0; i < newSelectedTypes.length; i++) { 438 this.selectedTypes.add(newSelectedTypes[i]); 439 } 440 setupSelectionsBasedOnSelectedTypes(); 441 } 442 443 } 444 445 453 protected boolean hasExportableExtension(String resourceName) { 454 if (selectedTypes == null) { 455 return true; 456 } 457 458 int separatorIndex = resourceName.lastIndexOf("."); if (separatorIndex == -1) { 460 return false; 461 } 462 463 String extension = resourceName.substring(separatorIndex + 1); 464 465 Iterator it = selectedTypes.iterator(); 466 while (it.hasNext()) { 467 if (extension.equalsIgnoreCase((String ) it.next())) { 468 return true; 469 } 470 } 471 472 return false; 473 } 474 475 483 protected void internalSaveWidgetValues() { 484 } 485 486 494 protected Object [] queryResourceTypesToExport() { 495 496 TypeFilteringDialog dialog = new TypeFilteringDialog(getContainer() 497 .getShell(), getTypesToExport()); 498 499 dialog.open(); 500 501 return dialog.getResult(); 502 } 503 504 509 protected void restoreResourceSpecificationWidgetValues() { 510 } 511 512 518 protected void saveWidgetValues() { 519 520 internalSaveWidgetValues(); 522 523 } 524 525 528 protected void setupBasedOnInitialSelections() { 529 530 Iterator it = this.initialResourceSelection.iterator(); 531 while (it.hasNext()) { 532 IResource currentResource = (IResource) it.next(); 533 if (currentResource.getType() == IResource.FILE) { 534 this.resourceGroup.initialCheckListItem(currentResource); 535 } else { 536 this.resourceGroup.initialCheckTreeItem(currentResource); 537 } 538 } 539 } 540 541 544 private void setupSelectionsBasedOnSelectedTypes() { 545 546 Runnable runnable = new Runnable () { 547 public void run() { 548 Map selectionMap = new Hashtable (); 549 Iterator resourceIterator = resourceGroup 551 .getAllWhiteCheckedItems().iterator(); 552 while (resourceIterator.hasNext()) { 553 IResource resource = (IResource) resourceIterator.next(); 555 if (resource.getType() == IResource.FILE) { 556 if (hasExportableExtension(resource.getName())) { 557 List resourceList = new ArrayList (); 558 IContainer parent = resource.getParent(); 559 if (selectionMap.containsKey(parent)) { 560 resourceList = (List ) selectionMap.get(parent); 561 } 562 resourceList.add(resource); 563 selectionMap.put(parent, resourceList); 564 } 565 } else { 566 setupSelectionsBasedOnSelectedTypes(selectionMap, 567 (IContainer) resource); 568 } 569 } 570 resourceGroup.updateSelections(selectionMap); 571 } 572 }; 573 574 BusyIndicator.showWhile(getShell().getDisplay(), runnable); 575 576 } 577 578 583 private void setupSelectionsBasedOnSelectedTypes(Map selectionMap, 584 IContainer parent) { 585 586 List selections = new ArrayList (); 587 IResource[] resources; 588 boolean hasFiles = false; 589 590 try { 591 resources = parent.members(); 592 } catch (CoreException exception) { 593 return; 595 } 596 597 for (int i = 0; i < resources.length; i++) { 598 IResource resource = resources[i]; 599 if (resource.getType() == IResource.FILE) { 600 if (hasExportableExtension(resource.getName())) { 601 hasFiles = true; 602 selections.add(resource); 603 } 604 } else { 605 setupSelectionsBasedOnSelectedTypes(selectionMap, 606 (IContainer) resource); 607 } 608 } 609 610 if (hasFiles) { 612 selectionMap.put(parent, selections); 613 } 614 } 615 616 620 protected boolean saveDirtyEditors() { 621 return IDEWorkbenchPlugin.getDefault().getWorkbench().saveAllEditors( 622 true); 623 } 624 625 628 protected void updateWidgetEnablements() { 629 630 boolean pageComplete = determinePageCompletion(); 631 setPageComplete(pageComplete); 632 if (pageComplete) { 633 setMessage(null); 634 } 635 super.updateWidgetEnablements(); 636 } 637 } 638 | Popular Tags |