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