01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.views.dependencies;
11:
12: import org.eclipse.jface.action.Action;
13: import org.eclipse.jface.action.ActionContributionItem;
14: import org.eclipse.jface.action.IMenuCreator;
15: import org.eclipse.pde.internal.ui.PDEPluginImages;
16: import org.eclipse.pde.internal.ui.PDEUIMessages;
17: import org.eclipse.swt.SWT;
18: import org.eclipse.swt.widgets.Control;
19: import org.eclipse.swt.widgets.Menu;
20: import org.eclipse.swt.widgets.MenuItem;
21:
22: public class HistoryDropDownAction extends Action implements
23: IMenuCreator {
24:
25: public static final int RESULTS_IN_DROP_DOWN = 10;
26:
27: private Menu fMenu;
28:
29: private DependenciesView fView;
30:
31: public HistoryDropDownAction(DependenciesView view) {
32: fView = view;
33: fMenu = null;
34: setToolTipText(PDEUIMessages.HistoryDropDownAction_tooltip);
35: setImageDescriptor(PDEPluginImages.DESC_HISTORY_LIST);
36: setDisabledImageDescriptor(PDEPluginImages.DESC_HISTORY_LIST_DISABLED);
37: setMenuCreator(this );
38: }
39:
40: protected void addActionToMenu(Menu parent, Action action) {
41: ActionContributionItem item = new ActionContributionItem(action);
42: item.fill(parent, -1);
43: }
44:
45: private boolean addEntries(Menu menu, String[] elements) {
46: boolean checked = false;
47:
48: int min = Math.min(elements.length, RESULTS_IN_DROP_DOWN);
49: for (int i = 0; i < min; i++) {
50: HistoryAction action = new HistoryAction(fView, elements[i]);
51: action.setChecked(elements[i].equals(fView.getInput()));
52: checked = checked || action.isChecked();
53: addActionToMenu(menu, action);
54: }
55: return checked;
56: }
57:
58: public void dispose() {
59: // action is reused, can be called several times.
60: if (fMenu != null) {
61: fMenu.dispose();
62: fMenu = null;
63: }
64: }
65:
66: public Menu getMenu(Control parent) {
67: if (fMenu != null) {
68: fMenu.dispose();
69: }
70: fMenu = new Menu(parent);
71: String[] elements = fView.getHistoryEntries();
72: boolean checked = addEntries(fMenu, elements);
73: if (elements.length > RESULTS_IN_DROP_DOWN) {
74: new MenuItem(fMenu, SWT.SEPARATOR);
75: Action others = new HistoryListAction(fView);
76: others.setChecked(checked);
77: addActionToMenu(fMenu, others);
78: }
79: return fMenu;
80: }
81:
82: public Menu getMenu(Menu parent) {
83: return null;
84: }
85:
86: public void run() {
87: (new HistoryListAction(fView)).run();
88: }
89: }
|