KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > figures > AbstractShapeFigure


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.figures;
29
30 import java.awt.Graphics2D JavaDoc;
31 import java.awt.geom.AffineTransform JavaDoc;
32 import java.awt.geom.Area JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.eclipse.draw2d.Graphics;
36 import org.eclipse.draw2d.IFigure;
37 import org.eclipse.draw2d.J2DGraphics;
38 import org.eclipse.draw2d.Shape;
39 import org.eclipse.draw2d.geometry.Rectangle;
40 import org.eclipse.gef.editparts.ZoomListener;
41
42 import org.nightlabs.editor2d.j2d.GeneralShape;
43 import org.nightlabs.editor2d.util.J2DUtil;
44 import org.nightlabs.editor2d.util.RenderUtil;
45
46 public class AbstractShapeFigure
47 extends Shape
48 implements ShapeFigure
49 {
50   public static final Logger LOGGER = Logger.getLogger(AbstractShapeFigure.class);
51   
52   protected J2DGraphics j2d;
53   protected AffineTransform JavaDoc at = new AffineTransform JavaDoc();
54   protected GeneralShape gp;
55   protected java.awt.Rectangle JavaDoc gpBounds;
56         
57   public void setBounds(Rectangle newBounds)
58   {
59     repaint();
60     super.setBounds(J2DUtil.toDraw2D(getGPBounds()));
61   }
62         
63     public AbstractShapeFigure() {
64     super();
65   }
66   
67   protected Graphics2D JavaDoc g2d;
68   protected void fillShape(Graphics graphics)
69   {
70     if (graphics instanceof J2DGraphics)
71     {
72       j2d = (J2DGraphics) graphics;
73       g2d = j2d.createGraphics2D();
74       g2d.setClip(null);
75       g2d.setPaint(J2DUtil.toAWTColor(getBackgroundColor()));
76       g2d.fill(getGeneralShape());
77       g2d.dispose();
78     }
79   }
80   
81   /* (non-Javadoc)
82    * @see org.eclipse.draw2d.Shape#outlineShape(org.eclipse.draw2d.Graphics)
83    */

84   protected void outlineShape(Graphics graphics)
85   {
86     if (graphics instanceof J2DGraphics)
87     {
88       j2d = (J2DGraphics) graphics;
89       g2d = j2d.createGraphics2D();
90       g2d.setClip(null);
91       g2d.setPaint(J2DUtil.toAWTColor(getForegroundColor()));
92       g2d.setStroke(RenderUtil.setStrokeStyle(lineWidth, lineStyle));
93       g2d.draw(getGeneralShape());
94       g2d.dispose();
95     }
96   }
97     
98   public GeneralShape getGeneralShape() {
99     return gp;
100   }
101   
102   public void setGeneralShape(GeneralShape generalShape) {
103     gp = generalShape;
104     outlineArea = null;
105   }
106   
107   public java.awt.Rectangle JavaDoc getGPBounds()
108   {
109     if (gp == null)
110       gpBounds = J2DUtil.toAWTRectangle(getBounds());
111     else
112       gpBounds = getGeneralShape().getBounds();
113           
114     return gpBounds;
115   }
116       
117   public void transform(AffineTransform JavaDoc at)
118   {
119     getGeneralShape().transform(at);
120     outlineArea = null;
121     bounds = J2DUtil.toDraw2D(getGeneralShape().getBounds());
122     repaint();
123   }
124         
125   public static final double DEFAULT_HIT_TOLERANCE = 5;
126   protected double hitTolerance = DEFAULT_HIT_TOLERANCE;
127   public double getHitTolerance() {
128     return hitTolerance;
129   }
130   public void setHitTolerance(double hitTolerance) {
131     this.hitTolerance = hitTolerance;
132   }
133   
134   protected Area JavaDoc outlineArea;
135   
136   protected ZoomListener zoomListener = new ZoomListener()
137   {
138     public void zoomChanged(double zoom)
139     {
140       hitTolerance = hitTolerance / zoom;
141     }
142   };
143   public ZoomListener getZoomListener() {
144     return zoomListener;
145   }
146   
147   /**
148    * @see IFigure#containsPoint(int, int)
149    */

150   public boolean containsPoint(int x, int y)
151   {
152     if (isFill())
153       return getGeneralShape().contains(x, y);
154     else
155     {
156       if (outlineArea == null) {
157         Rectangle outerBounds = getBounds().getCopy();
158         Rectangle innerBounds = getBounds().getCopy();
159         outerBounds.expand((int)hitTolerance, (int)hitTolerance);
160         innerBounds.shrink((int)hitTolerance, (int)hitTolerance);
161         GeneralShape outerGS = (GeneralShape) getGeneralShape().clone();
162         GeneralShape innerGS = (GeneralShape) getGeneralShape().clone();
163         J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds);
164         J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds);
165         outlineArea = new Area JavaDoc(outerGS);
166         Area JavaDoc innerArea = new Area JavaDoc(innerGS);
167         outlineArea.exclusiveOr(innerArea);
168       }
169       return outlineArea.contains(x,y);
170     }
171   }
172   
173   public void performScale(double factor)
174   {
175     at.setToIdentity();
176     at.scale(factor, factor);
177     transform(at);
178   }
179   
180   public void performTranslate(int dx, int dy)
181   {
182     at.setToIdentity();
183     at.translate(dx, dy);
184     transform(at);
185   }
186     
187   public GeneralShape getHandleShape() {
188     return getGeneralShape();
189   }
190     
191   protected boolean fill = true;;
192   /**
193    * Sets whether this shape should fill its region or not. It repaints this figure.
194    *
195    * @param b fill state
196    * @since 2.0
197    */

198   public void setFill(boolean b) {
199     fill = b;
200     super.setFill(b);
201   }
202   public boolean isFill() {
203    return fill;
204   }
205   
206   
207 ///**
208
//* Translates this Figure's bounds, without firing a move.
209
//* @param dx The amount to translate horizontally
210
//* @param dy The amount to translate vertically
211
//* @see #translate(int, int)
212
//* @since 2.0
213
//*/
214
//protected void primTranslate(int dx, int dy)
215
//{
216
// at.setToIdentity();
217
// at.translate(dx, dy);
218
// getGeneralShape().transform(at);
219
// super.primTranslate(dx, dy);
220
//}
221

222     public Rectangle getBounds()
223     {
224         if (getGeneralShape() != null) {
225           return J2DUtil.toDraw2D(getGeneralShape().getBounds());
226         }
227         else {
228           return super.getBounds();
229         }
230     }
231   
232 }
233
Popular Tags