View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg 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
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  /*
19   * Copyright (c) 2004 World Wide Web Consortium,
20   *
21   * (Massachusetts Institute of Technology, European Research Consortium for
22   * Informatics and Mathematics, Keio University). All Rights Reserved. This
23   * work is distributed under the W3C(r) Software License [1] in the hope that
24   * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26   *
27   * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28   */
29  
30  package org.vectomatic.dom.svg;
31  
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import com.google.gwt.core.client.JavaScriptException;
36  
37  /**
38   * Class for a CSS primitive value
39   * @author laaglu
40   */
41  public class OMCSSPrimitiveValue extends OMCSSValue {
42    public static final short CSS_UNKNOWN = 0;
43    public static final short CSS_NUMBER = 1;
44    public static final short CSS_PERCENTAGE = 2;
45    public static final short CSS_EMS = 3;
46    public static final short CSS_EXS = 4;
47    public static final short CSS_PX = 5;
48    public static final short CSS_CM = 6;
49    public static final short CSS_MM = 7;
50    public static final short CSS_IN = 8;
51    public static final short CSS_PT = 9;
52    public static final short CSS_PC = 10;
53    public static final short CSS_DEG = 11;
54    public static final short CSS_RAD = 12;
55    public static final short CSS_GRAD = 13;
56    public static final short CSS_MS = 14;
57    public static final short CSS_S = 15;
58    public static final short CSS_HZ = 16;
59    public static final short CSS_KHZ = 17;
60    public static final short CSS_DIMENSION = 18;
61    public static final short CSS_STRING = 19;
62    public static final short CSS_URI = 20;
63    public static final short CSS_IDENT = 21;
64    public static final short CSS_ATTR = 22;
65    public static final short CSS_COUNTER = 23;
66    public static final short CSS_RECT = 24;
67    public static final short CSS_RGBCOLOR = 25;
68    
69    private short primitiveType;
70    private float floatValue;
71    private String stringValue;
72    private static Map<Short,String> unitToString;
73    protected OMCSSPrimitiveValue(float f) {
74  	  this(f, CSS_NUMBER);
75    }
76  
77    public OMCSSPrimitiveValue(float floatValue, short primitiveType) {
78  	  super(CSS_PRIMITIVE_VALUE);
79  	  if (unitToString == null) {
80  		  unitToString = new HashMap<Short, String>();
81  		  unitToString.put(CSS_EMS, "em");
82  		  unitToString.put(CSS_EXS, "ex");
83  		  unitToString.put(CSS_PX, "px");
84  		  unitToString.put(CSS_IN, "in");
85  		  unitToString.put(CSS_CM, "cm");
86  		  unitToString.put(CSS_MM, "mm");
87  		  unitToString.put(CSS_PT, "pt");
88  		  unitToString.put(CSS_PC, "pc");
89  		  unitToString.put(CSS_PERCENTAGE, "%");
90  	  }
91  	  StringBuilder builder = new StringBuilder(Float.toString(floatValue));
92  	  String unit = unitToString.get(primitiveType);
93  	  if (unit != null) {
94  		  builder.append(unit);
95  	  }
96  	  setCssText(builder.toString());
97  	  this.floatValue = floatValue;
98  	  this.primitiveType = primitiveType;
99    }
100   public OMCSSPrimitiveValue(String stringValue, short primitiveType) {
101 	  super(CSS_PRIMITIVE_VALUE);
102 	  this.stringValue = stringValue;
103 	  this.primitiveType = primitiveType;
104 	  this.cssText = stringValue;
105   }
106 
107   // Implementation of the css::CSSPrimitiveValue W3C IDL interface
108   public final short getPrimitiveType() {
109     return this.primitiveType;
110   }
111   public final void setFloatValue(short unitType, float floatValue) throws JavaScriptException {
112 	this.primitiveType = unitType;
113     this.floatValue = floatValue;
114   }
115   public final float getFloatValue(short unitType) throws JavaScriptException {
116     return floatValue;
117   }
118   public final void setStringValue(short stringType, String stringValue) throws JavaScriptException {
119 	  this.primitiveType = stringType;
120 	  this.stringValue = stringValue;
121   }
122   public final String getStringValue() throws JavaScriptException {
123     return stringValue;
124   }
125   public final OMRGBColor getRGBColorValue() throws JavaScriptException {
126     throw new UnsupportedOperationException();
127   }
128   @Override
129   public String getDescription() {
130 	StringBuilder builder = new StringBuilder("OMCSSPrimitiveValue(primitiveType=");
131 	builder.append(primitiveType);
132 	builder.append(", value=");
133 	if (primitiveType >= CSS_NUMBER && CSS_NUMBER <= CSS_GRAD) {
134 		builder.append(floatValue);
135 	} else {
136 		builder.append(stringValue);
137 	}
138 	builder.append(", cssValueType=");
139 	builder.append(cssValueType);
140 	builder.append(", cssText=");
141 	builder.append(cssText);
142 	builder.append(")");
143 	return builder.toString();
144   }
145 
146 }