]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerMouseAdapter.java
9a28ed4f2f3df09ed8a33c60d25137acec94cdaf
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / GraphExplorerMouseAdapter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt;\r
13 \r
14 import org.eclipse.jface.viewers.ISelection;\r
15 import org.eclipse.jface.viewers.ISelectionProvider;\r
16 import org.eclipse.jface.viewers.StructuredSelection;\r
17 import org.eclipse.swt.events.MouseAdapter;\r
18 import org.eclipse.swt.events.MouseEvent;\r
19 import org.eclipse.swt.graphics.Point;\r
20 import org.eclipse.swt.widgets.Tree;\r
21 import org.eclipse.swt.widgets.TreeItem;\r
22 import org.simantics.browsing.ui.GraphExplorer;\r
23 import org.simantics.browsing.ui.NodeContext;\r
24 import org.simantics.utils.ui.AdaptionUtils;\r
25 import org.simantics.utils.ui.action.IPriorityAction;\r
26 \r
27 /**\r
28  * An abstract base class for implementing SWT mouse event handlers for\r
29  * SWT-based {@link GraphExplorer}.\r
30  * \r
31  * <p>\r
32  * Use {@link #getClickedContext(MouseEvent)} to find the possible NodeContext\r
33  * the user clicked. You may also reimplement\r
34  * {@link #handleContextDoubleClick(NodeContext)} for the easiest way to\r
35  * redefine double click handling.\r
36  * \r
37  * @author Tuukka Lehtonen\r
38  * \r
39  * @see IPriorityAction\r
40  */\r
41 public class GraphExplorerMouseAdapter extends MouseAdapter {\r
42 \r
43     protected GraphExplorer ge;\r
44 \r
45     public GraphExplorerMouseAdapter(GraphExplorer ge) {\r
46         this.ge = ge;\r
47     }\r
48 \r
49     protected ISelection getClickedContext(MouseEvent e) {\r
50         final Tree tree = (Tree) e.getSource();\r
51         Point point = new Point(e.x, e.y);\r
52         TreeItem item = tree.getItem(point);\r
53 \r
54         // No selectable item at point?\r
55         if (item == null)\r
56             return null;\r
57 \r
58         Object data = item.getData();\r
59         NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);\r
60         if (context == null)\r
61             return null;\r
62         //System.out.println("double clicked tree item data: " + data);\r
63 \r
64         ISelectionProvider sp = (ISelectionProvider) ge.getAdapter(ISelectionProvider.class);\r
65         ISelection selection = sp.getSelection();\r
66         //System.out.println("double clicked tree selection: " + selection);\r
67 \r
68         // Validate that the selection matches the clicked data.\r
69         NodeContext selectionContext = AdaptionUtils.adaptToSingle(selection, NodeContext.class);\r
70         if (!context.equals(selectionContext)) {\r
71             //ErrorLogger.defaultLogWarning("GraphExplorer selection does not match clicked tree item's NodeContext", new Exception("stacktrace"));\r
72             return new StructuredSelection(context);\r
73         }\r
74 \r
75         return selection;\r
76     }\r
77 \r
78     @Override\r
79     public void mouseDoubleClick(MouseEvent e) {\r
80         ISelection context = getClickedContext(e);\r
81         if (context == null)\r
82             return;\r
83 \r
84         Tree tree = (Tree) e.getSource();\r
85         handleContextDoubleClick(tree, context);\r
86     }\r
87 \r
88     protected void handleContextDoubleClick(Tree tree, ISelection selection) {\r
89     }\r
90 \r
91 }\r