Revert "Bug 319621 [Compatibility] All views now have view menus"

This is a partially hand-crafted revert of commit
7a2e6f2c24786040969b51f515f5d5a1ac0e3d7e due to intermediate
changes introduced to the StackRenderer file by commit
6046e7de40a4c7dc7a70dd1580afb05a2d3a8e57.
diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/MenuManagerRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/MenuManagerRenderer.java
index d23e103..61eb80b 100644
--- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/MenuManagerRenderer.java
+++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/MenuManagerRenderer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2011 IBM Corporation and others.
+ * Copyright (c) 2009, 2010 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -31,7 +31,6 @@
 import org.eclipse.e4.ui.model.application.ui.MElementContainer;
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.MUILabel;
-import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
@@ -438,7 +437,7 @@
 		if (!record.mergeIntoModel()) {
 			return false;
 		}
-		if (menuBar || ((EObject) menuModel).eContainer() instanceof MPart) {
+		if (menuBar) {
 			final IEclipseContext parentContext = modelService
 					.getContainingContext(menuModel);
 			parentContext.runAndTrack(new RunAndTrack() {
diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/StackRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/StackRenderer.java
index b99a3cc..5bb348e 100644
--- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/StackRenderer.java
+++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/StackRenderer.java
@@ -30,9 +30,6 @@
 import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
-import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
-import org.eclipse.e4.ui.model.application.ui.menu.MOpaqueMenuItem;
-import org.eclipse.e4.ui.model.application.ui.menu.MOpaqueMenuSeparator;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.services.IStylingEngine;
 import org.eclipse.e4.ui.widgets.CTabFolder;
@@ -44,7 +41,6 @@
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.emf.ecore.EObject;
-import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.accessibility.ACC;
@@ -112,13 +108,6 @@
 
 	private EventHandler dirtyUpdater;
 
-	/**
-	 * An event handler for listening to changes to the state of view menus and
-	 * its child menu items. Depending on what state these items are in, the
-	 * view menu should or should not be rendered in the tab folder.
-	 */
-	private EventHandler viewMenuUpdater;
-
 	private boolean ignoreTabSelChanges = false;
 
 	private ActivationJob activationJob = null;
@@ -332,90 +321,8 @@
 			}
 		};
 
-		viewMenuUpdater = new EventHandler() {
-			public void handleEvent(Event event) {
-				Object objElement = event
-						.getProperty(UIEvents.EventTags.ELEMENT);
-
-				// Ensure that this event is for a MMenuItem
-				if (!(objElement instanceof MMenuElement)) {
-					return;
-				}
-
-				EObject parent = ((EObject) objElement).eContainer();
-				while (parent instanceof MMenuElement) {
-					MUIElement element = (MUIElement) parent;
-					if (!element.isToBeRendered() || !element.isVisible()) {
-						return;
-					}
-
-					objElement = parent;
-					parent = parent.eContainer();
-				}
-
-				// if we're a view menu, the parent element is a part
-				if (!(parent instanceof MPart)) {
-					return;
-				}
-
-				MPart element = (MPart) parent;
-				MUIElement parentElement = element.getParent();
-				if (parentElement == null) {
-					MPlaceholder placeholder = element.getCurSharedRef();
-					if (placeholder == null) {
-						return;
-					}
-
-					parentElement = placeholder.getParent();
-					if (parentElement == null) {
-						return;
-					}
-				}
-
-				Object widget = parentElement.getWidget();
-				if (widget instanceof CTabFolder) {
-					Boolean newValue = (Boolean) event
-							.getProperty(UIEvents.EventTags.NEW_VALUE);
-					CTabFolder folder = (CTabFolder) widget;
-					if (newValue.booleanValue()) {
-						if (getViewMenuTB(folder) == null) {
-							disposeViewMenu(folder);
-							setupMenuButton(element, folder);
-							layoutTopRight(folder);
-						}
-					} else if (!isMenuVisible((MMenu) objElement)) {
-						disposeViewMenu(folder);
-					}
-				}
-			}
-		};
-		eventBroker.subscribe(UIEvents.Dirtyable.TOPIC_DIRTY, dirtyUpdater);
-		eventBroker.subscribe(UIEvents.UIElement.TOPIC_VISIBLE, viewMenuUpdater);
-		eventBroker.subscribe(UIEvents.UIElement.TOPIC_TOBERENDERED, viewMenuUpdater);
-	}
-
-	/**
-	 * Determines if the menu provided or any one of its children should be
-	 * rendered.
-	 * 
-	 * @param menu
-	 *            the menu to determine if it should be displayed in the tab
-	 *            folder
-	 * @return <tt>true</tt> if the menu should be drawn in the tab folder,
-	 *         <tt>false</tt> otherwise
-	 */
-	private boolean isMenuVisible(MMenu menu) {
-		if (menu.isToBeRendered() && menu.isVisible()) {
-			for (MMenuElement element : menu.getChildren()) {
-				if (element.isToBeRendered() && element.isVisible()) {
-					return true;
-				} else if (element instanceof MMenu
-						&& isMenuVisible((MMenu) element)) {
-					return true;
-				}
-			}
-		}
-		return false;
+		eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Dirtyable.TOPIC,
+				UIEvents.Dirtyable.DIRTY), dirtyUpdater);
 	}
 
 	protected void updateTab(CTabItem cti, MPart part, String attName,
@@ -448,7 +355,6 @@
 
 		eventBroker.unsubscribe(itemUpdater);
 		eventBroker.unsubscribe(dirtyUpdater);
-		eventBroker.unsubscribe(viewMenuUpdater);
 	}
 
 	private String getLabel(MUILabel itemPart, String newName) {
@@ -519,20 +425,10 @@
 		return (Composite) ctf.getData(TOP_RIGHT);
 	}
 
-	/**
-	 * Disposes of the view menu associated with the given tab folder.
-	 * 
-	 * @param ctf
-	 *            the tab folder to clear of its view menu
-	 */
-	public void disposeViewMenu(CTabFolder ctf) {
+	public void clearTR(CTabFolder ctf) {
 		ToolBar vmTB = getViewMenuTB(ctf);
 		if (vmTB != null && !vmTB.isDisposed())
 			vmTB.dispose();
-	}
-
-	public void clearTR(CTabFolder ctf) {
-		disposeViewMenu(ctf);
 
 		MToolBar viewTBModel = getViewTB(ctf);
 		if (viewTBModel != null && viewTBModel.getWidget() != null)
@@ -557,16 +453,7 @@
 		}
 
 		setupMenuButton(part, ctf);
-		layoutTopRight(ctf);
-	}
 
-	/**
-	 * Asks the specified tab folder to layout its top right control.
-	 * 
-	 * @param ctf
-	 *            the tab folder that should be laid out
-	 */
-	public void layoutTopRight(CTabFolder ctf) {
 		Composite trComp = getTRComposite(ctf);
 		if (trComp.getChildren().length > 0) {
 			trComp.setVisible(true);
@@ -576,7 +463,7 @@
 			trComp.setVisible(false);
 		}
 
-		trComp.pack();
+		trComp.layout();
 		ctf.layout();
 	}
 
@@ -861,19 +748,11 @@
 		adjustTR(ctf, part);
 	}
 
-	/**
-	 * Creates a view menu for the given part in the contained tab folder.
-	 * 
-	 * @param part
-	 *            the part that should have its view menu created
-	 * @param ctf
-	 *            the containing tab folder
-	 */
-	public void setupMenuButton(MPart part, CTabFolder ctf) {
+	private void setupMenuButton(MPart part, CTabFolder ctf) {
 		MMenu viewMenu = getViewMenu(part);
 
 		// View menu (if any)
-		if (viewMenu != null && hasVisibleMenuItems(viewMenu, part)) {
+		if (viewMenu != null) {
 			showMenuButton(part, ctf, viewMenu);
 		} else {
 			// hide the menu's TB
@@ -970,7 +849,7 @@
 			if (!display.readAndDispatch())
 				display.sleep();
 		}
-		if (!menu.isDisposed() && !(menu.getData() instanceof MenuManager)) {
+		if (!(menu.getData() instanceof MenuManager)) {
 			menu.dispose();
 		}
 	}
@@ -1165,73 +1044,10 @@
 			return null;
 		}
 		for (MMenu menu : part.getMenus()) {
-			if (menu.getTags().contains(TAG_VIEW_MENU)) {
+			if (menu.getTags().contains(TAG_VIEW_MENU) && menu.isToBeRendered()) {
 				return menu;
 			}
 		}
 		return null;
 	}
-
-	/**
-	 * Determine whether the given view menu has any visible menu items.
-	 * 
-	 * @param viewMenu
-	 *            the view menu to check
-	 * @param part
-	 *            the view menu's parent part
-	 * @return <tt>true</tt> if the specified view menu has visible children,
-	 *         <tt>false</tt> otherwise
-	 */
-	private boolean hasVisibleMenuItems(MMenu viewMenu, MPart part) {
-		if (!viewMenu.isToBeRendered() || !viewMenu.isVisible()) {
-			return false;
-		}
-
-		for (MMenuElement menuElement : viewMenu.getChildren()) {
-			if (menuElement.isToBeRendered() && menuElement.isVisible()) {
-				if (menuElement instanceof MOpaqueMenuItem) {
-					IContributionItem item = (IContributionItem) ((MOpaqueMenuItem) menuElement)
-							.getOpaqueItem();
-					if (item != null && item.isVisible()) {
-						return true;
-					}
-				} else if (menuElement instanceof MOpaqueMenuSeparator) {
-					IContributionItem item = (IContributionItem) ((MOpaqueMenuSeparator) menuElement)
-							.getOpaqueItem();
-					if (item != null && item.isVisible()) {
-						return true;
-					}
-				} else {
-					return true;
-				}
-			}
-		}
-
-		Object menuRenderer = viewMenu.getRenderer();
-		if (menuRenderer instanceof MenuManagerRenderer) {
-			MenuManager manager = ((MenuManagerRenderer) menuRenderer)
-					.getManager(viewMenu);
-			if (manager != null && manager.isVisible()) {
-				return true;
-			}
-		}
-
-		Control control = (Control) part.getWidget();
-		Menu menu = (Menu) renderer.createGui(viewMenu, control.getShell(),
-				part.getContext());
-		if (menu != null) {
-			menuRenderer = viewMenu.getRenderer();
-			if (menuRenderer instanceof MenuManagerRenderer) {
-				MenuManagerRenderer menuManagerRenderer = (MenuManagerRenderer) menuRenderer;
-				MenuManager manager = menuManagerRenderer.getManager(viewMenu);
-				if (manager != null) {
-					// remark ourselves as dirty so that the menu will be
-					// reconstructed
-					manager.markDirty();
-				}
-			}
-			return menu.getItemCount() != 0;
-		}
-		return false;
-	}
 }
diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/ActionBars.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/ActionBars.java
index 8e46f3d..55dc343 100644
--- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/ActionBars.java
+++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/ActionBars.java
@@ -13,12 +13,10 @@
 
 import org.eclipse.e4.ui.model.application.ui.MElementContainer;
 import org.eclipse.e4.ui.model.application.ui.MGenericStack;
-import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.widgets.CTabFolder;
-import org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer;
 import org.eclipse.e4.ui.workbench.renderers.swt.ToolBarManagerRenderer;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.IToolBarManager;
@@ -112,31 +110,9 @@
 				}
 			}
 		}
-
-		MUIElement parent = getParentModel();
-		if (parent != null) {
-			Object renderer = parent.getRenderer();
-			if (renderer instanceof StackRenderer) {
-				StackRenderer stackRenderer = (StackRenderer) renderer;
-				CTabFolder folder = (CTabFolder) parent.getWidget();
-				stackRenderer.disposeViewMenu(folder);
-				stackRenderer.setupMenuButton(part, folder);
-				stackRenderer.layoutTopRight(folder);
-			}
-		}
-
 		super.updateActionBars();
 	}
 
-	private MUIElement getParentModel() {
-		MElementContainer<MUIElement> parent = part.getParent();
-		if (parent == null) {
-			MPlaceholder placeholder = part.getCurSharedRef();
-			return placeholder == null ? null : placeholder.getParent();
-		}
-		return parent;
-	}
-
 	private Control getPackParent(Control control) {
 		Composite parent = control.getParent();
 		while (parent != null) {
diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/CompatibilityView.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/CompatibilityView.java
index b1b8008..8b27f3f 100644
--- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/CompatibilityView.java
+++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/e4/compatibility/CompatibilityView.java
@@ -78,15 +78,6 @@
 		return reference;
 	}
 
-	private MMenu getViewMenu() {
-		for (MMenu menu : part.getMenus()) {
-			if (menu.getTags().contains(StackRenderer.TAG_VIEW_MENU)) {
-				return menu;
-			}
-		}
-		return null;
-	}
-
 	/*
 	 * (non-Javadoc)
 	 * 
@@ -98,9 +89,6 @@
 	protected void createPartControl(IWorkbenchPart legacyPart, Composite parent) {
 		part.getContext().set(IViewPart.class, (IViewPart) legacyPart);
 
-		IEclipseContext context = getModel().getContext();
-		IRendererFactory rendererFactory = context.get(IRendererFactory.class);
-
 		// Some views (i.e. Console) require that the actual ToolBar be
 		// instantiated before they are
 		IActionBars actionBars = ((IViewPart) legacyPart).getViewSite().getActionBars();
@@ -108,22 +96,22 @@
 		Composite toolBarParent = new Composite(parent, SWT.NONE);
 		tbm.createControl(toolBarParent);
 
-		MenuManager mm = (MenuManager) actionBars.getMenuManager();
-		MMenu menu = getViewMenu();
-		if (menu != null) {
-			AbstractPartRenderer apr = rendererFactory.getRenderer(menu, parent);
-			if (apr instanceof MenuManagerRenderer) {
-				MenuManagerRenderer renderer = (MenuManagerRenderer) apr;
-				renderer.linkModelToManager(menu, mm);
-			}
-		}
-
 		super.createPartControl(legacyPart, parent);
 
 		// dispose the tb, it will be re-created when the tab is shown
 		toolBarParent.dispose();
 
-		menu = getViewMenu();
+		IEclipseContext context = getModel().getContext();
+		IRendererFactory rendererFactory = context.get(IRendererFactory.class);
+
+		MenuManager mm = (MenuManager) actionBars.getMenuManager();
+		MMenu menu = null;
+		for (MMenu me : part.getMenus()) {
+			if (me.getTags().contains(StackRenderer.TAG_VIEW_MENU)) {
+				menu = me;
+				break;
+			}
+		}
 		if (menu == null) {
 			menu = MenuFactoryImpl.eINSTANCE.createMenu();
 			menu.setElementId(part.getElementId());