1 /*******************************************************************************
\r
2 * Copyright (c) 2013 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 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import org.eclipse.jface.viewers.IFilter;
\r
15 import org.eclipse.swt.events.MouseAdapter;
\r
16 import org.eclipse.swt.events.MouseEvent;
\r
17 import org.eclipse.swt.graphics.Point;
\r
18 import org.eclipse.swt.widgets.Tree;
\r
19 import org.eclipse.swt.widgets.TreeItem;
\r
20 import org.simantics.browsing.ui.Column;
\r
21 import org.simantics.browsing.ui.GraphExplorer;
\r
22 import org.simantics.browsing.ui.NodeContext;
\r
25 * @author Tuukka Lehtonen
\r
27 public class SingleClickStartEditMouseListener extends MouseAdapter {
\r
29 public static final IFilter BUTTON1_FIRST_DOWN_FILTER = new IFilter() {
\r
31 public boolean select(Object toTest) {
\r
32 MouseEvent e = (MouseEvent) toTest;
\r
33 return e.button == 1 && e.count == 1;
\r
37 private GraphExplorer explorer;
\r
38 private IFilter eventFilter;
\r
40 private Column[] columns;
\r
42 public static SingleClickStartEditMouseListener attachWithSingleLeftDown(GraphExplorer explorer) {
\r
43 return attachTo(explorer, BUTTON1_FIRST_DOWN_FILTER);
\r
46 public static SingleClickStartEditMouseListener attachTo(GraphExplorer explorer, IFilter eventFilter) {
\r
47 SingleClickStartEditMouseListener l = new SingleClickStartEditMouseListener(explorer, eventFilter);
\r
48 explorer.addListener(l);
\r
52 public SingleClickStartEditMouseListener(GraphExplorer explorer, IFilter eventFilter) {
\r
53 if (explorer == null)
\r
54 throw new NullPointerException("null explorer");
\r
56 this.explorer = explorer;
\r
57 this.eventFilter = eventFilter;
\r
58 this.tree = (Tree) explorer.getControl();
\r
59 this.columns = explorer.getColumns();
\r
63 public void mouseDown(MouseEvent e) {
\r
64 if (eventFilter != null && !eventFilter.select(e))
\r
67 Point point = new Point(e.x, e.y);
\r
68 TreeItem item = tree.getItem(point);
\r
72 NodeContext ctx = (NodeContext) item.getData();
\r
76 int columnCount = tree.getColumnCount();
\r
77 for (int i = 0; i < columnCount && i < columns.length; i++) {
\r
78 if (!item.isDisposed() && item.getBounds(i).contains(point)) {
\r
79 explorer.startEditing(ctx, columns[i].getKey());
\r