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