]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/SingleClickStartEditMouseListener.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / SingleClickStartEditMouseListener.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
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;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class SingleClickStartEditMouseListener extends MouseAdapter {
28
29     public static final IFilter BUTTON1_FIRST_DOWN_FILTER = new IFilter() {
30         @Override
31         public boolean select(Object toTest) {
32             MouseEvent e = (MouseEvent) toTest;
33             return e.button == 1 && e.count == 1;
34         }
35     }; 
36
37     private GraphExplorer explorer;
38     private IFilter       eventFilter;
39     private Tree          tree;
40     private Column[]      columns;
41
42     public static SingleClickStartEditMouseListener attachWithSingleLeftDown(GraphExplorer explorer) {
43         return attachTo(explorer, BUTTON1_FIRST_DOWN_FILTER);
44     }
45
46     public static SingleClickStartEditMouseListener attachTo(GraphExplorer explorer, IFilter eventFilter) {
47         SingleClickStartEditMouseListener l = new SingleClickStartEditMouseListener(explorer, eventFilter);
48         explorer.addListener(l);
49         return l;
50     }
51
52     public SingleClickStartEditMouseListener(GraphExplorer explorer, IFilter eventFilter) {
53         if (explorer == null)
54             throw new NullPointerException("null explorer");
55
56         this.explorer = explorer;
57         this.eventFilter = eventFilter;
58         this.tree = (Tree) explorer.getControl();
59         this.columns = explorer.getColumns();
60     }
61
62     @Override
63     public void mouseDown(MouseEvent e) {
64         if (eventFilter != null && !eventFilter.select(e))
65             return;
66
67         Point point = new Point(e.x, e.y);
68         TreeItem item = tree.getItem(point);
69         if (item == null)
70             return;
71
72         NodeContext ctx = (NodeContext) item.getData();
73         if (ctx == null)
74             return;
75
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());
80             }
81         }
82     }
83
84 }