1 11 package org.eclipse.ui.internal.dialogs; 12 13 import com.ibm.icu.text.Collator; 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.IDialogSettings; 23 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 24 import org.eclipse.jface.resource.ImageDescriptor; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.DisposeEvent; 27 import org.eclipse.swt.events.DisposeListener; 28 import org.eclipse.swt.events.SelectionAdapter; 29 import org.eclipse.swt.events.SelectionEvent; 30 import org.eclipse.swt.events.SelectionListener; 31 import org.eclipse.swt.graphics.Font; 32 import org.eclipse.swt.graphics.Image; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.swt.graphics.Rectangle; 35 import org.eclipse.swt.layout.GridData; 36 import org.eclipse.swt.layout.GridLayout; 37 import org.eclipse.swt.widgets.Button; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Label; 41 import org.eclipse.swt.widgets.Layout; 42 import org.eclipse.swt.widgets.Shell; 43 import org.eclipse.swt.widgets.Table; 44 import org.eclipse.swt.widgets.TableColumn; 45 import org.eclipse.swt.widgets.TableItem; 46 import org.eclipse.ui.IEditorDescriptor; 47 import org.eclipse.ui.IEditorInput; 48 import org.eclipse.ui.IEditorPart; 49 import org.eclipse.ui.IEditorReference; 50 import org.eclipse.ui.IEditorRegistry; 51 import org.eclipse.ui.IPerspectiveDescriptor; 52 import org.eclipse.ui.IWorkbenchPage; 53 import org.eclipse.ui.IWorkbenchWindow; 54 import org.eclipse.ui.PartInitException; 55 import org.eclipse.ui.PlatformUI; 56 import org.eclipse.ui.dialogs.SelectionDialog; 57 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 58 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 59 import org.eclipse.ui.internal.WorkbenchImages; 60 import org.eclipse.ui.internal.WorkbenchMessages; 61 import org.eclipse.ui.internal.WorkbenchPage; 62 import org.eclipse.ui.internal.WorkbenchPartReference; 63 import org.eclipse.ui.internal.WorkbenchPlugin; 64 import org.eclipse.ui.internal.layout.CellData; 65 import org.eclipse.ui.internal.layout.CellLayout; 66 import org.eclipse.ui.internal.layout.Row; 67 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; 68 69 73 public class WorkbenchEditorsDialog extends SelectionDialog { 74 75 private IWorkbenchWindow window; 76 77 private Table editorsTable; 78 79 private Button saveSelected; 80 81 private Button closeSelected; 82 83 private Button selectClean; 84 85 private Button invertSelection; 86 87 private Button allSelection; 88 89 private boolean showAllPersp = false; 90 91 private int sortColumn; 92 93 private List elements = new ArrayList (); 94 95 private HashMap imageCache = new HashMap (11); 96 97 private HashMap disabledImageCache = new HashMap (11); 98 99 private boolean reverse = false; 100 101 private Collator collator = Collator.getInstance(); 102 103 private Rectangle bounds; 104 105 private int columnsWidth[]; 106 107 private static final String SORT = "sort"; 109 private static final String ALLPERSP = "allPersp"; 111 private static final String BOUNDS = "bounds"; 113 private static final String COLUMNS = "columns"; 115 private SelectionListener headerListener = new SelectionAdapter() { 116 public void widgetSelected(SelectionEvent e) { 117 int index = editorsTable.indexOf((TableColumn) e.widget); 118 if (index == sortColumn) { 119 reverse = !reverse; 120 } else { 121 sortColumn = index; 122 } 123 updateItems(); 124 } 125 }; 126 127 132 public WorkbenchEditorsDialog(IWorkbenchWindow window) { 133 super(window.getShell()); 134 this.window = window; 135 setTitle(WorkbenchMessages.WorkbenchEditorsDialog_title); 136 setShellStyle(getShellStyle() | SWT.RESIZE); 137 138 IDialogSettings s = getDialogSettings(); 139 if (s.get(ALLPERSP) == null) { 140 sortColumn = 0; 141 } else { 142 showAllPersp = s.getBoolean(ALLPERSP); 143 sortColumn = s.getInt(SORT); 144 String [] array = s.getArray(BOUNDS); 145 if (array != null) { 146 bounds = new Rectangle(0, 0, 0, 0); 147 bounds.x = new Integer (array[0]).intValue(); 148 bounds.y = new Integer (array[1]).intValue(); 149 bounds.width = new Integer (array[2]).intValue(); 150 bounds.height = new Integer (array[3]).intValue(); 151 } 152 array = s.getArray(COLUMNS); 153 if (array != null) { 154 columnsWidth = new int[array.length]; 155 for (int i = 0; i < columnsWidth.length; i++) { 156 columnsWidth[i] = new Integer (array[i]).intValue(); 157 } 158 } 159 } 160 } 161 162 165 protected void configureShell(Shell newShell) { 166 super.configureShell(newShell); 167 PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, 168 IWorkbenchHelpContextIds.WORKBENCH_EDITORS_DIALOG); 169 } 170 171 175 protected void createButtonsForButtonBar(Composite parent) { 176 createButton(parent, IDialogConstants.CANCEL_ID, 181 IDialogConstants.CANCEL_LABEL, false); 182 Button button = getButton(IDialogConstants.CANCEL_ID); 183 if (button != null) { 184 button.setText(WorkbenchMessages.WorkbenchEditorsDialog_close); 185 } 186 187 } 188 189 193 protected void initializeBounds() { 194 if (bounds != null) { 195 getShell().setBounds(bounds); 196 } else { 197 super.initializeBounds(); 198 } 199 } 200 201 205 protected Control createDialogArea(Composite parent) { 206 207 initializeDialogUnits(parent); 208 209 Font font = parent.getFont(); 210 211 Composite dialogArea = new Composite(parent, SWT.NONE); 212 CellLayout dialogAreaLayout = new CellLayout(1) 213 .setMargins( 214 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN), 215 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN)) 216 .setSpacing( 217 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING), 218 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING)) 219 .setRow(1, Row.growing()); 220 dialogArea.setLayout(dialogAreaLayout); 221 dialogArea.setLayoutData(new GridData(GridData.FILL_BOTH)); 222 223 Label l = new Label(dialogArea, SWT.NONE); 225 l.setText(WorkbenchMessages.WorkbenchEditorsDialog_label); 226 l.setFont(font); 227 l.setLayoutData(new CellData().align(SWT.FILL, SWT.CENTER)); 228 editorsTable = new Table(dialogArea, SWT.MULTI | SWT.BORDER 230 | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); 231 editorsTable.setLinesVisible(true); 232 editorsTable.setHeaderVisible(true); 233 editorsTable.setFont(font); 234 235 final int height = 16 * editorsTable.getItemHeight(); 236 final int width = (int) (2.5 * height); 237 238 CellData tableData = new CellData().align(SWT.FILL, SWT.FILL).setHint( 239 CellData.OVERRIDE, width, height); 240 241 editorsTable.setLayoutData(tableData); 242 editorsTable.setLayout(new Layout() { 243 protected Point computeSize(Composite composite, int wHint, 244 int hHint, boolean flushCache) { 245 return new Point(width, height); 246 } 247 248 protected void layout(Composite composite, boolean flushCache) { 249 TableColumn c[] = editorsTable.getColumns(); 250 if (columnsWidth == null) { 251 int w = editorsTable.getClientArea().width; 252 c[0].setWidth(w * 1 / 3); 253 c[1].setWidth(w - c[0].getWidth()); 254 } else { 255 c[0].setWidth(columnsWidth[0]); 256 c[1].setWidth(columnsWidth[1]); 257 } 258 editorsTable.setLayout(null); 259 } 260 }); 261 TableColumn tc = new TableColumn(editorsTable, SWT.NONE); 263 tc.setResizable(true); 264 tc.setText(WorkbenchMessages.WorkbenchEditorsDialog_name); 265 tc.addSelectionListener(headerListener); 266 tc = new TableColumn(editorsTable, SWT.NONE); 268 tc.setResizable(true); 269 tc.setText(WorkbenchMessages.WorkbenchEditorsDialog_path); 270 tc.addSelectionListener(headerListener); 271 272 Composite selectionButtons = new Composite(dialogArea, SWT.NULL); 274 Label compLabel = new Label(selectionButtons, SWT.NULL); 275 compLabel.setFont(font); 276 GridLayout layout = new GridLayout(); 277 layout.numColumns = 4; 278 selectionButtons.setLayout(layout); 279 280 selectClean = new Button(selectionButtons, SWT.PUSH); 282 selectClean.setText(WorkbenchMessages.WorkbenchEditorsDialog_selectClean); 283 selectClean.addSelectionListener(new SelectionAdapter() { 284 public void widgetSelected(SelectionEvent e) { 285 editorsTable.setSelection(selectClean(editorsTable.getItems())); 286 updateButtons(); 287 } 288 }); 289 selectClean.setFont(font); 290 setButtonLayoutData(selectClean); 291 292 invertSelection = new Button(selectionButtons, SWT.PUSH); 294 invertSelection.setText(WorkbenchMessages.WorkbenchEditorsDialog_invertSelection); 295 invertSelection.addSelectionListener(new SelectionAdapter() { 296 public void widgetSelected(SelectionEvent e) { 297 editorsTable.setSelection(invertedSelection(editorsTable 298 .getItems(), editorsTable.getSelection())); 299 updateButtons(); 300 } 301 }); 302 invertSelection.setFont(font); 303 setButtonLayoutData(invertSelection); 304 305 allSelection = new Button(selectionButtons, SWT.PUSH); 307 allSelection.setText(WorkbenchMessages.WorkbenchEditorsDialog_allSelection); 308 allSelection.addSelectionListener(new SelectionAdapter() { 309 public void widgetSelected(SelectionEvent e) { 310 editorsTable.setSelection(editorsTable.getItems()); 311 updateButtons(); 312 } 313 }); 314 allSelection.setFont(font); 315 setButtonLayoutData(allSelection); 316 317 Composite actionButtons = new Composite(dialogArea, SWT.NULL); 319 Label actLabel = new Label(actionButtons, SWT.NULL); 320 actLabel.setFont(font); 321 GridLayout actLayout = new GridLayout(); 322 actLayout.numColumns = 4; 323 actionButtons.setLayout(actLayout); 324 325 createButton(actionButtons, IDialogConstants.OK_ID, WorkbenchMessages.WorkbenchEditorsDialog_activate, 327 true); 328 329 closeSelected = new Button(actionButtons, SWT.PUSH); 331 closeSelected.setText(WorkbenchMessages.WorkbenchEditorsDialog_closeSelected); 332 closeSelected.addSelectionListener(new SelectionAdapter() { 333 public void widgetSelected(SelectionEvent e) { 334 closeItems(editorsTable.getSelection()); 335 } 336 }); 337 closeSelected.setFont(font); 338 setButtonLayoutData(closeSelected); 339 340 saveSelected = new Button(actionButtons, SWT.PUSH); 342 saveSelected.setText(WorkbenchMessages.WorkbenchEditorsDialog_saveSelected); 343 saveSelected.addSelectionListener(new SelectionAdapter() { 344 public void widgetSelected(SelectionEvent e) { 345 saveItems(editorsTable.getSelection(), null); 346 } 347 }); 348 saveSelected.setFont(font); 349 setButtonLayoutData(saveSelected); 350 351 final Button showAllPerspButton = new Button(dialogArea, SWT.CHECK); 353 showAllPerspButton.setText(WorkbenchMessages.WorkbenchEditorsDialog_showAllPersp); 354 showAllPerspButton.setSelection(showAllPersp); 355 showAllPerspButton.setFont(font); 356 setButtonLayoutData(showAllPerspButton); 357 showAllPerspButton.addSelectionListener(new SelectionAdapter() { 358 public void widgetSelected(SelectionEvent e) { 359 showAllPersp = showAllPerspButton.getSelection(); 360 updateItems(); 361 } 362 }); 363 updateItems(); 365 updateButtons(); 366 367 editorsTable.addSelectionListener(new SelectionAdapter() { 368 public void widgetSelected(SelectionEvent e) { 369 updateButtons(); 370 } 371 372 public void widgetDefaultSelected(SelectionEvent e) { 373 okPressed(); 374 } 375 }); 376 editorsTable.addDisposeListener(new DisposeListener() { 377 public void widgetDisposed(DisposeEvent e) { 378 for (Iterator images = imageCache.values().iterator(); images 379 .hasNext();) { 380 Image i = (Image) images.next(); 381 i.dispose(); 382 } 383 for (Iterator images = disabledImageCache.values().iterator(); images 384 .hasNext();) { 385 Image i = (Image) images.next(); 386 i.dispose(); 387 } 388 } 389 }); 390 editorsTable.setFocus(); 391 applyDialogFont(dialogArea); 392 return dialogArea; 393 } 394 395 398 private void updateButtons() { 399 TableItem selectedItems[] = editorsTable.getSelection(); 400 boolean hasDirty = false; 401 for (int i = 0; i < selectedItems.length; i++) { 402 Adapter editor = (Adapter) selectedItems[i].getData(); 403 if (editor.isDirty()) { 404 hasDirty = true; 405 break; 406 } 407 } 408 saveSelected.setEnabled(hasDirty); 409 410 TableItem allItems[] = editorsTable.getItems(); 411 boolean hasClean = false; 412 for (int i = 0; i < allItems.length; i++) { 413 Adapter editor = (Adapter) allItems[i].getData(); 414 if (!editor.isDirty()) { 415 hasClean = true; 416 break; 417 } 418 } 419 selectClean.setEnabled(hasClean); 420 invertSelection.setEnabled(allItems.length > 0); 421 closeSelected.setEnabled(selectedItems.length > 0); 422 423 Button ok = getOkButton(); 424 if (ok != null) { 425 ok.setEnabled(selectedItems.length == 1); 426 } 427 } 428 429 432 private void closeItems(TableItem items[]) { 433 if (items.length == 0) { 434 return; 435 } 436 for (int i = 0; i < items.length; i++) { 437 Adapter e = (Adapter) items[i].getData(); 438 e.close(); 439 } 440 updateItems(); 441 } 442 443 446 private void saveItems(TableItem items[], IProgressMonitor monitor) { 447 if (items.length == 0) { 448 return; 449 } 450 ProgressMonitorDialog pmd = new ProgressMonitorJobsDialog(getShell()); 451 pmd.open(); 452 for (int i = 0; i < items.length; i++) { 453 Adapter editor = (Adapter) items[i].getData(); 454 editor.save(pmd.getProgressMonitor()); 455 updateItem(items[i], editor); 456 } 457 pmd.close(); 458 updateItems(); 459 } 460 461 464 private TableItem[] selectClean(TableItem items[]) { 465 if (items.length == 0) { 466 return new TableItem[0]; 467 } 468 ArrayList cleanItems = new ArrayList (items.length); 469 for (int i = 0; i < items.length; i++) { 470 Adapter editor = (Adapter) items[i].getData(); 471 if (!editor.isDirty()) { 472 cleanItems.add(items[i]); 473 } 474 } 475 TableItem result[] = new TableItem[cleanItems.size()]; 476 cleanItems.toArray(result); 477 return result; 478 } 479 480 483 private TableItem[] invertedSelection(TableItem allItems[], 484 TableItem selectedItems[]) { 485 if (allItems.length == 0) { 486 return allItems; 487 } 488 ArrayList invertedSelection = new ArrayList (allItems.length 489 - selectedItems.length); 490 outerLoop: for (int i = 0; i < allItems.length; i++) { 491 for (int j = 0; j < selectedItems.length; j++) { 492 if (allItems[i] == selectedItems[j]) { 493 continue outerLoop; 494 } 495 } 496 invertedSelection.add(allItems[i]); 497 } 498 TableItem result[] = new TableItem[invertedSelection.size()]; 499 invertedSelection.toArray(result); 500 return result; 501 } 502 503 506 private void updateItem(TableItem item, Adapter editor) { 507 item.setData(editor); 508 item.setText(editor.getText()); 509 Image images[] = editor.getImage(); 510 for (int i = 0; i < images.length; i++) { 511 if (images[i] != null) { 512 item.setImage(i, images[i]); 513 } 514 } 515 } 516 517 520 private void updateEditors(IWorkbenchPage[] pages) { 521 for (int j = 0; j < pages.length; j++) { 522 IEditorReference editors[] = pages[j].getEditorReferences(); 523 for (int k = 0; k < editors.length; k++) { 524 elements.add(new Adapter(editors[k])); 525 } 526 } 527 } 528 529 532 private void updateItems() { 533 editorsTable.removeAll(); 534 elements = new ArrayList (); 535 if (showAllPersp) { 536 IWorkbenchWindow windows[] = window.getWorkbench() 537 .getWorkbenchWindows(); 538 for (int i = 0; i < windows.length; i++) { 539 updateEditors(windows[i].getPages()); 540 } 541 } else { 542 IWorkbenchPage page = window.getActivePage(); 543 if (page != null) { 544 updateEditors(new IWorkbenchPage[] { page }); 545 } 546 } 547 sort(); 548 Object selection = null; 549 if (window.getActivePage() != null) { 550 selection = window.getActivePage().getActiveEditor(); 551 } 552 for (Iterator iterator = elements.iterator(); iterator.hasNext();) { 553 Adapter e = (Adapter) iterator.next(); 554 TableItem item = new TableItem(editorsTable, SWT.NULL); 555 updateItem(item, e); 556 if ((selection != null) && (selection == e.editorRef)) { 557 editorsTable.setSelection(new TableItem[] { item }); 558 } 559 } 560 updateButtons(); 562 } 563 564 567 private void sort() { 568 if (sortColumn > (editorsTable.getColumnCount() - 1)) { 570 sortColumn = 0; 571 } 572 Adapter a[] = new Adapter[elements.size()]; 573 elements.toArray(a); 574 Arrays.sort(a); 575 elements = Arrays.asList(a); 576 } 577 578 581 protected void okPressed() { 582 TableItem items[] = editorsTable.getSelection(); 583 if (items.length != 1) { 584 super.okPressed(); 585 return; 586 } 587 588 saveDialogSettings(); 589 590 Adapter selection = (Adapter) items[0].getData(); 591 super.okPressed(); 595 selection.activate(); 596 } 597 598 601 private void saveDialogSettings() { 602 IDialogSettings s = getDialogSettings(); 603 s.put(ALLPERSP, showAllPersp); 604 s.put(SORT, sortColumn); 605 bounds = getShell().getBounds(); 606 String array[] = new String [4]; 607 array[0] = String.valueOf(bounds.x); 608 array[1] = String.valueOf(bounds.y); 609 array[2] = String.valueOf(bounds.width); 610 array[3] = String.valueOf(bounds.height); 611 s.put(BOUNDS, array); 612 array = new String [editorsTable.getColumnCount()]; 613 for (int i = 0; i < array.length; i++) { 614 array[i] = String.valueOf(editorsTable.getColumn(i).getWidth()); 615 } 616 s.put(COLUMNS, array); 617 } 618 619 622 private IDialogSettings getDialogSettings() { 623 IDialogSettings settings = WorkbenchPlugin.getDefault() 624 .getDialogSettings(); 625 IDialogSettings thisSettings = settings 626 .getSection(getClass().getName()); 627 if (thisSettings == null) { 628 thisSettings = settings.addNewSection(getClass().getName()); 629 } 630 return thisSettings; 631 } 632 633 637 private class Adapter implements Comparable { 638 IEditorReference editorRef; 639 640 IEditorInput input; 641 642 IEditorDescriptor desc; 643 644 String text[]; 645 646 Image images[]; 647 648 Adapter(IEditorReference ref) { 649 editorRef = ref; 650 } 651 652 Adapter(IEditorInput input, IEditorDescriptor desc) { 653 this.input = input; 654 this.desc = desc; 655 } 656 657 boolean isDirty() { 658 if (editorRef == null) { 659 return false; 660 } 661 return editorRef.isDirty(); 662 } 663 664 boolean isOpened() { 665 return editorRef != null; 666 } 667 668 void close() { 669 if (editorRef == null) { 670 return; 671 } 672 WorkbenchPage p = ((WorkbenchPartReference) editorRef).getPane() 673 .getPage(); 674 p.closeEditor(editorRef, true); 675 } 676 677 void save(IProgressMonitor monitor) { 678 if (editorRef == null) { 679 return; 680 } 681 IEditorPart editor = (IEditorPart) editorRef.getPart(true); 682 if (editor != null) { 683 editor.doSave(monitor); 684 } 685 } 686 687 String [] getText() { 688 if (text != null) { 689 return text; 690 } 691 text = new String [2]; 692 if (editorRef != null) { 693 if (editorRef.isDirty()) { 694 text[0] = "*" + editorRef.getTitle(); } else { 696 text[0] = editorRef.getTitle(); 697 } 698 text[1] = editorRef.getTitleToolTip(); 699 } else { 700 text[0] = input.getName(); 701 text[1] = input.getToolTipText(); 702 } 703 return text; 704 } 705 706 Image[] getImage() { 707 if (images != null) { 708 return images; 709 } 710 images = new Image[2]; 711 if (editorRef != null) { 712 images[0] = editorRef.getTitleImage(); 713 WorkbenchPage p = ((WorkbenchPartReference) editorRef) 714 .getPane().getPage(); 715 IPerspectiveDescriptor persp = p.getPerspective(); 716 ImageDescriptor image = persp.getImageDescriptor(); 717 if (image == null) { 718 image = WorkbenchImages 719 .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE); 720 } 721 } else { 722 ImageDescriptor image = null; 723 if (desc != null) { 724 image = desc.getImageDescriptor(); 725 } 726 if (image == null) { 727 IEditorRegistry registry = WorkbenchPlugin.getDefault() 728 .getEditorRegistry(); 729 image = registry.getImageDescriptor(input.getName()); 730 732 if (image == null) { 733 } 736 } 737 if (image != null) { 738 images[0] = (Image) disabledImageCache.get(image); 739 if (images[0] == null) { 740 Image enabled = image.createImage(); 741 Image disabled = new Image(editorsTable.getDisplay(), 742 enabled, SWT.IMAGE_DISABLE); 743 enabled.dispose(); 744 disabledImageCache.put(image, disabled); 745 images[0] = disabled; 746 } 747 } 748 } 749 return images; 750 } 751 752 private void activate() { 753 if (editorRef != null) { 754 IEditorPart editor = editorRef.getEditor(true); 755 WorkbenchPage p = (WorkbenchPage) editor.getEditorSite() 756 .getPage(); 757 Shell s = p.getWorkbenchWindow().getShell(); 758 if (s.getMinimized()) { 759 s.setMinimized(false); 760 } 761 s.moveAbove(null); 762 p.getWorkbenchWindow().setActivePage(p); 763 p.activate(editor); 764 } else { 765 IWorkbenchPage p = window.getActivePage(); 766 if (p != null) { 767 try { 768 p.openEditor(input, desc.getId(), true); 769 } catch (PartInitException e) { 770 } 771 } 772 } 773 } 774 775 public int compareTo(Object another) { 776 Adapter adapter = (Adapter) another; 777 int result = collator.compare(getText()[sortColumn], adapter 778 .getText()[sortColumn]); 779 if (result == 0) { 780 int column = sortColumn == 0 ? 1 : 0; 781 result = collator.compare(getText()[column], 782 adapter.getText()[column]); 783 } 784 if (reverse) { 785 return result * -1; 786 } 787 return result; 788 } 789 } 790 } 791 | Popular Tags |