1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.swing;
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Cursor;
17 import java.awt.Graphics2D;
18 import java.awt.Shape;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.awt.geom.AffineTransform;
22 import java.awt.geom.Rectangle2D;
23 import java.util.HashSet;
26 import javax.swing.JToggleButton;
28 import org.simantics.scenegraph.utils.DummyComponent;
30 public class JBooleanToggleValueNode extends ComponentNode<JToggleButton> implements ActionListener {
34 private static final long serialVersionUID = 3255791584573492072L;
36 protected static final BasicStroke STROKE = new BasicStroke(1.0f);
37 protected static final Rectangle2D BOUNDS = new Rectangle2D.Double(-10, -10, 20, 20);
39 protected Boolean value = null;
40 protected transient Set<ActionListener> actionListeners = new HashSet<ActionListener>();
43 public void setValue(Boolean value) {
45 if(component instanceof JToggleButton) {
46 ((JToggleButton)component).setSelected(value == null ? false : value);
50 @SyncField("transform")
51 public void setTransform(AffineTransform transform) {
52 assert(transform != null);
53 this.transform = transform;
54 this.transform.translate(-10, -10);
58 public void render(Graphics2D g2d) {
59 if (component != null) {
60 int width = (int)BOUNDS.getWidth();
61 int height = (int)BOUNDS.getHeight();
63 AffineTransform ot = g2d.getTransform();
66 if(transform != null) {
67 g2d.transform(transform);
68 sx = g2d.getTransform().getScaleX();
69 sy = g2d.getTransform().getScaleY();
70 g2d.scale(1/sx, 1/sy);
72 component.setSize((int)(width*sx), (int)(height*sy));
80 * Helper method to be used inside MonitorClass
83 public static Shape getOutline() {
88 public Rectangle2D getBoundsInLocal() {
93 * ClientSide method for handling event and transforming it
94 * NOTE: This is just a wrapper method
97 public void actionPerformed(ActionEvent e) {
98 Boolean s = component.isSelected();
99 performAction(new ActionEvent(new DummyComponent(), ActionEvent.ACTION_PERFORMED, ""+s));
103 * ServerSide method for calling the actionlisteners
107 protected void performAction(ActionEvent e) {
108 for(ActionListener listener : actionListeners)
109 listener.actionPerformed(e);
112 public void addActionListener(ActionListener l) {
113 actionListeners.add(l);
116 public void removeActionListener(ActionListener l) {
117 actionListeners.remove(l);
122 Boolean s = value == null ? false : value;
123 component = new JToggleButton(null, null, s);
124 component.setCursor(new Cursor(Cursor.HAND_CURSOR));
125 component.addActionListener(this);
126 component.setBackground(Color.RED);