]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram.connection/tests/org/simantics/diagram/connection/tests/TestApplet.java
f392f318334f874205c12030cfbd17c921d23d1c
[simantics/platform.git] / bundles / org.simantics.diagram.connection / tests / org / simantics / diagram / connection / tests / TestApplet.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.diagram.connection.tests;
13
14 import java.awt.Color;
15 import java.awt.Graphics;
16 import java.awt.Graphics2D;
17 import java.awt.HeadlessException;
18 import java.awt.RenderingHints;
19 import java.awt.event.KeyAdapter;
20 import java.awt.event.KeyEvent;
21 import java.awt.event.MouseAdapter;
22 import java.awt.event.MouseEvent;
23 import java.awt.event.MouseMotionListener;
24
25 import javax.swing.JApplet;
26
27 import org.simantics.diagram.connection.RouteGraph;
28 import org.simantics.diagram.connection.RouteLine;
29 import org.simantics.diagram.connection.RouteLink;
30 import org.simantics.diagram.connection.RouteTerminal;
31 import org.simantics.diagram.connection.actions.IAction;
32 import org.simantics.diagram.connection.actions.IReconnectAction;
33 import org.simantics.diagram.connection.actions.MoveAction;
34 import org.simantics.diagram.connection.actions.ReconnectLineAction;
35 import org.simantics.diagram.connection.rendering.ExampleConnectionStyle;
36 import org.simantics.diagram.connection.rendering.IRouteGraphRenderer;
37 import org.simantics.diagram.connection.rendering.StyledRouteGraphRenderer;
38 import org.simantics.diagram.connection.rendering.arrows.ArrowExampleLineEndStyle;
39 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
40 import org.simantics.diagram.connection.rendering.arrows.PlainExampleLineEndStyle;
41 import org.simantics.diagram.connection.tests.actions.HighlightReconnectPointsAction;
42 import org.simantics.diagram.connection.tests.actions.NewTerminalAction;
43
44 public class TestApplet extends JApplet {
45
46         private static final long serialVersionUID = 6135978753249808640L;
47         
48         public static final double TOLERANCE = IAction.TOLERANCE;
49     
50     RouteGraph rg;
51     IRouteGraphRenderer renderer = 
52             new StyledRouteGraphRenderer(new ExampleConnectionStyle());
53     
54     double posX = 0.0;
55     double posY = 0.0;
56     
57     double mouseX;
58     double mouseY;
59     
60     IAction currentAction;
61     
62     public TestApplet() {
63         this(new RouteGraph());
64         
65         RouteLine v = rg.addLine(false, 0);
66                 
67         rg.link(addTerminal(rg, -100, -100, 1, PlainExampleLineEndStyle.INSTANCE), v);
68         rg.link(addTerminal(rg, -100, 100, 1, PlainExampleLineEndStyle.INSTANCE), v);
69         rg.link(addTerminal(rg, 100, -80, 1, ArrowExampleLineEndStyle.INSTANCE), v);
70         rg.link(addTerminal(rg, 100, 100, 4, ArrowExampleLineEndStyle.INSTANCE), v);
71     }
72     
73     public TestApplet(RouteGraph rg_) throws HeadlessException {
74         this.rg = rg_;        
75         
76         addMouseListener(new MouseAdapter() {
77             
78             @Override
79             public void mousePressed(MouseEvent e) {
80                 if(e.getButton() == MouseEvent.BUTTON1) {
81                     mouseX = e.getX() - posX;
82                     mouseY = e.getY() - posY;
83                     
84                     if(e.isAltDown()) {
85                         currentAction = new NewTerminalAction(rg, mouseX, mouseY);
86                         repaint();
87                     }
88                     else if(currentAction instanceof NewTerminalAction) {
89                         ((NewTerminalAction)currentAction).finish(mouseX, mouseY);
90                         currentAction = null;
91                         repaint();
92                     }
93                     else if(currentAction instanceof HighlightReconnectPointsAction) {
94                         currentAction = ReconnectLineAction.create(rg, mouseX, mouseY);
95                         repaint();
96                     }
97                     else if(currentAction instanceof IReconnectAction) {
98                         ((IReconnectAction)currentAction).finish(mouseX, mouseY);
99                         currentAction = null;
100                         repaint();
101                     }
102                     else {
103                         currentAction = MoveAction.create(rg, mouseX, mouseY);
104                     }
105                 }
106             }
107             
108             @Override
109             public void mouseReleased(MouseEvent e) {
110                 if(currentAction instanceof MoveAction) {
111                     ((MoveAction)currentAction).finish(mouseX, mouseY);
112                     currentAction = null;
113                     repaint();
114                 }
115             }
116         });
117         
118         addMouseMotionListener(new MouseMotionListener() {          
119             @Override
120             public void mouseMoved(MouseEvent e) {
121                 mouseX = e.getX() - posX;
122                 mouseY = e.getY() - posY;
123                 
124                 if(currentAction != null)
125                     repaint();
126             }
127             
128             @Override
129             public void mouseDragged(MouseEvent e) {
130                 mouseX = e.getX() - posX;
131                 mouseY = e.getY() - posY;
132                 
133                 if(currentAction != null)
134                     repaint();
135             }
136         });
137         
138         addKeyListener(new KeyAdapter() {
139             @Override
140             public void keyPressed(KeyEvent e) {
141                 Object target = rg.pick(mouseX, mouseY, TOLERANCE);
142                 
143                 if(e.getKeyCode() == KeyEvent.VK_COMMA) {
144                     if(target instanceof RouteTerminal) {
145                         RouteTerminal terminal = (RouteTerminal)target;
146                         rg.rotate(terminal, -1);
147                         repaint();
148                     }                   
149                 }
150                 else if(e.getKeyCode() == KeyEvent.VK_PERIOD) {
151                     if(target instanceof RouteTerminal) {
152                         RouteTerminal terminal = (RouteTerminal)target;
153                         rg.rotate(terminal, 1);
154                         repaint();
155                     }                   
156                 }   
157                 else if(e.getKeyCode() == KeyEvent.VK_D) {
158                     if(target instanceof RouteTerminal) {
159                         RouteTerminal terminal = (RouteTerminal)target;
160                         rg.toggleDirectLines(terminal);
161                         repaint();
162                     }                   
163                 }  
164                 else if(e.getKeyCode() == KeyEvent.VK_S) {
165                     if(target instanceof RouteLine) {
166                         RouteLine rLine = (RouteLine)target;
167                         rg.split(rLine, rLine.isHorizontal() ? mouseX : mouseY);
168                         repaint();
169                     }
170                 }
171                 else if(e.getKeyCode() == KeyEvent.VK_DELETE) {
172                     if(target instanceof RouteLine) {
173                         RouteLine line = (RouteLine)target;
174                         rg.merge(line);                     
175                     }
176                     else if(target instanceof RouteLink) {
177                         rg.deleteCorner((RouteLink)target);
178                     }
179                     else if(target instanceof RouteTerminal) {
180                         rg.remove((RouteTerminal)target);
181                     }
182                     repaint();
183                 }
184                 else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
185                     if(currentAction != null)
186                         currentAction = null;
187                     repaint();
188                 }
189                 else if(e.getKeyCode() == KeyEvent.VK_P) {
190                     rg.print();
191                 }
192                 else if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
193                     if(currentAction == null)
194                         currentAction = new HighlightReconnectPointsAction(rg);
195                     repaint();
196                 }
197             }            
198             
199             @Override
200             public void keyReleased(KeyEvent e) {
201                 if(e.getKeyCode() == KeyEvent.VK_CONTROL) {
202                     if(currentAction instanceof HighlightReconnectPointsAction)
203                         currentAction = null;
204                     repaint();
205                 }
206             }
207             
208         });
209         
210     }
211     
212     private static RouteTerminal addTerminal(RouteGraph rg, double x, double y, int allowedDirections, ILineEndStyle style) {
213         return rg.addTerminal(x, y, x-20, y-20, x+20, y+20, allowedDirections, style);
214     }
215     
216     @Override
217     public void paint(Graphics g_) {
218         Graphics2D g = (Graphics2D)g_ ;
219         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
220         g.setColor(Color.BLACK);
221         g.setBackground(Color.WHITE);
222         g.clearRect(0, 0, getWidth(), getHeight());
223         posX = getWidth()*0.5;
224         posY = getHeight()*0.5;
225         g.translate(posX, posY);
226         
227         if(currentAction != null)
228             currentAction.render(g, renderer, mouseX, mouseY);
229         else
230             renderer.render(g, rg);
231     }
232 }