]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/drawable/ViewerCanvas.java
Importing of spreadsheets
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / drawable / ViewerCanvas.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.graphviz.drawable;
13
14 import java.awt.Canvas;
15 import java.awt.Color;
16 import java.awt.Graphics;
17 import java.awt.Graphics2D;
18 import java.awt.Point;
19 import java.awt.RenderingHints;
20 import java.awt.event.ComponentAdapter;
21 import java.awt.event.ComponentEvent;
22 import java.awt.event.KeyAdapter;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseWheelEvent;
27 import java.awt.event.MouseWheelListener;
28 import java.awt.geom.AffineTransform;
29 import java.awt.geom.Rectangle2D;
30 import java.awt.image.VolatileImage;
31
32 /**
33  * A canvas showing a given Drawable.
34  * 
35  * @author Hannu Niemist�
36  */
37 public class ViewerCanvas extends Canvas {
38
39     private static final long serialVersionUID = -1094658958968314511L;
40     
41     AffineTransform transform;
42     Point prev;
43     VolatileImage im;
44     
45     Drawable drawable;
46
47     public ViewerCanvas(Drawable drawable) {
48         this.drawable = drawable;
49         setBackground(Color.WHITE);    
50         addKeyListener(new KeyAdapter() {
51
52             @Override
53             public void keyPressed(KeyEvent e) {
54                 if(e.getKeyChar()=='1') {
55                     fit();
56                     repaint();
57                 }
58             }
59
60         });
61         addMouseListener(new MouseAdapter() {
62
63             @Override
64             public void mousePressed(MouseEvent e) {
65                 prev = e.getPoint();
66             }                           
67         });
68         addMouseMotionListener(new MouseAdapter() {
69             @Override
70             public void mouseDragged(MouseEvent e) {
71                 Point cur = e.getPoint();
72                 double deltaX = cur.getX()-prev.getX();
73                 double deltaY = cur.getY()-prev.getY();                                 
74                 transform.preConcatenate(AffineTransform.getTranslateInstance(deltaX, deltaY));                                         
75                 repaint();
76                 prev = cur;
77             }
78         });
79         addMouseWheelListener(new MouseWheelListener() {
80
81             @Override
82             public void mouseWheelMoved(MouseWheelEvent e) {
83                 if(transform != null) {
84                     double scale = Math.exp(-0.1*e.getUnitsToScroll());
85                     Point p = e.getPoint();
86                     AffineTransform mod = new AffineTransform();
87                     mod.translate(p.getX(), p.getY());
88                     mod.scale(scale, scale);
89                     mod.translate(-p.getX(), -p.getY());
90                     transform.preConcatenate(mod);                                              
91                     repaint();
92                 }
93             }
94
95         });
96         addComponentListener(new ComponentAdapter() {
97             @Override
98             public void componentResized(ComponentEvent e) {
99                 if(getWidth() > 0 && getHeight() > 0) {
100                         im = createVolatileImage(getWidth(), getHeight());
101                         repaint();
102                 }
103             }
104         });
105     }
106
107     public void fit() {
108         Rectangle2D bounds = drawable.getBounds();
109
110         Rectangle2D container = getBounds();
111         double scale = Math.min(
112             container.getWidth() / bounds.getWidth(), 
113             container.getHeight() / bounds.getHeight());
114
115         transform = new AffineTransform(
116             scale, 0.0,
117             0.0, scale,
118             container.getX() + container.getWidth() * 0.5 - bounds.getCenterX() * scale,
119             container.getY() + container.getHeight() * 0.5 - bounds.getCenterY() * scale
120         );
121     }
122
123     @Override
124     public void paint(Graphics _g) {
125         do {
126                 if (im == null || im.validate(getGraphicsConfiguration()) ==
127                         VolatileImage.IMAGE_INCOMPATIBLE) {
128                     im = createVolatileImage(getWidth(), getHeight());
129                 }
130                 
131                 Graphics2D g = (Graphics2D)im.getGraphics();
132                 
133                 g.clearRect(0, 0, im.getWidth(null), im.getHeight(null));
134         
135                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
136                 g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
137                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
138         
139                 if(transform == null)
140                     fit();
141                 g.setTransform(transform);
142         
143                 drawable.draw(g, null);
144             g.dispose();
145         } while(im.contentsLost());
146
147         _g.drawImage(im, 0, 0, this);
148     }
149
150     @Override
151     public void update(Graphics g) {
152         paint(g);
153     }
154     
155     public AffineTransform getTransform() {
156         return transform;
157     }
158
159 }
160