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