KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > area > Trait


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: Trait.java 474225 2006-11-13 10:08:19Z jeremias $ */
19
20 package org.apache.fop.area;
21
22 import java.awt.Color JavaDoc;
23 import java.io.Serializable JavaDoc;
24
25 import org.apache.fop.fo.Constants;
26 import org.apache.fop.fonts.FontTriplet;
27 import org.apache.fop.image.FopImage;
28 import org.apache.fop.traits.BorderProps;
29 import org.apache.fop.util.ColorUtil;
30
31 // properties should be serialized by the holder
32
/**
33  * Area traits used for rendering.
34  * This class represents an area trait that specifies a value for rendering.
35  */

36 public class Trait implements Serializable JavaDoc {
37
38     /**
39      * Id reference line, not resolved.
40      * not sure if this is needed.
41      */

42     //public static final Integer ID_LINK = new Integer(0);
43

44     /**
45      * Internal link trait.
46      * This is resolved and provides a link to an internal area.
47      */

48     public static final Integer JavaDoc INTERNAL_LINK = new Integer JavaDoc(1); //resolved
49

50     /**
51      * External link. A URL link to an external resource.
52      */

53     public static final Integer JavaDoc EXTERNAL_LINK = new Integer JavaDoc(2);
54
55     /**
56      * The font triplet for the current font.
57      */

58     public static final Integer JavaDoc FONT = new Integer JavaDoc(3);
59
60     /**
61      * Font size for the current font.
62      */

63     public static final Integer JavaDoc FONT_SIZE = new Integer JavaDoc(4);
64
65     /**
66      * The current color.
67      */

68     public static final Integer JavaDoc COLOR = new Integer JavaDoc(7);
69
70     /**
71      * The ID of the FO that produced an area.
72      */

73     public static final Integer JavaDoc PROD_ID = new Integer JavaDoc(8);
74
75     /**
76      * Background trait for an area.
77      */

78     public static final Integer JavaDoc BACKGROUND = new Integer JavaDoc(9);
79
80     /**
81      * Underline trait used when rendering inline parent.
82      */

83     public static final Integer JavaDoc UNDERLINE = new Integer JavaDoc(10);
84
85     /**
86      * Overline trait used when rendering inline parent.
87      */

88     public static final Integer JavaDoc OVERLINE = new Integer JavaDoc(11);
89
90     /**
91      * Linethrough trait used when rendering inline parent.
92      */

93     public static final Integer JavaDoc LINETHROUGH = new Integer JavaDoc(12);
94
95     /**
96      * Shadow offset.
97      */

98     //public static final Integer OFFSET = new Integer(13);
99

100     /**
101      * The shadow for text.
102      */

103     //public static final Integer SHADOW = new Integer(14);
104

105     /**
106      * The border start.
107      */

108     public static final Integer JavaDoc BORDER_START = new Integer JavaDoc(15);
109
110     /**
111      * The border end.
112      */

113     public static final Integer JavaDoc BORDER_END = new Integer JavaDoc(16);
114
115     /**
116      * The border before.
117      */

118     public static final Integer JavaDoc BORDER_BEFORE = new Integer JavaDoc(17);
119
120     /**
121      * The border after.
122      */

123     public static final Integer JavaDoc BORDER_AFTER = new Integer JavaDoc(18);
124
125     /**
126      * The padding start.
127      */

128     public static final Integer JavaDoc PADDING_START = new Integer JavaDoc(19);
129
130     /**
131      * The padding end.
132      */

133     public static final Integer JavaDoc PADDING_END = new Integer JavaDoc(20);
134
135     /**
136      * The padding before.
137      */

138     public static final Integer JavaDoc PADDING_BEFORE = new Integer JavaDoc(21);
139
140     /**
141      * The padding after.
142      */

143     public static final Integer JavaDoc PADDING_AFTER = new Integer JavaDoc(22);
144
145     /**
146      * The space start.
147      */

148     public static final Integer JavaDoc SPACE_START = new Integer JavaDoc(23);
149
150     /**
151      * The space end.
152      */

153     public static final Integer JavaDoc SPACE_END = new Integer JavaDoc(24);
154
155     /**
156      * break before
157      */

158     //public static final Integer BREAK_BEFORE = new Integer(25);
159

160     /**
161      * break after
162      */

163     //public static final Integer BREAK_AFTER = new Integer(26);
164

165     /**
166      * The start-indent trait.
167      */

168     public static final Integer JavaDoc START_INDENT = new Integer JavaDoc(27);
169
170     /**
171      * The end-indent trait.
172      */

173     public static final Integer JavaDoc END_INDENT = new Integer JavaDoc(28);
174
175     /** The space-before trait. */
176     public static final Integer JavaDoc SPACE_BEFORE = new Integer JavaDoc(29);
177     
178     /** The space-after trait. */
179     public static final Integer JavaDoc SPACE_AFTER = new Integer JavaDoc(30);
180     
181     /** The is-reference-area trait. */
182     public static final Integer JavaDoc IS_REFERENCE_AREA = new Integer JavaDoc(31);
183     
184     /** The is-viewport-area trait. */
185     public static final Integer JavaDoc IS_VIEWPORT_AREA = new Integer JavaDoc(32);
186     
187     /** Blinking trait used when rendering inline parent. */
188     public static final Integer JavaDoc BLINK = new Integer JavaDoc(33);
189     
190     /** Trait for color of underline decorations when rendering inline parent. */
191     public static final Integer JavaDoc UNDERLINE_COLOR = new Integer JavaDoc(34);
192     /** Trait for color of overline decorations when rendering inline parent. */
193     public static final Integer JavaDoc OVERLINE_COLOR = new Integer JavaDoc(35);
194     /** Trait for color of linethrough decorations when rendering inline parent. */
195     public static final Integer JavaDoc LINETHROUGH_COLOR = new Integer JavaDoc(36);
196     
197     /** Maximum value used by trait keys */
198     public static final int MAX_TRAIT_KEY = 36;
199     
200     private static final TraitInfo[] TRAIT_INFO = new TraitInfo[MAX_TRAIT_KEY + 1];
201     
202     private static class TraitInfo {
203         private String JavaDoc name;
204         private Class JavaDoc clazz; // Class of trait data
205

206         public TraitInfo(String JavaDoc name, Class JavaDoc clazz) {
207             this.name = name;
208             this.clazz = clazz;
209         }
210
211         public String JavaDoc getName() {
212             return this.name;
213         }
214
215         public Class JavaDoc getClazz() {
216             return this.clazz;
217         }
218     }
219
220     private static void put(Integer JavaDoc key, TraitInfo info) {
221         TRAIT_INFO[key.intValue()] = info;
222     }
223     
224     static {
225         // Create a hashmap mapping trait code to name for external representation
226
//put(ID_LINK, new TraitInfo("id-link", String.class));
227
put(INTERNAL_LINK, new TraitInfo("internal-link", String JavaDoc.class));
228         put(EXTERNAL_LINK, new TraitInfo("external-link", String JavaDoc.class));
229         put(FONT, new TraitInfo("font", FontTriplet.class));
230         put(FONT_SIZE, new TraitInfo("font-size", Integer JavaDoc.class));
231         put(COLOR, new TraitInfo("color", Color JavaDoc.class));
232         put(PROD_ID, new TraitInfo("prod-id", String JavaDoc.class));
233         put(BACKGROUND, new TraitInfo("background", Background.class));
234         put(UNDERLINE, new TraitInfo("underline-score", Boolean JavaDoc.class));
235         put(UNDERLINE_COLOR, new TraitInfo("underline-score-color", Color JavaDoc.class));
236         put(OVERLINE, new TraitInfo("overline-score", Boolean JavaDoc.class));
237         put(OVERLINE_COLOR, new TraitInfo("overline-score-color", Color JavaDoc.class));
238         put(LINETHROUGH, new TraitInfo("through-score", Boolean JavaDoc.class));
239         put(LINETHROUGH_COLOR, new TraitInfo("through-score-color", Color JavaDoc.class));
240         put(BLINK, new TraitInfo("blink", Boolean JavaDoc.class));
241         //put(OFFSET, new TraitInfo("offset", Integer.class));
242
//put(SHADOW, new TraitInfo("shadow", Integer.class));
243
put(BORDER_START,
244                           new TraitInfo("border-start", BorderProps.class));
245         put(BORDER_END,
246                           new TraitInfo("border-end", BorderProps.class));
247         put(BORDER_BEFORE,
248                           new TraitInfo("border-before", BorderProps.class));
249         put(BORDER_AFTER,
250                           new TraitInfo("border-after", BorderProps.class));
251         put(PADDING_START,
252                           new TraitInfo("padding-start", Integer JavaDoc.class));
253         put(PADDING_END,
254                           new TraitInfo("padding-end", Integer JavaDoc.class));
255         put(PADDING_BEFORE,
256                           new TraitInfo("padding-before", Integer JavaDoc.class));
257         put(PADDING_AFTER,
258                           new TraitInfo("padding-after", Integer JavaDoc.class));
259         put(SPACE_START,
260                           new TraitInfo("space-start", Integer JavaDoc.class));
261         put(SPACE_END,
262                           new TraitInfo("space-end", Integer JavaDoc.class));
263         //put(BREAK_BEFORE,
264
// new TraitInfo("break-before", Integer.class));
265
//put(BREAK_AFTER,
266
// new TraitInfo("break-after", Integer.class));
267
put(START_INDENT,
268                 new TraitInfo("start-indent", Integer JavaDoc.class));
269         put(END_INDENT,
270                 new TraitInfo("end-indent", Integer JavaDoc.class));
271         put(SPACE_BEFORE,
272                 new TraitInfo("space-before", Integer JavaDoc.class));
273         put(SPACE_AFTER,
274                 new TraitInfo("space-after", Integer JavaDoc.class));
275         put(IS_REFERENCE_AREA,
276                 new TraitInfo("is-reference-area", Boolean JavaDoc.class));
277         put(IS_VIEWPORT_AREA,
278                 new TraitInfo("is-viewport-area", Boolean JavaDoc.class));
279         
280     }
281
282     /**
283      * Get the trait name for a trait code.
284      *
285      * @param traitCode the trait code to get the name for
286      * @return the trait name
287      */

288     public static String JavaDoc getTraitName(Object JavaDoc traitCode) {
289         return TRAIT_INFO[((Integer JavaDoc)traitCode).intValue()].getName();
290     }
291
292     /**
293      * Get the trait code for a trait name.
294      *
295      * @param sTraitName the name of the trait to find
296      * @return the trait code object
297      */

298     /*
299     public static Object getTraitCode(String sTraitName) {
300         Iterator iter = TRAIT_INFO.entrySet().iterator();
301         while (iter.hasNext()) {
302             Map.Entry entry = (Map.Entry) iter.next();
303             TraitInfo ti = (TraitInfo) entry.getValue();
304             if (ti != null && ti.getName().equals(sTraitName)) {
305                 return entry.getKey();
306             }
307         }
308         return null;
309     }*/

310
311     /**
312      * Get the data storage class for the trait.
313      *
314      * @param traitCode the trait code to lookup
315      * @return the class type for the trait
316      */

317     public static Class JavaDoc getTraitClass(Object JavaDoc traitCode) {
318         return TRAIT_INFO[((Integer JavaDoc)traitCode).intValue()].getClazz();
319     }
320
321     /**
322      * The type of trait for an area.
323      */

324     private Object JavaDoc propType;
325
326     /**
327      * The data value of the trait.
328      */

329     private Object JavaDoc data;
330
331     /**
332      * Create a new empty trait.
333      */

334     public Trait() {
335         this.propType = null;
336         this.data = null;
337     }
338
339     /**
340      * Create a trait with the value and type.
341      *
342      * @param propType the type of trait
343      * @param data the data value
344      */

345     public Trait(Object JavaDoc propType, Object JavaDoc data) {
346         this.propType = propType;
347         this.data = data;
348     }
349
350     /**
351      * Returns the trait data value.
352      * @return the trait data value
353      */

354     public Object JavaDoc getData() {
355         return this.data;
356     }
357
358     /**
359      * Returns the property type.
360      * @return the property type
361      */

362     public Object JavaDoc getPropType() {
363         return this.propType;
364     }
365
366     /**
367      * Return the string for debugging.
368      * @see java.lang.Object#toString()
369      */

370     public String JavaDoc toString() {
371         return data.toString();
372     }
373
374     /**
375      * Make a trait value.
376      *
377      * @param oCode trait code
378      * @param sTraitValue trait value as String
379      * @return the trait value as object
380      */

381     /*
382     public static Object makeTraitValue(Object oCode, String sTraitValue) {
383         // Get the code from the name
384         // See what type of object it is
385         // Convert string value to an object of that type
386         Class tclass = getTraitClass(oCode);
387         if (tclass == null) {
388             return null;
389         }
390         if (tclass.equals(String.class)) {
391             return sTraitValue;
392         }
393         if (tclass.equals(Integer.class)) {
394             return new Integer(sTraitValue);
395         }
396         // See if the class has a constructor from string or can read from a string
397         try {
398             Object o = tclass.newInstance();
399             //return o.fromString(sTraitValue);
400         } catch (IllegalAccessException e1) {
401             log.error("Can't create instance of "
402                                + tclass.getName());
403             return null;
404         } catch (InstantiationException e2) {
405             log.error("Can't create instance of "
406                                + tclass.getName());
407             return null;
408         }
409
410
411         return null;
412     }*/

413
414     
415     /**
416      * Background trait structure.
417      * Used for storing back trait information which are related.
418      */

419     public static class Background implements Serializable JavaDoc {
420
421         /** The background color if any. */
422         private Color JavaDoc color = null;
423
424         /** The background image url if any. */
425         private String JavaDoc url = null;
426         
427         /** The background image if any. */
428         private FopImage fopimage = null;
429
430         /** Background repeat enum for images. */
431         private int repeat;
432
433         /** Background horizontal offset for images. */
434         private int horiz;
435
436         /** Background vertical offset for images. */
437         private int vertical;
438
439         /**
440          * Returns the background color.
441          * @return background color, null if n/a
442          */

443         public Color JavaDoc getColor() {
444             return color;
445         }
446
447         /**
448          * Returns the horizontal offset for images.
449          * @return the horizontal offset
450          */

451         public int getHoriz() {
452             return horiz;
453         }
454
455         /**
456          * Returns the image repetition behaviour for images.
457          * @return the image repetition behaviour
458          */

459         public int getRepeat() {
460             return repeat;
461         }
462
463         /**
464          * Returns the URL to the background image
465          * @return URL to the background image, null if n/a
466          */

467         public String JavaDoc getURL() {
468             return url;
469         }
470
471         /**
472          * Returns the FopImage representing the background image
473          * @return the background image, null if n/a
474          */

475         public FopImage getFopImage() {
476             return fopimage;
477         }
478
479         /**
480          * Returns the vertical offset for images.
481          * @return the vertical offset
482          */

483         public int getVertical() {
484             return vertical;
485         }
486
487         /**
488          * Sets the color.
489          * @param color The color to set
490          */

491         public void setColor(Color JavaDoc color) {
492             this.color = color;
493         }
494
495         /**
496          * Sets the horizontal offset.
497          * @param horiz The horizontal offset to set
498          */

499         public void setHoriz(int horiz) {
500             this.horiz = horiz;
501         }
502
503         /**
504          * Sets the image repetition behaviour for images.
505          * @param repeat The image repetition behaviour to set
506          */

507         public void setRepeat(int repeat) {
508             this.repeat = repeat;
509         }
510
511         /**
512          * Sets the image repetition behaviour for images.
513          * @param repeat The image repetition behaviour to set
514          */

515         public void setRepeat(String JavaDoc repeat) {
516             setRepeat(getConstantForRepeat(repeat));
517         }
518
519         /**
520          * Sets the URL to the background image.
521          * @param url The URL to set
522          */

523         public void setURL(String JavaDoc url) {
524             this.url = url;
525         }
526
527         /**
528          * Sets the FopImage to use as the background image.
529          * @param fopimage The FopImage to use
530          */

531         public void setFopImage(FopImage fopimage) {
532             this.fopimage = fopimage;
533         }
534
535         /**
536          * Sets the vertical offset for images.
537          * @param vertical The vertical offset to set
538          */

539         public void setVertical(int vertical) {
540             this.vertical = vertical;
541         }
542
543         private String JavaDoc getRepeatString() {
544             switch (getRepeat()) {
545             case Constants.EN_REPEAT: return "repeat";
546             case Constants.EN_REPEATX: return "repeat-x";
547             case Constants.EN_REPEATY: return "repeat-y";
548             case Constants.EN_NOREPEAT: return "no-repeat";
549             default: throw new IllegalStateException JavaDoc("Illegal repeat style: " + getRepeat());
550             }
551         }
552
553         private static int getConstantForRepeat(String JavaDoc repeat) {
554             if ("repeat".equalsIgnoreCase(repeat)) {
555                 return Constants.EN_REPEAT;
556             } else if ("repeat-x".equalsIgnoreCase(repeat)) {
557                 return Constants.EN_REPEATX;
558             } else if ("repeat-y".equalsIgnoreCase(repeat)) {
559                 return Constants.EN_REPEATY;
560             } else if ("no-repeat".equalsIgnoreCase(repeat)) {
561                 return Constants.EN_NOREPEAT;
562             } else {
563                 throw new IllegalStateException JavaDoc("Illegal repeat style: " + repeat);
564             }
565         }
566         
567         /**
568          * Return the string for debugging.
569          * @see java.lang.Object#toString()
570          */

571         public String JavaDoc toString() {
572             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
573             if (color != null) {
574                 sb.append("color=").append(ColorUtil.colorToString(color));
575             }
576             if (url != null) {
577                 if (color != null) {
578                     sb.append(",");
579                 }
580                 sb.append("url=").append(url);
581                 sb.append(",repeat=").append(getRepeatString());
582                 sb.append(",horiz=").append(horiz);
583                 sb.append(",vertical=").append(vertical);
584             }
585             return sb.toString();
586         }
587
588     }
589 }
590
591
Popular Tags