]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/AbstractTogglable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / AbstractTogglable.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.element.handler.impl;\r
13 \r
14 import org.simantics.g2d.canvas.ICanvasContext;\r
15 import org.simantics.g2d.element.IElement;\r
16 import org.simantics.g2d.element.handler.Togglable;\r
17 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
18 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
19 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
20 \r
21 \r
22 /**\r
23  * Base implementation for button handlers\r
24  * \r
25  * \r
26  * @author Toni Kalajainen\r
27  */\r
28 public abstract class AbstractTogglable extends AbstractClickable implements Togglable {\r
29 \r
30         private static final long serialVersionUID = 8409636755289629134L;\r
31         public static Key TOGGLE_KEY = new KeyOf(Boolean.class);\r
32 \r
33         public AbstractTogglable(Double strayDistance) {\r
34                 super(strayDistance);\r
35         }\r
36 \r
37         public AbstractTogglable() {\r
38                 super();\r
39         }\r
40 \r
41 \r
42         @Override\r
43         public boolean handleMouseEvent(IElement e, ICanvasContext ctx,\r
44                         MouseEvent me) {\r
45 \r
46                 boolean b = super.handleMouseEvent(e, ctx, me);\r
47                 \r
48                 return b;\r
49         }\r
50 \r
51         \r
52         protected void onClicked(GrabInfo gi, ICanvasContext ctx) {\r
53                 IElement e = gi.e;\r
54                 Boolean b = e.getHint(TOGGLE_KEY);\r
55                 if (b == null)\r
56                         b = false;\r
57                 e.setHint(TOGGLE_KEY, !b);\r
58         }\r
59         \r
60         @Override\r
61         public boolean isChecked(IElement e) {\r
62                 Boolean b = e.getHint(TOGGLE_KEY);\r
63                 return (b == null ? false : b);\r
64         }\r
65         \r
66 }\r