KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > traits > BorderProps


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: BorderProps.java 474225 2006-11-13 10:08:19Z jeremias $ */
19  
20 package org.apache.fop.traits;
21
22 import java.awt.Color JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.fop.apps.FOUserAgent;
27 import org.apache.fop.fo.Constants;
28 import org.apache.fop.fo.expr.PropertyException;
29 import org.apache.fop.util.ColorUtil;
30
31 /**
32  * Border properties.
33  * Class to store border trait propties for the area tree.
34  */

35 public class BorderProps implements Serializable JavaDoc {
36     
37     /** Separate border model */
38     public static final int SEPARATE = 0;
39     /** Collapsing border model, for borders inside a table */
40     public static final int COLLAPSE_INNER = 1;
41     /** Collapsing border model, for borders at the table's outer border */
42     public static final int COLLAPSE_OUTER = 2;
43     
44     /** Border style (one of EN_*) */
45     public int style; // Enum for border style
46
/** Border color */
47     public Color JavaDoc color;
48     /** Border width */
49     public int width;
50     /** Border mode (one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER) */
51     public int mode;
52
53     /**
54      * Constructs a new BorderProps instance.
55      * @param style border style (one of EN_*)
56      * @param width border width
57      * @param color border color
58      * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
59      */

60     public BorderProps(int style, int width, Color JavaDoc color, int mode) {
61         this.style = style;
62         this.width = width;
63         this.color = color;
64         this.mode = mode;
65     }
66
67     /**
68      * Constructs a new BorderProps instance.
69      * @param style border style (one of the XSL enum values for border style)
70      * @param width border width
71      * @param color border color
72      * @param mode border mode ((one of SEPARATE, COLLAPSE_INNER and COLLAPSE_OUTER)
73      */

74     public BorderProps(String JavaDoc style, int width, Color JavaDoc color, int mode) {
75         this(getConstantForStyle(style), width, color, mode);
76     }
77
78     /**
79      * @param bp the border properties or null
80      * @return the effective width of the clipped part of the border
81      */

82     public static int getClippedWidth(BorderProps bp) {
83         if ((bp != null) && (bp.mode != SEPARATE)) {
84             return bp.width / 2;
85         } else {
86             return 0;
87         }
88     }
89     
90     private String JavaDoc getStyleString() {
91         switch (style) {
92         case Constants.EN_NONE: return "none";
93         case Constants.EN_HIDDEN: return "hidden";
94         case Constants.EN_DOTTED: return "dotted";
95         case Constants.EN_DASHED: return "dashed";
96         case Constants.EN_SOLID: return "solid";
97         case Constants.EN_DOUBLE: return "double";
98         case Constants.EN_GROOVE: return "groove";
99         case Constants.EN_RIDGE: return "ridge";
100         case Constants.EN_INSET: return "inset";
101         case Constants.EN_OUTSET: return "outset";
102         default: throw new IllegalStateException JavaDoc("Illegal border style: " + style);
103         }
104     }
105     
106     private static int getConstantForStyle(String JavaDoc style) {
107         if ("none".equalsIgnoreCase(style)) {
108             return Constants.EN_NONE;
109         } else if ("hidden".equalsIgnoreCase(style)) {
110             return Constants.EN_HIDDEN;
111         } else if ("dotted".equalsIgnoreCase(style)) {
112             return Constants.EN_DOTTED;
113         } else if ("dashed".equalsIgnoreCase(style)) {
114             return Constants.EN_DASHED;
115         } else if ("solid".equalsIgnoreCase(style)) {
116             return Constants.EN_SOLID;
117         } else if ("double".equalsIgnoreCase(style)) {
118             return Constants.EN_DOUBLE;
119         } else if ("groove".equalsIgnoreCase(style)) {
120             return Constants.EN_GROOVE;
121         } else if ("ridge".equalsIgnoreCase(style)) {
122             return Constants.EN_RIDGE;
123         } else if ("inset".equalsIgnoreCase(style)) {
124             return Constants.EN_INSET;
125         } else if ("outset".equalsIgnoreCase(style)) {
126             return Constants.EN_OUTSET;
127         } else {
128             throw new IllegalStateException JavaDoc("Illegal border style: " + style);
129         }
130     }
131     
132     /** @see java.lang.Object#hashCode() */
133     public int hashCode() {
134         return toString().hashCode();
135     }
136
137     /** @see java.lang.Object#equals(java.lang.Object) */
138     public boolean equals(Object JavaDoc obj) {
139         if (obj == null) {
140             return false;
141         } else if (obj == this) {
142             return true;
143         } else {
144             if (obj instanceof BorderProps) {
145                 BorderProps other = (BorderProps)obj;
146                 return (style == other.style)
147                         && color.equals(other.color)
148                         && width == other.width
149                         && mode == other.mode;
150             }
151         }
152         return false;
153     }
154
155     /**
156      * Returns a BorderProps represtation of a string of the format as written by
157      * BorderProps.toString().
158      * @param foUserAgent FOP user agent caching ICC profiles
159      * @param s the string
160      * @return a BorderProps instance
161      */

162     public static BorderProps valueOf(FOUserAgent foUserAgent, String JavaDoc s) {
163         if (s.startsWith("(") && s.endsWith(")")) {
164             s = s.substring(1, s.length() - 1);
165             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
166             String JavaDoc style = st.nextToken();
167             String JavaDoc color = st.nextToken();
168             int width = Integer.parseInt(st.nextToken());
169             int mode = SEPARATE;
170             if (st.hasMoreTokens()) {
171                 String JavaDoc ms = st.nextToken();
172                 if ("collapse-inner".equalsIgnoreCase(ms)) {
173                     mode = COLLAPSE_INNER;
174                 } else if ("collapse-outer".equalsIgnoreCase(ms)) {
175                     mode = COLLAPSE_OUTER;
176                 }
177             }
178             Color JavaDoc c;
179             try {
180                 c = ColorUtil.parseColorString(foUserAgent, color);
181             } catch (PropertyException e) {
182                 throw new IllegalArgumentException JavaDoc(e.getMessage());
183             }
184             
185             return new BorderProps(style, width, c, mode);
186         } else {
187             throw new IllegalArgumentException JavaDoc("BorderProps must be surrounded by parentheses");
188         }
189     }
190
191     /** @see java.lang.Object#toString() */
192     public String JavaDoc toString() {
193         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
194         sbuf.append('(');
195         sbuf.append(getStyleString());
196         sbuf.append(',');
197         sbuf.append(ColorUtil.colorToString(color));
198         sbuf.append(',');
199         sbuf.append(width);
200         if (mode != SEPARATE) {
201             sbuf.append(',');
202             if (mode == COLLAPSE_INNER) {
203                 sbuf.append("collapse-inner");
204             } else {
205                 sbuf.append("collapse-outer");
206             }
207         }
208         sbuf.append(')');
209         return sbuf.toString();
210     }
211
212 }
213
Popular Tags