KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > EditorList


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.jface.action.IMenuListener;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.action.Separator;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.KeyEvent;
27 import org.eclipse.swt.events.KeyListener;
28 import org.eclipse.swt.events.MouseAdapter;
29 import org.eclipse.swt.events.MouseEvent;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Shell;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IEditorReference;
39 import org.eclipse.ui.IPartListener2;
40 import org.eclipse.ui.IPropertyListener;
41 import org.eclipse.ui.IWorkbenchPage;
42 import org.eclipse.ui.IWorkbenchPart;
43 import org.eclipse.ui.IWorkbenchPartReference;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.PlatformUI;
46
47 public class EditorList {
48     private WorkbenchWindow window;
49
50     private EditorStack workbook;
51
52     private Table editorsTable;
53
54     private static List JavaDoc editorListViews = new ArrayList JavaDoc();
55
56     private List JavaDoc elements = new ArrayList JavaDoc();
57
58     private SaveAction saveAction;
59
60     private CloseAction closeAction;
61
62     private SelectionAction selectCleanAction;
63
64     private SelectionAction InvertSelectionAction;
65
66     private SelectionAction selectAllAction;
67
68     private FullNameAction fullNameAction;
69
70     private SortAction nameSortAction;
71
72     private SortAction MRUSortAction;
73
74     private SetScopeAction windowScopeAction;
75
76     private SetScopeAction pageScopeAction;
77
78     private SetScopeAction tabGroupScopeAction;
79
80     private boolean dropDown; // initialized in constructor
81

82     // options same among all instances of editorList
83
private static int sortOrder; // initialized in constructor
84

85     private static int listScope; // initialized in constructor
86

87     private static boolean displayFullPath; // initialized in constructor
88

89     private static Collator collator = Collator.getInstance();
90
91     private static final int SELECT_ALL = 0;
92
93     private static final int INVERT_SELECTION = 1;
94
95     private static final int SELECT_CLEAN = 2;
96
97     private static final int NAME_SORT = IPreferenceConstants.EDITORLIST_NAME_SORT;
98
99     private static final int MRU_SORT = IPreferenceConstants.EDITORLIST_MRU_SORT;
100
101     private static final int SET_WINDOW_SCOPE = IPreferenceConstants.EDITORLIST_SET_WINDOW_SCOPE;
102
103     private static final int SET_PAGE_SCOPE = IPreferenceConstants.EDITORLIST_SET_PAGE_SCOPE;
104
105     private static final int SET_TAB_GROUP_SCOPE = IPreferenceConstants.EDITORLIST_SET_TAB_GROUP_SCOPE;
106
107     private static final String JavaDoc editorListData = "editorListData"; //$NON-NLS-1$
108

109     /**
110      * Listen for notifications from the editor part that its title has change or
111      * it's dirty, and update view
112      *
113      * @see IPropertyListener
114      */

115     private IPropertyListener propertyListener = new IPropertyListener() {
116         public void propertyChanged(Object JavaDoc source, int property) {
117             if (property == IEditorPart.PROP_DIRTY
118                     || property == IWorkbenchPart.PROP_TITLE) {
119                 if (source instanceof IEditorPart) {
120                     EditorSite site = (EditorSite) ((IEditorPart) source)
121                             .getEditorSite();
122                     IEditorReference ref = (IEditorReference) site.getPartReference();
123
124                     TableItem[] items = editorsTable.getItems();
125                     for (int i = 0; i < items.length; i++) {
126                         Adapter editor = (Adapter) items[i]
127                                 .getData(editorListData);
128                         if (editor.editorRef == ref) {
129                             updateItem(items[i], editor);
130                         }
131                     }
132                 }
133             }
134         }
135     };
136
137     private IPartListener2 partListener = new IPartListener2() {
138         int counter = 0;
139
140         private void updateEditorList(IWorkbenchPartReference ref) {
141             if (ref instanceof IEditorReference) {
142                 final Display display = window.getShell().getDisplay();
143                 final int TIMER_INTERVAL = 100;
144                 counter++;
145                 display.timerExec(TIMER_INTERVAL, new Runnable JavaDoc() {
146                     public void run() {
147                         counter--;
148                         // When closing the workbench the delay may allow the editorTable
149
// to dispose prior to running.
150
if ((counter == 0) && (editorsTable != null)) {
151                             updateItems();
152                             notifyEditorListViews();
153                         }
154                     }
155                 });
156             }
157         }
158
159         // select in navigator
160
public void partBroughtToTop(IWorkbenchPartReference ref) {
161             updateEditorList(ref);
162         }
163
164         // select tabs, open editor
165
public void partActivated(IWorkbenchPartReference ref) {
166             updateEditorList(ref);
167         }
168
169         // closeAll
170
public void partClosed(IWorkbenchPartReference ref) {
171             updateEditorList(ref);
172         }
173
174         // delete
175
public void partDeactivated(IWorkbenchPartReference ref) {
176             updateEditorList(ref);
177         }
178
179         public void partOpened(IWorkbenchPartReference ref) {
180         }
181
182         public void partHidden(IWorkbenchPartReference ref) {
183         }
184
185         public void partVisible(IWorkbenchPartReference ref) {
186         }
187
188         public void partInputChanged(IWorkbenchPartReference ref) {
189         }
190     };
191
192     public EditorList(IWorkbenchWindow window, EditorStack workbook) {
193         this.window = (WorkbenchWindow) window;
194         this.workbook = workbook;
195
196         listScope = WorkbenchPlugin.getDefault().getPreferenceStore().getInt(
197                 IPreferenceConstants.EDITORLIST_SELECTION_SCOPE);
198         sortOrder = WorkbenchPlugin.getDefault().getPreferenceStore().getInt(
199                 IPreferenceConstants.EDITORLIST_SORT_CRITERIA);
200         displayFullPath = WorkbenchPlugin.getDefault().getPreferenceStore()
201                 .getBoolean(IPreferenceConstants.EDITORLIST_DISPLAY_FULL_NAME);
202
203         // Special handling for scope selection. The concept of tab groups does
204
// not make sense in this situation, so over-ride to page scope
205
// uses dropDown for this
206
//
207
// in addition drop down used to indicate if the editorList must also
208
// close the viewform etc.
209
dropDown = (workbook != null);
210         if (!dropDown) {
211             // Views need to listen for part activation/deactivation.
212
window.getPartService().addPartListener(partListener);
213
214             // When selectionScope (listScope) is changed, or displayFullPath is
215
// changed there is no notification to the editorList views that this
216
// happened (i.e. if it happens from a different window or from
217
// the pullDown). Keep track of the views and update them
218
// appropriately.
219
editorListViews.add(this);
220         }
221
222         saveAction = new SaveAction();
223         closeAction = new CloseAction();
224         selectCleanAction = new SelectionAction(SELECT_CLEAN);
225         InvertSelectionAction = new SelectionAction(INVERT_SELECTION);
226         selectAllAction = new SelectionAction(SELECT_ALL);
227         fullNameAction = new FullNameAction();
228         nameSortAction = new SortAction(NAME_SORT);
229         MRUSortAction = new SortAction(MRU_SORT);
230         windowScopeAction = new SetScopeAction(SET_WINDOW_SCOPE);
231         pageScopeAction = new SetScopeAction(SET_PAGE_SCOPE);
232         tabGroupScopeAction = new SetScopeAction(SET_TAB_GROUP_SCOPE);
233     }
234
235     /**
236      * Create the EditorList table and menu items.
237      */

238     public Control createControl(Composite parent) {
239         editorsTable = new Table(parent, SWT.MULTI | SWT.V_SCROLL
240                 | SWT.H_SCROLL);
241         updateItems();
242         editorsTable.pack();
243         editorsTable.setFocus();
244
245         // Create the context menu
246
MenuManager menuMgr = new MenuManager("#PopUp"); //$NON-NLS-1$
247
menuMgr.setRemoveAllWhenShown(true);
248         menuMgr.addMenuListener(new IMenuListener() {
249             public void menuAboutToShow(IMenuManager manager) {
250                 setCheckedMenuItems();
251                 EditorList.this.fillContextMenu(manager);
252             }
253         });
254
255         editorsTable.setMenu(menuMgr.createContextMenu(editorsTable));
256         editorsTable.addKeyListener(new KeyListener() {
257             public void keyPressed(KeyEvent e) {
258                 if (e.character == SWT.ESC) {
259                     destroyControl();
260                 }
261                 if (e.character == ' ' || e.character == SWT.CR) {
262                     handleSelectionEvent(true);
263                 }
264             }
265
266             public void keyReleased(KeyEvent e) {
267             }
268         });
269
270         editorsTable.addMouseListener(new MouseAdapter() {
271             public void mouseDown(MouseEvent e) {
272                 if ((e.stateMask & SWT.CTRL) != 0
273                         || (e.stateMask & SWT.SHIFT) != 0) {
274                     return;
275                 }
276                 if (e.button != 3) {
277                     handleSelectionEvent(true);
278                 }
279             }
280         });
281
282         return editorsTable;
283     }
284
285     public void dispose() {
286         editorsTable = null;
287         editorListViews.remove(this);
288
289         // remove the listeners
290
elements = new ArrayList JavaDoc();
291         getAllEditors(elements);
292         for (Iterator JavaDoc iterator = elements.iterator(); iterator.hasNext();) {
293             Adapter e = (Adapter) iterator.next();
294             e.editorRef.removePropertyListener(propertyListener);
295         }
296         window.getPartService().removePartListener(partListener);
297     }
298
299     public void destroyControl() {
300         if (dropDown && (editorsTable != null)) {
301             Composite parent = editorsTable.getParent();
302             parent.dispose();
303             dispose();
304         }
305     }
306
307     public Control getControl() {
308         return editorsTable;
309     }
310
311     public int getItemCount() {
312         return editorsTable.getItemCount();
313     }
314
315     private void notifyEditorListViews() {
316         for (Iterator JavaDoc iterator = editorListViews.iterator(); iterator.hasNext();) {
317             EditorList editorList = (EditorList) iterator.next();
318             if (editorList != this) {
319                 editorList.updateItems();
320             }
321         }
322     }
323
324     private void handleSelectionEvent(boolean mouseEvent) {
325         TableItem[] selection = editorsTable.getSelection();
326         if (selection.length > 0) {
327             boolean enableSaveAction = false;
328             for (int i = 0; i < selection.length; i++) {
329                 Adapter editor = (Adapter) selection[i].getData(editorListData);
330                 if (editor.isDirty()) {
331                     enableSaveAction = true;
332                     break;
333                 }
334             }
335             saveAction.setEnabled(enableSaveAction);
336             closeAction.setEnabled(true);
337         } else {
338             saveAction.setEnabled(false);
339             closeAction.setEnabled(false);
340         }
341         if ((selection.length == 1) && mouseEvent) {
342             Adapter a = (Adapter) selection[0].getData(editorListData);
343             destroyControl();
344             a.activate(dropDown);
345         }
346         notifyEditorListViews();
347     }
348
349     private void setCheckedMenuItems() {
350         fullNameAction.setChecked(displayFullPath);
351         nameSortAction.setChecked(EditorList.sortOrder == NAME_SORT);
352         MRUSortAction.setChecked(EditorList.sortOrder == MRU_SORT);
353         windowScopeAction.setChecked(EditorList.listScope == SET_WINDOW_SCOPE);
354         pageScopeAction.setChecked(EditorList.listScope == SET_PAGE_SCOPE);
355         if (dropDown) {
356             tabGroupScopeAction
357                     .setChecked(EditorList.listScope == SET_TAB_GROUP_SCOPE);
358         } else {
359             tabGroupScopeAction.setEnabled(false);
360             if (listScope == SET_TAB_GROUP_SCOPE) {
361                 pageScopeAction.setChecked(true);
362             }
363         }
364     }
365
366     /**
367      * Updates the specified item
368      */

369     private void updateItem(TableItem item, Adapter editor) {
370         int index = fullNameAction.isChecked() ? 1 : 0;
371         item.setData(editorListData, editor);
372         item.setText(editor.getDisplayText()[index]);
373
374         Image image = editor.getImage();
375         if (image != null) {
376             item.setImage(image);
377         }
378
379         if (!dropDown) {
380             editor.editorRef.addPropertyListener(propertyListener);
381         }
382     }
383
384     /**
385      * Sorts the editors
386      */

387     private void sort() {
388         switch (sortOrder) {
389         case NAME_SORT:
390             Adapter a[] = new Adapter[elements.size()];
391             elements.toArray(a);
392             Arrays.sort(a);
393             elements = Arrays.asList(a);
394             break;
395         case MRU_SORT:
396             // The elements are already in MRU order
397
// TODO: Not in MRU if multiple windows open. Within each window
398
// group they are in order, but not overall
399
break;
400         default:
401             break;
402         }
403     }
404
405     /**
406      * Adds all editors to elements
407      */

408     private void updateEditors(IWorkbenchPage[] pages) {
409         for (int j = 0; j < pages.length; j++) {
410             IEditorReference editors[] = ((WorkbenchPage) pages[j])
411                     .getSortedEditors();
412             for (int k = editors.length - 1; k >= 0; k--) {
413                 elements.add(new Adapter(editors[k]));
414             }
415         }
416     }
417
418     private void getAllEditors(List JavaDoc elements) {
419         if (windowScopeAction.isChecked()) {
420             IWorkbenchWindow windows[] = window.getWorkbench()
421                     .getWorkbenchWindows();
422             for (int i = 0; i < windows.length; i++) {
423                 updateEditors(windows[i].getPages());
424             }
425             // TODO: when multiple windows open, loose files from one of the windows
426
// When the view is being restored the active window is not included
427
// in the collection. Handle this case, get editors from active page
428
if (!dropDown && elements.size() == 0) {
429                 IWorkbenchPage page = window.getActivePage();
430                 if (page != null) {
431                     updateEditors(new IWorkbenchPage[] { page });
432                 }
433             }
434         } else {
435             IWorkbenchPage page = window.getActivePage();
436             if (page != null) {
437                 if (pageScopeAction.isChecked()) {
438                     updateEditors(new IWorkbenchPage[] { page });
439                 } else {
440                     EditorPane editors[] = workbook.getEditors();
441                     for (int j = 0; j < editors.length; j++) {
442                         elements.add(new Adapter(editors[j]
443                                 .getEditorReference()));
444                     }
445                 }
446             }
447         }
448     }
449
450     /**
451      * Updates all items in the table
452      */

453     private void updateItems() {
454         setCheckedMenuItems();
455         editorsTable.removeAll();
456         elements = new ArrayList JavaDoc();
457         getAllEditors(elements);
458
459         sort();
460
461         Object JavaDoc selection = null;
462         if (window.getActivePage() != null) {
463             selection = window.getActivePage().getActiveEditor();
464         }
465         for (Iterator JavaDoc iterator = elements.iterator(); iterator.hasNext();) {
466             Adapter e = (Adapter) iterator.next();
467             TableItem item = new TableItem(editorsTable, SWT.NULL);
468             updateItem(item, e);
469             if ((selection != null)
470                     && (selection == e.editorRef.getPart(false))) {
471                 editorsTable.setSelection(new TableItem[] { item });
472                 saveAction.setEnabled(e.isDirty());
473             }
474         }
475     }
476
477     private void fillContextMenu(IMenuManager menuMgr) {
478         // SortBy SubMenu
479
MenuManager sortMenuMgr = new MenuManager(WorkbenchMessages.EditorList_SortBy_text);
480         sortMenuMgr.add(nameSortAction);
481         sortMenuMgr.add(MRUSortAction);
482
483         // ApplyTo SubMenu
484
MenuManager applyToMenuMgr = new MenuManager(WorkbenchMessages.EditorList_ApplyTo_text);
485         applyToMenuMgr.add(windowScopeAction);
486         applyToMenuMgr.add(pageScopeAction);
487         if (dropDown) {
488             applyToMenuMgr.add(tabGroupScopeAction);
489         }
490
491         // Main menu
492
menuMgr.add(saveAction);
493         menuMgr.add(closeAction);
494         menuMgr.add(new Separator());
495         menuMgr.add(selectCleanAction);
496         menuMgr.add(InvertSelectionAction);
497         menuMgr.add(selectAllAction);
498         menuMgr.add(new Separator());
499         menuMgr.add(fullNameAction);
500         menuMgr.add(sortMenuMgr);
501         menuMgr.add(applyToMenuMgr);
502     }
503
504     private class SaveAction extends Action {
505         /**
506          * Create an instance of this class
507          */

508         private SaveAction() {
509             setText(WorkbenchMessages.EditorList_saveSelected_text);
510             setToolTipText(WorkbenchMessages.EditorList_saveSelected_toolTip);
511             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
512                     IWorkbenchHelpContextIds.SAVE_ACTION);
513         }
514
515         /**
516          * Performs the save.
517          */

518         public void run() {
519             TableItem[] items = editorsTable.getSelection();
520             List JavaDoc dirtyEditorList = new ArrayList JavaDoc();
521
522             for (int i = 0; i < items.length; i++) {
523                 Adapter editor = (Adapter) items[i].getData(editorListData);
524                 Object JavaDoc element = editor.editorRef.getPart(false);
525                 if (editor.isDirty()) {
526                     dirtyEditorList.add(element);
527                 }
528             }
529
530             if (dirtyEditorList.size() != 0) {
531                 EditorManager.saveAll(dirtyEditorList, false, false, window);
532             }
533             destroyControl();
534         }
535     }
536
537     /**
538      * Closes the selected editor.
539      */

540     private class CloseAction extends Action {
541         /**
542          * Create an instance of this class
543          */

544         private CloseAction() {
545             setText(WorkbenchMessages.EditorList_closeSelected_text);
546             setToolTipText(WorkbenchMessages.EditorList_closeSelected_toolTip);
547             PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
548                     IWorkbenchHelpContextIds.CLOSE_PART_ACTION);
549         }
550
551         /**
552          * Close the selected editor.
553          */

554         public void run() {
555             TableItem[] items = editorsTable.getSelection();
556             Adapter[] editorRef = new Adapter[items.length];
557             List JavaDoc dirtyEditorList = new ArrayList JavaDoc();
558
559             // store the editor references as editorsTable will
560
// be disposed when focus is lost.
561
for (int i = 0; i < items.length; i++) {
562                 editorRef[i] = (Adapter) items[i].getData(editorListData);
563                 Object JavaDoc element = editorRef[i].editorRef.getPart(false);
564                 if (editorRef[i].isDirty()) {
565                     dirtyEditorList.add(element);
566                 }
567             }
568
569             boolean result = true;
570             if (dirtyEditorList.size() != 0) {
571                 result = EditorManager.saveAll(dirtyEditorList, true, true, window);
572             }
573
574             // now close the editors
575
if (result) {
576                 for (int i = 0; i < editorRef.length; i++) {
577                     editorRef[i].close();
578                 }
579             }
580
581             notifyEditorListViews();
582             destroyControl();
583         }
584     }
585
586     /**
587      * Selects editors.
588      */

589     private class SelectionAction extends Action {
590         private int selectionType;
591
592         /**
593          * Create an instance of this class
594          */

595         private SelectionAction(int selectionType) {
596             this.selectionType = selectionType;
597
598             switch (selectionType) {
599             case SELECT_ALL:
600                 setText(WorkbenchMessages.EditorList_selectAll_text);
601                 setToolTipText(WorkbenchMessages.EditorList_selectAll_toolTip);
602                 break;
603             case INVERT_SELECTION:
604                 setText(WorkbenchMessages.EditorList_invertSelection_text);
605                 setToolTipText(WorkbenchMessages.EditorList_invertSelection_toolTip);
606                 break;
607             case SELECT_CLEAN:
608                 setText(WorkbenchMessages.EditorList_selectClean_text);
609                 setToolTipText(WorkbenchMessages.EditorList_selectClean_toolTip);
610                 break;
611             default:
612                 break;
613             }
614             // WorkbenchHelp.setHelp(this, IHelpContextIds.SELECTION_ACTION);
615
}
616
617         private TableItem[] invertSelection(TableItem[] allItems,
618                 TableItem[] selectedItems) {
619             if (allItems.length == 0) {
620                 return allItems;
621             }
622             ArrayList JavaDoc invertedSelection = new ArrayList JavaDoc(allItems.length
623                     - selectedItems.length);
624             outerLoop: for (int i = 0; i < allItems.length; i++) {
625                 for (int j = 0; j < selectedItems.length; j++) {
626                     if (allItems[i] == selectedItems[j]) {
627                         continue outerLoop;
628                     }
629                 }
630                 invertedSelection.add(allItems[i]);
631             }
632
633             TableItem result[] = new TableItem[invertedSelection.size()];
634             invertedSelection.toArray(result);
635             return result;
636         }
637
638         private TableItem[] selectClean(TableItem[] allItems) {
639             if (allItems.length == 0) {
640                 return new TableItem[0];
641             }
642             ArrayList JavaDoc cleanItems = new ArrayList JavaDoc(allItems.length);
643             for (int i = 0; i < allItems.length; i++) {
644                 Adapter editor = (Adapter) allItems[i].getData(editorListData);
645                 if (!editor.isDirty()) {
646                     cleanItems.add(allItems[i]);
647                 }
648             }
649             TableItem result[] = new TableItem[cleanItems.size()];
650             cleanItems.toArray(result);
651
652             return result;
653         }
654
655         /**
656          * Select editors.
657          */

658         public void run() {
659             switch (selectionType) {
660             case SELECT_ALL:
661                 editorsTable.setSelection(editorsTable.getItems());
662                 break;
663             case INVERT_SELECTION:
664                 editorsTable.setSelection(invertSelection(editorsTable
665                         .getItems(), editorsTable.getSelection()));
666                 break;
667             case SELECT_CLEAN:
668                 editorsTable.setSelection(selectClean(editorsTable.getItems()));
669                 break;
670             }
671             handleSelectionEvent(false);
672         }
673     }
674
675     /**
676      * Displays the full file name.
677      */

678     private class FullNameAction extends Action {
679         /**
680          * Create an instance of this class
681          */

682         private FullNameAction() {
683             setText(WorkbenchMessages.EditorList_FullName_text);
684             setToolTipText(WorkbenchMessages.EditorList_FullName_toolTip);
685        
686         }
687
688         /**
689          * Display full file name.
690          */

691         public void run() {
692             displayFullPath = !displayFullPath;
693             WorkbenchPlugin.getDefault().getPreferenceStore().setValue(
694                     IPreferenceConstants.EDITORLIST_DISPLAY_FULL_NAME,
695                     displayFullPath);
696             setChecked(displayFullPath);
697             int[] indices = editorsTable.getSelectionIndices();
698             updateItems();
699             if (dropDown) {
700                 //TODO commented out for presentation work
701
//workbook.resizeEditorList();
702
}
703             editorsTable.setSelection(indices);
704             notifyEditorListViews();
705         }
706     }
707
708     private class SortAction extends Action {
709         private int sortOrder;
710
711         /**
712          * Create an instance of this class
713          */

714         private SortAction(int sortOrder) {
715             this.sortOrder = sortOrder;
716             switch (sortOrder) {
717             case NAME_SORT:
718                 setText(WorkbenchMessages.EditorList_SortByName_text);
719                 setToolTipText(WorkbenchMessages.EditorList_SortByName_toolTip);
720                 break;
721             case MRU_SORT:
722                 setText(WorkbenchMessages.EditorList_SortByMostRecentlyUsed_text);
723                 setToolTipText(WorkbenchMessages.EditorList_SortByMostRecentlyUsed_toolTip);
724                 break;
725             default:
726                 break;
727             }
728             // WorkbenchHelp.setHelp(this, IHelpContextIds.SORT_ACTION);
729
}
730
731         /**
732          * Performs the sort.
733          */

734         public void run() {
735             EditorList.sortOrder = this.sortOrder;
736             WorkbenchPlugin.getDefault().getPreferenceStore().setValue(
737                     IPreferenceConstants.EDITORLIST_SORT_CRITERIA,
738                     this.sortOrder);
739             TableItem[] items = editorsTable.getItems();
740             if (items.length == 0) {
741                 return;
742             }
743             updateItems();
744             notifyEditorListViews();
745         }
746     }
747
748     private class SetScopeAction extends Action {
749         private int whichScope;
750
751         /**
752          * Create an instance of this class
753          */

754         private SetScopeAction(int whichScope) {
755             this.whichScope = whichScope;
756             switch (whichScope) {
757             case SET_WINDOW_SCOPE:
758                 setText(WorkbenchMessages.EditorList_DisplayAllWindows_text);
759                 setToolTipText(WorkbenchMessages.EditorList_DisplayAllWindows_toolTip);
760                 break;
761             case SET_PAGE_SCOPE:
762                 setText(WorkbenchMessages.EditorList_DisplayAllPage_text);
763                 setToolTipText(WorkbenchMessages.EditorList_DisplayAllPage_toolTip);
764                 break;
765             case SET_TAB_GROUP_SCOPE:
766                 setText(WorkbenchMessages.EditorList_DisplayTabGroup_text);
767                 setToolTipText(WorkbenchMessages.EditorList_DisplayTabGroup_toolTip);
768                 break;
769             default:
770                 break;
771             }
772             // WorkbenchHelp.setHelp(this, IHelpContextIds.SORT_EDITOR_SCOPE_ACTION);
773
}
774
775         /**
776          * Display the appropriate scope.
777          */

778         public void run() {
779             EditorList.listScope = this.whichScope;
780             WorkbenchPlugin.getDefault().getPreferenceStore().setValue(
781                     IPreferenceConstants.EDITORLIST_SELECTION_SCOPE,
782                     this.whichScope);
783             updateItems();
784             if (dropDown) {
785                 //TODO commented out for presentation work
786
//workbook.resizeEditorList();
787
}
788             notifyEditorListViews();
789         }
790     }
791
792     /**
793      * A helper inner class
794      */

795     private class Adapter implements Comparable JavaDoc {
796         IEditorReference editorRef;
797
798         String JavaDoc text[], displayText[];
799
800         Image images[];
801
802         Adapter(IEditorReference ref) {
803             editorRef = ref;
804         }
805
806         boolean isDirty() {
807             return editorRef.isDirty();
808         }
809
810         void close() {
811             WorkbenchPage p = ((WorkbenchPartReference) editorRef).getPane()
812                     .getPage();
813             p.closeEditor(editorRef, false);
814         }
815
816         // file name without any dirty indication, used for sorting
817
String JavaDoc[] getText() {
818             text = new String JavaDoc[2];
819             text[0] = editorRef.getTitle();
820             text[1] = editorRef.getTitleToolTip();
821             return text;
822         }
823
824         // file name with dirty indication, used for displaying
825
String JavaDoc[] getDisplayText() {
826             displayText = new String JavaDoc[2];
827
828             if (editorRef.isDirty()) {
829                 displayText[0] = "*" + editorRef.getTitle(); //$NON-NLS-1$
830
displayText[1] = "*" + editorRef.getTitleToolTip(); //$NON-NLS-1$
831
} else {
832                 displayText[0] = editorRef.getTitle();
833                 displayText[1] = editorRef.getTitleToolTip();
834             }
835             return displayText;
836         }
837
838         Image getImage() {
839             return editorRef.getTitleImage();
840         }
841
842         private void activate(boolean activate) {
843             IEditorPart editor = editorRef.getEditor(true);
844             if (editor != null) {
845                 WorkbenchPage p = (WorkbenchPage) editor.getEditorSite()
846                         .getPage();
847                 Shell s = p.getWorkbenchWindow().getShell();
848                 if (s.getMinimized()) {
849                     s.setMinimized(false);
850                 }
851                 s.moveAbove(null);
852                 p.getWorkbenchWindow().setActivePage(p);
853                 if (activate) {
854                     if (editor == p.getActivePart()) {
855                         editor.setFocus();
856                     } else {
857                         p.activate(editor);
858                     }
859                 } else {
860                     p.bringToTop(editor);
861                 }
862             }
863         }
864
865         public int compareTo(Object JavaDoc another) {
866             int index = fullNameAction.isChecked() ? 1 : 0;
867             Adapter adapter = (Adapter) another;
868             int result = collator.compare(getText()[index],
869                     adapter.getText()[index]);
870             return result;
871         }
872     }
873 }
874
Popular Tags