]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/testcases/org/simantics/scenegraph/tests/TestRouteGraphNodeApplet.java
Spreadsheet changes
[simantics/platform.git] / bundles / org.simantics.scenegraph / testcases / org / simantics / scenegraph / tests / TestRouteGraphNodeApplet.java
1 package org.simantics.scenegraph.tests;
2 import java.awt.AWTEvent;
3 import java.awt.BasicStroke;
4 import java.awt.Color;
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;
17
18 import javax.swing.JApplet;
19
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;
35
36
37 /**
38  * @author Tuukka Lehtonen
39  */
40 public class TestRouteGraphNodeApplet extends JApplet implements Runnable {
41
42     private static final long serialVersionUID = -2097427018689455381L;
43
44     private static final int FPS = 60;
45
46     protected VolatileImage backbuffer = null;
47     protected boolean interrupted = false;
48
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);
53
54     G2DSceneGraph sg;
55     RouteGraph rg;
56     RouteGraphNode rgn;
57     BasicConnectionStyle style = new BasicConnectionStyle(LINE_COLOR1, Color.BLACK, 3.0, LINE_STROKE, ROUTE_LINE_STROKE, 8, 0.0);
58
59     Timer timer = new Timer();
60     TimerTask task = new TimerTask() {
61         boolean toggle = false;
62         @Override
63         public void run() {
64             //Color color = toggle ? LINE_COLOR1 : LINE_COLOR2;
65             //style.setLineColor(color);
66             rgn.setRenderer(new StyledRouteGraphRenderer(style));
67             toggle ^= true;
68         }
69     };
70
71     public void init() {
72         setSize(600, 600);
73         setFocusable(true);
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);
77
78         backbuffer = createVolatileImage(getWidth(), getHeight());
79
80         this.rg = new RouteGraph();
81         RouteLine v = rg.addLine(false, 0);
82         v.setData( "L1" );
83
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);
88
89         sg = new G2DSceneGraph();
90         NavigationNode nav = sg.addNode("navigation", NavigationNode.class);
91
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));
95
96         rgn = temp.addNode("rg1", RouteGraphNode.class);
97         rgn.setTransform(AffineTransform.getTranslateInstance(100, 100));
98         rgn.setRouteGraph(rg);
99         rgn.setRenderer(new StyledRouteGraphRenderer(style));
100
101         rgn.setRouteGraphListener(new IRouteGraphListener() {
102             @Override
103             public void routeGraphChanged(RouteGraphChangeEvent event) {
104                 event.node.getRouteGraphDelta().print();
105             }
106         });
107
108         Thread t = new Thread(this);
109         t.start();
110
111         timer.schedule(task, 1000, 1000);
112     }
113
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);
116         rt.setData( data );
117         return rt;
118     }
119
120     public void run() {
121         while(!interrupted) {
122             repaint();
123             long ival = 1000 / FPS;
124             try {
125                 Thread.sleep(ival);
126             } catch (InterruptedException e) {
127                 e.printStackTrace();
128             }
129         }
130     }
131
132     public void update(Graphics g) {
133         int result = backbuffer.validate(getGraphicsConfiguration());           
134         if (result == VolatileImage.IMAGE_INCOMPATIBLE) {
135             backbuffer = this.createVolatileImage(getWidth(), getHeight());
136         }
137
138         Graphics2D g2d = backbuffer.createGraphics();
139         g2d.clearRect(0, 0, getWidth(), getHeight());
140         g2d.setClip(0, 0, getWidth(), getHeight());
141         sg.render(g2d);
142         g2d.dispose();
143         g.drawImage(backbuffer, 0, 0, this);
144     }
145     
146     public void paint(Graphics g) {
147         update(g);
148     }
149
150     public void processEvent(AWTEvent event) {
151         if(event.getID() == ComponentEvent.COMPONENT_RESIZED) {
152             backbuffer = this.createVolatileImage(getWidth(), getHeight());
153             repaint();
154         }
155
156         {
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();
164                     }
165                     break;
166                     
167                 case MouseEvent.MOUSE_RELEASED:
168                     handler.mouseReleased((MouseEvent)event);
169                     break;
170                     
171                 case MouseEvent.MOUSE_CLICKED:
172                     handler.mouseClicked((MouseEvent)event);
173                     break;
174                     
175                 case MouseEvent.MOUSE_MOVED:
176                     handler.mouseMoved((MouseEvent)event);
177                     break;
178
179                 case MouseEvent.MOUSE_DRAGGED:
180                     handler.mouseDragged((MouseEvent)event);
181                     break;
182                     
183                 case MouseEvent.MOUSE_WHEEL:
184                     handler.mouseWheelMoved((MouseWheelEvent)event);
185                     break;
186                     
187                 case MouseEvent.MOUSE_ENTERED:
188                     handler.mouseEntered((MouseEvent)event);
189                     break;
190                     
191                 case MouseEvent.MOUSE_EXITED:
192                     handler.mouseExited((MouseEvent)event);
193                     break;
194                     
195                 case KeyEvent.KEY_PRESSED:
196                     handler.keyPressed((KeyEvent)event);
197                     break;
198                     
199                 case KeyEvent.KEY_RELEASED:
200                     handler.keyReleased((KeyEvent)event);
201                     break;
202                     
203                 case KeyEvent.KEY_TYPED:
204                     handler.keyTyped((KeyEvent)event);
205                     break;
206                     
207                 case FocusEvent.FOCUS_GAINED:
208                     handler.focusGained((FocusEvent)event);
209                     break;
210                     
211                 case FocusEvent.FOCUS_LOST:
212                     handler.focusLost((FocusEvent)event);
213                     break;
214                     
215                 default:
216                     break;
217             }
218         }
219     }
220
221 }