1 11 12 package org.eclipse.ui.internal.actions; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.jface.action.IAction; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.swt.events.MenuAdapter; 23 import org.eclipse.swt.events.MenuEvent; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Menu; 26 import org.eclipse.swt.widgets.MenuItem; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.IWorkbenchWindowActionDelegate; 29 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 30 import org.eclipse.ui.IWorkingSet; 31 import org.eclipse.ui.activities.WorkbenchActivityHelper; 32 import org.eclipse.ui.internal.WorkbenchPlugin; 33 import org.eclipse.ui.internal.registry.WorkingSetRegistry; 34 35 40 public abstract class AbstractWorkingSetPulldownDelegate implements 41 IWorkbenchWindowActionDelegate, IWorkbenchWindowPulldownDelegate2 { 42 43 private Menu menubarMenu; 44 45 private Menu toolbarMenu; 46 47 private ISelection selection; 48 49 private IWorkbenchWindow window; 50 51 54 public AbstractWorkingSetPulldownDelegate() { 55 super(); 56 } 57 58 public void dispose() { 59 if (menubarMenu != null) { 60 menubarMenu.dispose(); 61 menubarMenu = null; 62 } 63 if (toolbarMenu != null) { 64 toolbarMenu.dispose(); 65 toolbarMenu = null; 66 } 67 } 68 69 public Menu getMenu(Control parent) { 70 if (toolbarMenu != null) { 71 toolbarMenu.dispose(); 72 } 73 toolbarMenu = new Menu(parent); 74 initMenu(toolbarMenu); 75 return toolbarMenu; 76 } 77 78 public Menu getMenu(Menu parent) { 79 if (menubarMenu != null) { 80 menubarMenu.dispose(); 81 } 82 menubarMenu = new Menu(parent); 83 initMenu(menubarMenu); 84 return menubarMenu; 85 } 86 87 90 private void initMenu(Menu menu) { 91 menu.addMenuListener(new MenuAdapter() { 92 public void menuShown(MenuEvent e) { 93 Menu m = (Menu) e.widget; 94 MenuItem[] items = m.getItems(); 95 for (int i = 0; i < items.length; i++) { 96 items[i].dispose(); 97 } 98 fillMenu(m); 99 } 100 101 }); 102 } 103 104 107 protected abstract void fillMenu(Menu m); 108 109 115 protected IWorkingSet[][] splitSets() { 116 IWorkingSet[] allSets = getWindow().getWorkbench().getWorkingSetManager() 117 .getWorkingSets(); 118 119 Map map = new HashMap (); 120 WorkingSetRegistry registry = WorkbenchPlugin.getDefault() 121 .getWorkingSetRegistry(); 122 123 for (int i = 0; i < allSets.length; i++) { 124 String setType = allSets[i].getId(); 125 if (WorkbenchActivityHelper.filterItem(registry 126 .getWorkingSetDescriptor(setType))) { 127 continue; 128 } 129 List setsOfType = (List ) map.get(setType); 130 if (setsOfType == null) { 131 setsOfType = new ArrayList (); 132 map.put(setType, setsOfType); 133 } 134 setsOfType.add(allSets[i]); 135 } 136 137 IWorkingSet[][] typedSets = new IWorkingSet[map.keySet().size()][]; 138 int i = 0; 139 for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { 140 List setsOfType = (List ) map.get(iter.next()); 141 typedSets[i] = new IWorkingSet[setsOfType.size()]; 142 setsOfType.toArray(typedSets[i++]); 143 } 144 return typedSets; 145 } 146 147 public void init(IWorkbenchWindow window) { 148 this.window = window; 149 } 150 151 protected IWorkbenchWindow getWindow() { 152 return window; 153 } 154 155 public void selectionChanged(IAction action, ISelection selection) { 156 this.selection = selection; 157 } 158 159 protected ISelection getSelection() { 160 return selection; 161 } 162 } | Popular Tags |