Revision: 7650
Author: victormote
Date: 2006-06-16 10:36:25 -0700 (Fri, 16 Jun 2006)
ViewCVS: http://svn.sourceforge.net/foray/?rev=7650&view=rev
Log Message:
-----------
1. Add factory method for the components.
2. Rename factory methods for consistency.
Modified Paths:
--------------
trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/Background.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/BorderColor.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtColor.java
trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtShadowEffect.java
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/Background.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/Background.java 2006-06-16 16:50:40 UTC (rev 7649)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/Background.java 2006-06-16 17:36:25 UTC (rev 7650)
@@ -202,7 +202,7 @@
if (keyword > -1) {
return Constants.FOPROP_BACKGROUND_POSITION;
}
- DtColor color = DtColor.makeColorDT(input);
+ DtColor color = DtColor.makeDtColor(input);
if (color != null) {
return Constants.FOPROP_BACKGROUND_COLOR;
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/BorderColor.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/BorderColor.java 2006-06-16 16:50:40 UTC (rev 7649)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/fo/prop/BorderColor.java 2006-06-16 17:36:25 UTC (rev 7650)
@@ -60,7 +60,7 @@
public PropertyValue createPropertyValue(PropertyList propertyList,
short enumeration, String value) throws PropertyException {
- DtColor color = DtColor.makeColorDT(value);
+ DtColor color = DtColor.makeDtColor(value);
if (color != null) {
return color;
}
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtColor.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtColor.java 2006-06-16 16:50:40 UTC (rev 7649)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtColor.java 2006-06-16 17:36:25 UTC (rev 7650)
@@ -27,11 +27,18 @@
import org.foray.fotree.PropertyException;
import java.awt.Color;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
- * A "color" property datatype in XSL-FO. See XSL-FO Std. 1.0, Sec. 5.11.
+ * <p>A "color" property datatype in XSL-FO. See XSL-FO Std. 1.0, Sec. 5.11.</p>
+ *
+ * <p>To prevent creation of multiple instances for the same underlying color,
+ * the constructor is private.
+ * To obtain an instance, use one of the following static methods instead:
+ * {@link #makeDtColor(String)}, {@link #makeDtColor(Color)}, or
+ * {@link #makeDtColor(float, float, float, float)}.</p>
*/
public class DtColor extends Datatype implements ExprColor {
@@ -69,22 +76,22 @@
* the color values.
*/
private static final DtColor[] vals = {
- makeColorDT(new Color(0, 255, 255)), // aqua
- makeColorDT(new Color(0, 0, 0)), // black
- makeColorDT(new Color(0, 0, 255)), // blue
- makeColorDT(new Color(255, 0, 255)), // fuchsia
- makeColorDT(new Color(128, 128, 128)), // gray
- makeColorDT(new Color(0, 128, 0)), // green
- makeColorDT(new Color(0, 255, 0)), // lime
- makeColorDT(new Color(128, 0, 0)), // maroon
- makeColorDT(new Color(0, 0, 128)), // navy
- makeColorDT(new Color(128, 128, 0)), // olive
- makeColorDT(new Color(128, 0, 128)), // purple
- makeColorDT(new Color(255, 0, 0)), // red
- makeColorDT(new Color(192, 192, 192)), // silver
- makeColorDT(new Color(0, 128, 128)), // teal
- makeColorDT(new Color(255, 255, 255)), // white
- makeColorDT(new Color(255, 255, 0)), // yellow
+ makeDtColor(new Color(0, 255, 255)), // aqua
+ makeDtColor(new Color(0, 0, 0)), // black
+ makeDtColor(new Color(0, 0, 255)), // blue
+ makeDtColor(new Color(255, 0, 255)), // fuchsia
+ makeDtColor(new Color(128, 128, 128)), // gray
+ makeDtColor(new Color(0, 128, 0)), // green
+ makeDtColor(new Color(0, 255, 0)), // lime
+ makeDtColor(new Color(128, 0, 0)), // maroon
+ makeDtColor(new Color(0, 0, 128)), // navy
+ makeDtColor(new Color(128, 128, 0)), // olive
+ makeDtColor(new Color(128, 0, 128)), // purple
+ makeDtColor(new Color(255, 0, 0)), // red
+ makeDtColor(new Color(192, 192, 192)), // silver
+ makeDtColor(new Color(0, 128, 128)), // teal
+ makeDtColor(new Color(255, 255, 255)), // white
+ makeDtColor(new Color(255, 255, 0)), // yellow
};
/*
@@ -229,11 +236,20 @@
public static final Color COLOR_RGB_TRANSPARENT = new Color(255, 255, 255,
0);
+ /**
+ * List of DtColor instances created. The purpose of this list is to
+ * prevent identical colors from being created more than once.
+ * TODO: Sort this by the numeric color value stored in the java.awt.Color
+ * so that a binary search can be used.
+ */
+ private static ArrayList colorsUsed = new ArrayList(20);
+
private Color value;
/**
* Private Constructor. Use one of the following static methods instead:
- * {@link #makeColorDT(String)}.
+ * {@link #makeDtColor(String)}, {@link #makeDtColor(Color)}, or
+ * {@link #makeDtColor(float, float, float, float)}.
*/
private DtColor(Color color) {
this.value = color;
@@ -257,14 +273,83 @@
* @return A ColorDT instance, or null if input is not a defined XSL-FO
* named color.
*/
- public static DtColor makeColorDT(String input) {
+ public static DtColor makeDtColor(String input) {
+ /* The names colors are static instances anyway, so no need to check
+ * the list of used colors. */
return mapNameToColorRGB(input);
}
- public static DtColor makeColorDT(Color color) {
+ public static DtColor makeDtColor(Color color) {
+ DtColor foundColor = DtColor.findColor(color);
+ /* If it has already been created, return the previously-created
+ * instance. */
+ if (foundColor != null) {
+ return foundColor;
+ }
+ /* Otherwise, create a new instance. */
return new DtColor(color);
}
+ public static DtColor makeDtColor(float red, float green, float blue,
+ float alpha) {
+ DtColor foundColor = DtColor.findColor(red, green, blue, alpha);
+ /* If it has already been created, return the previously-created
+ * instance. */
+ if (foundColor != null) {
+ return foundColor;
+ }
+ /* Otherwise, create a new instance. */
+ Color newColor = new Color(red, green, blue, alpha);
+ return new DtColor(newColor);
+ }
+
+ /**
+ * Looks for a given color in the list of used colors to see if it has
+ * already been created.
+ * @param color The color sought in the list of used colors.
+ * @return The already-created instance for <code>color</code>, or null
+ * if it has not yet been created.
+ */
+ private static DtColor findColor(Color color) {
+ for (int i = 0; i < DtColor.colorsUsed.size(); i++) {
+ DtColor usedColor = (DtColor) DtColor.colorsUsed.get(i);
+ if (usedColor.value.equals(color)) {
+ return usedColor;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Looks for a given color in the list of used colors to see if it has
+ * already been created.
+ * @param red The red component of the color sought.
+ * @param green The green component of the color sought.
+ * @param blue The blue component of the color sought.
+ * @param alpha The alpha component of the color sought.
+ * @return The already-created instance, or null if it has not yet been
+ * created.
+ */
+ private static DtColor findColor(float red, float green, float blue,
+ float alpha) {
+ for (int i = 0; i < DtColor.colorsUsed.size(); i++) {
+ DtColor usedColor = (DtColor) DtColor.colorsUsed.get(i);
+ Color colorValue = usedColor.value;
+ float[] components = colorValue.getRGBComponents(null);
+ if (components == null
+ || components.length != 4) {
+ continue;
+ }
+ if (components[0] == red
+ && components[1] == green
+ && components[2] == blue
+ && components[3] == alpha) {
+ return usedColor;
+ }
+ }
+ return null;
+ }
+
public static DtColor mapNameToColorRGB(String colorName) {
// Do a binary search to find the name in the names list.
int index = Arrays.binarySearch(names, colorName);
Modified: trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtShadowEffect.java
===================================================================
--- trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtShadowEffect.java 2006-06-16 16:50:40 UTC (rev 7649)
+++ trunk/foray/foray-fotree/src/java/org/foray/fotree/value/DtShadowEffect.java 2006-06-16 17:36:25 UTC (rev 7650)
@@ -101,7 +101,7 @@
}
for (int i = 0; i < st.countTokens(); i++) {
String token = st.nextToken();
- DtColor aColor = DtColor.makeColorDT(token);
+ DtColor aColor = DtColor.makeDtColor(token);
if (aColor != null) {
// If it is a color, it must be either the first or last token.
if (i == 0 || i == st.countTokens() - 1
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|