001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.dialogs;
011:
012: import com.ibm.icu.text.Collator;
013: import java.util.ArrayList;
014: import java.util.Arrays;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.dialogs.IDialogSettings;
022: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
023: import org.eclipse.jface.resource.ImageDescriptor;
024: import org.eclipse.swt.SWT;
025: import org.eclipse.swt.events.DisposeEvent;
026: import org.eclipse.swt.events.DisposeListener;
027: import org.eclipse.swt.events.SelectionAdapter;
028: import org.eclipse.swt.events.SelectionEvent;
029: import org.eclipse.swt.events.SelectionListener;
030: import org.eclipse.swt.graphics.Font;
031: import org.eclipse.swt.graphics.Image;
032: import org.eclipse.swt.graphics.Point;
033: import org.eclipse.swt.graphics.Rectangle;
034: import org.eclipse.swt.layout.GridData;
035: import org.eclipse.swt.layout.GridLayout;
036: import org.eclipse.swt.widgets.Button;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.swt.widgets.Control;
039: import org.eclipse.swt.widgets.Label;
040: import org.eclipse.swt.widgets.Layout;
041: import org.eclipse.swt.widgets.Shell;
042: import org.eclipse.swt.widgets.Table;
043: import org.eclipse.swt.widgets.TableColumn;
044: import org.eclipse.swt.widgets.TableItem;
045: import org.eclipse.ui.IEditorDescriptor;
046: import org.eclipse.ui.IEditorInput;
047: import org.eclipse.ui.IEditorPart;
048: import org.eclipse.ui.IEditorReference;
049: import org.eclipse.ui.IEditorRegistry;
050: import org.eclipse.ui.IPerspectiveDescriptor;
051: import org.eclipse.ui.IWorkbenchPage;
052: import org.eclipse.ui.IWorkbenchWindow;
053: import org.eclipse.ui.PartInitException;
054: import org.eclipse.ui.PlatformUI;
055: import org.eclipse.ui.dialogs.SelectionDialog;
056: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
057: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
058: import org.eclipse.ui.internal.WorkbenchImages;
059: import org.eclipse.ui.internal.WorkbenchMessages;
060: import org.eclipse.ui.internal.WorkbenchPage;
061: import org.eclipse.ui.internal.WorkbenchPartReference;
062: import org.eclipse.ui.internal.WorkbenchPlugin;
063: import org.eclipse.ui.internal.layout.CellData;
064: import org.eclipse.ui.internal.layout.CellLayout;
065: import org.eclipse.ui.internal.layout.Row;
066: import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
067:
068: /**
069: * Implements a dialog showing all opened editors in the workbench
070: * and the recent closed editors
071: */
072: public class WorkbenchEditorsDialog extends SelectionDialog {
073:
074: private IWorkbenchWindow window;
075:
076: private Table editorsTable;
077:
078: private Button saveSelected;
079:
080: private Button closeSelected;
081:
082: private Button selectClean;
083:
084: private Button invertSelection;
085:
086: private Button allSelection;
087:
088: private boolean showAllPersp = false;
089:
090: private int sortColumn;
091:
092: private List elements = new ArrayList();
093:
094: private HashMap imageCache = new HashMap(11);
095:
096: private HashMap disabledImageCache = new HashMap(11);
097:
098: private boolean reverse = false;
099:
100: private Collator collator = Collator.getInstance();
101:
102: private Rectangle bounds;
103:
104: private int columnsWidth[];
105:
106: private static final String SORT = "sort"; //$NON-NLS-1$
107:
108: private static final String ALLPERSP = "allPersp"; //$NON-NLS-1$
109:
110: private static final String BOUNDS = "bounds"; //$NON-NLS-1$
111:
112: private static final String COLUMNS = "columns"; //$NON-NLS-1$
113:
114: private SelectionListener headerListener = new SelectionAdapter() {
115: public void widgetSelected(SelectionEvent e) {
116: int index = editorsTable.indexOf((TableColumn) e.widget);
117: if (index == sortColumn) {
118: reverse = !reverse;
119: } else {
120: sortColumn = index;
121: }
122: updateItems();
123: }
124: };
125:
126: /**
127: * Constructor for WorkbenchEditorsDialog.
128: *
129: * @param window the window
130: */
131: public WorkbenchEditorsDialog(IWorkbenchWindow window) {
132: super (window.getShell());
133: this .window = window;
134: setTitle(WorkbenchMessages.WorkbenchEditorsDialog_title);
135:
136: IDialogSettings s = getDialogSettings();
137: if (s.get(ALLPERSP) == null) {
138: sortColumn = 0;
139: } else {
140: showAllPersp = s.getBoolean(ALLPERSP);
141: sortColumn = s.getInt(SORT);
142: String[] array = s.getArray(BOUNDS);
143: if (array != null) {
144: bounds = new Rectangle(0, 0, 0, 0);
145: bounds.x = new Integer(array[0]).intValue();
146: bounds.y = new Integer(array[1]).intValue();
147: bounds.width = new Integer(array[2]).intValue();
148: bounds.height = new Integer(array[3]).intValue();
149: }
150: array = s.getArray(COLUMNS);
151: if (array != null) {
152: columnsWidth = new int[array.length];
153: for (int i = 0; i < columnsWidth.length; i++) {
154: columnsWidth[i] = new Integer(array[i]).intValue();
155: }
156: }
157: }
158: }
159:
160: /* (non-Javadoc)
161: * Method declared on Window.
162: */
163: protected void configureShell(Shell newShell) {
164: super .configureShell(newShell);
165: PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
166: IWorkbenchHelpContextIds.WORKBENCH_EDITORS_DIALOG);
167: }
168:
169: /*
170: * (non-Javadoc)
171: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
172: */
173: protected void createButtonsForButtonBar(Composite parent) {
174: // Typically we would use the parent's createButtonsForButtonBar.
175: // However, we only want a Cancel button and not an OK button. The
176: // OK button will be used later (in createDialogArea) to activate
177: // the selected editor.
178: createButton(parent, IDialogConstants.CANCEL_ID,
179: IDialogConstants.CANCEL_LABEL, false);
180: Button button = getButton(IDialogConstants.CANCEL_ID);
181: if (button != null) {
182: button
183: .setText(WorkbenchMessages.WorkbenchEditorsDialog_close);
184: }
185:
186: }
187:
188: /**
189: * Initialize the dialog bounds with the bounds saved
190: * from the settings.
191: */
192: protected void initializeBounds() {
193: if (bounds != null) {
194: getShell().setBounds(bounds);
195: } else {
196: super .initializeBounds();
197: }
198: }
199:
200: /**
201: * Creates the contents of this dialog, initializes the
202: * listener and the update thread.
203: */
204: protected Control createDialogArea(Composite parent) {
205:
206: initializeDialogUnits(parent);
207:
208: Font font = parent.getFont();
209:
210: Composite dialogArea = new Composite(parent, SWT.NONE);
211: CellLayout dialogAreaLayout = new CellLayout(1)
212: .setMargins(
213: convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN),
214: convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN))
215: .setSpacing(
216: convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING),
217: convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING))
218: .setRow(1, Row.growing());
219: dialogArea.setLayout(dialogAreaLayout);
220: dialogArea.setLayoutData(new GridData(GridData.FILL_BOTH));
221:
222: //Label over the table
223: Label l = new Label(dialogArea, SWT.NONE);
224: l.setText(WorkbenchMessages.WorkbenchEditorsDialog_label);
225: l.setFont(font);
226: l.setLayoutData(new CellData().align(SWT.FILL, SWT.CENTER));
227: //Table showing the editors name, full path and perspective
228: editorsTable = new Table(dialogArea, SWT.MULTI | SWT.BORDER
229: | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
230: editorsTable.setLinesVisible(true);
231: editorsTable.setHeaderVisible(true);
232: editorsTable.setFont(font);
233:
234: final int height = 16 * editorsTable.getItemHeight();
235: final int width = (int) (2.5 * height);
236:
237: CellData tableData = new CellData().align(SWT.FILL, SWT.FILL)
238: .setHint(CellData.OVERRIDE, width, height);
239:
240: editorsTable.setLayoutData(tableData);
241: editorsTable.setLayout(new Layout() {
242: protected Point computeSize(Composite composite, int wHint,
243: int hHint, boolean flushCache) {
244: return new Point(width, height);
245: }
246:
247: protected void layout(Composite composite,
248: 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: //Name column
262: TableColumn tc = new TableColumn(editorsTable, SWT.NONE);
263: tc.setResizable(true);
264: tc.setText(WorkbenchMessages.WorkbenchEditorsDialog_name);
265: tc.addSelectionListener(headerListener);
266: //Full path column
267: tc = new TableColumn(editorsTable, SWT.NONE);
268: tc.setResizable(true);
269: tc.setText(WorkbenchMessages.WorkbenchEditorsDialog_path);
270: tc.addSelectionListener(headerListener);
271:
272: // A composite for selection option buttons
273: 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: //Select clean editors button
281: selectClean = new Button(selectionButtons, SWT.PUSH);
282: selectClean
283: .setText(WorkbenchMessages.WorkbenchEditorsDialog_selectClean);
284: selectClean.addSelectionListener(new SelectionAdapter() {
285: public void widgetSelected(SelectionEvent e) {
286: editorsTable.setSelection(selectClean(editorsTable
287: .getItems()));
288: updateButtons();
289: }
290: });
291: selectClean.setFont(font);
292: setButtonLayoutData(selectClean);
293:
294: //Invert selection button
295: invertSelection = new Button(selectionButtons, SWT.PUSH);
296: invertSelection
297: .setText(WorkbenchMessages.WorkbenchEditorsDialog_invertSelection);
298: invertSelection.addSelectionListener(new SelectionAdapter() {
299: public void widgetSelected(SelectionEvent e) {
300: editorsTable.setSelection(invertedSelection(
301: editorsTable.getItems(), editorsTable
302: .getSelection()));
303: updateButtons();
304: }
305: });
306: invertSelection.setFont(font);
307: setButtonLayoutData(invertSelection);
308:
309: //Select all button
310: allSelection = new Button(selectionButtons, SWT.PUSH);
311: allSelection
312: .setText(WorkbenchMessages.WorkbenchEditorsDialog_allSelection);
313: allSelection.addSelectionListener(new SelectionAdapter() {
314: public void widgetSelected(SelectionEvent e) {
315: editorsTable.setSelection(editorsTable.getItems());
316: updateButtons();
317: }
318: });
319: allSelection.setFont(font);
320: setButtonLayoutData(allSelection);
321:
322: // A composite for selected editor action buttons
323: Composite actionButtons = new Composite(dialogArea, SWT.NULL);
324: Label actLabel = new Label(actionButtons, SWT.NULL);
325: actLabel.setFont(font);
326: GridLayout actLayout = new GridLayout();
327: actLayout.numColumns = 4;
328: actionButtons.setLayout(actLayout);
329:
330: // Activate selected editor button
331: createButton(actionButtons, IDialogConstants.OK_ID,
332: WorkbenchMessages.WorkbenchEditorsDialog_activate, true);
333:
334: //Close selected editors button
335: closeSelected = new Button(actionButtons, SWT.PUSH);
336: closeSelected
337: .setText(WorkbenchMessages.WorkbenchEditorsDialog_closeSelected);
338: closeSelected.addSelectionListener(new SelectionAdapter() {
339: public void widgetSelected(SelectionEvent e) {
340: closeItems(editorsTable.getSelection());
341: }
342: });
343: closeSelected.setFont(font);
344: setButtonLayoutData(closeSelected);
345:
346: //Save selected editors button
347: saveSelected = new Button(actionButtons, SWT.PUSH);
348: saveSelected
349: .setText(WorkbenchMessages.WorkbenchEditorsDialog_saveSelected);
350: saveSelected.addSelectionListener(new SelectionAdapter() {
351: public void widgetSelected(SelectionEvent e) {
352: saveItems(editorsTable.getSelection(), null);
353: }
354: });
355: saveSelected.setFont(font);
356: setButtonLayoutData(saveSelected);
357:
358: //Show only active perspective button
359: final Button showAllPerspButton = new Button(dialogArea,
360: SWT.CHECK);
361: showAllPerspButton
362: .setText(WorkbenchMessages.WorkbenchEditorsDialog_showAllPersp);
363: showAllPerspButton.setSelection(showAllPersp);
364: showAllPerspButton.setFont(font);
365: setButtonLayoutData(showAllPerspButton);
366: showAllPerspButton.addSelectionListener(new SelectionAdapter() {
367: public void widgetSelected(SelectionEvent e) {
368: showAllPersp = showAllPerspButton.getSelection();
369: updateItems();
370: }
371: });
372: //Create the items and update buttons state
373: updateItems();
374: updateButtons();
375:
376: editorsTable.addSelectionListener(new SelectionAdapter() {
377: public void widgetSelected(SelectionEvent e) {
378: updateButtons();
379: }
380:
381: public void widgetDefaultSelected(SelectionEvent e) {
382: okPressed();
383: }
384: });
385: editorsTable.addDisposeListener(new DisposeListener() {
386: public void widgetDisposed(DisposeEvent e) {
387: for (Iterator images = imageCache.values().iterator(); images
388: .hasNext();) {
389: Image i = (Image) images.next();
390: i.dispose();
391: }
392: for (Iterator images = disabledImageCache.values()
393: .iterator(); images.hasNext();) {
394: Image i = (Image) images.next();
395: i.dispose();
396: }
397: }
398: });
399: editorsTable.setFocus();
400: applyDialogFont(dialogArea);
401: return dialogArea;
402: }
403:
404: /**
405: * Updates the button state (enabled/disabled)
406: */
407: private void updateButtons() {
408: TableItem selectedItems[] = editorsTable.getSelection();
409: boolean hasDirty = false;
410: for (int i = 0; i < selectedItems.length; i++) {
411: Adapter editor = (Adapter) selectedItems[i].getData();
412: if (editor.isDirty()) {
413: hasDirty = true;
414: break;
415: }
416: }
417: saveSelected.setEnabled(hasDirty);
418:
419: TableItem allItems[] = editorsTable.getItems();
420: boolean hasClean = false;
421: for (int i = 0; i < allItems.length; i++) {
422: Adapter editor = (Adapter) allItems[i].getData();
423: if (!editor.isDirty()) {
424: hasClean = true;
425: break;
426: }
427: }
428: selectClean.setEnabled(hasClean);
429: invertSelection.setEnabled(allItems.length > 0);
430: closeSelected.setEnabled(selectedItems.length > 0);
431:
432: Button ok = getOkButton();
433: if (ok != null) {
434: ok.setEnabled(selectedItems.length == 1);
435: }
436: }
437:
438: /**
439: * Closes the specified editors
440: */
441: private void closeItems(TableItem items[]) {
442: if (items.length == 0) {
443: return;
444: }
445: for (int i = 0; i < items.length; i++) {
446: Adapter e = (Adapter) items[i].getData();
447: e.close();
448: }
449: updateItems();
450: }
451:
452: /**
453: * Saves the specified editors
454: */
455: private void saveItems(TableItem items[], IProgressMonitor monitor) {
456: if (items.length == 0) {
457: return;
458: }
459: ProgressMonitorDialog pmd = new ProgressMonitorJobsDialog(
460: getShell());
461: pmd.open();
462: for (int i = 0; i < items.length; i++) {
463: Adapter editor = (Adapter) items[i].getData();
464: editor.save(pmd.getProgressMonitor());
465: updateItem(items[i], editor);
466: }
467: pmd.close();
468: updateItems();
469: }
470:
471: /**
472: * Returns all clean editors from items[];
473: */
474: private TableItem[] selectClean(TableItem items[]) {
475: if (items.length == 0) {
476: return new TableItem[0];
477: }
478: ArrayList cleanItems = new ArrayList(items.length);
479: for (int i = 0; i < items.length; i++) {
480: Adapter editor = (Adapter) items[i].getData();
481: if (!editor.isDirty()) {
482: cleanItems.add(items[i]);
483: }
484: }
485: TableItem result[] = new TableItem[cleanItems.size()];
486: cleanItems.toArray(result);
487: return result;
488: }
489:
490: /**
491: * Returns all clean editors from items[];
492: */
493: private TableItem[] invertedSelection(TableItem allItems[],
494: TableItem selectedItems[]) {
495: if (allItems.length == 0) {
496: return allItems;
497: }
498: ArrayList invertedSelection = new ArrayList(allItems.length
499: - selectedItems.length);
500: outerLoop: for (int i = 0; i < allItems.length; i++) {
501: for (int j = 0; j < selectedItems.length; j++) {
502: if (allItems[i] == selectedItems[j]) {
503: continue outerLoop;
504: }
505: }
506: invertedSelection.add(allItems[i]);
507: }
508: TableItem result[] = new TableItem[invertedSelection.size()];
509: invertedSelection.toArray(result);
510: return result;
511: }
512:
513: /**
514: * Updates the specified item
515: */
516: private void updateItem(TableItem item, Adapter editor) {
517: item.setData(editor);
518: item.setText(editor.getText());
519: Image images[] = editor.getImage();
520: for (int i = 0; i < images.length; i++) {
521: if (images[i] != null) {
522: item.setImage(i, images[i]);
523: }
524: }
525: }
526:
527: /**
528: * Adds all editors to elements
529: */
530: private void updateEditors(IWorkbenchPage[] pages) {
531: for (int j = 0; j < pages.length; j++) {
532: IEditorReference editors[] = pages[j].getEditorReferences();
533: for (int k = 0; k < editors.length; k++) {
534: elements.add(new Adapter(editors[k]));
535: }
536: }
537: }
538:
539: /**
540: * Updates all items in the table
541: */
542: private void updateItems() {
543: editorsTable.removeAll();
544: elements = new ArrayList();
545: if (showAllPersp) {
546: IWorkbenchWindow windows[] = window.getWorkbench()
547: .getWorkbenchWindows();
548: for (int i = 0; i < windows.length; i++) {
549: updateEditors(windows[i].getPages());
550: }
551: } else {
552: IWorkbenchPage page = window.getActivePage();
553: if (page != null) {
554: updateEditors(new IWorkbenchPage[] { page });
555: }
556: }
557: sort();
558: Object selection = null;
559: if (window.getActivePage() != null) {
560: selection = window.getActivePage().getActiveEditor();
561: }
562: for (Iterator iterator = elements.iterator(); iterator
563: .hasNext();) {
564: Adapter e = (Adapter) iterator.next();
565: TableItem item = new TableItem(editorsTable, SWT.NULL);
566: updateItem(item, e);
567: if ((selection != null) && (selection == e.editorRef)) {
568: editorsTable.setSelection(new TableItem[] { item });
569: }
570: }
571: // update the buttons, because the selection may have changed
572: updateButtons();
573: }
574:
575: /**
576: * Sorts all the editors according to the table header
577: */
578: private void sort() {
579: //Backward compatible. Table used to have 3 columns.
580: if (sortColumn > (editorsTable.getColumnCount() - 1)) {
581: sortColumn = 0;
582: }
583: Adapter a[] = new Adapter[elements.size()];
584: elements.toArray(a);
585: Arrays.sort(a);
586: elements = Arrays.asList(a);
587: }
588:
589: /**
590: * The user has selected a resource and the dialog is closing.
591: */
592: protected void okPressed() {
593: TableItem items[] = editorsTable.getSelection();
594: if (items.length != 1) {
595: super .okPressed();
596: return;
597: }
598:
599: saveDialogSettings();
600:
601: Adapter selection = (Adapter) items[0].getData();
602: //It would be better to activate before closing the
603: //dialog but it does not work when the editor is in other
604: //window. Must investigate.
605: super .okPressed();
606: selection.activate();
607: }
608:
609: /**
610: * Saves the dialog settings.
611: */
612: private void saveDialogSettings() {
613: IDialogSettings s = getDialogSettings();
614: s.put(ALLPERSP, showAllPersp);
615: s.put(SORT, sortColumn);
616: bounds = getShell().getBounds();
617: String array[] = new String[4];
618: array[0] = String.valueOf(bounds.x);
619: array[1] = String.valueOf(bounds.y);
620: array[2] = String.valueOf(bounds.width);
621: array[3] = String.valueOf(bounds.height);
622: s.put(BOUNDS, array);
623: array = new String[editorsTable.getColumnCount()];
624: for (int i = 0; i < array.length; i++) {
625: array[i] = String.valueOf(editorsTable.getColumn(i)
626: .getWidth());
627: }
628: s.put(COLUMNS, array);
629: }
630:
631: /**
632: * Return a dialog setting section for this dialog
633: */
634: private IDialogSettings getDialogSettings() {
635: IDialogSettings settings = WorkbenchPlugin.getDefault()
636: .getDialogSettings();
637: IDialogSettings this Settings = settings.getSection(getClass()
638: .getName());
639: if (this Settings == null) {
640: this Settings = settings.addNewSection(getClass().getName());
641: }
642: return this Settings;
643: }
644:
645: /**
646: * A helper inner class to adapt EditorHistoryItem and IEditorPart
647: * in the same type.
648: */
649: private class Adapter implements Comparable {
650: IEditorReference editorRef;
651:
652: IEditorInput input;
653:
654: IEditorDescriptor desc;
655:
656: String text[];
657:
658: Image images[];
659:
660: Adapter(IEditorReference ref) {
661: editorRef = ref;
662: }
663:
664: Adapter(IEditorInput input, IEditorDescriptor desc) {
665: this .input = input;
666: this .desc = desc;
667: }
668:
669: boolean isDirty() {
670: if (editorRef == null) {
671: return false;
672: }
673: return editorRef.isDirty();
674: }
675:
676: boolean isOpened() {
677: return editorRef != null;
678: }
679:
680: void close() {
681: if (editorRef == null) {
682: return;
683: }
684: WorkbenchPage p = ((WorkbenchPartReference) editorRef)
685: .getPane().getPage();
686: p.closeEditor(editorRef, true);
687: }
688:
689: void save(IProgressMonitor monitor) {
690: if (editorRef == null) {
691: return;
692: }
693: IEditorPart editor = (IEditorPart) editorRef.getPart(true);
694: if (editor != null) {
695: editor.doSave(monitor);
696: }
697: }
698:
699: String[] getText() {
700: if (text != null) {
701: return text;
702: }
703: text = new String[2];
704: if (editorRef != null) {
705: if (editorRef.isDirty()) {
706: text[0] = "*" + editorRef.getTitle(); //$NON-NLS-1$
707: } else {
708: text[0] = editorRef.getTitle();
709: }
710: text[1] = editorRef.getTitleToolTip();
711: } else {
712: text[0] = input.getName();
713: text[1] = input.getToolTipText();
714: }
715: return text;
716: }
717:
718: Image[] getImage() {
719: if (images != null) {
720: return images;
721: }
722: images = new Image[2];
723: if (editorRef != null) {
724: images[0] = editorRef.getTitleImage();
725: WorkbenchPage p = ((WorkbenchPartReference) editorRef)
726: .getPane().getPage();
727: IPerspectiveDescriptor persp = p.getPerspective();
728: ImageDescriptor image = persp.getImageDescriptor();
729: if (image == null) {
730: image = WorkbenchImages
731: .getImageDescriptor(IWorkbenchGraphicConstants.IMG_ETOOL_DEF_PERSPECTIVE);
732: }
733: } else {
734: ImageDescriptor image = null;
735: if (desc != null) {
736: image = desc.getImageDescriptor();
737: }
738: if (image == null) {
739: IEditorRegistry registry = WorkbenchPlugin
740: .getDefault().getEditorRegistry();
741: image = registry
742: .getImageDescriptor(input.getName());
743: //TODO: how can this honour content types? Guessing at the content type perhaps?
744:
745: if (image == null) {
746: // @issue what should be the default image?
747: // image = registry.getDefaultEditor().getImageDescriptor();
748: }
749: }
750: if (image != null) {
751: images[0] = (Image) disabledImageCache.get(image);
752: if (images[0] == null) {
753: Image enabled = image.createImage();
754: Image disabled = new Image(editorsTable
755: .getDisplay(), enabled,
756: SWT.IMAGE_DISABLE);
757: enabled.dispose();
758: disabledImageCache.put(image, disabled);
759: images[0] = disabled;
760: }
761: }
762: }
763: return images;
764: }
765:
766: private void activate() {
767: if (editorRef != null) {
768: IEditorPart editor = editorRef.getEditor(true);
769: WorkbenchPage p = (WorkbenchPage) editor
770: .getEditorSite().getPage();
771: Shell s = p.getWorkbenchWindow().getShell();
772: if (s.getMinimized()) {
773: s.setMinimized(false);
774: }
775: s.moveAbove(null);
776: p.getWorkbenchWindow().setActivePage(p);
777: p.activate(editor);
778: } else {
779: IWorkbenchPage p = window.getActivePage();
780: if (p != null) {
781: try {
782: p.openEditor(input, desc.getId(), true);
783: } catch (PartInitException e) {
784: }
785: }
786: }
787: }
788:
789: public int compareTo(Object another) {
790: Adapter adapter = (Adapter) another;
791: int result = collator.compare(getText()[sortColumn],
792: adapter.getText()[sortColumn]);
793: if (result == 0) {
794: int column = sortColumn == 0 ? 1 : 0;
795: result = collator.compare(getText()[column], adapter
796: .getText()[column]);
797: }
798: if (reverse) {
799: return result * -1;
800: }
801: return result;
802: }
803: }
804: }
|