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.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
26 * An abstract base class for implementing SWT mouse event handlers for
\r
27 * SWT-based {@link GraphExplorer}.
\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
35 * @author Tuukka Lehtonen
\r
37 * @see IPriorityAction
\r
39 public class GraphExplorerMouseAdapter extends MouseAdapter {
\r
41 protected GraphExplorer ge;
\r
43 public GraphExplorerMouseAdapter(GraphExplorer ge) {
\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
52 // // No selectable item at point?
\r
53 // if (item == null)
\r
56 // Object data = item.getData();
\r
57 Object data = ge.getClicked(e);
\r
61 NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);
\r
62 if (context == null)
\r
64 //System.out.println("double clicked tree item data: " + data);
\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
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
81 public void mouseDoubleClick(MouseEvent e) {
\r
82 ISelection context = getClickedContext(e);
\r
83 if (context == null)
\r
86 Control tree = (Control)e.getSource();
\r
87 handleContextDoubleClick(tree, context);
\r
90 protected void handleContextDoubleClick(Control tree, ISelection selection) {
\r