Author: lgiessmann Date: Fri Jul 1 02:37:25 2011 New Revision: 541
Log: gdl-frontend: Widgets: implemented a class that wraps a CSS Color value - instead of the GWT color value, instances of this class throw a InvalidGdlException if a color value is invalid or unsupported
Added: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/ColorValue.java branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/NumUnitValue.java
Added: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/ColorValue.java ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/ColorValue.java Fri Jul 1 02:37:25 2011 (r541) @@ -0,0 +1,172 @@ +package us.isidor.gdl.anaToMia.Widgets.values; + +import us.isidor.gdl.anaToMia.Widgets.environment.InvalidGdlSchemaException; + +public class ColorValue { + private String stringValue = null; + + + // some constructors + public ColorValue(){ + this.stringValue = "#000000"; + } + + + public ColorValue(String color) throws InvalidGdlSchemaException{ + String value = color.toLowerCase(); + + if(value.matches("^(maroon|red|orange|yellow|olive|purple|fuchsia|white|lime|green|navy|blue|aqua|teal|black|silver|gray)$")){ + this.stringValue = cssColorToRRGGBB(CssColor.valueOf(value)); + }else if(value.matches("^#[0-9a-f]{6}$")) { + this.stringValue = value; + }else if(value.matches("^#[0-9a-f]{3}$")) { + this.stringValue = "#" + value.charAt(1) + value.charAt(1) + value.charAt(2) + value.charAt(2) + value.charAt(3) + value.charAt(3); + }else if(value.matches("^rgb\( *\+?[0-9]{1,3} *, *\+?[0-9]{1,3} *, *\+?[0-9]{1,3} *\)$")){ + String[] rgb = value.substring(4, value.length() - 1).split(","); + this.stringValue = "#" + decToHexIntegerString(rgb[0]) + decToHexIntegerString(rgb[1]) + decToHexIntegerString(rgb[2]); + }else if(value.matches("^rgb\( *\+?[0-9]{1,3}% *, *\+?[0-9]{1,3}% *, *\+?[0-9]{1,3}% *\)$")){ + String[] rgb = value.substring(4, value.length() - 1).split(","); + this.stringValue = "#" + percentToHexIntegerString(rgb[0]) + percentToHexIntegerString(rgb[1]) + percentToHexIntegerString(rgb[2]); + }else { + throw new InvalidGdlSchemaException("a ColorValue must be a value of the format #RRGGBB, #RGB, rdg(rrr,ggg,bbb), rgb(rrr%,ggg%,bbb%) or a CssColor, but is " + color); + } + } + + + // converts an integer string of a decimal to a hex representation + private String decToHexIntegerString(String decIntegerString){ + int intValue = Integer.valueOf(decIntegerString.replaceFirst("\+", "").trim()); + intValue = intValue > 255 ? 255 : intValue; + String result = Integer.toHexString(intValue); + return result.length() == 1 ? "0" + result : result; + } + + + // converts an integer representing a percentage value string + // to a hex representation + private String percentToHexIntegerString(String percentageString){ + String rawValue = percentageString.replaceFirst("%", "").replaceFirst("\+", "").trim(); + int percentValue = Integer.valueOf(rawValue); + String result = Integer.toHexString((int)(255 * ((float)percentValue / 100))); + return result.length() == 1 ? "0" + result : result; + } + + + public ColorValue(int r, int g, int b) throws InvalidGdlSchemaException { + this("rgb(" + r + "," + g + "," + b + ")"); + } + + + public ColorValue(CssColor color){ + this.stringValue = cssColorToRRGGBB(color); + } + + + // a helper method that parses CssColor instances + // to a string of the format ##RRGGBB + private String cssColorToRRGGBB(CssColor color){ + switch(color){ + case maroon: return "#800000"; + case red: return "#ff0000"; + case orange: return "#ffa500"; + case yellow: return "#ffff00"; + case olive: return "#808000"; + case purple: return "#800080"; + case fuchsia: return "#ff00ff"; + case white: return "#ffffff"; + case lime: return "#00ff00"; + case green: return "#008000"; + case navy: return "#000080"; + case blue: return "#0000ff"; + case aqua: return "#00ffff"; + case teal: return "#008080"; + case black: return "#000000"; + case silver: return "#c0c0c0"; + default: return "#808080"; + } + } + + + // returns a string of the format #RRGGBB + public String getStringValue(){ + return this.stringValue; + } + + + // returns an int array of the form [r, g, b] of the stored + // color value + public int[] getRGBvalue(){ + String r = "" + this.stringValue.charAt(1) + this.stringValue.charAt(2); + String g = "" + this.stringValue.charAt(3) + this.stringValue.charAt(4); + String b = "" + this.stringValue.charAt(5) + this.stringValue.charAt(6); + + return new int[]{Integer.valueOf(r, 16), Integer.valueOf(g, 16), Integer.valueOf(b, 16)}; + } + + + // returns a CSSColor instance of the stored value, if it is + // a value that corresponds to the defined keywords, otherwise + // the return value is null + public CssColor getCssColor(){ + if(this.stringValue.equals("#800000")){ + return CssColor.maroon; + }else if(this.stringValue.equals("#ff0000")){ + return CssColor.red; + }else if(this.stringValue.equals("#ffa500")) { + return CssColor.orange; + }else if(this.stringValue.equals("#ffff00")){ + return CssColor.yellow; + }else if(this.stringValue.equals("#808000")) { + return CssColor.olive; + }else if(this.stringValue.equals("#800080")){ + return CssColor.purple; + }else if(this.stringValue.equals("#ff00ff")) { + return CssColor.fuchsia; + }else if(this.stringValue.equals("#ffffff")){ + return CssColor.white; + }else if(this.stringValue.equals("#00ff00")) { + return CssColor.lime; + }else if(this.stringValue.equals("#008000")) { + return CssColor.green; + }else if(this.stringValue.equals("#000080")) { + return CssColor.navy; + }else if(this.stringValue.equals("#0000ff")) { + return CssColor.blue; + }else if(this.stringValue.equals("#00ffff")) { + return CssColor.aqua; + }else if(this.stringValue.equals("#008080")) { + return CssColor.teal; + }else if(this.stringValue.equals("#000000")) { + return CssColor.black; + }else if(this.stringValue.equals("#c0c0c0")) { + return CssColor.silver; + }else if(this.stringValue.equals("#808080")) { + return CssColor.gray; + }else { + return null; + } + } + + + // represents the color key words that are defined in CSS chapter 4.3.6 + // (http://www.w3.org/TR/CSS21/syndata.html#value-def-color) + public enum CssColor{ + maroon, + red, + orange, + yellow, + olive, + purple, + fuchsia, + white, + lime, + green, + navy, + blue, + aqua, + teal, + black, + silver, + gray + } +}
Added: branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/NumUnitValue.java ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ branches/gdl-frontend/src/anaToMia/GDL_Widgets/src/us/isidor/gdl/anaToMia/Widgets/values/NumUnitValue.java Fri Jul 1 02:37:25 2011 (r541) @@ -0,0 +1,79 @@ +package us.isidor.gdl.anaToMia.Widgets.values; + +import us.isidor.gdl.anaToMia.Widgets.environment.InvalidGdlSchemaException; + +public class NumUnitValue { + private CssUnit unit = CssUnit.pixel; + private float value = 0f; + + + // some constructors + public NumUnitValue(){} + + + public NumUnitValue(String numUnit) throws InvalidGdlSchemaException { + if(numUnit.endsWith("px")){ + this.value = makeFloat(numUnit, 2); + this.unit = CssUnit.pixel; + }else if (numUnit.endsWith("pt")){ + this.value = makeFloat(numUnit, 2); + this.unit = CssUnit.point; + } else if(numUnit.endsWith("%")){ + this.value = makeFloat(numUnit, 1); + this.unit = CssUnit.percentage; + } else { + throw new InvalidGdlSchemaException("numeric values supported by the GDL containing a unit definition must be of the form <numeric-value>(pt|px|%), but found: " + numUnit); + } + } + + + // a helper method that returns a float parsed of the passed stringToParse, + // whereas the tailing endToIgnore characters are ignored + private float makeFloat(String stringToParse, int endToIgnore) throws InvalidGdlSchemaException { + if(stringToParse == null || stringToParse.length() <= endToIgnore){ + throw new InvalidGdlSchemaException("numeric values supported by the GDL containing a unit definition must be of the form <numeric-value>(pt|px|%), but found: " + stringToParse); + } + + String str = stringToParse.substring(0, stringToParse.length() - endToIgnore); + + try{ + return Float.valueOf(str); + }catch(NumberFormatException e){ + throw new InvalidGdlSchemaException("numeric values supported by the GDL containing a unit definition must be of the form <numeric-value>(pt|px|%), but found: " + stringToParse); + } + } + + + // returns the value represented by this instance as a css string + public String getStringValue(){ + switch(this.unit){ + case pixel: return (int)this.value + "px"; + case point: return (int)this.value + "pt"; + default: return this.value + "%"; + } + } + + + // returns the numeric value as a float + public float getNumValue(){ + if(this.unit == CssUnit.percentage){ + return this.value; + } else { + return (int)this.value; + } + } + + + // returns the CssUnit that is represented by this instance + public CssUnit getUnit(){ + return this.unit; + } + + + // a subset of CSS units that are supported by the GDL + public enum CssUnit { + point, + pixel, + percentage + } +}