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