--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.ISelectionProvider;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
+import org.eclipse.swt.events.MouseAdapter;\r
+import org.eclipse.swt.events.MouseEvent;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.utils.ui.AdaptionUtils;\r
+import org.simantics.utils.ui.action.IPriorityAction;\r
+\r
+/**\r
+ * An abstract base class for implementing SWT mouse event handlers for\r
+ * SWT-based {@link GraphExplorer}.\r
+ * \r
+ * <p>\r
+ * Use {@link #getClickedContext(MouseEvent)} to find the possible NodeContext\r
+ * the user clicked. You may also reimplement\r
+ * {@link #handleContextDoubleClick(NodeContext)} for the easiest way to\r
+ * redefine double click handling.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see IPriorityAction\r
+ */\r
+public class GraphExplorerMouseAdapter extends MouseAdapter {\r
+\r
+ protected GraphExplorer ge;\r
+\r
+ public GraphExplorerMouseAdapter(GraphExplorer ge) {\r
+ this.ge = ge;\r
+ }\r
+\r
+ protected ISelection getClickedContext(MouseEvent e) {\r
+ final Tree tree = (Tree) e.getSource();\r
+ Point point = new Point(e.x, e.y);\r
+ TreeItem item = tree.getItem(point);\r
+\r
+ // No selectable item at point?\r
+ if (item == null)\r
+ return null;\r
+\r
+ Object data = item.getData();\r
+ NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);\r
+ if (context == null)\r
+ return null;\r
+ //System.out.println("double clicked tree item data: " + data);\r
+\r
+ ISelectionProvider sp = (ISelectionProvider) ge.getAdapter(ISelectionProvider.class);\r
+ ISelection selection = sp.getSelection();\r
+ //System.out.println("double clicked tree selection: " + selection);\r
+\r
+ // Validate that the selection matches the clicked data.\r
+ NodeContext selectionContext = AdaptionUtils.adaptToSingle(selection, NodeContext.class);\r
+ if (!context.equals(selectionContext)) {\r
+ //ErrorLogger.defaultLogWarning("GraphExplorer selection does not match clicked tree item's NodeContext", new Exception("stacktrace"));\r
+ return new StructuredSelection(context);\r
+ }\r
+\r
+ return selection;\r
+ }\r
+\r
+ @Override\r
+ public void mouseDoubleClick(MouseEvent e) {\r
+ ISelection context = getClickedContext(e);\r
+ if (context == null)\r
+ return;\r
+\r
+ Tree tree = (Tree) e.getSource();\r
+ handleContextDoubleClick(tree, context);\r
+ }\r
+\r
+ protected void handleContextDoubleClick(Tree tree, ISelection selection) {\r
+ }\r
+\r
+}\r