]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/test/TestRouter.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / test / TestRouter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.g2d.routing.test;
13
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Frame;
17 import java.awt.Graphics;
18 import java.awt.Graphics2D;
19 import java.awt.Image;
20 import java.awt.RenderingHints;
21 import java.awt.Stroke;
22 import java.awt.event.KeyEvent;
23 import java.awt.event.KeyListener;
24 import java.awt.event.MouseEvent;
25 import java.awt.event.MouseListener;
26 import java.awt.event.MouseMotionListener;
27 import java.awt.event.WindowEvent;
28 import java.awt.event.WindowListener;
29 import java.awt.geom.Ellipse2D;
30 import java.awt.geom.Path2D;
31 import java.awt.geom.Rectangle2D;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import org.simantics.g2d.routing.Connection;
39 import org.simantics.g2d.routing.Obstacle;
40 import org.simantics.g2d.routing.algorithm1.RouterOld;
41
42 public class TestRouter extends Frame {
43
44         private static final long serialVersionUID = 2181877722124429003L;
45         
46         List<Obstacle> obstacles = new ArrayList<Obstacle>();
47         List<Connection> connections = new ArrayList<Connection>();
48         Connection connection;
49         
50         RouterOld router = new RouterOld();
51         
52         double startX, startY;
53         double curX, curY;
54         int mouseButtons = 0;
55
56         public TestRouter() {
57                 addWindowListener(new WindowListener() {
58
59                         @Override
60                         public void windowActivated(WindowEvent e) {
61                         }
62
63                         @Override
64                         public void windowClosed(WindowEvent e) {               
65                         }
66
67                         @Override
68                         public void windowClosing(WindowEvent e) {
69                                 System.exit(0);                 
70                         }
71
72                         @Override
73                         public void windowDeactivated(WindowEvent e) {
74                         }
75
76                         @Override
77                         public void windowDeiconified(WindowEvent e) {
78                         }
79
80                         @Override
81                         public void windowIconified(WindowEvent e) {
82                         }
83
84                         @Override
85                         public void windowOpened(WindowEvent e) {
86                         }
87                         
88                 });
89                 
90                 addMouseListener(new MouseListener() {
91
92                         @Override
93                         public void mouseClicked(MouseEvent e) {
94                                 // TODO Auto-generated method stub
95                                 
96                         }
97
98                         @Override
99                         public void mouseEntered(MouseEvent e) {
100                                 // TODO Auto-generated method stub
101                                 
102                         }
103
104                         @Override
105                         public void mouseExited(MouseEvent e) {
106                                 // TODO Auto-generated method stub
107                                 
108                         }
109
110                         @Override
111                         public void mousePressed(MouseEvent e) {
112                                 mouseButtons = e.getButton();
113                                 curX = startX = e.getX();
114                                 curY = startY = e.getY();
115                                 if(e.getButton() == MouseEvent.BUTTON3) {
116                                         if(connection == null)
117                                                 connection = new Connection(new double[0], 0xf, 0xf);
118                                         connection.routePath = 
119                                                 Arrays.copyOf(connection.routePath, connection.routePath.length+2);
120                                         connection.routePath[connection.routePath.length-2] = curX;
121                                         connection.routePath[connection.routePath.length-1] = curY;
122                                 }
123                                 repaint();
124                         }
125
126                         @Override
127                         public void mouseReleased(MouseEvent e) {
128                                 mouseButtons = 0;
129                                 if(e.getButton() == MouseEvent.BUTTON1) {       
130                                         double x0 = startX;
131                                         double y0 = startY;
132                                         double x1 = e.getX();
133                                         double y1 = e.getY();
134                                         if(x0 > x1) {
135                                                 double temp = x0;
136                                                 x0 = x1;
137                                                 x1 = temp;
138                                         }
139                                         if(y0 > y1) {
140                                                 double temp = y0;
141                                                 y0 = y1;
142                                                 y1 = temp;
143                                         }
144                                         Obstacle obs = new Obstacle(new Rectangle2D.Double(x0, y0, x1-x0, y1-y0));
145                                         obstacles.add(obs);
146                                         router.addObstacle(obs);
147                                 }                               
148                                 
149                                 repaint();                              
150                         }
151                         
152                 });
153                 
154                 addMouseMotionListener(new MouseMotionListener() {
155
156                         @Override
157                         public void mouseDragged(MouseEvent e) {
158                                 curX = e.getX();
159                                 curY = e.getY();
160                                 repaint();
161                         }
162
163                         @Override
164                         public void mouseMoved(MouseEvent e) {
165                                 curX = e.getX();
166                                 curY = e.getY();
167                                 if(connection != null)
168                                         repaint();
169                         }
170                         
171                 });
172                 
173                 addKeyListener(new KeyListener() {
174
175                         @Override
176                         public void keyPressed(KeyEvent e) {
177                                 if(e.getKeyChar() == 'c') {
178                                         if(connection != null) {
179                                                 connection.routePath = 
180                                                         Arrays.copyOf(connection.routePath, connection.routePath.length+2);
181                                                 connection.routePath[connection.routePath.length-2] = curX;
182                                                 connection.routePath[connection.routePath.length-1] = curY;
183                                                 
184                                                 connections.add(connection);
185                                                 router.addConnection(connection);                                               
186                                         }
187                                         connection = null;
188                                         repaint();
189                                 }
190                         }
191
192                         @Override
193                         public void keyReleased(KeyEvent e) {
194                                 // TODO Auto-generated method stub
195                                 
196                         }
197
198                         @Override
199                         public void keyTyped(KeyEvent e) {
200                                 // TODO Auto-generated method stub
201                                 
202                         }
203                         
204                 });
205                 
206                 setSize(640, 480);
207                 setVisible(true);
208         }
209         
210         @Override
211         public void paint(Graphics _g) {
212                 final Graphics2D g = (Graphics2D)_g;
213                 Map<Object, Object> hints = new HashMap<Object, Object>();
214                 hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
215                 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
216                 g.addRenderingHints(hints);
217                 
218                 /*Path2D path = new Path2D.Double();
219                 path.moveTo(0.0, 0.0);
220                 path.lineTo(100.0, 100.0);
221                 path.lineTo(400.0, 100.0);
222                                 
223                 g.draw(path);
224                 */
225                 
226                 g.setColor(Color.GRAY);
227                 g.setStroke(new BasicStroke(1.0f));
228                 for(Obstacle obs : obstacles)
229                         g.fill(obs.shape);              
230                 
231                 Obstacle curObs = null;
232                 if(mouseButtons == 1) {
233                         double x0 = startX;
234                         double y0 = startY;
235                         double x1 = curX;
236                         double y1 = curY;
237                         if(x0 > x1) {
238                                 double temp = x0;
239                                 x0 = x1;
240                                 x1 = temp;
241                         }
242                         if(y0 > y1) {
243                                 double temp = y0;
244                                 y0 = y1;
245                                 y1 = temp;
246                         }
247                         curObs = new Obstacle(new Rectangle2D.Double(x0, y0, x1-x0, y1-y0));
248                         g.fill(curObs.shape);
249                         router.addObstacle(curObs);
250                 }
251                 
252                 Connection curConnection = null;
253                 if(connection != null) {
254                         double[] points = Arrays.copyOf(connection.routePath, connection.routePath.length+2);
255                         points[points.length-2] = curX;
256                         points[points.length-1] = curY;
257                         curConnection = new Connection(points, connection.startDirections, connection.endDirections);
258                         router.addConnection(curConnection);
259                 }
260                 
261                 g.setColor(Color.BLACK);
262                 g.setStroke(new BasicStroke(3.0f));             
263                 for(Connection c : connections)
264                         draw(g, c);
265                 if(curConnection != null) {
266                         draw(g, curConnection);
267                         router.removeConnection(curConnection);
268                 }
269                 if(curObs != null)
270                         router.removeObstacle(curObs);          
271         }
272         
273         public void draw(Graphics2D g, Connection c) {
274                 Path2D path = router.getPath(c);
275                 if(path == null)
276                         return;
277                 g.setColor(Color.WHITE);
278                 g.setStroke(stroke5);
279                 g.draw(path);
280                 g.setColor(Color.BLACK);
281                 g.setStroke(stroke1);
282                 g.draw(path);
283                 double[] points = c.routePath;
284                 for(int i=0;i<points.length;i+=2) {
285                         double x = points[i];
286                         double y = points[i+1];
287                         g.setStroke(stroke1);
288                         g.draw(new Ellipse2D.Double(x-5.0, y-5.0, 10.0, 10.0));
289                 }
290         }
291         
292         static final Stroke stroke1 = new BasicStroke(1.0f);
293         static final Stroke stroke3 = new BasicStroke(3.0f);
294         static final Stroke stroke5 = new BasicStroke(5.0f);
295         
296         static void drawRectangle(Graphics2D g, double x0, double y0, double x1, double y1) {
297                 if(x0 > x1) {
298                         double temp = x0;
299                         x0 = x1;
300                         x1 = temp;
301                 }
302                 if(y0 > y1) {
303                         double temp = y0;
304                         y0 = y1;
305                         y1 = temp;
306                 }
307                 x0 += 5.0;
308                 y0 += 5.0;
309                 x1 -= 5.0;
310                 y1 -= 5.0;
311                 Rectangle2D rect = new Rectangle2D.Double(x0, y0, x1-x0, y1-y0);
312                 g.fill(rect);                   
313         }
314         
315         Image offScreenImage;
316         int offScreenImageWidth;
317         int offScreenImageHeight;
318
319         public void update( Graphics g ) {
320                 if ( offScreenImage == null || getWidth() != offScreenImageWidth || getHeight() != offScreenImageHeight) {
321                         offScreenImageWidth = getWidth();
322                         offScreenImageHeight = getHeight();
323                         offScreenImage = createImage(offScreenImageWidth, offScreenImageHeight);
324                 }
325
326                 Graphics gOffScreenImage = offScreenImage.getGraphics();
327
328                 gOffScreenImage.clearRect(0, 0, getWidth(), getHeight());
329                 paint( gOffScreenImage );
330
331                 g.drawImage(offScreenImage, 0, 0, this);
332
333                 gOffScreenImage.dispose();
334         } 
335         
336         public static void main(String[] args) {
337                 new TestRouter();
338         }
339         
340 }