KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > TextTitle


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * --------------
23  * TextTitle.java
24  * --------------
25  * (C) Copyright 2000-2003, by David Berry and Contributors.
26  *
27  * Original Author: David Berry;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  * Nicolas Brodu;
30  *
31  * $Id: TextTitle.java,v 1.13 2003/10/29 10:23:21 mungady Exp $
32  *
33  * Changes (from 18-Sep-2001)
34  * --------------------------
35  * 18-Sep-2001 : Added standard header (DG);
36  * 07-Nov-2001 : Separated the JCommon Class Library classes, JFreeChart now requires
37  * jcommon.jar (DG);
38  * 09-Jan-2002 : Updated Javadoc comments (DG);
39  * 07-Feb-2002 : Changed Insets --> Spacer in AbstractTitle.java (DG);
40  * 06-Mar-2002 : Updated import statements (DG);
41  * 25-Jun-2002 : Removed redundant imports (DG);
42  * 18-Sep-2002 : Fixed errors reported by Checkstyle (DG);
43  * 28-Oct-2002 : Small modifications while changing JFreeChart class (DG);
44  * 13-Mar-2003 : Changed width used for relative spacing to fix bug 703050 (DG);
45  * 26-Mar-2003 : Implemented Serializable (DG);
46  * 15-Jul-2003 : Fixed null pointer exception (DG);
47  * 11-Sep-2003 : Implemented Cloneable (NB)
48  * 22-Sep-2003 : Added checks for null values and thow nullpointer exceptions (TM);
49  * Background paint was not serialized.
50  * 07-Oct-2003 : Added fix for exception caused by empty string in title (DG);
51  * 29-Oct-2003 : Added workaround for text alignment in PDF output (DG);
52  *
53  */

54
55 package org.jfree.chart;
56
57 import java.awt.Color JavaDoc;
58 import java.awt.Font JavaDoc;
59 import java.awt.FontMetrics JavaDoc;
60 import java.awt.Graphics2D JavaDoc;
61 import java.awt.Paint JavaDoc;
62 import java.awt.font.FontRenderContext JavaDoc;
63 import java.awt.font.LineMetrics JavaDoc;
64 import java.awt.geom.Rectangle2D JavaDoc;
65 import java.io.IOException JavaDoc;
66 import java.io.ObjectInputStream JavaDoc;
67 import java.io.ObjectOutputStream JavaDoc;
68 import java.io.Serializable JavaDoc;
69
70 import org.jfree.chart.event.TitleChangeEvent;
71 import org.jfree.io.SerialUtilities;
72 import org.jfree.util.ObjectUtils;
73
74 /**
75  * A standard chart title.
76  *
77  * @author David Berry
78  */

79 public class TextTitle extends AbstractTitle implements Serializable JavaDoc, Cloneable JavaDoc {
80
81     /** The default font. */
82     public static final Font JavaDoc DEFAULT_FONT = new Font JavaDoc("SansSerif", Font.BOLD, 12);
83
84     /** The default text color. */
85     public static final Paint JavaDoc DEFAULT_TEXT_PAINT = Color.black;
86
87     /** The title text. */
88     private String JavaDoc text;
89
90     /** The font used to display the title. */
91     private Font JavaDoc font;
92
93     /** The paint used to display the title text. */
94     private transient Paint JavaDoc paint;
95
96     /** The background paint. */
97     private transient Paint JavaDoc backgroundPaint;
98     
99     /**
100      * Creates a new title, using default attributes where necessary.
101      *
102      * @param text the title text.
103      */

104     public TextTitle(String JavaDoc text) {
105
106         this(text,
107              TextTitle.DEFAULT_FONT,
108              TextTitle.DEFAULT_TEXT_PAINT,
109              AbstractTitle.DEFAULT_POSITION,
110              AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT,
111              AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT,
112              AbstractTitle.DEFAULT_SPACER);
113
114     }
115
116     /**
117      * Creates a new title, using default attributes where necessary.
118      *
119      * @param text the title text.
120      * @param font the title font.
121      */

122     public TextTitle(String JavaDoc text, Font JavaDoc font) {
123
124         this(text, font,
125              TextTitle.DEFAULT_TEXT_PAINT,
126              AbstractTitle.DEFAULT_POSITION,
127              AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT,
128              AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT,
129              AbstractTitle.DEFAULT_SPACER);
130
131     }
132
133     /**
134      * Creates a new title, using default attributes where necessary.
135      *
136      * @param text the title text.
137      * @param font the title font.
138      * @param paint the title color.
139      */

140     public TextTitle(String JavaDoc text, Font JavaDoc font, Paint JavaDoc paint) {
141
142         this(text, font, paint,
143              AbstractTitle.DEFAULT_POSITION,
144              AbstractTitle.DEFAULT_HORIZONTAL_ALIGNMENT,
145              AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT,
146              AbstractTitle.DEFAULT_SPACER);
147
148     }
149     /**
150      * Creates a new title, using default attributes where necessary.
151      * <P>
152      * For the horizontal alignment, use the constants (LEFT, RIGHT and CENTER) defined in the
153      * AbstractTitle class.
154      *
155      * @param text the title text.
156      * @param font the title font.
157      * @param horizontalAlignment the horizontal alignment.
158      */

159     public TextTitle(String JavaDoc text, Font JavaDoc font, int horizontalAlignment) {
160
161         this(text, font,
162              TextTitle.DEFAULT_TEXT_PAINT,
163              AbstractTitle.DEFAULT_POSITION,
164              horizontalAlignment,
165              AbstractTitle.DEFAULT_VERTICAL_ALIGNMENT,
166              AbstractTitle.DEFAULT_SPACER);
167
168     }
169
170     /**
171      * Constructs a TextTitle with the specified properties.
172      * <p>
173      * For the titlePosition, horizontalAlignment and verticalAlignment, use the constants
174      * defined in the AbstractTitle class.
175      *
176      * @param text the text for the title (not null).
177      * @param font the title font (not null).
178      * @param paint the title color (not null).
179      * @param position the title position.
180      * @param horizontalAlignment the horizontal alignment.
181      * @param verticalAlignment the vertical alignment.
182      * @param spacer the space to leave around the outside of the title.
183      */

184     public TextTitle(String JavaDoc text, Font JavaDoc font, Paint JavaDoc paint, int position,
185                      int horizontalAlignment, int verticalAlignment,
186                      Spacer spacer) {
187
188         super(position, horizontalAlignment, verticalAlignment, spacer);
189         if (text == null)
190         {
191             throw new NullPointerException JavaDoc("TextTitle(..): Text is null");
192         }
193         if (font == null)
194         {
195             throw new NullPointerException JavaDoc("TextTitle(..): Font is null");
196         }
197         if (paint == null)
198         {
199             throw new NullPointerException JavaDoc("TextTitle(..): Paint is null");
200         }
201         this.text = text;
202         this.font = font;
203         this.paint = paint;
204         this.backgroundPaint = null;
205         
206     }
207
208     /**
209      * Returns the title text.
210      *
211      * @return the text.
212      */

213     public String JavaDoc getText() {
214         return text;
215     }
216
217     /**
218      * Sets the title to the specified text. This method notifies registered
219      * listeners that the title has been modified.
220      *
221      * @param text the new text.
222      */

223     public void setText(String JavaDoc text) {
224
225         if (text == null)
226         {
227             throw new NullPointerException JavaDoc("TextTitle.setText(..): Text is null");
228         }
229         if (!this.text.equals(text)) {
230             this.text = text;
231             notifyListeners(new TitleChangeEvent(this));
232         }
233
234     }
235
236     /**
237      * Returns the font used to display the title string.
238      *
239      * @return the font.
240      */

241     public Font JavaDoc getFont() {
242         return this.font;
243     }
244
245     /**
246      * Sets the font used to display the title string. Registered listeners are notified that
247      * the title has been modified.
248      *
249      * @param font the new font (null not permitted).
250      */

251     public void setFont(Font JavaDoc font) {
252
253         // check argument...
254
if (font == null) {
255             throw new IllegalArgumentException JavaDoc("TextTitle.setFont(...): null font not permitted.");
256         }
257
258         // make the change...
259
if (!this.font.equals(font)) {
260             this.font = font;
261             notifyListeners(new TitleChangeEvent(this));
262         }
263
264     }
265
266     /**
267      * Returns the paint used to display the title string.
268      *
269      * @return the paint.
270      */

271     public Paint JavaDoc getPaint() {
272         return this.paint;
273     }
274
275     /**
276      * Sets the paint used to display the title string. Registered listeners are notified that
277      * the title has been modified.
278      *
279      * @param paint the new paint (null not permitted).
280      */

281     public void setPaint(Paint JavaDoc paint) {
282
283         // check argument...
284
if (paint == null) {
285             throw new IllegalArgumentException JavaDoc("TextTitle.setPaint(...): "
286                                                + "null paint not permitted.");
287         }
288
289         // make the change...
290
if (!this.paint.equals(paint)) {
291             this.paint = paint;
292             notifyListeners(new TitleChangeEvent(this));
293         }
294
295     }
296
297     /**
298      * Returns the background paint.
299      *
300      * @return the paint.
301      */

302     public Paint JavaDoc getBackgroundPaint() {
303         return this.backgroundPaint;
304     }
305
306     /**
307      * Sets the background paint. Registered listeners are notified that
308      * the title has been modified.
309      *
310      * @param paint the new background paint.
311      */

312     public void setBackgroundPaint(Paint JavaDoc paint) {
313
314         // make the change...
315
this.backgroundPaint = paint;
316         notifyListeners(new TitleChangeEvent(this));
317
318     }
319
320
321     /**
322      * Returns true for the positions that are valid for TextTitle (TOP and
323      * BOTTOM for now) and false for all other positions.
324      *
325      * @param position the position.
326      *
327      * @return <code>true</code> if position is <code>TOP</code> or <code>BOTTOM</code>.
328      */

329     public boolean isValidPosition(int position) {
330
331         return ((position == AbstractTitle.TOP) || (position == AbstractTitle.BOTTOM));
332
333     }
334
335     /**
336      * Returns the preferred width of the title.
337      *
338      * @param g2 the graphics device.
339      *
340      * @return the preferred width of the title.
341      */

342     public double getPreferredWidth(Graphics2D JavaDoc g2) {
343
344         // get the title width...
345
g2.setFont(font);
346         FontMetrics JavaDoc fm = g2.getFontMetrics(font);
347         Rectangle2D JavaDoc titleBounds = fm.getStringBounds(text, g2);
348         double result = titleBounds.getWidth();
349
350         // add extra space...
351
Spacer spacer = getSpacer();
352         if (spacer != null) {
353             result = spacer.getAdjustedWidth(result);
354         }
355
356         return result;
357
358     }
359
360     /**
361      * Returns the preferred height of the title.
362      *
363      * @param g2 the graphics device.
364      *
365      * @return the preferred height of the title.
366      */

367     public double getPreferredHeight(Graphics2D JavaDoc g2) {
368
369         double result = 0.0;
370
371         // text cannot be null...
372
//if (this.text != null) {
373
// get the title height...
374
g2.setFont(font);
375             FontRenderContext JavaDoc frc = g2.getFontRenderContext();
376             LineMetrics JavaDoc lineMetrics = font.getLineMetrics(text, frc);
377             result = lineMetrics.getHeight();
378
379             // add extra space...
380
Spacer spacer = getSpacer();
381             if (spacer != null) {
382                 result = spacer.getAdjustedHeight(result);
383             }
384         //}
385
return result;
386
387     }
388
389     /**
390      * Draws the title on a Java 2D graphics device (such as the screen or a printer).
391      *
392      * @param g2 the graphics device.
393      * @param area the area within which the title (and plot) should be drawn.
394      */

395     public void draw(Graphics2D JavaDoc g2, Rectangle2D JavaDoc area) {
396
397         // TM: text cannot be null
398
// TM: the position is always valid...
399
drawHorizontal(g2, area);
400     }
401
402
403     /**
404      * Draws the title on a Java 2D graphics device (such as the screen or a printer).
405      *
406      * @param g2 the graphics device.
407      * @param area the area within which the title should be drawn.
408      */

409     protected void drawHorizontal(Graphics2D JavaDoc g2, Rectangle2D JavaDoc area) {
410
411         if (this.text.equals("")) {
412             return;
413         }
414         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
415         FontMetrics JavaDoc fm = g2.getFontMetrics(this.font);
416         Rectangle2D JavaDoc titleBounds = fm.getStringBounds(this.text, g2);
417         LineMetrics JavaDoc lineMetrics = font.getLineMetrics(this.text, frc);
418
419         double titleWidth = titleBounds.getWidth();
420         double leftSpace = 0.0;
421         double rightSpace = 0.0;
422         double titleHeight = lineMetrics.getHeight();
423         double topSpace = 0.0;
424         double bottomSpace = 0.0;
425
426         // spacer cannot be null.
427
Spacer spacer = getSpacer();
428         //if (spacer != null) {
429
leftSpace = spacer.getLeftSpace(area.getWidth());
430             rightSpace = spacer.getRightSpace(area.getWidth());
431             topSpace = spacer.getTopSpace(titleHeight);
432             bottomSpace = spacer.getBottomSpace(titleHeight);
433         //}
434

435         double titleY = area.getY() + topSpace;
436
437         // work out the vertical alignment...
438
int verticalAlignment = getVerticalAlignment();
439         if (verticalAlignment == TOP) {
440             titleY = titleY + titleHeight - lineMetrics.getLeading() - lineMetrics.getDescent();
441         }
442         else {
443             if (verticalAlignment == MIDDLE) {
444                 double space = (area.getHeight() - topSpace - bottomSpace - titleHeight);
445                 titleY = titleY + (space / 2) + titleHeight
446                                 - lineMetrics.getLeading() - lineMetrics.getDescent();
447             }
448             else {
449                 if (verticalAlignment == BOTTOM) {
450                     titleY = area.getMaxY() - bottomSpace
451                                             - lineMetrics.getLeading()
452                                             - lineMetrics.getDescent();
453                 }
454             }
455         }
456
457         // work out the horizontal alignment...
458
int horizontalAlignment = getHorizontalAlignment();
459         double titleX = area.getX() + leftSpace;
460         if (horizontalAlignment == CENTER) {
461             titleX = titleX + ((area.getWidth() - leftSpace - rightSpace) / 2) - (titleWidth / 2);
462         }
463         else if (horizontalAlignment == LEFT) {
464             titleX = area.getX() + leftSpace;
465         }
466         else if (horizontalAlignment == RIGHT) {
467             titleX = area.getMaxX() - rightSpace - titleWidth;
468         }
469
470         if (this.backgroundPaint != null) {
471             g2.setPaint(this.backgroundPaint);
472             g2.fill(area);
473         }
474         g2.setFont(this.font);
475         g2.setPaint(this.paint);
476         g2.drawString(text, (float) (titleX), (float) (titleY));
477
478     }
479
480     /**
481      * Tests this title for equality with another object.
482      *
483      * @param obj the object.
484      *
485      * @return <code>true</code> or <code>false</code>.
486      */

487     public boolean equals(Object JavaDoc obj) {
488
489         if (obj == null) {
490             return false;
491         }
492
493         if (obj == this) {
494             return true;
495         }
496
497         if (obj instanceof TextTitle) {
498
499             TextTitle t = (TextTitle) obj;
500             if (super.equals(obj)) {
501                 if (ObjectUtils.equal(this.text, t.text) == false)
502                 {
503                     return false;
504                 }
505                 if (ObjectUtils.equal(this.font, t.font) == false)
506                 {
507                     return false;
508                 }
509                 if (ObjectUtils.equal(this.paint, t.paint) == false)
510                 {
511                     return false;
512                 }
513                 return true;
514             }
515         }
516
517         return false;
518
519     }
520
521     /**
522      * Provides serialization support.
523      *
524      * @param stream the output stream.
525      *
526      * @throws IOException if there is an I/O error.
527      */

528     private void writeObject(ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
529         stream.defaultWriteObject();
530         SerialUtilities.writePaint(this.paint, stream);
531         SerialUtilities.writePaint(this.backgroundPaint, stream);
532     }
533
534     /**
535      * Provides serialization support.
536      *
537      * @param stream the input stream.
538      *
539      * @throws IOException if there is an I/O error.
540      * @throws ClassNotFoundException if there is a classpath problem.
541      */

542     private void readObject(ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
543         stream.defaultReadObject();
544         this.paint = SerialUtilities.readPaint(stream);
545         this.backgroundPaint = SerialUtilities.readPaint(stream);
546     }
547
548
549     // Default clone method OK
550

551 }
552
553
Popular Tags