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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\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
28 * An abstract base class for implementing SWT mouse event handlers for
\r
29 * SWT-based {@link GraphExplorer}.
\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
37 * @author Tuukka Lehtonen
\r
39 * @see IPriorityAction
\r
41 public class GraphExplorerMouseAdapter extends MouseAdapter {
\r
43 protected GraphExplorer ge;
\r
45 public GraphExplorerMouseAdapter(GraphExplorer ge) {
\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
54 // No selectable item at point?
\r
58 Object data = item.getData();
\r
59 NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);
\r
60 if (context == null)
\r
62 //System.out.println("double clicked tree item data: " + data);
\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
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
79 public void mouseDoubleClick(MouseEvent e) {
\r
80 ISelection context = getClickedContext(e);
\r
81 if (context == null)
\r
84 Tree tree = (Tree) e.getSource();
\r
85 handleContextDoubleClick(tree, context);
\r
88 protected void handleContextDoubleClick(Tree tree, ISelection selection) {
\r