More work to unify the behavior of the Remote Connections status and the News Reader status widgets (bug #10411).
authorEd Swartz <ed.swartz@nokia.com>
Tue, 29 Dec 2009 11:31:25 -0600
changeset 724 6ab9c30e708f
parent 723 81b14a47b8dc
child 725 d9cf6eb22de0
More work to unify the behavior of the Remote Connections status and the News Reader status widgets (bug #10411).
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionStatusSelectorContribution.java
core/com.nokia.carbide.cpp.news.reader/src/com/nokia/carbide/cpp/internal/news/reader/ui/NewsControlContribution.java
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionStatusSelectorContribution.java	Tue Dec 29 11:18:15 2009 -0600
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionStatusSelectorContribution.java	Tue Dec 29 11:31:25 2009 -0600
@@ -80,10 +80,10 @@
 public class ConnectionStatusSelectorContribution extends WorkbenchWindowControlContribution {
 
 	/**
-	 * This is a portion of the ICommand id used for the icon in the
+	 * This is the id on the command in the toolbar contribution associated with this 
 	 * widget.  Keep this in sync with the extension point!
 	 */
-	private static final String OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_SUFFIX = "openRemoteConnectionsView";
+	private static final String OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_ID = "openRemoteConnectionsView";
 	private Composite container;
 	private CLabel connectionInfo;
 	private ToolItem connectionIcon;
@@ -92,6 +92,7 @@
 	private ListenerBlock listenerBlock;
 	private ToolTip tooltip;
 	private MouseAdapter toolbarListener;
+	private ToolBar toolbar;
 
 	/**
 	 * Contains all the listeners.  In most cases we just recreate the contribution status item.
@@ -186,14 +187,13 @@
 		// to find the expected Eclipse-generated toolitem.  Unfortunately,
 		// controlling all this ourselves is even uglier (I tried).
 
-		final ToolBar toolbar; 
 		if (parent instanceof ToolBar) {
 			toolbar = (ToolBar) parent;
 			ToolItem[] items = toolbar.getItems();
 			for (ToolItem item : items) {
 				Object data = item.getData();
 				if (data instanceof CommandContributionItem &&
-						((CommandContributionItem) data).getId().contains(OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_SUFFIX)) {
+						((CommandContributionItem) data).getId().contains(OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_ID)) {
 					connectionIcon = item;
 					break;
 				}
@@ -402,12 +402,11 @@
 		removeListeners();
 		if (connectionIcon != null)
 			connectionIcon.dispose();
-		if (connectionInfo != null && !connectionInfo.isDisposed()) {
-			if (toolbarListener != null && !connectionInfo.getParent().isDisposed())
-				connectionInfo.getParent().removeMouseListener(toolbarListener);
-			toolbarListener = null;
+		if (toolbarListener != null && !toolbar.isDisposed())
+			toolbar.removeMouseListener(toolbarListener);
+		if (connectionInfo != null)
 			connectionInfo.dispose();
-		}
+		
 		super.dispose();
 	}
 
--- a/core/com.nokia.carbide.cpp.news.reader/src/com/nokia/carbide/cpp/internal/news/reader/ui/NewsControlContribution.java	Tue Dec 29 11:18:15 2009 -0600
+++ b/core/com.nokia.carbide.cpp.news.reader/src/com/nokia/carbide/cpp/internal/news/reader/ui/NewsControlContribution.java	Tue Dec 29 11:31:25 2009 -0600
@@ -35,9 +35,12 @@
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.menus.CommandContributionItem;
 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
 
 import com.nokia.carbide.cpp.internal.news.reader.CarbideNewsReaderPlugin;
@@ -53,12 +56,22 @@
 public class NewsControlContribution extends WorkbenchWindowControlContribution
 		implements IFeedCacheChangedlistener {
 
+	/** This is the id on the command in the toolbar contribution associated with this 
+	 * widget.  Keep this in sync with the extension point! */
+	private static final CharSequence NEWS_TRIM_COMMAND_ID = "newsTrimCommand"; //$NON-NLS-1$
+	
 	// private data
 	private static NewsControlContribution sharedInstance;
 	private Label label;
 	private boolean alert;
 	private Composite container;
 
+	private ToolItem newsIconItem;
+
+	private MouseAdapter toolbarListener;
+
+	private ToolBar toolbar;
+
 	/**
 	 * The constructor.
 	 */
@@ -74,6 +87,22 @@
 	 */
 	@Override
 	protected Control createControl(Composite parent) {
+		
+		if (parent instanceof ToolBar) {
+			toolbar = (ToolBar) parent;
+			ToolItem[] items = toolbar.getItems();
+			for (ToolItem item : items) {
+				Object data = item.getData();
+				if (data instanceof CommandContributionItem &&
+						((CommandContributionItem) data).getId().contains(NEWS_TRIM_COMMAND_ID)) {
+					newsIconItem = item;
+					break;
+				}
+			}
+		} else {
+			toolbar = null;
+		}
+		
 		container = new Composite(parent, SWT.NONE);
 		GridLayoutFactory.fillDefaults().margins(2, 2).applyTo(container);
 
@@ -88,11 +117,44 @@
 			alert = false;
 		}
 		
+		if (newsIconItem != null) {
+			// Yuck, toolbars and items have a wonky UI.  We need to do these events on the parent,
+			// since the ToolItem itself is just an area inside the parent.  (#getControl() is only for separators ?!)
+
+			// On icon: left click = open view, right click = menu
+			if (toolbar != null) {
+				if (toolbarListener != null)
+					toolbar.removeMouseListener(toolbarListener);
+				
+				toolbarListener = new MouseAdapter() {
+					/* (non-Javadoc)
+					 * @see org.eclipse.swt.events.MouseAdapter#mouseDown(org.eclipse.swt.events.MouseEvent)
+					 */
+					@Override
+					public void mouseDown(MouseEvent event) {
+						ToolItem item = toolbar.getItem(new Point(event.x, event.y));
+						if (item == newsIconItem) {
+							if (event.button == 1) {
+								NewsEditor.openEditor();
+							} else if (event.button == 3) {
+								Point screenLoc = toolbar.toDisplay(event.x, event.y);
+								handleNewsMenu(screenLoc);
+							}
+						}
+					}
+				};
+				toolbar.addMouseListener(toolbarListener);
+			}
+		}
+		
+		// On label: left or right click = menu
 		label.addMouseListener(new MouseAdapter() {
 			@Override
 			public void mouseDown(MouseEvent e) {
-				Point screenLoc = label.toDisplay(e.x, e.y);
-				handleNewsMenu(screenLoc);
+				if (e.button == 1 || e.button == 3) {
+					Point screenLoc = label.toDisplay(e.x, e.y);
+					handleNewsMenu(screenLoc);
+				}
 			}
 		});
 		
@@ -106,6 +168,10 @@
 	 */
 	public void dispose() {
 		FeedCacheManager.removeFeedCacheChangedListener(this);
+		if (toolbarListener != null && toolbar != null && !toolbar.isDisposed()) {
+			toolbar.removeMouseListener(toolbarListener);
+			toolbarListener = null;
+		}
 		super.dispose();
 	}