KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chart2d > Object2DProperties


1 /**
2  * Chart2D, a java library for drawing two dimensional charts.
3  * Copyright (C) 2001 Jason J. Simas
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  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * The author of this library may be contacted at:
19  * E-mail: jjsimas@users.sourceforge.net
20  * Street Address: J J Simas, 887 Tico Road, Ojai, CA 93023-3555 USA
21  */

22
23
24 package net.sourceforge.chart2d;
25
26
27 import java.awt.Color JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.util.Vector JavaDoc;
31
32
33 /**
34  * A data structure for holding the properties common to all Object2D objects.
35  * A Object2D object is one with an encolosing area and title.
36  * Pass this to any number of Object2D objects.
37  */

38 public final class Object2DProperties extends Properties {
39
40
41   /**
42    * Signifies left.
43    */

44   public static final int LEFT = 0;
45
46   /**
47    * Signifies right.
48    */

49   public static final int RIGHT = 1;
50
51   /**
52    * Signifies top.
53    */

54   public static final int TOP = 2;
55
56   /**
57    * Signifies bottom.
58    */

59   public static final int BOTTOM = 3;
60
61   /**
62    * Signifies none.
63    */

64   public static final int NONE = 6;
65
66   /**
67    * The default is true.
68    */

69   public static final boolean OBJECT_MAGNIFY_WHEN_RESIZE_DEFAULT = true;
70
71   /**
72    * The default is true.
73    */

74   public final static boolean OBJECT_BORDER_EXISTENCE_DEFAULT = true;
75
76   /**
77    * The default is 2.
78    */

79   public final static int OBJECT_BORDER_THICKNESS_MODEL_DEFAULT = 2;
80
81   /**
82    * The default is Color.black.
83    */

84   public final static Color JavaDoc OBJECT_BORDER_COLOR_DEFAULT = Color.black;
85
86   /**
87    * The default is true.
88    */

89   public final static boolean OBJECT_GAP_EXISTENCE_DEFAULT = true;
90
91   /**
92    * The default is 5.
93    */

94   public final static int OBJECT_GAP_THICKNESS_MODEL_DEFAULT = 5;
95
96   /**
97    * The default is true.
98    */

99   public final static boolean OBJECT_BACKGROUND_EXISTENCE_DEFAULT = true;
100
101   /**
102    * The default is new Color (215, 215, 215).
103    */

104   public final static Color JavaDoc OBJECT_BACKGROUND_COLOR_DEFAULT = new Color JavaDoc (215, 215, 255);
105
106   /**
107    * The default is TOP.
108    */

109   public final static int OBJECT_BACKGROUND_LIGHT_SOURCE_DEFAULT = TOP;
110
111   /**
112    * The default is true.
113    */

114   public final static boolean OBJECT_TITLE_EXISTENCE_DEFAULT = true;
115
116   /**
117    * The default is "".
118    */

119   public final static String JavaDoc OBJECT_TITLE_TEXT_DEFAULT = "";
120
121   /**
122    * The default is 12.
123    */

124   public final static int OBJECT_TITLE_FONT_POINT_MODEL_DEFAULT = 12;
125
126   /**
127    * The default is "SansSerif".
128    */

129   public final static String JavaDoc OBJECT_TITLE_FONT_NAME_DEFAULT = "SansSerif";
130
131   /**
132    * The default is Color.black.
133    */

134   public final static Color JavaDoc OBJECT_TITLE_FONT_COLOR_DEFAULT = Color.black;
135
136   /**
137    * The default is Font.PLAIN.
138    */

139   public final static int OBJECT_TITLE_FONT_STYLE_DEFAULT = Font.PLAIN;
140
141   /**
142    * The default is true.
143    */

144   public final static boolean OBJECT_TITLE_BETWEEN_REST_GAP_EXISTENCE_DEFAULT = true;
145
146   /**
147    * The default is 3.
148    */

149   public final static int OBJECT_TITLE_BETWEEN_REST_GAP_THICKNESS_MODEL_DEFAULT = 3;
150
151
152   private boolean objectMagnifyWhenResize;
153   private boolean objectBorderExistence;
154   private int objectBorderThicknessModel;
155   private Color JavaDoc objectBorderColor;
156   private boolean objectGapExistence;
157   private int objectGapThicknessModel;
158   private boolean objectBackgroundExistence;
159   private Color JavaDoc objectBackgroundColor;
160   private int objectBackgroundLightSource;
161   private boolean objectTitleExistence;
162   private String JavaDoc objectTitleText;
163   private int objectTitleFontPointModel;
164   private String JavaDoc objectTitleFontName;
165   private Color JavaDoc objectTitleFontColor;
166   private int objectTitleFontStyle;
167   private boolean objectTitleBetweenRestGapExistence;
168   private int objectTitleBetweenRestGapThicknessModel;
169
170   private boolean needsUpdate = true;
171   private final Vector JavaDoc object2DVector = new Vector JavaDoc (5, 5);
172   private final Vector JavaDoc needsUpdateVector = new Vector JavaDoc (5, 5);
173
174
175   /**
176    * Creates a Object2DProperties object with the documented default values.
177    */

178   public Object2DProperties() {
179
180     needsUpdate = true;
181     setObject2DPropertiesToDefaults();
182   }
183
184
185   /**
186    * Creates a Object2DProperties object with property values copied from another object.
187    * The copying is a deep copy.
188    * @param object2DProps The properties to copy.
189    */

190   public Object2DProperties (Object2DProperties object2DProps) {
191
192     needsUpdate = true;
193     setObject2DProperties (object2DProps);
194   }
195
196
197   /**
198    * Sets all properties to their default values.
199    */

200   public final void setObject2DPropertiesToDefaults() {
201
202     needsUpdate = true;
203     setObjectMagnifyWhenResize (OBJECT_MAGNIFY_WHEN_RESIZE_DEFAULT);
204     setObjectBorderExistence (OBJECT_BORDER_EXISTENCE_DEFAULT);
205     setObjectBorderThicknessModel (OBJECT_BORDER_THICKNESS_MODEL_DEFAULT);
206     setObjectBorderColor (OBJECT_BORDER_COLOR_DEFAULT);
207     setObjectGapExistence (OBJECT_GAP_EXISTENCE_DEFAULT);
208     setObjectGapThicknessModel (OBJECT_GAP_THICKNESS_MODEL_DEFAULT);
209     setObjectBackgroundExistence (OBJECT_BACKGROUND_EXISTENCE_DEFAULT);
210     setObjectBackgroundColor (OBJECT_BACKGROUND_COLOR_DEFAULT);
211     setObjectBackgroundLightSource (OBJECT_BACKGROUND_LIGHT_SOURCE_DEFAULT);
212     setObjectTitleExistence (OBJECT_TITLE_EXISTENCE_DEFAULT);
213     setObjectTitleText (OBJECT_TITLE_TEXT_DEFAULT);
214     setObjectTitleFontPointModel (OBJECT_TITLE_FONT_POINT_MODEL_DEFAULT);
215     setObjectTitleFontName (OBJECT_TITLE_FONT_NAME_DEFAULT);
216     setObjectTitleFontColor (OBJECT_TITLE_FONT_COLOR_DEFAULT);
217     setObjectTitleFontStyle (OBJECT_TITLE_FONT_STYLE_DEFAULT);
218     setObjectTitleBetweenRestGapExistence (OBJECT_TITLE_BETWEEN_REST_GAP_EXISTENCE_DEFAULT);
219     setObjectTitleBetweenRestGapThicknessModel (
220       OBJECT_TITLE_BETWEEN_REST_GAP_THICKNESS_MODEL_DEFAULT);
221   }
222
223
224   /**
225    * Sets all properties to be the values of another Object2DProperties object.
226    * The copying is a deep copy.
227    * @param object2DProps The properties to copy.
228    */

229   public final void setObject2DProperties (Object2DProperties object2DProps) {
230
231     needsUpdate = true;
232     setObjectMagnifyWhenResize (object2DProps.getObjectMagnifyWhenResize());
233     setObjectBorderExistence (object2DProps.getObjectBorderExistence());
234     setObjectBorderThicknessModel (object2DProps.getObjectBorderThicknessModel());
235     setObjectBorderColor (object2DProps.getObjectBorderColor());
236     setObjectGapExistence (object2DProps.getObjectGapExistence());
237     setObjectGapThicknessModel (object2DProps.getObjectGapThicknessModel());
238     setObjectBackgroundExistence (object2DProps.getObjectBackgroundExistence());
239     setObjectBackgroundColor (object2DProps.getObjectBackgroundColor());
240     setObjectBackgroundLightSource (object2DProps.getObjectBackgroundLightSource());
241     setObjectTitleExistence (object2DProps.getObjectTitleExistence());
242     setObjectTitleText (object2DProps.getObjectTitleText());
243     setObjectTitleFontPointModel (object2DProps.getObjectTitleFontPointModel());
244     setObjectTitleFontName (object2DProps.getObjectTitleFontName());
245     setObjectTitleFontColor (object2DProps.getObjectTitleFontColor());
246     setObjectTitleFontStyle (object2DProps.getObjectTitleFontStyle());
247     setObjectTitleBetweenRestGapExistence (object2DProps.getObjectTitleBetweenRestGapExistence());
248     setObjectTitleBetweenRestGapThicknessModel (
249       object2DProps.getObjectTitleBetweenRestGapThicknessModel());
250   }
251
252
253   /**
254    * Sets whether a object's components will grow or shrink as the size of
255    * the space allocated to the object grows or shrinks.
256    * The Object's preferred size will be the model size (i.e. the size at which it isn't magnified).
257    * @param magnify If true, the object will be magnified on resize.
258    */

259   public final void setObjectMagnifyWhenResize (boolean magnify) {
260
261     needsUpdate = true;
262     objectMagnifyWhenResize = magnify;
263   }
264
265
266   /**
267    * Sets whether a border around the object will exist.
268    * @param existence If true, then a object border exists.
269    */

270   public final void setObjectBorderExistence (boolean existence) {
271
272     needsUpdate = true;
273     objectBorderExistence = existence;
274   }
275
276
277   /**
278    * Sets the thickness of the border for the model size of the object.
279    * @param thickness The model thickness of the object's border.
280    */

281   public final void setObjectBorderThicknessModel (int thickness) {
282
283     needsUpdate = true;
284     objectBorderThicknessModel = thickness;
285   }
286
287
288   /**
289    * Sets the color of the object's border.
290    * @param color The color of the border.
291    */

292   public final void setObjectBorderColor (Color JavaDoc color) {
293
294     needsUpdate = true;
295     objectBorderColor = color;
296   }
297
298
299   /**
300    * Sets whether a gap between the border or edge of the object and the object's interior
301    * components exists.
302    * @param existence If true, then a gap exists.
303    */

304   public final void setObjectGapExistence (boolean existence) {
305
306     needsUpdate = true;
307     objectGapExistence = existence;
308   }
309
310
311   /**
312    * Sets the thickness of the object's gap for the model size of the object.
313    * @param thickness The model thickness of the object's gap.
314    */

315   public final void setObjectGapThicknessModel (int thickness) {
316
317     needsUpdate = true;
318     objectGapThicknessModel = thickness;
319   }
320
321
322   /**
323    * Sets whether the object will have a painted background or not.
324    * If not, then the background of the content pane to which the object was
325    * added will show through which by default is gray, or if the object was not
326    * added to a content pane but only a BufferedImage of the object is obtained,
327    * then the background will be the default background of BufferedImage which
328    * is black. The existence of a background can improve performance considerably.
329    * @param existence If true, a background for the object will be painted.
330    */

331   public final void setObjectBackgroundExistence (boolean existence) {
332
333     needsUpdate = true;
334     objectBackgroundExistence = existence;
335   }
336
337
338   /**
339    * Sets the color of the background for the object.
340    * @param color The color of the object's background.
341    */

342   public final void setObjectBackgroundColor (Color JavaDoc color) {
343
344     needsUpdate = true;
345     objectBackgroundColor = color;
346   }
347
348
349   /**
350    * Sets the light source of the object's background.
351    * If there is a light source, the side of the source will be one shade brighter than the
352    * specified background color. Use the TOP, BOTTOM, LEFT, RIGHT, and NONE fields.
353    * @param source The source of the light.
354    */

355   public final void setObjectBackgroundLightSource (int source) {
356
357     needsUpdate = true;
358     objectBackgroundLightSource = source;
359   }
360
361
362   /**
363    * Sets whether the object is to have a title.
364    * @param existence If true, then the object will have a title.
365    */

366   public final void setObjectTitleExistence (boolean existence) {
367
368     needsUpdate = true;
369     objectTitleExistence = existence;
370   }
371
372
373   /**
374    * Sets the text for the object's title.
375    * @param text The text for the object's title.
376    */

377   public final void setObjectTitleText (String JavaDoc text) {
378
379     needsUpdate = true;
380     objectTitleText = text;
381   }
382
383
384   /**
385    * Sets the point of the font of the title for the object at its model size.
386    * @param point The model font point for the object's title.
387    */

388   public final void setObjectTitleFontPointModel (int point) {
389
390     needsUpdate = true;
391     objectTitleFontPointModel = point;
392   }
393
394
395   /**
396    * Sets the name of the font for the object's title.
397    * Accepts all values accepted by java.awt.Font.
398    * @param name The name of the font for the object's title.
399    */

400   public final void setObjectTitleFontName (String JavaDoc name) {
401
402     needsUpdate = true;
403     objectTitleFontName = name;
404   }
405
406
407   /**
408    * Sets the color of the font for the object's title.
409    * @param color The color of the font for the object's title.
410    */

411   public final void setObjectTitleFontColor (Color JavaDoc color) {
412
413     needsUpdate = true;
414     objectTitleFontColor = color;
415   }
416
417
418   /**
419    * Sets the style of the font for the object's title.
420    * Accepts all values that java.awt.Font accepts.
421    * @param style The style of the font for the object's title.
422    */

423   public final void setObjectTitleFontStyle (int style) {
424
425     needsUpdate = true;
426     objectTitleFontStyle = style;
427   }
428
429
430   /**
431    * Sets whether a gap below the title and the rest of the object's components exists.
432    * @param existence If true, then a gap exists.
433    */

434   public final void setObjectTitleBetweenRestGapExistence (boolean existence) {
435
436     needsUpdate = true;
437     objectTitleBetweenRestGapExistence = existence;
438   }
439
440
441   /**
442    * Sets the thickness of the gap below the title and the rest of the object's components for
443    * the object's model size.
444    * @param thickness The model thickness of the gap.
445    */

446   public final void setObjectTitleBetweenRestGapThicknessModel (int thickness) {
447
448     needsUpdate = true;
449     objectTitleBetweenRestGapThicknessModel = thickness;
450   }
451
452
453   /**
454    * Gets whether a object's components will grow or shrink as the size of the space allocated to
455    * the object grows or shrinks.
456    * @return If true, the object will be magnified on resize.
457    */

458   public final boolean getObjectMagnifyWhenResize() {
459     return objectMagnifyWhenResize;
460   }
461
462
463   /**
464    * Gets whether a border around the object will exist.
465    * @return If true, then a object border exists.
466    */

467   public final boolean getObjectBorderExistence() {
468     return objectBorderExistence;
469   }
470
471
472   /**
473    * Gets the thickness of the border for the model size of the object.
474    * @return The model thickness of the object's border.
475    */

476   public final int getObjectBorderThicknessModel() {
477     return objectBorderThicknessModel;
478   }
479
480
481   /**
482    * Gets the color of the object's border.
483    * @return The color of the border.
484    */

485   public final Color JavaDoc getObjectBorderColor() {
486     return objectBorderColor;
487   }
488
489
490   /**
491    * Gets whether a gap between the border or edge of the object and the object's interior components
492    * exists.
493    * @return If true, then a gap exists.
494    */

495   public final boolean getObjectGapExistence() {
496     return objectGapExistence;
497   }
498
499
500   /**
501    * Gets the thickness of the object's gap for the model size of the object.
502    * @return The model thickness of the object's gap.
503    */

504   public final int getObjectGapThicknessModel() {
505     return objectGapThicknessModel;
506   }
507
508
509   /**
510    * Gets whether the object will have a painted background or not.
511    * If not, then the background of the content pane to which the object was
512    * added will show through which by default is gray, or if the object was not
513    * added to a content pane but only a BufferedImage of the object is obtained,
514    * then the background will be the default background of BufferedImage which
515    * is black. Painting the background improves performance considerably.
516    * @return If true, a background for the object will be painted.
517    */

518   public final boolean getObjectBackgroundExistence() {
519     return objectBackgroundExistence;
520   }
521
522
523   /**
524    * Gets the color of the background for the object.
525    * @return The color of the object's background.
526    */

527   public final Color JavaDoc getObjectBackgroundColor() {
528     return objectBackgroundColor;
529   }
530
531
532   /**
533    * Gets the light source of the object's background.
534    * If there is a light source, the side of the source will be one shade brighter than the
535    * specified background color. Use the TOP, BOTTOM, LEFT, RIGHT, and NONE fields.
536    * @return The source of the light.
537    */

538   public final int getObjectBackgroundLightSource() {
539     return objectBackgroundLightSource;
540   }
541
542
543   /**
544    * Gets whether the object is to have a title.
545    * @return If true, then the object will have a title.
546    */

547   public final boolean getObjectTitleExistence() {
548     return objectTitleExistence;
549   }
550
551
552   /**
553    * Gets the text for the object's title.
554    * @return The text for the object's title.
555    */

556   public final String JavaDoc getObjectTitleText() {
557     return objectTitleText;
558   }
559
560
561   /**
562    * Gets the point of the font of the title for the object at its model size.
563    * @return The model font point for the object's title.
564    */

565   public final int getObjectTitleFontPointModel() {
566     return objectTitleFontPointModel;
567   }
568
569
570   /**
571    * Gets the name of the font for the object's title.
572    * Accepts all values accepted by java.awt.Font.
573    * @return The name of the font for the object's title.
574    */

575   public final String JavaDoc getObjectTitleFontName() {
576     return objectTitleFontName;
577   }
578
579
580   /**
581    * Gets the color of the font for the object's title.
582    * @return The color of the font for the object's title.
583    */

584   public final Color JavaDoc getObjectTitleFontColor() {
585     return objectTitleFontColor;
586   }
587
588
589   /**
590    * Gets the style of the font for the object's title.
591    * Accepts all values that java.awt.Font accepts.
592    * @return The style of the font for the object's title.
593    */

594   public final int getObjectTitleFontStyle() {
595     return objectTitleFontStyle;
596   }
597
598
599   /**
600    * Gets whether a gap below the title and the rest of the object's components exists.
601    * @return If true, then a gap exists.
602    */

603   public final boolean getObjectTitleBetweenRestGapExistence() {
604     return objectTitleBetweenRestGapExistence;
605   }
606
607
608   /**
609    * Gets the thickness of the gap below the title and the rest of the object's components for
610    * the object's model size.
611    * @return The model thickness of the gap.
612    */

613   public final int getObjectTitleBetweenRestGapThicknessModel() {
614     return objectTitleBetweenRestGapThicknessModel;
615   }
616
617
618   /**
619    * Gets whether this object needs to be updated with new properties.
620    * @param object2D The object that may need to be updated.
621    * @return If true then needs update.
622    */

623   final boolean getObject2DNeedsUpdate (Object2D object2D) {
624
625     if (needsUpdate) return true;
626
627     int index = -1;
628     if ((index = object2DVector.indexOf (object2D)) != -1) {
629       return ((Boolean JavaDoc)needsUpdateVector.get (index)).booleanValue();
630     }
631
632     return false;
633   }
634
635
636   /**
637    * Adds an Object2D to the set of objects using these properties.
638    * @param object2D The Object2D to add.
639    */

640   final void addObject2D (Object2D object2D) {
641
642     if (!object2DVector.contains (object2D)) {
643       object2DVector.add (object2D);
644       needsUpdateVector.add (new Boolean JavaDoc (true));
645     }
646   }
647
648
649   /**
650    * Removes a Object2D from the set of objects using these properties.
651    * @param object2D The Object2D to remove.
652    */

653   final void removeObject2D (Object2D object2D) {
654
655     int index = -1;
656     if ((index = object2DVector.indexOf (object2D)) != -1) {
657       object2DVector.remove (index);
658       needsUpdateVector.remove (index);
659     }
660   }
661
662
663   /**
664    * Validates the properties of this object.
665    * If debug is true then prints a messages indicating whether each property is valid.
666    * Returns true if all the properties were valid and false otherwise.
667    * @param debug If true then will print status messages.
668    * @return If true then valid.
669    */

670   final boolean validate (boolean debug) {
671
672     if (debug) System.out.println ("Validating Object2DProperties");
673
674     boolean valid = true;
675
676     if (objectBorderThicknessModel < 0) {
677       valid = false;
678       if (debug) System.out.println ("ObjectBorderThicknessModel < 0");
679     }
680     if (objectBorderColor == null) {
681       valid = false;
682       if (debug) System.out.println ("ObjectBorderColor == null");
683     }
684     if (objectGapThicknessModel < 0) {
685       valid = false;
686       if (debug) System.out.println ("ObjectGapThicknessModel < 0");
687     }
688     if (objectBackgroundColor == null) {
689       valid = false;
690       if (debug) System.out.println ("ObjectBackgroundColor == null");
691     }
692     if (objectBackgroundLightSource != TOP && objectBackgroundLightSource != BOTTOM &&
693       objectBackgroundLightSource != LEFT && objectBackgroundLightSource != RIGHT &&
694       objectBackgroundLightSource != NONE) {
695       valid = false;
696       if (debug) System.out.println ("Problem with ObjectBackgroundLightSource");
697     }
698     if (objectTitleText == null) {
699       valid = false;
700       if (debug) System.out.println ("ObjectTitleText == null");
701     }
702     if (objectTitleFontPointModel < 0) {
703       valid = false;
704       if (debug) System.out.println ("ObjectTitleFontPointModel < 0");
705     }
706     if (objectTitleFontName == null || !isFontNameExists (objectTitleFontName)) {
707       valid = false;
708       if (debug) System.out.println ("Problem with ObjectTitleFontName");
709     }
710     if (objectTitleFontColor == null) {
711       valid = false;
712       if (debug) System.out.println ("ObjectTitleFontColor == null");
713     }
714     if (objectTitleFontStyle != Font.PLAIN && objectTitleFontStyle != Font.ITALIC &&
715       objectTitleFontStyle != Font.BOLD && objectTitleFontStyle != (Font.ITALIC|Font.BOLD)) {
716       valid = false;
717       if (debug) System.out.println ("Problem with ObjectTitleFontStyle");
718     }
719     if (objectTitleBetweenRestGapThicknessModel < 0) {
720       valid = false;
721       if (debug) System.out.println ("ObjectTitleBetweenRestGapThicknessModel < 0");
722     }
723
724     if (debug) {
725       if (valid) System.out.println ("Object2DProperties was valid");
726       else System.out.println ("Object2DProperties was invalid");
727     }
728
729     return valid;
730   }
731
732
733   /**
734    * Updates the properties of this Object2D.
735    * @param object2D The object to update.
736    */

737   final void updateObject2D (Object2D object2D) {
738
739     if (getObject2DNeedsUpdate (object2D)) {
740
741       if (needsUpdate) {
742         for (int i = 0; i < needsUpdateVector.size(); ++i) {
743           needsUpdateVector.set (i, new Boolean JavaDoc (true));
744         }
745         needsUpdate = false;
746       }
747
748       int index = -1;
749       if ((index = object2DVector.indexOf (object2D)) != -1) {
750         needsUpdateVector.set (index, new Boolean JavaDoc (false));
751       }
752     }
753   }
754 }
Popular Tags