KickJava   Java API By Example, From Geeks To Geeks.

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


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.Area JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.eclipse.draw2d.Figure;
35 import org.eclipse.draw2d.Graphics;
36 import org.eclipse.draw2d.J2DGraphics;
37 import org.eclipse.draw2d.geometry.Rectangle;
38 import org.eclipse.gef.editparts.ZoomListener;
39 import org.nightlabs.editor2d.DrawComponent;
40 import org.nightlabs.editor2d.ShapeDrawComponent;
41 import org.nightlabs.editor2d.j2d.GeneralShape;
42 import org.nightlabs.editor2d.render.Renderer;
43 import org.nightlabs.editor2d.util.J2DUtil;
44
45
46 public class DrawComponentFigure
47 //extends SmartUpdateFigure
48
extends Figure
49 implements RendererFigure
50 {
51     public static final Logger LOGGER = Logger.getLogger(DrawComponentFigure.class.getName());
52     
53   protected J2DGraphics j2d;
54   protected Graphics2D JavaDoc g2d;
55   public void paint(Graphics graphics)
56   {
57     if (graphics instanceof J2DGraphics) {
58       j2d = (J2DGraphics) graphics;
59       g2d = j2d.createGraphics2D();
60       g2d.setClip(null);
61       if (renderer != null)
62         renderer.paint(drawComponent, g2d);
63             
64       g2d.dispose();
65     }
66   }
67   
68   public void paint(Graphics2D JavaDoc graphics)
69   {
70     if (renderer == null && drawComponent != null)
71         renderer = drawComponent.getRenderer();
72     if (renderer != null)
73       renderer.paint(drawComponent, graphics);
74   }
75   
76   protected Renderer renderer;
77   public void setRenderer(Renderer renderer) {
78     this.renderer = renderer;
79   }
80   
81   protected DrawComponent drawComponent;
82   public void setDrawComponent(DrawComponent drawComponent) {
83     this.drawComponent = drawComponent;
84   }
85   
86   public boolean containsPoint(int x, int y)
87   {
88     if (drawComponent != null) {
89       if (drawComponent instanceof ShapeDrawComponent) {
90         ShapeDrawComponent sdc = (ShapeDrawComponent) drawComponent;
91         if (sdc.isFill()) {
92           return sdc.getGeneralShape().contains(x,y);
93         }
94         else
95         {
96           if (outlineArea == null)
97           {
98             Rectangle outerBounds = getBounds().getCopy();
99             Rectangle innerBounds = getBounds().getCopy();
100             outerBounds.expand((int)hitTolerance, (int)hitTolerance);
101             innerBounds.shrink((int)hitTolerance, (int)hitTolerance);
102             GeneralShape outerGS = (GeneralShape) sdc.getGeneralShape().clone();
103             GeneralShape innerGS = (GeneralShape) sdc.getGeneralShape().clone();
104             J2DUtil.transformGeneralShape(outerGS, getBounds(), outerBounds);
105             J2DUtil.transformGeneralShape(innerGS, getBounds(), innerBounds);
106             outlineArea = new Area JavaDoc(outerGS);
107             Area JavaDoc innerArea = new Area JavaDoc(innerGS);
108             outlineArea.exclusiveOr(innerArea);
109           }
110           boolean contains = outlineArea.contains(x,y);
111           outlineArea = null;
112           return contains;
113         }
114       }
115     }
116     return super.containsPoint(x, y);
117   }
118   
119   public static final double DEFAULT_HIT_TOLERANCE = 3;
120   protected double hitTolerance = DEFAULT_HIT_TOLERANCE;
121   public double getHitTolerance() {
122     return hitTolerance;
123   }
124   public void setHitTolerance(double hitTolerance) {
125     this.hitTolerance = hitTolerance;
126   }
127   
128   protected Area JavaDoc outlineArea;
129   
130   protected ZoomListener zoomListener = new ZoomListener() {
131     public void zoomChanged(double zoom) {
132       hitTolerance = DEFAULT_HIT_TOLERANCE / zoom;
133     }
134   };
135   public ZoomListener getZoomListener() {
136     return zoomListener;
137   }
138
139     public Rectangle getBounds() {
140         if (drawComponent != null)
141             return J2DUtil.toDraw2D(drawComponent.getBounds());
142         
143         return super.getBounds();
144     }
145     
146 // protected Label tooltip = new Label();
147
// protected Label getTooltip()
148
// {
149
// tooltip.setText(getTooltipText(drawComponent));
150
// return tooltip;
151
// }
152
//
153
// protected DescriptorManager descMan = new DescriptorManager();
154
// public void setDescriptorManager(DescriptorManager descMan) {
155
// this.descMan = descMan;
156
// }
157
//
158
// protected String getTooltipText(DrawComponent dc)
159
// {
160
// descMan.setDrawComponent(drawComponent);
161
// return descMan.getEntriesAsString(true);
162
// }
163
//
164
// @Override
165
// public IFigure getToolTip() {
166
// return getToolTip();
167
// }
168

169 }
170
Popular Tags