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