]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/GraphExplorerMouseAdapter.java
Sync git svn branch with SVN repository r33144.
[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.widgets.Control;\r
20 import org.simantics.browsing.ui.GraphExplorer;\r
21 import org.simantics.browsing.ui.NodeContext;\r
22 import org.simantics.utils.ui.AdaptionUtils;\r
23 import org.simantics.utils.ui.action.IPriorityAction;\r
24 \r
25 /**\r
26  * An abstract base class for implementing SWT mouse event handlers for\r
27  * SWT-based {@link GraphExplorer}.\r
28  * \r
29  * <p>\r
30  * Use {@link #getClickedContext(MouseEvent)} to find the possible NodeContext\r
31  * the user clicked. You may also reimplement\r
32  * {@link #handleContextDoubleClick(NodeContext)} for the easiest way to\r
33  * redefine double click handling.\r
34  * \r
35  * @author Tuukka Lehtonen\r
36  * \r
37  * @see IPriorityAction\r
38  */\r
39 public class GraphExplorerMouseAdapter extends MouseAdapter {\r
40 \r
41     protected GraphExplorer ge;\r
42 \r
43     public GraphExplorerMouseAdapter(GraphExplorer ge) {\r
44         this.ge = ge;\r
45     }\r
46 \r
47     protected ISelection getClickedContext(MouseEvent e) {\r
48 //        final Tree tree = (Tree) e.getSource();\r
49 //        Point point = new Point(e.x, e.y);\r
50 //        TreeItem item = tree.getItem(point);\r
51 //\r
52 //        // No selectable item at point?\r
53 //        if (item == null)\r
54 //            return null;\r
55 //\r
56 //        Object data = item.getData();\r
57         Object data = ge.getClicked(e);\r
58         if (data == null)\r
59                 return null;\r
60         \r
61         NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);\r
62         if (context == null)\r
63             return null;\r
64         //System.out.println("double clicked tree item data: " + data);\r
65 \r
66         ISelectionProvider sp = (ISelectionProvider) ge.getAdapter(ISelectionProvider.class);\r
67         ISelection selection = sp.getSelection();\r
68         //System.out.println("double clicked tree selection: " + selection);\r
69 \r
70         // Validate that the selection matches the clicked data.\r
71         NodeContext selectionContext = AdaptionUtils.adaptToSingle(selection, NodeContext.class);\r
72         if (!context.equals(selectionContext)) {\r
73             //ErrorLogger.defaultLogWarning("GraphExplorer selection does not match clicked tree item's NodeContext", new Exception("stacktrace"));\r
74             return new StructuredSelection(context);\r
75         }\r
76 \r
77         return selection;\r
78     }\r
79 \r
80     @Override\r
81     public void mouseDoubleClick(MouseEvent e) {\r
82         ISelection context = getClickedContext(e);\r
83         if (context == null)\r
84             return;\r
85 \r
86         Control tree = (Control)e.getSource();\r
87         handleContextDoubleClick(tree, context);\r
88     }\r
89 \r
90     protected void handleContextDoubleClick(Control tree, ISelection selection) {\r
91     }\r
92 \r
93 }\r