1 11 package org.eclipse.ui.internal; 12 13 import org.eclipse.core.runtime.IConfigurationElement; 14 import org.eclipse.core.runtime.ISafeRunnable; 15 import org.eclipse.core.runtime.SafeRunner; 16 import org.eclipse.jface.action.IMenuCreator; 17 import org.eclipse.swt.widgets.Control; 18 import org.eclipse.swt.widgets.Menu; 19 import org.eclipse.ui.IActionDelegate; 20 import org.eclipse.ui.IWorkbenchWindow; 21 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate; 22 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 23 import org.eclipse.ui.WorkbenchException; 24 25 28 public class WWinPluginPulldown extends WWinPluginAction { 29 30 34 private final IMenuCreator menuProxy; 35 36 private class MenuProxy implements IMenuCreator { 37 38 44 private class MenuLoader implements ISafeRunnable { 45 46 50 private final Control control; 51 52 55 private final IWorkbenchWindowPulldownDelegate delegate; 56 57 61 private Menu menu = null; 62 63 67 private final Menu parent; 68 69 79 private MenuLoader( 80 final IWorkbenchWindowPulldownDelegate2 delegate, 81 final Menu parent) { 82 this.delegate = delegate; 83 this.parent = parent; 84 this.control = null; 85 } 86 87 97 private MenuLoader(final IWorkbenchWindowPulldownDelegate delegate, 98 final Control parent) { 99 this.delegate = delegate; 100 this.parent = null; 101 this.control = parent; 102 } 103 104 109 private Menu getMenu() { 110 return menu; 111 } 112 113 116 public void handleException(Throwable exception) { 117 } 119 120 123 public void run() throws Exception { 124 if (parent == null) { 125 menu = delegate.getMenu(control); 126 } else { 127 menu = ((IWorkbenchWindowPulldownDelegate2) delegate) 128 .getMenu(parent); 129 } 130 } 131 } 132 133 136 public Menu getMenu(Control parent) { 137 IWorkbenchWindowPulldownDelegate delegate = getPulldownDelegate(); 138 if (delegate != null) { 139 final MenuLoader menuLoader = new MenuLoader(delegate, parent); 140 SafeRunner.run(menuLoader); 141 return menuLoader.getMenu(); 142 } 143 144 return null; 145 } 146 147 150 public Menu getMenu(Menu parent) { 151 IWorkbenchWindowPulldownDelegate delegate = getPulldownDelegate(); 152 153 if (delegate instanceof IWorkbenchWindowPulldownDelegate2) { 154 IWorkbenchWindowPulldownDelegate2 delegate2 = (IWorkbenchWindowPulldownDelegate2) delegate; 155 final MenuLoader menuLoader = new MenuLoader(delegate2, parent); 156 SafeRunner.run(menuLoader); 157 return menuLoader.getMenu(); 158 } 159 160 return null; 161 } 162 163 166 public void dispose() { 167 } 169 } 170 171 186 public WWinPluginPulldown(IConfigurationElement actionElement, 187 IWorkbenchWindow window, String id, int style) { 188 super(actionElement, window, id, style); 189 menuProxy = new MenuProxy(); 190 setMenuCreator(menuProxy); 191 } 192 193 196 protected IActionDelegate validateDelegate(Object obj) 197 throws WorkbenchException { 198 if (obj instanceof IWorkbenchWindowPulldownDelegate) { 199 return (IWorkbenchWindowPulldownDelegate) obj; 200 } 201 202 throw new WorkbenchException( 203 "Action must implement IWorkbenchWindowPulldownDelegate"); } 205 206 210 protected IWorkbenchWindowPulldownDelegate getPulldownDelegate() { 211 IActionDelegate delegate = getDelegate(); 212 if (delegate == null) { 213 createDelegate(); 214 delegate = getDelegate(); 215 } 216 return (IWorkbenchWindowPulldownDelegate) delegate; 217 } 218 } 219 | Popular Tags |