KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > viewsupport > HistoryDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.viewsupport;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.events.MenuAdapter;
19 import org.eclipse.swt.events.MenuEvent;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Menu;
23
24 import org.eclipse.jface.action.Action;
25 import org.eclipse.jface.action.IMenuCreator;
26 import org.eclipse.jface.action.IMenuListener;
27 import org.eclipse.jface.action.IMenuManager;
28 import org.eclipse.jface.action.MenuManager;
29 import org.eclipse.jface.action.Separator;
30
31 import org.eclipse.ui.IWorkbenchActionConstants;
32
33 /*package*/ class HistoryDropDownAction extends Action {
34
35     private class HistoryAction extends Action {
36         private final Object JavaDoc fElement;
37
38         public HistoryAction(Object JavaDoc element, int accelerator) {
39             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
40
Assert.isNotNull(element);
41             fElement= element;
42
43             String JavaDoc label= fHistory.getText(element);
44             if (accelerator < 10) {
45                     //add the numerical accelerator
46
label= new StringBuffer JavaDoc().append('&').append(accelerator).append(' ').append(label).toString();
47             }
48
49             setText(label);
50             setImageDescriptor(fHistory.getImageDescriptor(element));
51         }
52
53         public void run() {
54             fHistory.setActiveEntry(fElement);
55         }
56     }
57
58     private class HistoryMenuCreator implements IMenuCreator {
59
60         public Menu getMenu(Menu parent) {
61             return null;
62         }
63
64         public Menu getMenu(Control parent) {
65             if (fMenu != null) {
66                 fMenu.dispose();
67             }
68             final MenuManager manager= new MenuManager();
69             manager.setRemoveAllWhenShown(true);
70             manager.addMenuListener(new IMenuListener() {
71                 public void menuAboutToShow(IMenuManager manager2) {
72                     List JavaDoc entries= fHistory.getHistoryEntries();
73                     boolean checkOthers= addEntryMenuItems(manager2, entries);
74                     
75                     manager2.add(new Separator());
76                     
77                     Action others= new HistoryListAction(fHistory);
78                     others.setChecked(checkOthers);
79                     manager2.add(others);
80                     
81                     Action clearAction= fHistory.getClearAction();
82                     if (clearAction != null) {
83                         manager2.add(clearAction);
84                     }
85                     
86                     manager2.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
87                     
88                     fHistory.addMenuEntries(manager);
89                 }
90                 
91                 private boolean addEntryMenuItems(IMenuManager manager2, List JavaDoc entries) {
92                     if (entries.isEmpty()) {
93                         return false;
94                     }
95                     
96                     boolean checkOthers= true;
97                     int min= Math.min(entries.size(), RESULTS_IN_DROP_DOWN);
98                     for (int i= 0; i < min; i++) {
99                         Object JavaDoc entry= entries.get(i);
100                         HistoryAction action= new HistoryAction(entry, i + 1);
101                         boolean check= entry.equals(fHistory.getCurrentEntry());
102                         action.setChecked(check);
103                         if (check)
104                             checkOthers= false;
105                         manager2.add(action);
106                     }
107                     return checkOthers;
108                 }
109             });
110             
111             fMenu= manager.createContextMenu(parent);
112             
113             //workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=129973
114
final Display display= parent.getDisplay();
115             fMenu.addMenuListener(new MenuAdapter() {
116                 public void menuHidden(final MenuEvent e) {
117                     display.asyncExec(new Runnable JavaDoc() {
118                         public void run() {
119                             manager.removeAll();
120                             if (fMenu != null) {
121                                 fMenu.dispose();
122                                 fMenu= null;
123                             }
124                         }
125                     });
126                 }
127             });
128             return fMenu;
129         }
130
131         public void dispose() {
132             fHistory= null;
133         
134             if (fMenu != null) {
135                 fMenu.dispose();
136                 fMenu= null;
137             }
138         }
139     }
140
141     public static final int RESULTS_IN_DROP_DOWN= 10;
142
143     private ViewHistory fHistory;
144     private Menu fMenu;
145
146     public HistoryDropDownAction(ViewHistory history) {
147         fHistory= history;
148         fMenu= null;
149         setMenuCreator(new HistoryMenuCreator());
150         fHistory.configureHistoryDropDownAction(this);
151     }
152
153     public void run() {
154         new HistoryListAction(fHistory).run();
155     }
156 }
157
Popular Tags