From: <kon...@us...> - 2010-09-27 18:03:04
|
Revision: 1136 http://cishell.svn.sourceforge.net/cishell/?rev=1136&view=rev Author: kongchinhua Date: 2010-09-27 18:02:57 +0000 (Mon, 27 Sep 2010) Log Message: ----------- Add ColorRegistry and ColorSchema for color management support - ColorSchema: Defined the scope of available colors of an application - ColorRegistry: assign color based on the given ColorSchema Reviewed by Patrick Modified Paths: -------------- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorRegistry.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorSchema.java Modified: trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF =================================================================== --- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-09-20 16:50:16 UTC (rev 1135) +++ trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-09-27 18:02:57 UTC (rev 1136) @@ -30,6 +30,7 @@ prefuse.util, prefuse.util.collections Export-Package: org.cishell.utilities, + org.cishell.utilities.color, org.cishell.utilities.database, org.cishell.utilities.dictionary, org.cishell.utilities.mutateParameter, Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorRegistry.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorRegistry.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorRegistry.java 2010-09-27 18:02:57 UTC (rev 1136) @@ -0,0 +1,83 @@ +package org.cishell.utilities.color; + +import java.awt.Color; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +/** + * ColorRegistry provide algorithm to assign color to + * specific item that defined in <E>. It use generic + * so that the implementation can be use for any type + * of item. + * + * To use ColorRegistry, create your own ColorSchema + * that hold the set of available colors and also + * a default color if the color is out + * @author kongch + * + */ +public class ColorRegistry<K> { + private int currentIndex; + private ColorSchema colorSchema; + private Map<K, Color> registedColors; + + public ColorRegistry(ColorSchema colorSchema) { + this.currentIndex = 0; + this.colorSchema = colorSchema; + this.registedColors = new HashMap<K, Color>(); + } + + /** + * Get all the keys that hold the specified color. + * @return Return the registered keys + */ + public Set<K> getKeySet() { + return registedColors.keySet(); + } + + /** + * Request a color for the specific key. + * @param key - key must be type of <E> + * @return Return color that assigned to the specific + * key. If all the colors are fully used, the default + * color denied by the ColorSchema will be returned + */ + public Color getColorOf(K key) { + if (registedColors.containsKey(key)) { + return registedColors.get(key); + } else { + return reserveColorFor(key); + } + } + + /** + * Clear all entry and reset to initial state. + */ + public void clear() { + registedColors.clear(); + } + + /* + * Request a color from the color schema + */ + private Color reserveColorFor(K key) { + + Color color = colorSchema.get(getNextIndex()); + registedColors.put(key, color); + + return color; + } + + /* + * Return next color index + */ + private int getNextIndex() { + int index = currentIndex; + + if (currentIndex < colorSchema.size()) { + currentIndex++; + } + return index; + } +} Property changes on: trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorRegistry.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorSchema.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorSchema.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorSchema.java 2010-09-27 18:02:57 UTC (rev 1136) @@ -0,0 +1,83 @@ +package org.cishell.utilities.color; + +import java.awt.Color; + +/** + * ColorSchema defined a set of colors to be used for + * an application. This allows the interchange of color + * schema set if needed. + * + * Schema contains a set of available color and a + * default color value. The defaultColor will be + * set to BLACK if it is not given + * @author kongch + * + */ +public class ColorSchema { + private int totalColors; + private Color[] colors; + private Color defaultColor; + + public ColorSchema(Color[] colors, Color defaultColor) { + setColors(colors); + setDefaultColor(defaultColor); + } + + /** + * The size of the color in schema which is not including + * defaultColor. + * @return Return the size of the color array + */ + public int size() { + return this.totalColors; + } + + /** + * Get color by index. + * @param index - index of the color in the schema + * @return Return a defaultColor if the given index is + * out of bound. Else return the color of the given + * index + */ + public Color get(int index) { + if (index > this.totalColors) { + return getDefaultColor(); + } + + return this.colors[index]; + } + + /** + * Get the set of available color. + * @return Always return the set of color under schema + */ + public Color[] getColors() { + return this.colors; + } + + /** + * Get the default color. + * @return Always return the default color + */ + public Color getDefaultColor() { + return this.defaultColor; + } + + private void setColors(Color[] colors) { + if (colors == null) { + this.colors = new Color[]{}; + } else { + this.colors = colors; + } + this.totalColors = this.colors.length; + } + + private void setDefaultColor(Color defaultColor) { + /* Assigned to BLACK if it is not given */ + if (defaultColor == null) { + this.defaultColor = Color.BLACK; + } else { + this.defaultColor = defaultColor; + } + } +} Property changes on: trunk/core/org.cishell.utilities/src/org/cishell/utilities/color/ColorSchema.java ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |