KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > render > AbstractShapeStringRenderer


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

26
27 package org.nightlabs.editor2d.render;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Font JavaDoc;
31 import java.awt.Graphics2D JavaDoc;
32 import java.awt.Point JavaDoc;
33 import java.awt.Rectangle JavaDoc;
34 import java.awt.font.FontRenderContext JavaDoc;
35 import java.awt.font.GlyphVector JavaDoc;
36 import java.awt.geom.AffineTransform JavaDoc;
37 import java.awt.geom.GeneralPath JavaDoc;
38 import java.awt.geom.Rectangle2D JavaDoc;
39
40 import org.apache.log4j.Logger;
41 import org.nightlabs.editor2d.DrawComponent;
42 import org.nightlabs.editor2d.ShapeDrawComponent;
43 import org.nightlabs.editor2d.j2d.GeneralShape;
44 import org.nightlabs.editor2d.util.EditorModelUtil;
45 import org.nightlabs.editor2d.util.GeomUtil;
46
47 public abstract class AbstractShapeStringRenderer
48 extends BaseShapeRenderer
49 implements StringRenderer, ShapeRenderer
50 {
51     public static final Logger LOGGER = Logger.getLogger(AbstractShapeStringRenderer.class);
52     
53     public AbstractShapeStringRenderer() {
54         super();
55     }
56
57     public static final Font JavaDoc DEFAULT_FONT = new Font JavaDoc("Arial", Font.BOLD, 12);
58     public static final Color JavaDoc DEFAULT_COLOR = Color.BLACK;
59     
60   protected void init()
61   {
62     super.init();
63     fontColor = DEFAULT_COLOR;
64     font = DEFAULT_FONT;
65   }
66       
67   protected String JavaDoc s = null;
68   
69   /**
70    * paints the ShapeDrawComponent and draws the String returned
71    * in the abstract Method getString(ShapeDrawComponent sdc) on top of it
72    */

73   public void paint(DrawComponent dc, Graphics2D JavaDoc g2d)
74   {
75     ShapeDrawComponent sdc = (ShapeDrawComponent) dc;
76     GeneralShape shape = sdc.getGeneralShape();
77     if (showFillColor) {
78       g2d.setPaint(getFillColor(sdc));
79       g2d.fill(shape);
80     }
81     if (showLineColor) {
82       g2d.setPaint(getLineColor(sdc));
83       g2d.setStroke(stroke);
84       g2d.draw(shape);
85     }
86     if (showString == true)
87     {
88         String JavaDoc s = getString(sdc);
89         paintString(sdc, s, g2d);
90     }
91   }
92   
93     protected GlyphVector JavaDoc glyphVector = null;
94     protected Rectangle JavaDoc glyphBounds = null;
95     protected Rectangle JavaDoc shapeBounds = null;
96     protected Point JavaDoc glyphLocation = null;
97     
98     protected GlyphVector JavaDoc createGlyphVector(String JavaDoc s, FontRenderContext JavaDoc frc) {
99       return font.createGlyphVector(frc, s);
100     }
101     
102   protected void paintString(ShapeDrawComponent sdc, String JavaDoc s, Graphics2D JavaDoc g2d)
103   {
104     if (s != null)
105     {
106       glyphVector = createGlyphVector(getString(sdc), g2d.getFontRenderContext());
107       glyphBounds = glyphVector.getVisualBounds().getBounds();
108       shapeBounds = sdc.getBounds();
109       glyphLocation = EditorModelUtil.getLeftTopCenterLocation(glyphBounds.getBounds(), shapeBounds);
110       g2d.drawGlyphVector(glyphVector, glyphLocation.x, glyphLocation.y);
111     }
112   }
113         
114 // protected void paintString(ShapeDrawComponent sdc, String s, Graphics2D g2d)
115
// {
116
// if (s != null)
117
// {
118
// g2d.setPaint(fontColor);
119
// glyphVector = createGlyphVector(getString(sdc), g2d.getFontRenderContext());
120
// glyphBounds = glyphVector.getVisualBounds().getBounds();
121
// shapeBounds = sdc.getBounds();
122
// // if the string fits into the shapeBounds render it
123
// if (shapeBounds.contains(glyphBounds)) {
124
// glyphLocation = EditorModelUtil.getLeftTopCenterLocation(glyphBounds.getBounds(), shapeBounds);
125
// g2d.drawGlyphVector(glyphVector, glyphLocation.x, glyphLocation.y);
126
// }
127
// // else transform the GlyphVector into a Shapae and transform it so that it fits
128
// // into the shapeBounds
129
// // COMMENT: TOO SLOW
130
// else {
131
// AffineTransform at = GeomUtil.getAffineTransform(glyphBounds, shapeBounds);
132
// GeneralPath gp = new GeneralPath(glyphVector.getOutline());
133
// gp.transform(at);
134
// glyphLocation = EditorModelUtil.getLeftTopCenterLocation(gp.getBounds(), shapeBounds);
135
// g2d.draw(gp);
136
// }
137
// }
138
// }
139

140 // protected void paintString(ShapeDrawComponent sdc, String s, Graphics2D g2d)
141
// {
142
// if (s != null)
143
// {
144
// g2d.setPaint(fontColor);
145
//
146
// int shapeHeight = sdc.getHeight();
147
// int shapeWidth = sdc.getWidth();
148
// int stringWidth = g2d.getFontMetrics().stringWidth(s);
149
// int stringHeight = g2d.getFontMetrics().getHeight();
150
// int diffWidth = (shapeWidth - stringWidth) / 2;
151
// int diffHeight = (shapeHeight - stringHeight) / 2;
152
// int shapeX = sdc.getX();
153
// int shapeY = sdc.getY();
154
// int stringX = shapeX + diffWidth + stringWidth;
155
// int stringY = shapeY + diffHeight + stringHeight;
156
//
157
// Rectangle stringBounds = new Rectangle(stringX, stringY, stringWidth, stringHeight);
158
// Rectangle sdcBounds = sdc.getBounds();
159
// g2d.drawString(s, stringX, stringY);
160
//
161
// // TODO: Font must be adopted to shapeSize or shortend and should not be rendererd when a minimum
162
// // size has been reached
163
// LOGGER.debug("stringBounds = "+stringBounds);
164
// LOGGER.debug("sdcBounds = "+sdcBounds);
165
// }
166
// }
167

168   /**
169    *
170    * @param sdc The ShapeDrawComponent to get the String from
171    * @return the String to render
172    */

173   public abstract String JavaDoc getString(ShapeDrawComponent sdc);
174   
175   /**
176    * Inheritans can override this Method to determine what the fillColor
177    * should be.
178    *
179    * By default the FillColor of the ShapeDrawComponent is returned
180    *
181    * @param sdc The ShapeDrawComponent to get the FillColor from (the default)
182    * @return the FillColor to fill the ShapeDrawComponent
183    */

184   protected Color JavaDoc getFillColor(ShapeDrawComponent sdc) {
185     return sdc.getFillColor();
186   }
187   
188   /**
189    * Inheritans can override this Method to determine what the lineColor
190    * should be.
191    *
192    * By default the LineColor of the ShapeDrawComponent is returned
193    *
194    * @param sdc The ShapeDrawComponent to get the LineColor from (the default)
195    * @return the LineColor to fill the Line of the ShapeDrawComponent
196    */

197   protected Color JavaDoc getLineColor(ShapeDrawComponent sdc) {
198     return sdc.getLineColor();
199   }
200   
201   protected boolean showString = true;
202   
203   /**
204    * @see StringRenderer#isShowString()
205    */

206     public boolean isShowString() {
207         return showString;
208     }
209     
210   /**
211    * @see StringRenderer#setShowString(boolean)
212    */

213     public void setShowString(boolean showString) {
214         this.showString = showString;
215     }
216             
217   protected Font JavaDoc font = DEFAULT_FONT;
218
219   /**
220    * @see StringRenderer#getFont()
221    */

222     public Font JavaDoc getFont() {
223         return font;
224     }
225
226     /**
227      * @see StringRenderer#setFont(Font)
228      */

229     public void setFont(Font JavaDoc f) {
230         this.font = f;
231     }
232   
233   protected Color JavaDoc fontColor = null;
234
235   /**
236    * @see StringRenderer#getFontColor()
237    */

238     public Color JavaDoc getFontColor() {
239         return fontColor;
240     }
241
242     /**
243      * @see StringRenderer#setFontColor(Color)
244      */

245     public void setFontColor(Color JavaDoc fontColor) {
246         this.fontColor = fontColor;
247     }
248     
249     protected boolean showLineColor = true;
250     
251     /**
252     *
253     * @see ShapeRenderer#isShowLineColor()
254     *
255     */

256     public boolean isShowLineColor() {
257         return showLineColor;
258     }
259     
260     /**
261     *
262     * @see ShapeRenderer#setShowLineColor(boolean)
263     */

264     public void setShowLineColor(boolean showLineColor) {
265         this.showLineColor = showLineColor;
266     }
267     
268     protected boolean showFillColor = true;
269     
270     /**
271     *
272     * @see ShapeRenderer#isShowFillColor()
273     */

274     public boolean isShowFillColor() {
275         return showFillColor;
276     }
277     
278     /**
279     *
280     * @see ShapeRenderer#setShowFillColor(boolean)
281     */

282     public void setShowFillColor(boolean showColor) {
283         this.showFillColor = showColor;
284     }
285     
286 }
287
Popular Tags