1 package org.simantics.scenegraph.tests;
2 import java.awt.AWTEvent;
3 import java.awt.BasicStroke;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.Stroke;
8 import java.awt.event.ComponentEvent;
9 import java.awt.event.FocusEvent;
10 import java.awt.event.KeyEvent;
11 import java.awt.event.MouseEvent;
12 import java.awt.event.MouseWheelEvent;
13 import java.awt.geom.AffineTransform;
14 import java.awt.image.VolatileImage;
15 import java.util.Timer;
16 import java.util.TimerTask;
18 import javax.swing.JApplet;
20 import org.simantics.diagram.connection.RouteGraph;
21 import org.simantics.diagram.connection.RouteLine;
22 import org.simantics.diagram.connection.RouteTerminal;
23 import org.simantics.diagram.connection.rendering.BasicConnectionStyle;
24 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
25 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
26 import org.simantics.diagram.connection.rendering.arrows.PlainExampleLineEndStyle;
27 import org.simantics.scenegraph.g2d.G2DFocusManager;
28 import org.simantics.scenegraph.g2d.G2DParentNode;
29 import org.simantics.scenegraph.g2d.G2DSceneGraph;
30 import org.simantics.scenegraph.g2d.events.EventDelegator;
31 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
32 import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener;
33 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent;
34 import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
38 * @author Tuukka Lehtonen
40 public class TestRouteGraphNodeApplet extends JApplet implements Runnable {
42 private static final long serialVersionUID = -2097427018689455381L;
44 private static final int FPS = 60;
46 protected VolatileImage backbuffer = null;
47 protected boolean interrupted = false;
49 Color LINE_COLOR1 = Color.RED;
50 Color LINE_COLOR2 = Color.GREEN;
51 Stroke LINE_STROKE = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL);
52 Stroke ROUTE_LINE_STROKE = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
57 BasicConnectionStyle style = new BasicConnectionStyle(LINE_COLOR1, Color.BLACK, 3.0, LINE_STROKE, ROUTE_LINE_STROKE, 8, 0.0);
59 Timer timer = new Timer();
60 TimerTask task = new TimerTask() {
61 boolean toggle = false;
64 //Color color = toggle ? LINE_COLOR1 : LINE_COLOR2;
65 //style.setLineColor(color);
66 rgn.setRenderer(new StyledRouteGraphRenderer(style));
74 setFocusCycleRoot(true);
75 enableInputMethods(true);
76 enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK | AWTEvent.COMPONENT_EVENT_MASK | AWTEvent.PAINT_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK);
78 backbuffer = createVolatileImage(getWidth(), getHeight());
80 this.rg = new RouteGraph();
81 RouteLine v = rg.addLine(false, 0);
84 rg.link(addTerminal("T1", rg, -100, -100, 1, PlainExampleLineEndStyle.INSTANCE), v);
85 // rg.link(addTerminal("T2", rg, -100, 100, 1, PlainExampleLineEndStyle.INSTANCE), v);
86 // rg.link(addTerminal("T3", rg, 100, -80, 1, ArrowExampleLineEndStyle.INSTANCE), v);
87 // rg.link(addTerminal("T4", rg, 100, 100, 4, ArrowExampleLineEndStyle.INSTANCE), v);
89 sg = new G2DSceneGraph();
90 NavigationNode nav = sg.addNode("navigation", NavigationNode.class);
92 // This just tests that RouteGraphNode handles transforms correctly
93 final G2DParentNode temp = nav.addNode("temp", G2DParentNode.class);
94 temp.setTransform(AffineTransform.getTranslateInstance(50, 50));
96 rgn = temp.addNode("rg1", RouteGraphNode.class);
97 rgn.setTransform(AffineTransform.getTranslateInstance(100, 100));
98 rgn.setRouteGraph(rg);
99 rgn.setRenderer(new StyledRouteGraphRenderer(style));
101 rgn.setRouteGraphListener(new IRouteGraphListener() {
103 public void routeGraphChanged(RouteGraphChangeEvent event) {
104 event.node.getRouteGraphDelta().print();
108 Thread t = new Thread(this);
111 timer.schedule(task, 1000, 1000);
114 private static RouteTerminal addTerminal(Object data, RouteGraph rg, double x, double y, int allowedDirections, ILineEndStyle style) {
115 RouteTerminal rt = rg.addTerminal(x, y, x-20, y-20, x+20, y+20, allowedDirections, style);
121 while(!interrupted) {
123 long ival = 1000 / FPS;
126 } catch (InterruptedException e) {
132 public void update(Graphics g) {
133 int result = backbuffer.validate(getGraphicsConfiguration());
134 if (result == VolatileImage.IMAGE_INCOMPATIBLE) {
135 backbuffer = this.createVolatileImage(getWidth(), getHeight());
138 Graphics2D g2d = backbuffer.createGraphics();
139 g2d.clearRect(0, 0, getWidth(), getHeight());
140 g2d.setClip(0, 0, getWidth(), getHeight());
143 g.drawImage(backbuffer, 0, 0, this);
146 public void paint(Graphics g) {
150 public void processEvent(AWTEvent event) {
151 if(event.getID() == ComponentEvent.COMPONENT_RESIZED) {
152 backbuffer = this.createVolatileImage(getWidth(), getHeight());
157 EventDelegator handler = sg.getEventDelegator();
158 switch(event.getID()) {
159 case MouseEvent.MOUSE_PRESSED:
160 G2DFocusManager.INSTANCE.clearFocus();
161 handler.mousePressed((MouseEvent)event);
162 if(G2DFocusManager.INSTANCE.getFocusOwner() == null) {
163 this.requestFocusInWindow();
167 case MouseEvent.MOUSE_RELEASED:
168 handler.mouseReleased((MouseEvent)event);
171 case MouseEvent.MOUSE_CLICKED:
172 handler.mouseClicked((MouseEvent)event);
175 case MouseEvent.MOUSE_MOVED:
176 handler.mouseMoved((MouseEvent)event);
179 case MouseEvent.MOUSE_DRAGGED:
180 handler.mouseDragged((MouseEvent)event);
183 case MouseEvent.MOUSE_WHEEL:
184 handler.mouseWheelMoved((MouseWheelEvent)event);
187 case MouseEvent.MOUSE_ENTERED:
188 handler.mouseEntered((MouseEvent)event);
191 case MouseEvent.MOUSE_EXITED:
192 handler.mouseExited((MouseEvent)event);
195 case KeyEvent.KEY_PRESSED:
196 handler.keyPressed((KeyEvent)event);
199 case KeyEvent.KEY_RELEASED:
200 handler.keyReleased((KeyEvent)event);
203 case KeyEvent.KEY_TYPED:
204 handler.keyTyped((KeyEvent)event);
207 case FocusEvent.FOCUS_GAINED:
208 handler.focusGained((FocusEvent)event);
211 case FocusEvent.FOCUS_LOST:
212 handler.focusLost((FocusEvent)event);