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