]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/routing/test/TestStopSet.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / routing / test / TestStopSet.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.event.MouseEvent;
22 import java.awt.event.MouseListener;
23 import java.awt.event.MouseMotionListener;
24 import java.awt.event.WindowEvent;
25 import java.awt.event.WindowListener;
26 import java.awt.geom.Rectangle2D;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.simantics.g2d.routing.algorithm1.Penalty;
33 import org.simantics.g2d.routing.algorithm1.Rectangle;
34 import org.simantics.g2d.routing.algorithm1.StopSet;
35 import org.simantics.g2d.routing.algorithm1.StopSet.IStopProcedure;
36 import org.simantics.g2d.routing.algorithm1.StopSet.Line;
37 import org.simantics.g2d.routing.algorithm1.StopSet.Stop;
38
39 public class TestStopSet extends Frame {
40
41         private static final long serialVersionUID = 2181877722124429003L;
42         
43         Collection<Rectangle> rectangles = new ArrayList<Rectangle>();
44         Rectangle source;
45         Rectangle target;
46                 
47         double startX, startY;
48         double curX, curY;
49         int mouseButtons = 0;
50
51         public TestStopSet() {
52                 addWindowListener(new WindowListener() {
53
54                         @Override
55                         public void windowActivated(WindowEvent e) {
56                         }
57
58                         @Override
59                         public void windowClosed(WindowEvent e) {               
60                         }
61
62                         @Override
63                         public void windowClosing(WindowEvent e) {
64                                 System.exit(0);                 
65                         }
66
67                         @Override
68                         public void windowDeactivated(WindowEvent e) {
69                         }
70
71                         @Override
72                         public void windowDeiconified(WindowEvent e) {
73                         }
74
75                         @Override
76                         public void windowIconified(WindowEvent e) {
77                         }
78
79                         @Override
80                         public void windowOpened(WindowEvent e) {
81                         }
82                         
83                 });
84                 
85                 addMouseListener(new MouseListener() {
86
87                         @Override
88                         public void mouseClicked(MouseEvent e) {
89                                 // TODO Auto-generated method stub
90                                 
91                         }
92
93                         @Override
94                         public void mouseEntered(MouseEvent e) {
95                                 // TODO Auto-generated method stub
96                                 
97                         }
98
99                         @Override
100                         public void mouseExited(MouseEvent e) {
101                                 // TODO Auto-generated method stub
102                                 
103                         }
104
105                         @Override
106                         public void mousePressed(MouseEvent e) {
107                                 mouseButtons = e.getButton();
108                                 curX = startX = e.getX();
109                                 curY = startY = e.getY();
110                                 if(e.getButton() == MouseEvent.BUTTON2)
111                                         source = null;
112                                 else if(e.getButton() == MouseEvent.BUTTON3)
113                                         target = null;
114                                 repaint();
115                         }
116
117                         @Override
118                         public void mouseReleased(MouseEvent e) {
119                                 mouseButtons = 0;
120                                 double x0 = startX;
121                                 double y0 = startY;
122                                 double x1 = e.getX();
123                                 double y1 = e.getY();
124                                 if(x0 > x1) {
125                                         double temp = x0;
126                                         x0 = x1;
127                                         x1 = temp;
128                                 }
129                                 if(y0 > y1) {
130                                         double temp = y0;
131                                         y0 = y1;
132                                         y1 = temp;
133                                 }
134                                 Rectangle rect = new Rectangle(x0, y0, x1, y1);
135                                 
136                                 if(e.getButton() == MouseEvent.BUTTON1)                         
137                                         rectangles.add(rect);
138                                 else if(e.getButton() == MouseEvent.BUTTON2)
139                                         source = rect;
140                                 else if(e.getButton() == MouseEvent.BUTTON3)
141                                         target = rect;
142                                 
143                                 repaint();                              
144                         }
145                         
146                 });
147                 
148                 addMouseMotionListener(new MouseMotionListener() {
149
150                         @Override
151                         public void mouseDragged(MouseEvent e) {
152                                 curX = e.getX();
153                                 curY = e.getY();
154                                 repaint();
155                         }
156
157                         @Override
158                         public void mouseMoved(MouseEvent e) {                                          
159                         }
160                         
161                 });
162                 
163                 setSize(640, 480);
164                 setVisible(true);
165         }
166         
167         @Override
168         public void paint(Graphics _g) {
169                 final Graphics2D g = (Graphics2D)_g;
170                 Map<Object, Object> hints = new HashMap<Object, Object>();
171                 hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
172                 hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
173                 g.addRenderingHints(hints);
174                 
175                 /*Path2D path = new Path2D.Double();
176                 path.moveTo(0.0, 0.0);
177                 path.lineTo(100.0, 100.0);
178                 path.lineTo(400.0, 100.0);
179                                 
180                 g.draw(path);
181                 */
182                 
183                 g.setColor(Color.GRAY);
184                 g.setStroke(new BasicStroke(1.0f));
185                 for(Rectangle rect : rectangles)
186                         drawRectangle(g, rect.x0, rect.y0, rect.x1, rect.y1);           
187                 
188                 if(source != null) {
189                         g.setColor(Color.BLUE);
190                         drawRectangle(g, source.x0, source.y0, source.x1, source.y1);
191                 }
192                 if(target != null) {
193                         g.setColor(Color.GREEN);
194                         drawRectangle(g, target.x0, target.y0, target.x1, target.y1);
195                 }
196                 if(mouseButtons != 0) {
197                         if(mouseButtons == 1)
198                                 g.setColor(Color.BLACK);
199                         else if(mouseButtons == 2)
200                                 g.setColor(Color.BLUE);
201                         else if(mouseButtons == 3)
202                                 g.setColor(Color.GREEN);
203                         drawRectangle(g, startX, startY, curX, curY);
204                 }
205                 
206                 Collection<Stop> stops = new ArrayList<Stop>(rectangles.size());
207                 Penalty penalty = new Penalty(100.0, 0.0);
208                 for(Rectangle rect : rectangles)
209                         stops.add(new Stop(penalty, rect.x0, rect.x1, rect.y0));
210                 StopSet ss = new StopSet(stops);
211                 
212                 if(target != null) {
213                         g.setColor(Color.GREEN);
214                         g.setStroke(new BasicStroke(1.0f));
215                         drawFront(g, ss, target.x0, target.x1, target.y1);
216                 }
217         }
218         
219         public final double PADDING = 2.0;
220         
221         void drawFront(final Graphics2D g, final StopSet ss, final double x0, final double x1, final double y) {
222                 ss.findStops(x0, x1, y, new IStopProcedure() {
223
224                         @Override
225                         public void blockEnd(double y1) {
226                                 if(y1 == Double.POSITIVE_INFINITY)
227                                         y1 = 100000.0;
228                                 Rectangle2D rect = new Rectangle2D.Double(x0 + PADDING, y + PADDING, x1-x0 - PADDING*2, y1-y - PADDING*2);
229                                 g.draw(rect);                                                   
230                         }
231
232                         @Override
233                         public void continuation(double min, double max, int pos, Line line) {
234                                 drawFront(g, ss, pos, line, min, max);
235                         }
236                         
237                 });
238         }
239         
240         void drawFront(final Graphics2D g, final StopSet ss, int pos, final Line line, final double x0, final double x1) {
241                 StopSet.continueStop(pos, line, x0, x1, new IStopProcedure() {
242
243                         @Override
244                         public void blockEnd(double y1) {
245                                 if(y1 == Double.POSITIVE_INFINITY)
246                                         y1 = 100000.0;
247                                 Rectangle2D rect = new Rectangle2D.Double(x0 + PADDING, line.y + PADDING, x1-x0 - PADDING*2, y1-line.y - PADDING*2);
248                                 g.draw(rect);                                                   
249                         }
250
251                         @Override
252                         public void continuation(double min, double max, int pos, Line line) {
253                                 drawFront(g, ss, pos, line, min, max);
254                         }
255                         
256                 });
257         }
258         
259         static void drawRectangle(Graphics2D g, double x0, double y0, double x1, double y1) {
260                 if(x0 > x1) {
261                         double temp = x0;
262                         x0 = x1;
263                         x1 = temp;
264                 }
265                 if(y0 > y1) {
266                         double temp = y0;
267                         y0 = y1;
268                         y1 = temp;
269                 }
270                 Rectangle2D rect = new Rectangle2D.Double(x0, y0, x1-x0, y1-y0);
271                 g.fill(rect);                   
272         }
273         
274         Image offScreenImage;
275         int offScreenImageWidth;
276         int offScreenImageHeight;
277
278         public void update( Graphics g ) {
279                 if ( offScreenImage == null || getWidth() != offScreenImageWidth || getHeight() != offScreenImageHeight) {
280                         offScreenImageWidth = getWidth();
281                         offScreenImageHeight = getHeight();
282                         offScreenImage = createImage(offScreenImageWidth, offScreenImageHeight);
283                 }
284
285                 Graphics gOffScreenImage = offScreenImage.getGraphics();
286
287                 gOffScreenImage.clearRect(0, 0, getWidth(), getHeight());
288                 paint( gOffScreenImage );
289
290                 g.drawImage(offScreenImage, 0, 0, this);
291
292                 gOffScreenImage.dispose();
293         } 
294         
295         public static void main(String[] args) {
296                 new TestStopSet();
297         }
298         
299 }