Bug 431868 - [Trim] Allow to hide ToolControl via right click menu 

Fixes issues reported by Dani (additional warning messages an the Quick
Access bar does not get visible immediately

Change-Id: I5acf65d21d9c53db5e5ae928649b4425480cd81a
Signed-off-by: Lars Vogel <Lars.Vogel@gmail.com>
diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolBarManagerRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolBarManagerRenderer.java
index 77ef82b..86a4249 100644
--- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolBarManagerRenderer.java
+++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolBarManagerRenderer.java
@@ -8,7 +8,7 @@
  * Contributors:
  *     IBM Corporation - initial API and implementation
  *     Maxime Porhel <maxime.porhel@obeo.fr> Obeo - Bug 410426
- *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 426535, 433234
+ *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 426535, 433234, 431868
  *     Maxime Porhel <maxime.porhel@obeo.fr> Obeo - Bug 431778
  *******************************************************************************/
 package org.eclipse.e4.ui.workbench.renderers.swt;
@@ -41,6 +41,7 @@
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.SideValue;
 import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
+import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.menu.MDirectToolItem;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledToolItem;
@@ -1053,16 +1054,18 @@
 	}
 
 	/**
-	 * Removes the IPresentationEngine.HIDDEN_EXPLICITLY from the toolbar entres
+	 * Removes the IPresentationEngine.HIDDEN_EXPLICITLY from the trimbar
+	 * entries. Having a separate logic for toolbars and toolcontrols would be
+	 * confusing for the user, hence we remove this tag for both these types
 	 *
 	 * @param toolbarModel
 	 */
 	private void removeHiddenTags(MToolBar toolbarModel) {
 		MWindow mWindow = modelService.getTopLevelWindowFor(toolbarModel);
-		List<MToolBar> toolBars = modelService.findElements(mWindow, null,
-				MToolBar.class, null);
-		for (MToolBar mToolBar : toolBars) {
-			mToolBar.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY);
+		List<MTrimElement> trimElements = modelService.findElements(mWindow,
+				null, MTrimElement.class, null);
+		for (MTrimElement trimElement : trimElements) {
+			trimElement.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY);
 		}
 	}
 
diff --git a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolControlRenderer.java b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolControlRenderer.java
index 279c511..c7993d8 100644
--- a/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolControlRenderer.java
+++ b/bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/ToolControlRenderer.java
@@ -1,38 +1,61 @@
 /*******************************************************************************
- * Copyright (c) 2010, 2012 IBM Corporation and others.
+ * Copyright (c) 2010, 2014 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
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     Sopot Cela <sopotcela@gmail.com> - Bug 431868
+ *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 431868
  *******************************************************************************/
 package org.eclipse.e4.ui.workbench.renderers.swt;
 
+import java.util.List;
+import javax.inject.Inject;
 import org.eclipse.e4.core.contexts.EclipseContextFactory;
 import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.core.services.contributions.IContributionFactory;
+import org.eclipse.e4.ui.di.UIEventTopic;
 import org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer;
 import org.eclipse.e4.ui.internal.workbench.swt.CSSRenderingUtils;
+import org.eclipse.e4.ui.model.application.MApplication;
 import org.eclipse.e4.ui.model.application.ui.MUIElement;
 import org.eclipse.e4.ui.model.application.ui.SideValue;
 import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar;
+import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
+import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;
+import org.eclipse.e4.ui.workbench.IPresentationEngine;
+import org.eclipse.e4.ui.workbench.UIEvents;
+import org.eclipse.e4.ui.workbench.UIEvents.EventTags;
 import org.eclipse.e4.ui.workbench.swt.factories.IRendererFactory;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
 import org.eclipse.swt.widgets.Widget;
+import org.osgi.service.event.Event;
 
 /**
  * Create a contribute part.
  */
 public class ToolControlRenderer extends SWTPartRenderer {
 
+	@Inject
+	private MApplication application;
+	/**
+	 * The context menu for this trim stack's items.
+	 */
+	private Menu toolControlMenu;
+
 	@Override
 	public Object createWidget(final MUIElement element, Object parent) {
 		if (!(element instanceof MToolControl)
@@ -104,7 +127,89 @@
 		}
 		CSSRenderingUtils cssUtils = parentContext.get(CSSRenderingUtils.class);
 		newCtrl = cssUtils.frameMeIfPossible(newCtrl, null, vertical, true);
+		createToolControlMenu(toolControl, newCtrl);
 		return newCtrl;
 	}
 
+	@Inject
+	@Optional
+	private void subscribeTopicTagsChanged(
+			@UIEventTopic(UIEvents.ApplicationElement.TOPIC_TAGS) Event event) {
+
+		Object changedObj = event.getProperty(EventTags.ELEMENT);
+
+		if (!(changedObj instanceof MToolControl))
+			return;
+
+		final MUIElement changedElement = (MUIElement) changedObj;
+
+		if (UIEvents.isADD(event)) {
+			if (UIEvents.contains(event, UIEvents.EventTags.NEW_VALUE,
+					IPresentationEngine.HIDDEN_EXPLICITLY)) {
+				changedElement.setVisible(false);
+			}
+		} else if (UIEvents.isREMOVE(event)) {
+			if (UIEvents.contains(event, UIEvents.EventTags.OLD_VALUE,
+					IPresentationEngine.HIDDEN_EXPLICITLY)) {
+				changedElement.setVisible(true);
+			}
+		}
+	}
+
+	@Inject
+	@Optional
+	private void subscribeTopicAppStartup(
+			@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) Event event) {
+		List<MToolControl> toolControls = modelService.findElements(
+				application, null, MToolControl.class, null);
+		for (MToolControl toolControl : toolControls) {
+			if (toolControl.getTags().contains(
+					IPresentationEngine.HIDDEN_EXPLICITLY)) {
+				toolControl.setVisible(false);
+			}
+		}
+	}
+
+	private void createToolControlMenu(final MToolControl toolControl,
+			Control renderedCtrl) {
+		toolControlMenu = new Menu(renderedCtrl);
+		MenuItem hideItem = new MenuItem(toolControlMenu, SWT.NONE);
+		hideItem.setText(Messages.ToolBarManagerRenderer_MenuCloseText);
+		hideItem.addListener(SWT.Selection, new Listener() {
+			public void handleEvent(org.eclipse.swt.widgets.Event event) {
+				toolControl.getTags()
+						.add(IPresentationEngine.HIDDEN_EXPLICITLY);
+			}
+		});
+
+		new MenuItem(toolControlMenu, SWT.SEPARATOR);
+
+		MenuItem restoreHiddenItems = new MenuItem(toolControlMenu, SWT.NONE);
+		restoreHiddenItems
+				.setText(Messages.ToolBarManagerRenderer_MenuRestoreText);
+		restoreHiddenItems.addListener(SWT.Selection, new Listener() {
+			public void handleEvent(org.eclipse.swt.widgets.Event event) {
+				removeHiddenTags(toolControl);
+			}
+		});
+		renderedCtrl.setMenu(toolControlMenu);
+
+	}
+
+	/**
+	 * Removes the IPresentationEngine.HIDDEN_EXPLICITLY from the trimbar
+	 * entries. Having a separate logic for toolbars and toolcontrols would be
+	 * confusing for the user, hence we remove this tag for both these types
+	 *
+	 * @param toolbarModel
+	 */
+	private void removeHiddenTags(MToolControl toolControl) {
+		MWindow mWindow = modelService.getTopLevelWindowFor(toolControl);
+		List<MTrimElement> trimElements = modelService.findElements(mWindow,
+				null, MTrimElement.class, null);
+		for (MTrimElement trimElement : trimElements) {
+			trimElement.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY);
+		}
+	}
+
 }
diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java
index 656c7f6..c3c982f 100644
--- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java
+++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchPage.java
@@ -10,6 +10,7 @@
  *     Christian Janz  - <christian.janz@gmail.com> Fix for Bug 385592
  *     Marc-Andre Laperle (Ericsson) - Fix for Bug 413590
  *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 431340, 431348, 426535, 433234
+ *     Lars Vogel <Lars.Vogel@gmail.com> - Bug 431868
  *******************************************************************************/
 
 package org.eclipse.ui.internal;
@@ -62,9 +63,9 @@
 import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
 import org.eclipse.e4.ui.model.application.ui.basic.MStackElement;
+import org.eclipse.e4.ui.model.application.ui.basic.MTrimElement;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
 import org.eclipse.e4.ui.model.application.ui.basic.MWindowElement;
-import org.eclipse.e4.ui.model.application.ui.menu.MToolBar;
 import org.eclipse.e4.ui.workbench.IPresentationEngine;
 import org.eclipse.e4.ui.workbench.UIEvents;
 import org.eclipse.e4.ui.workbench.modeling.EModelService;
@@ -3375,10 +3376,11 @@
 		tags.clear();
 		tags.addAll(dummyPerspective.getTags());
 
-		// remove HIDDEN_EXPLICITLY tag from toolbar
-		List<MToolBar> toolBars = modelService.findElements(window, null, MToolBar.class, null);
-		for (MToolBar mToolBar : toolBars) {
-			mToolBar.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY);
+		// remove HIDDEN_EXPLICITLY tag from trim elements
+		List<MTrimElement> trimElements = modelService.findElements(window, null,
+				MTrimElement.class, null);
+		for (MTrimElement mTrimElement : trimElements) {
+			mTrimElement.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY);
 		}
 
 		partService.requestActivation();