1 17 18 19 20 package org.apache.fop.traits; 21 22 import java.awt.Color ; 23 import java.io.Serializable ; 24 import java.util.StringTokenizer ; 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 35 public class BorderProps implements Serializable { 36 37 38 public static final int SEPARATE = 0; 39 40 public static final int COLLAPSE_INNER = 1; 41 42 public static final int COLLAPSE_OUTER = 2; 43 44 45 public int style; 47 public Color color; 48 49 public int width; 50 51 public int mode; 52 53 60 public BorderProps(int style, int width, Color color, int mode) { 61 this.style = style; 62 this.width = width; 63 this.color = color; 64 this.mode = mode; 65 } 66 67 74 public BorderProps(String style, int width, Color color, int mode) { 75 this(getConstantForStyle(style), width, color, mode); 76 } 77 78 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 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 ("Illegal border style: " + style); 103 } 104 } 105 106 private static int getConstantForStyle(String 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 ("Illegal border style: " + style); 129 } 130 } 131 132 133 public int hashCode() { 134 return toString().hashCode(); 135 } 136 137 138 public boolean equals(Object 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 162 public static BorderProps valueOf(FOUserAgent foUserAgent, String s) { 163 if (s.startsWith("(") && s.endsWith(")")) { 164 s = s.substring(1, s.length() - 1); 165 StringTokenizer st = new StringTokenizer (s, ","); 166 String style = st.nextToken(); 167 String color = st.nextToken(); 168 int width = Integer.parseInt(st.nextToken()); 169 int mode = SEPARATE; 170 if (st.hasMoreTokens()) { 171 String 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 c; 179 try { 180 c = ColorUtil.parseColorString(foUserAgent, color); 181 } catch (PropertyException e) { 182 throw new IllegalArgumentException (e.getMessage()); 183 } 184 185 return new BorderProps(style, width, c, mode); 186 } else { 187 throw new IllegalArgumentException ("BorderProps must be surrounded by parentheses"); 188 } 189 } 190 191 192 public String toString() { 193 StringBuffer sbuf = new StringBuffer (); 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 |