package org.simantics.scenegraph.tests; import java.awt.AWTEvent; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.event.ComponentEvent; import java.awt.event.FocusEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.geom.AffineTransform; import java.awt.image.VolatileImage; import java.util.Timer; import java.util.TimerTask; import javax.swing.JApplet; import org.simantics.diagram.connection.RouteGraph; import org.simantics.diagram.connection.RouteLine; import org.simantics.diagram.connection.RouteTerminal; import org.simantics.diagram.connection.rendering.BasicConnectionStyle; import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer; import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle; import org.simantics.diagram.connection.rendering.arrows.PlainExampleLineEndStyle; import org.simantics.scenegraph.g2d.G2DFocusManager; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.G2DSceneGraph; import org.simantics.scenegraph.g2d.events.EventDelegator; import org.simantics.scenegraph.g2d.nodes.NavigationNode; import org.simantics.scenegraph.g2d.nodes.connection.IRouteGraphListener; import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphChangeEvent; import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode; /** * @author Tuukka Lehtonen */ public class TestRouteGraphNodeApplet extends JApplet implements Runnable { private static final long serialVersionUID = -2097427018689455381L; private static final int FPS = 60; protected VolatileImage backbuffer = null; protected boolean interrupted = false; Color LINE_COLOR1 = Color.RED; Color LINE_COLOR2 = Color.GREEN; Stroke LINE_STROKE = new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL); Stroke ROUTE_LINE_STROKE = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); G2DSceneGraph sg; RouteGraph rg; RouteGraphNode rgn; BasicConnectionStyle style = new BasicConnectionStyle(LINE_COLOR1, Color.BLACK, 3.0, LINE_STROKE, ROUTE_LINE_STROKE, 8, 0.0); Timer timer = new Timer(); TimerTask task = new TimerTask() { boolean toggle = false; @Override public void run() { //Color color = toggle ? LINE_COLOR1 : LINE_COLOR2; //style.setLineColor(color); rgn.setRenderer(new StyledRouteGraphRenderer(style)); toggle ^= true; } }; public void init() { setSize(600, 600); setFocusable(true); setFocusCycleRoot(true); enableInputMethods(true); 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); backbuffer = createVolatileImage(getWidth(), getHeight()); this.rg = new RouteGraph(); RouteLine v = rg.addLine(false, 0); v.setData( "L1" ); rg.link(addTerminal("T1", rg, -100, -100, 1, PlainExampleLineEndStyle.INSTANCE), v); // rg.link(addTerminal("T2", rg, -100, 100, 1, PlainExampleLineEndStyle.INSTANCE), v); // rg.link(addTerminal("T3", rg, 100, -80, 1, ArrowExampleLineEndStyle.INSTANCE), v); // rg.link(addTerminal("T4", rg, 100, 100, 4, ArrowExampleLineEndStyle.INSTANCE), v); sg = new G2DSceneGraph(); NavigationNode nav = sg.addNode("navigation", NavigationNode.class); // This just tests that RouteGraphNode handles transforms correctly final G2DParentNode temp = nav.addNode("temp", G2DParentNode.class); temp.setTransform(AffineTransform.getTranslateInstance(50, 50)); rgn = temp.addNode("rg1", RouteGraphNode.class); rgn.setTransform(AffineTransform.getTranslateInstance(100, 100)); rgn.setRouteGraph(rg); rgn.setRenderer(new StyledRouteGraphRenderer(style)); rgn.setRouteGraphListener(new IRouteGraphListener() { @Override public void routeGraphChanged(RouteGraphChangeEvent event) { event.node.getRouteGraphDelta().print(); } }); Thread t = new Thread(this); t.start(); timer.schedule(task, 1000, 1000); } private static RouteTerminal addTerminal(Object data, RouteGraph rg, double x, double y, int allowedDirections, ILineEndStyle style) { RouteTerminal rt = rg.addTerminal(x, y, x-20, y-20, x+20, y+20, allowedDirections, style); rt.setData( data ); return rt; } public void run() { while(!interrupted) { repaint(); long ival = 1000 / FPS; try { Thread.sleep(ival); } catch (InterruptedException e) { e.printStackTrace(); } } } public void update(Graphics g) { int result = backbuffer.validate(getGraphicsConfiguration()); if (result == VolatileImage.IMAGE_INCOMPATIBLE) { backbuffer = this.createVolatileImage(getWidth(), getHeight()); } Graphics2D g2d = backbuffer.createGraphics(); g2d.clearRect(0, 0, getWidth(), getHeight()); g2d.setClip(0, 0, getWidth(), getHeight()); sg.render(g2d); g2d.dispose(); g.drawImage(backbuffer, 0, 0, this); } public void paint(Graphics g) { update(g); } public void processEvent(AWTEvent event) { if(event.getID() == ComponentEvent.COMPONENT_RESIZED) { backbuffer = this.createVolatileImage(getWidth(), getHeight()); repaint(); } { EventDelegator handler = sg.getEventDelegator(); switch(event.getID()) { case MouseEvent.MOUSE_PRESSED: G2DFocusManager.INSTANCE.clearFocus(); handler.mousePressed((MouseEvent)event); if(G2DFocusManager.INSTANCE.getFocusOwner() == null) { this.requestFocusInWindow(); } break; case MouseEvent.MOUSE_RELEASED: handler.mouseReleased((MouseEvent)event); break; case MouseEvent.MOUSE_CLICKED: handler.mouseClicked((MouseEvent)event); break; case MouseEvent.MOUSE_MOVED: handler.mouseMoved((MouseEvent)event); break; case MouseEvent.MOUSE_DRAGGED: handler.mouseDragged((MouseEvent)event); break; case MouseEvent.MOUSE_WHEEL: handler.mouseWheelMoved((MouseWheelEvent)event); break; case MouseEvent.MOUSE_ENTERED: handler.mouseEntered((MouseEvent)event); break; case MouseEvent.MOUSE_EXITED: handler.mouseExited((MouseEvent)event); break; case KeyEvent.KEY_PRESSED: handler.keyPressed((KeyEvent)event); break; case KeyEvent.KEY_RELEASED: handler.keyReleased((KeyEvent)event); break; case KeyEvent.KEY_TYPED: handler.keyTyped((KeyEvent)event); break; case FocusEvent.FOCUS_GAINED: handler.focusGained((FocusEvent)event); break; case FocusEvent.FOCUS_LOST: handler.focusLost((FocusEvent)event); break; default: break; } } } }