KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 25.11.2004 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.figures;
9
10 import java.awt.Graphics2D JavaDoc;
11 import java.awt.geom.AffineTransform JavaDoc;
12 import java.awt.geom.Area JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.draw2d.Graphics;
16 import org.eclipse.draw2d.IFigure;
17 import org.eclipse.draw2d.J2DGraphics;
18 import org.eclipse.draw2d.Shape;
19 import org.eclipse.draw2d.geometry.Rectangle;
20 import org.eclipse.gef.editparts.ZoomListener;
21
22 import com.nightlabs.editor2d.j2d.GeneralShape;
23 import com.nightlabs.editor2d.util.J2DUtil;
24
25 public class AbstractShapeFigure
26 extends Shape
27 implements ShapeFigure
28 {
29   public static final Logger LOGGER = Logger.getLogger(AbstractShapeFigure.class);
30   
31   protected J2DGraphics j2d;
32   protected AffineTransform JavaDoc at = new AffineTransform JavaDoc();
33   protected GeneralShape gp;
34   protected java.awt.Rectangle JavaDoc gpBounds;
35         
36   public void setBounds(Rectangle newBounds)
37   {
38     repaint();
39     super.setBounds(J2DUtil.toDraw2D(getGPBounds()));
40   }
41         
42     public AbstractShapeFigure() {
43     super();
44   }
45   
46   protected Graphics2D JavaDoc g2d;
47   protected void fillShape(Graphics graphics)
48   {
49     if (graphics instanceof J2DGraphics)
50     {
51       j2d = (J2DGraphics) graphics;
52       g2d = j2d.createGraphics2D();
53       g2d.setClip(null);
54       g2d.setPaint(J2DUtil.toAWTColor(getBackgroundColor()));
55       g2d.fill(getGeneralShape());
56       g2d.dispose();
57     }
58   }
59   
60   /* (non-Javadoc)
61    * @see org.eclipse.draw2d.Shape#outlineShape(org.eclipse.draw2d.Graphics)
62    */

63   protected void outlineShape(Graphics graphics)
64   {
65     if (graphics instanceof J2DGraphics)
66     {
67       j2d = (J2DGraphics) graphics;
68       g2d = j2d.createGraphics2D();
69       g2d.setClip(null);
70       g2d.setPaint(J2DUtil.toAWTColor(getForegroundColor()));
71       g2d.draw(getGeneralShape());
72       g2d.dispose();
73     }
74   }
75     
76   public GeneralShape getGeneralShape() {
77     return gp;
78   }
79   
80   public void setGeneralShape(GeneralShape generalShape) {
81     gp = generalShape;
82     outlineArea = null;
83   }
84   
85   public java.awt.Rectangle JavaDoc getGPBounds()
86   {
87     if (gp == null)
88       gpBounds = J2DUtil.toAWTRectangle(getBounds());
89     else
90       gpBounds = getGeneralShape().getBounds();
91           
92     return gpBounds;
93   }
94       
95   public void transform(AffineTransform JavaDoc at)
96   {
97     getGeneralShape().transform(at);
98     outlineArea = null;
99     bounds = J2DUtil.toDraw2D(getGeneralShape().getBounds());
100     repaint();
101   }
102         
103   public static final double DEFAULT_HIT_TOLERANCE = 5;
104   protected double hitTolerance = DEFAULT_HIT_TOLERANCE;
105   public double getHitTolerance() {
106     return hitTolerance;
107   }
108   public void setHitTolerance(double hitTolerance) {
109     this.hitTolerance = hitTolerance;
110   }
111   
112   protected Area JavaDoc outlineArea;
113   
114   protected ZoomListener zoomListener = new ZoomListener()
115   {
116     public void zoomChanged(double zoom)
117     {
118       hitTolerance = hitTolerance / zoom;
119     }
120   };
121   public ZoomListener getZoomListener() {
122     return zoomListener;
123   }
124   
125   /**
126    * @see IFigure#containsPoint(int, int)
127    */

128   public boolean containsPoint(int x, int y)
129   {
130     if (isFill())
131       return getGeneralShape().contains(x, y);
132     else
133     {
134       if (outlineArea == null) {
135         Rectangle outerBounds = getBounds().getCopy();
136         Rectangle innerBounds = getBounds().getCopy();
137         outerBounds.expand((int)hitTolerance, (int)hitTolerance);
138         innerBounds.shrink((int)hitTolerance, (int)hitTolerance);
139         GeneralShape outerGS = (GeneralShape) getGeneralShape().clone();
140         GeneralShape innerGS = (GeneralShape) getGeneralShape().clone();
141         J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds);
142         J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds);
143         outlineArea = new Area JavaDoc(outerGS);
144         Area JavaDoc innerArea = new Area JavaDoc(innerGS);
145         outlineArea.exclusiveOr(innerArea);
146       }
147       return outlineArea.contains(x,y);
148     }
149   }
150   
151   public void performScale(double factor)
152   {
153     at.setToIdentity();
154     at.scale(factor, factor);
155     transform(at);
156   }
157   
158   public void performTranslate(int dx, int dy)
159   {
160     at.setToIdentity();
161     at.translate(dx, dy);
162     transform(at);
163   }
164     
165   public GeneralShape getHandleShape() {
166     return getGeneralShape();
167   }
168     
169   protected boolean fill = true;;
170   /**
171    * Sets whether this shape should fill its region or not. It repaints this figure.
172    *
173    * @param b fill state
174    * @since 2.0
175    */

176   public void setFill(boolean b) {
177     fill = b;
178     super.setFill(b);
179   }
180   public boolean isFill() {
181    return fill;
182   }
183   
184   
185 ///**
186
//* Translates this Figure's bounds, without firing a move.
187
//* @param dx The amount to translate horizontally
188
//* @param dy The amount to translate vertically
189
//* @see #translate(int, int)
190
//* @since 2.0
191
//*/
192
//protected void primTranslate(int dx, int dy)
193
//{
194
// at.setToIdentity();
195
// at.translate(dx, dy);
196
// getGeneralShape().transform(at);
197
// super.primTranslate(dx, dy);
198
//}
199

200     public Rectangle getBounds()
201     {
202         if (getGeneralShape() != null) {
203           return J2DUtil.toDraw2D(getGeneralShape().getBounds());
204         }
205         else {
206           return super.getBounds();
207         }
208     }
209   
210 }
211
Popular Tags