1 11 12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Menu; 20 21 import org.eclipse.jface.action.ActionContributionItem; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.action.IMenuCreator; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 26 33 public class ClasspathModifierDropDownAction extends BuildpathModifierAction implements IMenuCreator { 34 35 36 private Menu fMenu; 37 private List fActions; 38 private BuildpathModifierAction fFirstValidAction; 40 41 46 public ClasspathModifierDropDownAction() { 47 super(null, null, BuildpathModifierAction.DROP_DOWN_ACTION, IAction.AS_DROP_DOWN_MENU); 48 49 fActions= new ArrayList (); 50 fFirstValidAction= null; 51 52 setText(""); setToolTipText(""); } 55 56 59 public String getDetailedDescription() { 60 if (fFirstValidAction != null) { 61 return fFirstValidAction.getDetailedDescription(); 62 } else if (fActions.size() > 0) { 63 return ((BuildpathModifierAction)fActions.get(0)).getDetailedDescription(); 64 } else { 65 return ""; } 67 } 68 69 72 public void run() { 73 fFirstValidAction.run(); 74 } 75 76 public IMenuCreator getMenuCreator() { 77 return this; 78 } 79 80 public Menu getMenu(Control parent) { 81 if (fMenu != null) { 82 fMenu.dispose(); 83 } 84 fMenu = new Menu(parent); 85 createEntries(fMenu); 86 return fMenu; 87 88 } 89 90 public Menu getMenu(Menu parent) { 91 return fMenu; 92 } 93 94 99 public void addAction(BuildpathModifierAction action) { 100 fActions.add(action); 101 update(); 102 } 103 104 109 public void removeAction(BuildpathModifierAction action) { 110 fActions.remove(action); 111 update(); 112 } 113 114 120 private void addActionToMenu(Menu parent, IAction action) { 121 ActionContributionItem item = new ActionContributionItem(action); 122 item.fill(parent, -1); 123 } 124 125 130 private void createEntries(Menu menu) { 131 for(int i= 0; i < fActions.size(); i++) { 132 IAction action= (IAction)fActions.get(i); 133 addActionToMenu(menu, action); 134 } 135 } 136 137 public void dispose() { 138 if (fMenu != null) { 139 fMenu.dispose(); 140 fMenu = null; 141 } 142 } 143 144 147 protected boolean canHandle(IStructuredSelection elements) { 148 update(); 149 return fFirstValidAction != null; 150 } 151 152 private void update() { 153 for (Iterator iterator= fActions.iterator(); iterator.hasNext();) { 154 BuildpathModifierAction action= (BuildpathModifierAction)iterator.next(); 155 if (action.isEnabled()) { 156 if (action != fFirstValidAction) { 157 updateButton(action); 158 } 159 fFirstValidAction= action; 160 return; 161 } 162 } 163 if (fFirstValidAction != null) { 164 if (fActions.size() > 0) { 165 updateButton((BuildpathModifierAction)fActions.get(0)); 166 } else { 167 updateButton(this); 168 } 169 } 170 fFirstValidAction= null; 171 } 172 173 private void updateButton(BuildpathModifierAction action) { 174 setImageDescriptor(action.getImageDescriptor()); 175 setDisabledImageDescriptor(action.getDisabledImageDescriptor()); 176 setText(action.getText()); 177 setToolTipText(action.getToolTipText()); 178 } 179 } 180 | Popular Tags |