|
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 |
|
| 12 |
package org.eclipse.e4.ui.workbench.renderers.swt; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.HashSet; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Map; |
| 19 |
import javax.annotation.PostConstruct; |
| 20 |
import javax.annotation.PreDestroy; |
| 21 |
import javax.inject.Inject; |
| 22 |
import org.eclipse.e4.core.contexts.ContextInjectionFactory; |
| 23 |
import org.eclipse.e4.core.contexts.IEclipseContext; |
| 24 |
import org.eclipse.e4.core.services.events.IEventBroker; |
| 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.model.application.MApplication; |
| 29 |
import org.eclipse.e4.ui.model.application.ui.MElementContainer; |
| 30 |
import org.eclipse.e4.ui.model.application.ui.MUIElement; |
| 31 |
import org.eclipse.e4.ui.model.application.ui.MUILabel; |
| 32 |
import org.eclipse.e4.ui.model.application.ui.basic.MWindow; |
| 33 |
import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem; |
| 34 |
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; |
| 35 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenu; |
| 36 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution; |
| 37 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement; |
| 38 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem; |
| 39 |
import org.eclipse.e4.ui.model.application.ui.menu.MMenuSeparator; |
| 40 |
import org.eclipse.e4.ui.model.application.ui.menu.MPopupMenu; |
| 41 |
import org.eclipse.e4.ui.workbench.IResourceUtilities; |
| 42 |
import org.eclipse.e4.ui.workbench.UIEvents; |
| 43 |
import org.eclipse.e4.ui.workbench.modeling.ExpressionContext; |
| 44 |
import org.eclipse.e4.ui.workbench.swt.util.ISWTResourceUtilities; |
| 45 |
import org.eclipse.emf.common.util.URI; |
| 46 |
import org.eclipse.emf.ecore.EObject; |
| 47 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 48 |
import org.eclipse.jface.action.AbstractGroupMarker; |
| 49 |
import org.eclipse.jface.action.GroupMarker; |
| 50 |
import org.eclipse.jface.action.IContributionItem; |
| 51 |
import org.eclipse.jface.action.IMenuListener; |
| 52 |
import org.eclipse.jface.action.IMenuManager; |
| 53 |
import org.eclipse.jface.action.MenuManager; |
| 54 |
import org.eclipse.jface.action.Separator; |
| 55 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 56 |
import org.eclipse.swt.widgets.Control; |
| 57 |
import org.eclipse.swt.widgets.Decorations; |
| 58 |
import org.eclipse.swt.widgets.Menu; |
| 59 |
import org.osgi.service.event.Event; |
| 60 |
import org.osgi.service.event.EventHandler; |
| 61 |
|
| 62 |
/** |
| 63 |
* |
| 64 |
*/ |
| 65 |
public class NewMenuRenderer extends SWTPartRenderer { |
| 66 |
private static final String NO_LABEL = "UnLabled"; //$NON-NLS-1$ |
| 67 |
|
| 68 |
private Map<MMenu, MenuManager> modelToManager = new HashMap<MMenu, MenuManager>(); |
| 69 |
|
| 70 |
private Map<MMenuItem, IContributionItem> modelToContribution = new HashMap<MMenuItem, IContributionItem>(); |
| 71 |
|
| 72 |
private Map<MMenuElement, ContributionRecord> modelContributionToRecord = new HashMap<MMenuElement, ContributionRecord>(); |
| 73 |
|
| 74 |
@Inject |
| 75 |
private Logger logger; |
| 76 |
|
| 77 |
@Inject |
| 78 |
private MApplication application; |
| 79 |
|
| 80 |
@Inject |
| 81 |
IEventBroker eventBroker; |
| 82 |
private EventHandler itemUpdater = new EventHandler() { |
| 83 |
public void handleEvent(Event event) { |
| 84 |
// Ensure that this event is for a MMenuItem |
| 85 |
if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MMenuItem)) |
| 86 |
return; |
| 87 |
|
| 88 |
MMenuItem itemModel = (MMenuItem) event |
| 89 |
.getProperty(UIEvents.EventTags.ELEMENT); |
| 90 |
|
| 91 |
IContributionItem ici = modelToContribution.get(itemModel); |
| 92 |
if (ici == null) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
String attName = (String) event |
| 97 |
.getProperty(UIEvents.EventTags.ATTNAME); |
| 98 |
if (UIEvents.UILabel.LABEL.equals(attName)) { |
| 99 |
ici.update(); |
| 100 |
} else if (UIEvents.UILabel.ICONURI.equals(attName)) { |
| 101 |
ici.update(); |
| 102 |
} |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
private EventHandler toBeRenderedUpdater = new EventHandler() { |
| 107 |
public void handleEvent(Event event) { |
| 108 |
// Ensure that this event is for a MMenuItem |
| 109 |
if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MMenuItem)) |
| 110 |
return; |
| 111 |
|
| 112 |
MMenuItem itemModel = (MMenuItem) event |
| 113 |
.getProperty(UIEvents.EventTags.ELEMENT); |
| 114 |
String attName = (String) event |
| 115 |
.getProperty(UIEvents.EventTags.ATTNAME); |
| 116 |
if (UIEvents.UIElement.TOBERENDERED.equals(attName)) { |
| 117 |
Object obj = itemModel.getParent(); |
| 118 |
if (!(obj instanceof MMenu)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
MenuManager parent = modelToManager.get(obj); |
| 122 |
if (itemModel.isToBeRendered()) { |
| 123 |
if (parent != null) { |
| 124 |
modelProcessSwitch(parent, itemModel); |
| 125 |
} |
| 126 |
} else { |
| 127 |
IContributionItem ici = modelToContribution |
| 128 |
.remove(itemModel); |
| 129 |
if (ici != null && parent != null) { |
| 130 |
parent.remove(ici); |
| 131 |
} |
| 132 |
if (ici != null) { |
| 133 |
ici.dispose(); |
| 134 |
} |
| 135 |
} |
| 136 |
} else if (UIEvents.UIElement.VISIBLE.equals(attName)) { |
| 137 |
IContributionItem ici = modelToContribution.get(itemModel); |
| 138 |
if (ici == null) { |
| 139 |
return; |
| 140 |
} |
| 141 |
ici.setVisible(itemModel.isVisible()); |
| 142 |
} |
| 143 |
} |
| 144 |
}; |
| 145 |
|
| 146 |
private EventHandler selectionUpdater = new EventHandler() { |
| 147 |
public void handleEvent(Event event) { |
| 148 |
// Ensure that this event is for a MToolItem |
| 149 |
if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MMenuItem)) |
| 150 |
return; |
| 151 |
|
| 152 |
MMenuItem itemModel = (MMenuItem) event |
| 153 |
.getProperty(UIEvents.EventTags.ELEMENT); |
| 154 |
IContributionItem ici = modelToContribution.get(itemModel); |
| 155 |
if (ici != null) { |
| 156 |
ici.update(); |
| 157 |
} |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
private EventHandler enabledUpdater = new EventHandler() { |
| 162 |
public void handleEvent(Event event) { |
| 163 |
// Ensure that this event is for a MMenuItem |
| 164 |
if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MMenuItem)) |
| 165 |
return; |
| 166 |
|
| 167 |
MMenuItem itemModel = (MMenuItem) event |
| 168 |
.getProperty(UIEvents.EventTags.ELEMENT); |
| 169 |
IContributionItem ici = modelToContribution.get(itemModel); |
| 170 |
if (ici != null) { |
| 171 |
ici.update(); |
| 172 |
} |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
private IMenuListener visibilityCalculationListener = new IMenuListener() { |
| 177 |
public void menuAboutToShow(IMenuManager manager) { |
| 178 |
MenuManager menuManager = (MenuManager) manager; |
| 179 |
if (menuManager.getMenu() == null) { |
| 180 |
return; |
| 181 |
} |
| 182 |
Menu menu = menuManager.getMenu(); |
| 183 |
Object obj = menu.getData(AbstractPartRenderer.OWNING_ME); |
| 184 |
if (obj == null && menu.getParentItem() != null) { |
| 185 |
obj = menu.getParentItem().getData( |
| 186 |
AbstractPartRenderer.OWNING_ME); |
| 187 |
} |
| 188 |
if (!(obj instanceof MMenu)) { |
| 189 |
return; |
| 190 |
} |
| 191 |
MMenu menuModel = (MMenu) obj; |
| 192 |
final IEclipseContext parentContext = modelService |
| 193 |
.getContainingContext(menuModel); |
| 194 |
HashSet<ContributionRecord> records = new HashSet<ContributionRecord>(); |
| 195 |
for (MMenuElement element : menuModel.getChildren()) { |
| 196 |
ContributionRecord record = modelContributionToRecord |
| 197 |
.get(element); |
| 198 |
if (records.add(record)) { |
| 199 |
record.updateVisibility(parentContext); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
@PostConstruct |
| 206 |
public void init() { |
| 207 |
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC), |
| 208 |
itemUpdater); |
| 209 |
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, |
| 210 |
UIEvents.Item.SELECTED), selectionUpdater); |
| 211 |
eventBroker |
| 212 |
.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, |
| 213 |
UIEvents.Item.ENABLED), enabledUpdater); |
| 214 |
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UIElement.TOPIC, |
| 215 |
UIEvents.UIElement.TOBERENDERED), toBeRenderedUpdater); |
| 216 |
|
| 217 |
} |
| 218 |
|
| 219 |
@PreDestroy |
| 220 |
public void contextDisposed() { |
| 221 |
eventBroker.unsubscribe(itemUpdater); |
| 222 |
eventBroker.unsubscribe(selectionUpdater); |
| 223 |
eventBroker.unsubscribe(enabledUpdater); |
| 224 |
eventBroker.unsubscribe(toBeRenderedUpdater); |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* (non-Javadoc) |
| 229 |
* |
| 230 |
* @see |
| 231 |
* org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer#createWidget |
| 232 |
* (org.eclipse.e4.ui.model.application.ui.MUIElement, java.lang.Object) |
| 233 |
*/ |
| 234 |
@Override |
| 235 |
public Object createWidget(MUIElement element, Object parent) { |
| 236 |
if (!(element instanceof MMenu)) |
| 237 |
return null; |
| 238 |
|
| 239 |
final MMenu menuModel = (MMenu) element; |
| 240 |
Menu newMenu = null; |
| 241 |
|
| 242 |
if (parent instanceof Decorations) { |
| 243 |
MUIElement container = (MUIElement) ((EObject) element) |
| 244 |
.eContainer(); |
| 245 |
if (container instanceof MWindow) { |
| 246 |
MenuManager menuBarManager = new MenuManager(NO_LABEL, |
| 247 |
menuModel.getElementId()); |
| 248 |
modelToManager.put(menuModel, menuBarManager); |
| 249 |
menuBarManager.addMenuListener(visibilityCalculationListener); |
| 250 |
newMenu = menuBarManager.createMenuBar((Decorations) parent); |
| 251 |
((Decorations) parent).setMenuBar(newMenu); |
| 252 |
newMenu.setData(menuBarManager); |
| 253 |
} else { |
| 254 |
MenuManager popupManager = new MenuManager(NO_LABEL, |
| 255 |
menuModel.getElementId()); |
| 256 |
modelToManager.put(menuModel, popupManager); |
| 257 |
popupManager.addMenuListener(visibilityCalculationListener); |
| 258 |
newMenu = popupManager.createContextMenu((Control) parent); |
| 259 |
((Control) parent).setMenu(newMenu); |
| 260 |
newMenu.setData(popupManager); |
| 261 |
} |
| 262 |
} else if (parent instanceof Menu) { |
| 263 |
// Object data = ((Menu) parent).getData(); |
| 264 |
logger.debug(new Exception(), "Trying to render a sub menu " //$NON-NLS-1$ |
| 265 |
+ menuModel + "\n\t" + parent); //$NON-NLS-1$ |
| 266 |
|
| 267 |
} else if (parent instanceof Control) { |
| 268 |
MenuManager popupManager = new MenuManager(NO_LABEL, |
| 269 |
menuModel.getElementId()); |
| 270 |
modelToManager.put(menuModel, popupManager); |
| 271 |
popupManager.addMenuListener(visibilityCalculationListener); |
| 272 |
newMenu = popupManager.createContextMenu((Control) parent); |
| 273 |
((Control) parent).setMenu(newMenu); |
| 274 |
newMenu.setData(popupManager); |
| 275 |
} |
| 276 |
processContributions(menuModel); |
| 277 |
return newMenu; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @param menuModel |
| 282 |
*/ |
| 283 |
private void processContributions(MMenu menuModel) { |
| 284 |
final ArrayList<MMenuContribution> toContribute = new ArrayList<MMenuContribution>(); |
| 285 |
ContributionsAnalyzer.XXXgatherMenuContributions(menuModel, |
| 286 |
application.getMenuContributions(), menuModel.getElementId(), |
| 287 |
toContribute, null, menuModel instanceof MPopupMenu); |
| 288 |
generateContributions(menuModel, toContribute); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* @param menuModel |
| 293 |
* @param toContribute |
| 294 |
*/ |
| 295 |
private void generateContributions(MMenu menuModel, |
| 296 |
ArrayList<MMenuContribution> toContribute) { |
| 297 |
HashSet<String> existingMenuIds = new HashSet<String>(); |
| 298 |
HashSet<String> existingSeparatorNames = new HashSet<String>(); |
| 299 |
for (MMenuElement child : menuModel.getChildren()) { |
| 300 |
String elementId = child.getElementId(); |
| 301 |
if (child instanceof MMenu && elementId != null) { |
| 302 |
existingMenuIds.add(elementId); |
| 303 |
} else if (child instanceof MMenuSeparator && elementId != null) { |
| 304 |
existingSeparatorNames.add(elementId); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
MenuManager manager = modelToManager.get(menuModel); |
| 309 |
boolean done = toContribute.size() == 0; |
| 310 |
while (!done) { |
| 311 |
ArrayList<MMenuContribution> curList = new ArrayList<MMenuContribution>( |
| 312 |
toContribute); |
| 313 |
int retryCount = toContribute.size(); |
| 314 |
toContribute.clear(); |
| 315 |
|
| 316 |
for (MMenuContribution menuContribution : curList) { |
| 317 |
if (!processAddition(menuModel, manager, menuContribution, |
| 318 |
existingMenuIds, existingSeparatorNames)) { |
| 319 |
toContribute.add(menuContribution); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// We're done if the retryList is now empty (everything done) or |
| 324 |
// if the list hasn't changed at all (no hope) |
| 325 |
done = (toContribute.size() == 0) |
| 326 |
|| (toContribute.size() == retryCount); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* @param menuModel |
| 332 |
* @param manager |
| 333 |
* @param menuContribution |
| 334 |
* @return true if the menuContribution was processed |
| 335 |
*/ |
| 336 |
private boolean processAddition(MMenu menuModel, MenuManager manager, |
| 337 |
MMenuContribution menuContribution, |
| 338 |
final HashSet<String> existingMenuIds, |
| 339 |
HashSet<String> existingSeparatorNames) { |
| 340 |
int idx = getIndex(menuModel, menuContribution.getPositionInParent()); |
| 341 |
if (idx == -1) { |
| 342 |
return false; |
| 343 |
} |
| 344 |
ContributionRecord record = new ContributionRecord(menuModel, |
| 345 |
menuContribution); |
| 346 |
record.generate(); |
| 347 |
for (MMenuElement copy : record.generatedElements) { |
| 348 |
modelContributionToRecord.put(copy, record); |
| 349 |
if (copy instanceof MMenu |
| 350 |
&& existingMenuIds.contains(copy.getElementId())) { |
| 351 |
// skip this, it's already there |
| 352 |
continue; |
| 353 |
} else if (copy instanceof MMenuSeparator |
| 354 |
&& existingSeparatorNames.contains(copy.getElementId())) { |
| 355 |
// skip this, it's already there |
| 356 |
continue; |
| 357 |
} |
| 358 |
menuModel.getChildren().add(idx++, copy); |
| 359 |
if (copy instanceof MMenu && copy.getElementId() != null) { |
| 360 |
existingMenuIds.add(copy.getElementId()); |
| 361 |
} else if (copy instanceof MMenuSeparator |
| 362 |
&& copy.getElementId() != null) { |
| 363 |
existingSeparatorNames.add(copy.getElementId()); |
| 364 |
} |
| 365 |
} |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
private static int getIndex(MElementContainer<?> menuModel, |
| 370 |
String positionInParent) { |
| 371 |
String id = null; |
| 372 |
String modifier = null; |
| 373 |
if (positionInParent != null && positionInParent.length() > 0) { |
| 374 |
String[] array = positionInParent.split("="); //$NON-NLS-1$ |
| 375 |
modifier = array[0]; |
| 376 |
id = array[1]; |
| 377 |
} |
| 378 |
if (id == null) { |
| 379 |
return menuModel.getChildren().size(); |
| 380 |
} |
| 381 |
|
| 382 |
int idx = 0; |
| 383 |
int size = menuModel.getChildren().size(); |
| 384 |
while (idx < size) { |
| 385 |
if (id.equals(menuModel.getChildren().get(idx).getElementId())) { |
| 386 |
if ("after".equals(modifier)) { //$NON-NLS-1$ |
| 387 |
idx++; |
| 388 |
} |
| 389 |
return idx; |
| 390 |
} |
| 391 |
idx++; |
| 392 |
} |
| 393 |
return id.equals("additions") ? menuModel.getChildren().size() : -1; //$NON-NLS-1$ |
| 394 |
} |
| 395 |
|
| 396 |
static class ContributionRecord { |
| 397 |
MMenu menuModel; |
| 398 |
MMenuContribution menuContribution; |
| 399 |
ArrayList<MMenuElement> generatedElements = new ArrayList<MMenuElement>(); |
| 400 |
|
| 401 |
public ContributionRecord(MMenu menuModel, |
| 402 |
MMenuContribution contribution) { |
| 403 |
this.menuModel = menuModel; |
| 404 |
this.menuContribution = contribution; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* @param context |
| 409 |
*/ |
| 410 |
public void updateVisibility(IEclipseContext context) { |
| 411 |
ExpressionContext exprContext = new ExpressionContext(context); |
| 412 |
boolean isVisible = ContributionsAnalyzer.isVisible( |
| 413 |
menuContribution, exprContext); |
| 414 |
for (MMenuElement item : generatedElements) { |
| 415 |
item.setVisible(isVisible); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
public void generate() { |
| 420 |
for (MMenuElement item : menuContribution.getChildren()) { |
| 421 |
MMenuElement copy = (MMenuElement) EcoreUtil |
| 422 |
.copy((EObject) item); |
| 423 |
generatedElements.add(copy); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void removeMenuContributions(final MMenu menuModel, |
| 429 |
final ArrayList<MMenuElement> menuContributionsToRemove) { |
| 430 |
for (MMenuElement item : menuContributionsToRemove) { |
| 431 |
menuModel.getChildren().remove(item); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
* (non-Javadoc) |
| 437 |
* |
| 438 |
* @see |
| 439 |
* org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer#processContents |
| 440 |
* (org.eclipse.e4.ui.model.application.ui.MElementContainer) |
| 441 |
*/ |
| 442 |
@Override |
| 443 |
public void processContents(MElementContainer<MUIElement> container) { |
| 444 |
// I can either simply stop processing, or we can walk the model |
| 445 |
// ourselves like the "old" days |
| 446 |
// EMF gives us null lists if empty |
| 447 |
if (container == null) |
| 448 |
return; |
| 449 |
|
| 450 |
MenuManager parentManager = modelToManager.get(container); |
| 451 |
if (parentManager == null) { |
| 452 |
return; |
| 453 |
} |
| 454 |
// Process any contents of the newly created ME |
| 455 |
List<MUIElement> parts = container.getChildren(); |
| 456 |
if (parts != null) { |
| 457 |
MUIElement[] plist = parts.toArray(new MUIElement[parts.size()]); |
| 458 |
for (int i = 0; i < plist.length; i++) { |
| 459 |
MUIElement childME = plist[i]; |
| 460 |
modelProcessSwitch(parentManager, (MMenuElement) childME); |
| 461 |
} |
| 462 |
} |
| 463 |
parentManager.update(false); |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* @param parentManager |
| 468 |
* @param menuModel |
| 469 |
*/ |
| 470 |
private void processMenu(MenuManager parentManager, MMenu menuModel) { |
| 471 |
String menuText = getText(menuModel); |
| 472 |
ImageDescriptor desc = getImageDescriptor(menuModel); |
| 473 |
MenuManager menuManager = new MenuManager(menuText, desc, |
| 474 |
menuModel.getElementId()); |
| 475 |
modelToManager.put(menuModel, menuManager); |
| 476 |
menuManager.addMenuListener(visibilityCalculationListener); |
| 477 |
parentManager.add(menuManager); |
| 478 |
processContributions(menuModel); |
| 479 |
List<MMenuElement> parts = menuModel.getChildren(); |
| 480 |
if (parts != null) { |
| 481 |
MMenuElement[] plist = parts |
| 482 |
.toArray(new MMenuElement[parts.size()]); |
| 483 |
for (int i = 0; i < plist.length; i++) { |
| 484 |
MMenuElement childME = plist[i]; |
| 485 |
modelProcessSwitch(menuManager, childME); |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* @param menuManager |
| 492 |
* @param childME |
| 493 |
*/ |
| 494 |
void modelProcessSwitch(MenuManager menuManager, MMenuElement childME) { |
| 495 |
if (childME instanceof MHandledMenuItem) { |
| 496 |
MHandledMenuItem itemModel = (MHandledMenuItem) childME; |
| 497 |
processHandledItem(menuManager, itemModel); |
| 498 |
} else if (childME instanceof MDirectMenuItem) { |
| 499 |
MDirectMenuItem itemModel = (MDirectMenuItem) childME; |
| 500 |
processDirectItem(menuManager, itemModel, null); |
| 501 |
} else if (childME instanceof MMenuSeparator) { |
| 502 |
MMenuSeparator sep = (MMenuSeparator) childME; |
| 503 |
processSeparator(menuManager, sep); |
| 504 |
} else if (childME instanceof MMenu) { |
| 505 |
MMenu itemModel = (MMenu) childME; |
| 506 |
processMenu(menuManager, itemModel); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* @param menuManager |
| 512 |
* @param itemModel |
| 513 |
*/ |
| 514 |
private void processSeparator(MenuManager menuManager, |
| 515 |
MMenuSeparator itemModel) { |
| 516 |
AbstractGroupMarker marker = null; |
| 517 |
if (itemModel.isVisible()) { |
| 518 |
marker = new Separator(); |
| 519 |
marker.setId(itemModel.getElementId()); |
| 520 |
} else { |
| 521 |
if (itemModel.getElementId() != null) { |
| 522 |
marker = new GroupMarker(itemModel.getElementId()); |
| 523 |
} |
| 524 |
} |
| 525 |
menuManager.add(marker); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* @param parentManager |
| 530 |
* @param itemModel |
| 531 |
* @param id |
| 532 |
* TODO |
| 533 |
*/ |
| 534 |
void processDirectItem(MenuManager parentManager, |
| 535 |
MDirectMenuItem itemModel, String id) { |
| 536 |
final IEclipseContext lclContext = getContext(itemModel); |
| 537 |
DirectContributionItem ci = ContextInjectionFactory.make( |
| 538 |
DirectContributionItem.class, lclContext); |
| 539 |
ci.setModel(itemModel); |
| 540 |
parentManager.add(ci); |
| 541 |
modelToContribution.put(itemModel, ci); |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* @param parentManager |
| 546 |
* @param itemModel |
| 547 |
*/ |
| 548 |
void processHandledItem(MenuManager parentManager, |
| 549 |
MHandledMenuItem itemModel) { |
| 550 |
final IEclipseContext lclContext = getContext(itemModel); |
| 551 |
HandledContributionItem ci = ContextInjectionFactory.make( |
| 552 |
HandledContributionItem.class, lclContext); |
| 553 |
ci.setModel(itemModel); |
| 554 |
parentManager.add(ci); |
| 555 |
modelToContribution.put(itemModel, ci); |
| 556 |
} |
| 557 |
|
| 558 |
private String getText(MMenu menuModel) { |
| 559 |
String text = menuModel.getLabel(); |
| 560 |
if (text == null || text.length() == 0) { |
| 561 |
return NO_LABEL; |
| 562 |
} |
| 563 |
return text; |
| 564 |
} |
| 565 |
|
| 566 |
private ImageDescriptor getImageDescriptor(MUILabel element) { |
| 567 |
IEclipseContext localContext = context; |
| 568 |
String iconURI = element.getIconURI(); |
| 569 |
if (iconURI != null && iconURI.length() > 0) { |
| 570 |
ISWTResourceUtilities resUtils = (ISWTResourceUtilities) localContext |
| 571 |
.get(IResourceUtilities.class.getName()); |
| 572 |
return resUtils.imageDescriptorFromURI(URI.createURI(iconURI)); |
| 573 |
} |
| 574 |
return null; |
| 575 |
} |
| 576 |
} |