KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 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.callhierarchy;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Menu;
17 import org.eclipse.swt.widgets.MenuItem;
18
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.ActionContributionItem;
21 import org.eclipse.jface.action.IMenuCreator;
22
23 import org.eclipse.ui.PlatformUI;
24
25 import org.eclipse.jdt.core.IMethod;
26
27 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
28 import org.eclipse.jdt.internal.ui.JavaPluginImages;
29
30
31 class HistoryDropDownAction extends Action implements IMenuCreator {
32     
33     private static class ClearHistoryAction extends Action {
34
35         private CallHierarchyViewPart fView;
36         
37         public ClearHistoryAction(CallHierarchyViewPart view) {
38             super(CallHierarchyMessages.HistoryDropDownAction_clearhistory_label);
39             fView= view;
40         }
41             
42         public void run() {
43             fView.setHistoryEntries(new IMethod[0]);
44             fView.setMethod(null);
45         }
46     }
47     
48     public static final int RESULTS_IN_DROP_DOWN = 10;
49     private CallHierarchyViewPart fView;
50     private Menu fMenu;
51
52     public HistoryDropDownAction(CallHierarchyViewPart view) {
53         fView = view;
54         fMenu = null;
55         setToolTipText(CallHierarchyMessages.HistoryDropDownAction_tooltip);
56         JavaPluginImages.setLocalImageDescriptors(this, "history_list.gif"); //$NON-NLS-1$
57

58         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_HISTORY_DROP_DOWN_ACTION);
59
60         setMenuCreator(this);
61     }
62
63     public Menu getMenu(Menu parent) {
64         return null;
65     }
66
67     public Menu getMenu(Control parent) {
68         if (fMenu != null) {
69             fMenu.dispose();
70         }
71         fMenu= new Menu(parent);
72         IMethod[] elements= fView.getHistoryEntries();
73         addEntries(fMenu, elements);
74         new MenuItem(fMenu, SWT.SEPARATOR);
75         addActionToMenu(fMenu, new HistoryListAction(fView));
76         addActionToMenu(fMenu, new ClearHistoryAction(fView));
77         return fMenu;
78     }
79
80     public void dispose() {
81         fView = null;
82
83         if (fMenu != null) {
84             fMenu.dispose();
85             fMenu = null;
86         }
87     }
88
89     protected void addActionToMenu(Menu parent, Action action) {
90         ActionContributionItem item = new ActionContributionItem(action);
91         item.fill(parent, -1);
92     }
93
94     private boolean addEntries(Menu menu, IMethod[] elements) {
95         boolean checked = false;
96
97         int min = Math.min(elements.length, RESULTS_IN_DROP_DOWN);
98
99         for (int i = 0; i < min; i++) {
100             HistoryAction action = new HistoryAction(fView, elements[i]);
101             action.setChecked(elements[i].equals(fView.getMethod()));
102             checked = checked || action.isChecked();
103             addActionToMenu(menu, action);
104         }
105
106         return checked;
107     }
108
109     public void run() {
110         (new HistoryListAction(fView)).run();
111     }
112 }
113
Popular Tags