--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.swt;\r
+\r
+import org.eclipse.jface.viewers.IFilter;\r
+import org.eclipse.swt.events.MouseAdapter;\r
+import org.eclipse.swt.events.MouseEvent;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.swt.widgets.TreeItem;\r
+import org.simantics.browsing.ui.Column;\r
+import org.simantics.browsing.ui.GraphExplorer;\r
+import org.simantics.browsing.ui.NodeContext;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SingleClickStartEditMouseListener extends MouseAdapter {\r
+\r
+ public static final IFilter BUTTON1_FIRST_DOWN_FILTER = new IFilter() {\r
+ @Override\r
+ public boolean select(Object toTest) {\r
+ MouseEvent e = (MouseEvent) toTest;\r
+ return e.button == 1 && e.count == 1;\r
+ }\r
+ }; \r
+\r
+ private GraphExplorer explorer;\r
+ private IFilter eventFilter;\r
+ private Tree tree;\r
+ private Column[] columns;\r
+\r
+ public static SingleClickStartEditMouseListener attachWithSingleLeftDown(GraphExplorer explorer) {\r
+ return attachTo(explorer, BUTTON1_FIRST_DOWN_FILTER);\r
+ }\r
+\r
+ public static SingleClickStartEditMouseListener attachTo(GraphExplorer explorer, IFilter eventFilter) {\r
+ SingleClickStartEditMouseListener l = new SingleClickStartEditMouseListener(explorer, eventFilter);\r
+ explorer.addListener(l);\r
+ return l;\r
+ }\r
+\r
+ public SingleClickStartEditMouseListener(GraphExplorer explorer, IFilter eventFilter) {\r
+ if (explorer == null)\r
+ throw new NullPointerException("null explorer");\r
+\r
+ this.explorer = explorer;\r
+ this.eventFilter = eventFilter;\r
+ this.tree = (Tree) explorer.getControl();\r
+ this.columns = explorer.getColumns();\r
+ }\r
+\r
+ @Override\r
+ public void mouseDown(MouseEvent e) {\r
+ if (eventFilter != null && !eventFilter.select(e))\r
+ return;\r
+\r
+ Point point = new Point(e.x, e.y);\r
+ TreeItem item = tree.getItem(point);\r
+ if (item == null)\r
+ return;\r
+\r
+ NodeContext ctx = (NodeContext) item.getData();\r
+ if (ctx == null)\r
+ return;\r
+\r
+ int columnCount = tree.getColumnCount();\r
+ for (int i = 0; i < columnCount && i < columns.length; i++) {\r
+ if (!item.isDisposed() && item.getBounds(i).contains(point)) {\r
+ explorer.startEditing(ctx, columns[i].getKey());\r
+ }\r
+ }\r
+ }\r
+\r
+}
\ No newline at end of file