KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > typehierarchy > 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.typehierarchy;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Menu;
16 import org.eclipse.swt.widgets.MenuItem;
17
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.ActionContributionItem;
20 import org.eclipse.jface.action.IMenuCreator;
21
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.jdt.core.IJavaElement;
25
26 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
27 import org.eclipse.jdt.internal.ui.JavaPluginImages;
28
29 public class HistoryDropDownAction extends Action implements IMenuCreator {
30     
31     public static class ClearHistoryAction extends Action {
32
33         private TypeHierarchyViewPart fView;
34         
35         public ClearHistoryAction(TypeHierarchyViewPart view) {
36             super(TypeHierarchyMessages.HistoryDropDownAction_clearhistory_label);
37             fView= view;
38         }
39             
40         public void run() {
41             fView.setHistoryEntries(new IJavaElement[0]);
42             fView.setInputElement(null);
43         }
44     }
45
46     public static final int RESULTS_IN_DROP_DOWN= 10;
47
48     private TypeHierarchyViewPart fHierarchyView;
49     private Menu fMenu;
50     
51     public HistoryDropDownAction(TypeHierarchyViewPart view) {
52         fHierarchyView= view;
53         fMenu= null;
54         setToolTipText(TypeHierarchyMessages.HistoryDropDownAction_tooltip);
55         JavaPluginImages.setLocalImageDescriptors(this, "history_list.gif"); //$NON-NLS-1$
56
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.TYPEHIERARCHY_HISTORY_ACTION);
57         setMenuCreator(this);
58     }
59
60     public void dispose() {
61         // action is reused, can be called several times.
62
if (fMenu != null) {
63             fMenu.dispose();
64             fMenu= null;
65         }
66     }
67
68     public Menu getMenu(Menu parent) {
69         return null;
70     }
71
72     public Menu getMenu(Control parent) {
73         if (fMenu != null) {
74             fMenu.dispose();
75         }
76         fMenu= new Menu(parent);
77         IJavaElement[] elements= fHierarchyView.getHistoryEntries();
78         addEntries(fMenu, elements);
79         new MenuItem(fMenu, SWT.SEPARATOR);
80         addActionToMenu(fMenu, new HistoryListAction(fHierarchyView));
81         addActionToMenu(fMenu, new ClearHistoryAction(fHierarchyView));
82         return fMenu;
83     }
84     
85     private boolean addEntries(Menu menu, IJavaElement[] elements) {
86         boolean checked= false;
87         
88         int min= Math.min(elements.length, RESULTS_IN_DROP_DOWN);
89         for (int i= 0; i < min; i++) {
90             HistoryAction action= new HistoryAction(fHierarchyView, elements[i]);
91             action.setChecked(elements[i].equals(fHierarchyView.getInputElement()));
92             checked= checked || action.isChecked();
93             addActionToMenu(menu, action);
94         }
95         
96         
97         return checked;
98     }
99     
100
101     protected void addActionToMenu(Menu parent, Action action) {
102         ActionContributionItem item= new ActionContributionItem(action);
103         item.fill(parent, -1);
104     }
105
106     public void run() {
107         (new HistoryListAction(fHierarchyView)).run();
108     }
109 }
110
Popular Tags