1 11 package org.eclipse.pde.internal.ui.view; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.action.ActionContributionItem; 15 import org.eclipse.jface.action.IMenuCreator; 16 import org.eclipse.pde.internal.ui.PDEPluginImages; 17 import org.eclipse.pde.internal.ui.PDEUIMessages; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Menu; 21 import org.eclipse.swt.widgets.MenuItem; 22 23 public class HistoryDropDownAction extends Action implements 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 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 } 90 | Popular Tags |