|
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.e4.ui.workbench.renderers.swt; |
| 12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.lang.reflect.Method; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.HashSet; |
| 18 |
import java.util.List; |
| 19 |
import javax.inject.Inject; |
| 20 |
import org.eclipse.core.commands.ParameterizedCommand; |
| 21 |
import org.eclipse.core.runtime.ISafeRunnable; |
| 22 |
import org.eclipse.core.runtime.SafeRunner; |
| 23 |
import org.eclipse.e4.core.commands.EHandlerService; |
| 24 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 25 |
import org.eclipse.e4.core.services.log.Logger; |
| 26 |
import org.eclipse.e4.ui.internal.workbench.ContributionsAnalyzer; |
| 27 |
import org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer; |
| 28 |
import org.eclipse.e4.ui.internal.workbench.swt.Policy; |
| 29 |
import org.eclipse.e4.ui.internal.workbench.swt.WorkbenchSWTActivator; |
| 30 |
import org.eclipse.e4.ui.model.application.MApplication; |
| 31 |
import org.eclipse.e4.ui.model.application.ui.MContext; |
| 32 |
import org.eclipse.e4.ui.model.application.ui.MCoreExpression; |
| 33 |
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; |
| 34 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenu; |
| 35 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; |
| 36 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; |
| 37 |
import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu; |
| 38 |
import org.eclipse.e4.ui.model.application.ui.menu.MRenderedMenu; |
| 39 |
import org.eclipse.e4.ui.workbench.modeling.EModelService; |
| 40 |
import org.eclipse.e4.ui.workbench.modeling.ExpressionContext; |
| 41 |
import org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer.ContributionRecord; |
| 42 |
import org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory; |
| 43 |
import org.eclipse.jface.action.MenuManager; |
| 44 |
import org.eclipse.swt.SWT; |
| 45 |
import org.eclipse.swt.widgets.Event; |
| 46 |
import org.eclipse.swt.widgets.Listener; |
| 47 |
import org.eclipse.swt.widgets.Menu; |
| 48 |
import org.eclipse.swt.widgets.Widget; |
| 49 |
|
| 50 |
public class MenuManagerRendererFilter implements Listener { |
| 51 |
public static final String NUL_MENU_ITEM = "(None Applicable)"; //$NON-NLS-1$ |
| 52 |
|
| 53 |
private static final String TMP_ORIGINAL_CONTEXT = "MenuServiceFilter.original.context"; //$NON-NLS-1$ |
| 54 |
|
| 55 |
private static void trace(String msg, Widget menu, MMenu menuModel) { |
| 56 |
WorkbenchSWTActivator.trace(Policy.MENUS, msg + ": " + menu + ": " //$NON-NLS-1$ //$NON-NLS-2$ |
| 57 |
+ menuModel, null); |
| 58 |
} |
| 59 |
|
| 60 |
private static Method aboutToShow; |
| 61 |
|
| 62 |
private static Method aboutToHide; |
| 63 |
|
| 64 |
public static Method getAboutToShow() { |
| 65 |
if (aboutToShow == null) { |
| 66 |
try { |
| 67 |
aboutToShow = MenuManager.class |
| 68 |
.getDeclaredMethod("handleAboutToShow"); //$NON-NLS-1$ |
| 69 |
aboutToShow.setAccessible(true); |
| 70 |
} catch (SecurityException e) { |
| 71 |
// TODO Auto-generated catch block |
| 72 |
e.printStackTrace(); |
| 73 |
} catch (NoSuchMethodException e) { |
| 74 |
// TODO Auto-generated catch block |
| 75 |
e.printStackTrace(); |
| 76 |
} |
| 77 |
} |
| 78 |
return aboutToShow; |
| 79 |
} |
| 80 |
|
| 81 |
public static Method getAboutToHide() { |
| 82 |
if (aboutToHide == null) { |
| 83 |
try { |
| 84 |
aboutToHide = MenuManager.class |
| 85 |
.getDeclaredMethod("handleAboutToHide"); //$NON-NLS-1$ |
| 86 |
aboutToHide.setAccessible(true); |
| 87 |
} catch (SecurityException e) { |
| 88 |
// TODO Auto-generated catch block |
| 89 |
e.printStackTrace(); |
| 90 |
} catch (NoSuchMethodException e) { |
| 91 |
// TODO Auto-generated catch block |
| 92 |
e.printStackTrace(); |
| 93 |
} |
| 94 |
} |
| 95 |
return aboutToHide; |
| 96 |
} |
| 97 |
|
| 98 |
@Inject |
| 99 |
private MApplication application; |
| 100 |
|
| 101 |
@Inject |
| 102 |
private Logger logger; |
| 103 |
|
| 104 |
@Inject |
| 105 |
private EModelService modelService; |
| 106 |
|
| 107 |
@Inject |
| 108 |
private IRendererFactory rendererFactory; |
| 109 |
|
| 110 |
private HashMap<Menu, Runnable> pendingCleanup = new HashMap<Menu, Runnable>(); |
| 111 |
|
| 112 |
private class SafeWrapper implements ISafeRunnable { |
| 113 |
Event event; |
| 114 |
|
| 115 |
public void handleException(Throwable e) { |
| 116 |
if (e instanceof Error) { |
| 117 |
// errors are deadly, we shouldn't ignore these |
| 118 |
throw (Error) e; |
| 119 |
} |
| 120 |
// log exceptions otherwise |
| 121 |
if (logger != null) { |
| 122 |
logger.error(e); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public void run() throws Exception { |
| 127 |
safeHandleEvent(event); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private SafeWrapper safeWrapper = new SafeWrapper(); |
| 132 |
|
| 133 |
public void handleEvent(final Event event) { |
| 134 |
// wrap the handling in a SafeRunner so that exceptions do not prevent |
| 135 |
// the menu from being shown |
| 136 |
safeWrapper.event = event; |
| 137 |
SafeRunner.run(safeWrapper); |
| 138 |
} |
| 139 |
|
| 140 |
private void safeHandleEvent(Event event) { |
| 141 |
if (!(event.widget instanceof Menu)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
final Menu menu = (Menu) event.widget; |
| 145 |
if (event.type == SWT.Dispose) { |
| 146 |
trace("handleMenu.Dispose", menu, null); //$NON-NLS-1$ |
| 147 |
cleanUp(menu); |
| 148 |
} |
| 149 |
Object obj = menu.getData(AbstractPartRenderer.OWNING_ME); |
| 150 |
if (obj == null && menu.getParentItem() != null) { |
| 151 |
obj = menu.getParentItem().getData(AbstractPartRenderer.OWNING_ME); |
| 152 |
} |
| 153 |
if (obj instanceof MPopupMenu) { |
| 154 |
handleContextMenu(event, menu, (MPopupMenu) obj); |
| 155 |
} else if (obj instanceof MMenu) { |
| 156 |
handleMenu(event, menu, (MMenu) obj); |
| 157 |
} else { |
| 158 |
trace("Incorrect menu model to work with: " + obj, menu, null); //$NON-NLS-1$ |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
private void handleMenu(final Event event, final Menu menu, |
| 163 |
final MMenu menuModel) { |
| 164 |
if ((menu.getStyle() & SWT.BAR) != 0) { |
| 165 |
// don't process the menu bar, it's not fair :-) |
| 166 |
return; |
| 167 |
} |
| 168 |
switch (event.type) { |
| 169 |
case SWT.Show: |
| 170 |
trace("handleMenu.Show", menu, menuModel); //$NON-NLS-1$ |
| 171 |
cleanUp(menu); |
| 172 |
showMenu(event, menu, menuModel); |
| 173 |
break; |
| 174 |
case SWT.Hide: |
| 175 |
trace("handleMenu.Hide", menu, menuModel); //$NON-NLS-1$ |
| 176 |
// TODO we'll clean up on show |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
public void showMenu(final Event event, final Menu menu, |
| 182 |
final MMenu menuModel) { |
| 183 |
AbstractPartRenderer obj = rendererFactory.getRenderer(menuModel, |
| 184 |
menu.getParent()); |
| 185 |
if (!(obj instanceof MenuManagerRenderer)) { |
| 186 |
trace("Not the correct renderer: " + obj, menu, menuModel); //$NON-NLS-1$ |
| 187 |
return; |
| 188 |
} |
| 189 |
MenuManagerRenderer renderer = (MenuManagerRenderer) obj; |
| 190 |
MenuManager menuManager = renderer.getManager(menuModel); |
| 191 |
if (menuModel.getWidget() == null) { |
| 192 |
renderer.bindWidget(menuModel, menuManager.getMenu()); |
| 193 |
} |
| 194 |
|
| 195 |
Method handleAboutToShow = getAboutToShow(); |
| 196 |
try { |
| 197 |
handleAboutToShow.invoke(menuManager); |
| 198 |
} catch (IllegalArgumentException e) { |
| 199 |
// TODO Auto-generated catch block |
| 200 |
e.printStackTrace(); |
| 201 |
} catch (IllegalAccessException e) { |
| 202 |
// TODO Auto-generated catch block |
| 203 |
e.printStackTrace(); |
| 204 |
} catch (InvocationTargetException e) { |
| 205 |
// TODO Auto-generated catch block |
| 206 |
e.printStackTrace(); |
| 207 |
} |
| 208 |
|
| 209 |
// |
| 210 |
// TODO merge MRenderedMenuItems back into the model |
| 211 |
// this will make the 3.x world visible to the model |
| 212 |
// without actually modeling it fully |
| 213 |
// |
| 214 |
|
| 215 |
final IEclipseContext evalContext; |
| 216 |
if (menuModel instanceof MContext) { |
| 217 |
evalContext = ((MContext) menuModel).getContext(); |
| 218 |
} else { |
| 219 |
evalContext = modelService.getContainingContext(menuModel); |
| 220 |
} |
| 221 |
updateElementVisibility(menuModel, renderer, menuManager, evalContext, |
| 222 |
true); |
| 223 |
|
| 224 |
// last thing to do, kill the event and update the menu manager |
| 225 |
event.type = SWT.None; |
| 226 |
event.doit = false; |
| 227 |
menuManager.update(false); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* @param menuModel |
| 232 |
* @param renderer |
| 233 |
* @param menuManager |
| 234 |
* @param evalContext |
| 235 |
*/ |
| 236 |
private void updateElementVisibility(final MMenu menuModel, |
| 237 |
MenuManagerRenderer renderer, MenuManager menuManager, |
| 238 |
final IEclipseContext evalContext, boolean recurse) { |
| 239 |
final ExpressionContext exprContext = new ExpressionContext(evalContext); |
| 240 |
HashSet<ContributionRecord> records = new HashSet<ContributionRecord>(); |
| 241 |
for (MMenuElement element : menuModel.getChildren()) { |
| 242 |
ContributionRecord record = renderer.getContributionRecord(element); |
| 243 |
if (record != null) { |
| 244 |
if (records.add(record)) { |
| 245 |
record.updateVisibility(evalContext); |
| 246 |
} |
| 247 |
} else { |
| 248 |
updateVisibility(menuManager, element, exprContext); |
| 249 |
} |
| 250 |
if (recurse && element.isVisible() && element instanceof MMenu) { |
| 251 |
MMenu childMenu = (MMenu) element; |
| 252 |
MenuManager childManager = renderer.getManager(childMenu); |
| 253 |
if (childManager != null) { |
| 254 |
updateElementVisibility(childMenu, renderer, childManager, |
| 255 |
evalContext, false); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* @param menuManager |
| 263 |
* @param element |
| 264 |
* @param evalContext |
| 265 |
*/ |
| 266 |
private void updateVisibility(MenuManager menuManager, |
| 267 |
MMenuElement element, ExpressionContext evalContext) { |
| 268 |
if (!(element.getVisibleWhen() instanceof MCoreExpression)) { |
| 269 |
return; |
| 270 |
} |
| 271 |
boolean val = ContributionsAnalyzer.isVisible( |
| 272 |
(MCoreExpression) element.getVisibleWhen(), evalContext); |
| 273 |
if (val != element.isVisible()) { |
| 274 |
element.setVisible(val); |
| 275 |
menuManager.markDirty(); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
private void handleContextMenu(final Event event, final Menu menu, |
| 280 |
final MPopupMenu menuModel) { |
| 281 |
switch (event.type) { |
| 282 |
case SWT.Show: |
| 283 |
trace("handleContextMenu.Show", menu, menuModel); //$NON-NLS-1$ |
| 284 |
cleanUp(menu); |
| 285 |
showPopup(event, menu, menuModel); |
| 286 |
break; |
| 287 |
case SWT.Hide: |
| 288 |
trace("handleContextMenu.Hide", menu, menuModel); //$NON-NLS-1$ |
| 289 |
hidePopup(event, menu, menuModel); |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
public void hidePopup(Event event, Menu menu, MPopupMenu menuModel) { |
| 295 |
final IEclipseContext popupContext = menuModel.getContext(); |
| 296 |
final IEclipseContext originalChild = (IEclipseContext) popupContext |
| 297 |
.get(TMP_ORIGINAL_CONTEXT); |
| 298 |
popupContext.remove(TMP_ORIGINAL_CONTEXT); |
| 299 |
if (!menu.isDisposed()) { |
| 300 |
menu.getDisplay().asyncExec(new Runnable() { |
| 301 |
public void run() { |
| 302 |
if (originalChild == null) { |
| 303 |
popupContext.deactivate(); |
| 304 |
} else { |
| 305 |
originalChild.activate(); |
| 306 |
} |
| 307 |
} |
| 308 |
}); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
public void showPopup(final Event event, final Menu menu, |
| 313 |
final MPopupMenu menuModel) { |
| 314 |
// System.err.println("showPopup: " + menuModel + "\n\t" + menu); |
| 315 |
// we need some context foolery here |
| 316 |
final IEclipseContext popupContext = menuModel.getContext(); |
| 317 |
final IEclipseContext parentContext = popupContext.getParent(); |
| 318 |
final IEclipseContext originalChild = parentContext.getActiveChild(); |
| 319 |
popupContext.activate(); |
| 320 |
popupContext.set(TMP_ORIGINAL_CONTEXT, originalChild); |
| 321 |
|
| 322 |
final ArrayList<MMenuContribution> toContribute = new ArrayList<MMenuContribution>(); |
| 323 |
final ArrayList<MMenuElement> menuContributionsToRemove = new ArrayList<MMenuElement>(); |
| 324 |
ExpressionContext eContext = new ExpressionContext(popupContext); |
| 325 |
ContributionsAnalyzer.gatherMenuContributions(menuModel, |
| 326 |
application.getMenuContributions(), menuModel.getElementId(), |
| 327 |
toContribute, eContext, true); |
| 328 |
|
| 329 |
for (String tag : menuModel.getTags()) { |
| 330 |
if (tag.startsWith("popup:") && tag.length() > 6) { //$NON-NLS-1$ |
| 331 |
ContributionsAnalyzer.gatherMenuContributions(menuModel, |
| 332 |
application.getMenuContributions(), tag.substring(6), |
| 333 |
toContribute, eContext, false); |
| 334 |
} |
| 335 |
} |
| 336 |
ContributionsAnalyzer.addMenuContributions(menuModel, toContribute, |
| 337 |
menuContributionsToRemove); |
| 338 |
|
| 339 |
// create a cleanup routine for the Hide or next Show |
| 340 |
pendingCleanup.put(menu, new Runnable() { |
| 341 |
public void run() { |
| 342 |
if (!menu.isDisposed()) { |
| 343 |
unrender(menuContributionsToRemove); |
| 344 |
} |
| 345 |
removeMenuContributions(menuModel, menuContributionsToRemove); |
| 346 |
} |
| 347 |
}); |
| 348 |
render(menu, menuModel); |
| 349 |
} |
| 350 |
|
| 351 |
private void render(final Menu menu, final MMenu menuModel) { |
| 352 |
trace("render", menu, menuModel); //$NON-NLS-1$ |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
void setEnabled(MHandledMenuItem item) { |
| 357 |
if (!item.isToBeRendered() || !item.isVisible() |
| 358 |
|| item.getWidget() == null) { |
| 359 |
return; |
| 360 |
} |
| 361 |
ParameterizedCommand cmd = item.getWbCommand(); |
| 362 |
if (cmd == null) { |
| 363 |
return; |
| 364 |
} |
| 365 |
final IEclipseContext lclContext = modelService |
| 366 |
.getContainingContext(item); |
| 367 |
EHandlerService service = lclContext.get(EHandlerService.class); |
| 368 |
item.setEnabled(service.canExecute(cmd)); |
| 369 |
} |
| 370 |
|
| 371 |
private void unrender(final List<MMenuElement> menuModel) { |
| 372 |
trace("unrender", null, null); //$NON-NLS-1$ |
| 373 |
} |
| 374 |
|
| 375 |
private void removeMenuContributions(final MMenu menuModel, |
| 376 |
final ArrayList<MMenuElement> menuContributionsToRemove) { |
| 377 |
for (MMenuElement item : menuContributionsToRemove) { |
| 378 |
trace("removeMenuContributions " + item, //$NON-NLS-1$ |
| 379 |
(Widget) menuModel.getWidget(), menuModel); |
| 380 |
menuModel.getChildren().remove(item); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
public void showRenderedMenu(final Event event, final Menu menu, |
| 385 |
final MRenderedMenu menuModel) { |
| 386 |
if (!(menuModel.getContributionManager() instanceof MenuManager)) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
MenuManager manager = (MenuManager) menuModel.getContributionManager(); |
| 391 |
Method handleAboutToShow = getAboutToShow(); |
| 392 |
try { |
| 393 |
handleAboutToShow.invoke(manager); |
| 394 |
} catch (IllegalArgumentException e) { |
| 395 |
// TODO Auto-generated catch block |
| 396 |
e.printStackTrace(); |
| 397 |
} catch (IllegalAccessException e) { |
| 398 |
// TODO Auto-generated catch block |
| 399 |
e.printStackTrace(); |
| 400 |
} catch (InvocationTargetException e) { |
| 401 |
// TODO Auto-generated catch block |
| 402 |
e.printStackTrace(); |
| 403 |
} |
| 404 |
|
| 405 |
if (menuModel.getChildren().size() == 1 |
| 406 |
&& menuModel.getChildren().get(0) instanceof MPopupMenu) { |
| 407 |
showPopup(event, menu, (MPopupMenu) menuModel.getChildren().get(0)); |
| 408 |
} else { |
| 409 |
showMenu(event, menu, menuModel); |
| 410 |
} |
| 411 |
event.type = SWT.None; |
| 412 |
event.doit = false; |
| 413 |
} |
| 414 |
|
| 415 |
public void cleanUp(final Menu menu) { |
| 416 |
trace("cleanUp", menu, null); //$NON-NLS-1$ |
| 417 |
if (pendingCleanup.isEmpty()) { |
| 418 |
return; |
| 419 |
} |
| 420 |
Runnable cleanUp = pendingCleanup.remove(menu); |
| 421 |
if (cleanUp != null) { |
| 422 |
trace("cleanUp.run()", menu, null); //$NON-NLS-1$ |
| 423 |
cleanUp.run(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
public void dispose() { |
| 428 |
Menu[] keys = pendingCleanup.keySet().toArray( |
| 429 |
new Menu[pendingCleanup.size()]); |
| 430 |
for (Menu menu : keys) { |
| 431 |
cleanUp(menu); |
| 432 |
} |
| 433 |
} |
| 434 |
} |