public void handleEvent(Event event) { // Ensure that this event is for a MMenuItem if (!(event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MToolBarElement)) return; MToolBarElement itemModel = (MToolBarElement) event.getProperty(UIEvents.EventTags.ELEMENT); String attName = (String) event.getProperty(UIEvents.EventTags.ATTNAME); if (UIEvents.UIElement.TOBERENDERED.equals(attName)) { Object obj = itemModel.getParent(); if (!(obj instanceof MToolBar)) { return; } ToolBarManager parent = getManager((MToolBar) obj); if (itemModel.isToBeRendered()) { if (parent != null) { modelProcessSwitch(parent, itemModel); parent.update(true); ToolBar tb = parent.getControl(); if (tb != null && !tb.isDisposed()) { tb.pack(true); tb.getShell().layout(new Control[] {tb}, SWT.DEFER); } } } else { IContributionItem ici = modelToContribution.remove(itemModel); if (ici != null && parent != null) { parent.remove(ici); } if (ici != null) { ici.dispose(); } } } else if (UIEvents.UIElement.VISIBLE.equals(attName)) { IContributionItem ici = getContribution(itemModel); if (ici == null) { return; } ici.setVisible(itemModel.isVisible()); ToolBarManager parent = (ToolBarManager) ((ContributionItem) ici).getParent(); if (parent != null) { parent.markDirty(); parent.update(true); // MUIElement tbModel = itemModel.getParent(); // disposeToolbarIfNecessary((MToolBar) tbModel); ToolBar tb = parent.getControl(); if (tb != null && !tb.isDisposed()) { tb.pack(true); tb.getShell().layout(new Control[] {tb}, SWT.DEFER); } } } }
/** * @param parentManager * @param itemModel * @param ci */ private void addToManager( ToolBarManager parentManager, MToolBarElement model, IContributionItem ci) { MElementContainer<MUIElement> parent = model.getParent(); // technically this shouldn't happen if (parent == null) { parentManager.add(ci); } else { int index = parent.getChildren().indexOf(model); // shouldn't be -1, but better safe than sorry if (index > parentManager.getSize() || index == -1) { parentManager.add(ci); } else { parentManager.insert(index, ci); } } }