--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.element.handler.impl;\r
+\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.Togglable;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+\r
+/**\r
+ * Base implementation for button handlers\r
+ * \r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public abstract class AbstractTogglable extends AbstractClickable implements Togglable {\r
+\r
+ private static final long serialVersionUID = 8409636755289629134L;\r
+ public static Key TOGGLE_KEY = new KeyOf(Boolean.class);\r
+\r
+ public AbstractTogglable(Double strayDistance) {\r
+ super(strayDistance);\r
+ }\r
+\r
+ public AbstractTogglable() {\r
+ super();\r
+ }\r
+\r
+\r
+ @Override\r
+ public boolean handleMouseEvent(IElement e, ICanvasContext ctx,\r
+ MouseEvent me) {\r
+\r
+ boolean b = super.handleMouseEvent(e, ctx, me);\r
+ \r
+ return b;\r
+ }\r
+\r
+ \r
+ protected void onClicked(GrabInfo gi, ICanvasContext ctx) {\r
+ IElement e = gi.e;\r
+ Boolean b = e.getHint(TOGGLE_KEY);\r
+ if (b == null)\r
+ b = false;\r
+ e.setHint(TOGGLE_KEY, !b);\r
+ }\r
+ \r
+ @Override\r
+ public boolean isChecked(IElement e) {\r
+ Boolean b = e.getHint(TOGGLE_KEY);\r
+ return (b == null ? false : b);\r
+ }\r
+ \r
+}\r