1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
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;
26 * An abstract base class for implementing SWT mouse event handlers for
27 * SWT-based {@link GraphExplorer}.
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.
35 * @author Tuukka Lehtonen
37 * @see IPriorityAction
39 public class GraphExplorerMouseAdapter extends MouseAdapter {
41 protected GraphExplorer ge;
43 public GraphExplorerMouseAdapter(GraphExplorer ge) {
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);
52 // // No selectable item at point?
56 // Object data = item.getData();
57 Object data = ge.getClicked(e);
61 NodeContext context = AdaptionUtils.adaptToSingle(data, NodeContext.class);
64 //System.out.println("double clicked tree item data: " + data);
66 ISelectionProvider sp = (ISelectionProvider) ge.getAdapter(ISelectionProvider.class);
67 ISelection selection = sp.getSelection();
68 //System.out.println("double clicked tree selection: " + selection);
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);
81 public void mouseDoubleClick(MouseEvent e) {
82 ISelection context = getClickedContext(e);
86 Control tree = (Control)e.getSource();
87 handleContextDoubleClick(tree, context);
90 protected void handleContextDoubleClick(Control tree, ISelection selection) {