001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties;
031:
032: import java.awt.Color;
033: import java.io.IOException;
034:
035: import com.jeta.forms.store.AbstractJETAPersistable;
036: import com.jeta.forms.store.JETAObjectInput;
037: import com.jeta.forms.store.JETAObjectOutput;
038:
039: /**
040: * Used for storing colors. We don't serialize java.awt.Color objects directly
041: * because on OS X there are Apple specific color objects that can be saved
042: * indirectly which would cause problems on other platforms. So, all Color
043: * values must be stored in ColorHolder objects.
044: *
045: * @author Jeff Tassin
046: */
047: public class ColorHolder extends AbstractJETAPersistable {
048: static final long serialVersionUID = -4953997689182177487L;
049:
050: /**
051: * The version for this class.
052: */
053: public static final int VERSION = 1;
054:
055: private int m_red;
056: private int m_green;
057: private int m_blue;
058:
059: private transient Color m_color;
060:
061: /**
062: * Creates a <code>ColorHolder</code> instance that defaults to black.
063: */
064: public ColorHolder() {
065:
066: }
067:
068: /**
069: * Creates a <code>ColorHolder</code> instance with the specified color.
070: *
071: * @param c
072: * the color
073: */
074: public ColorHolder(Color c) {
075: if (c != null) {
076: m_red = c.getRed();
077: m_green = c.getGreen();
078: m_blue = c.getBlue();
079: }
080: }
081:
082: public boolean equals(Object obj) {
083: if (obj instanceof ColorHolder) {
084: ColorHolder ch = (ColorHolder) obj;
085: return (m_red == ch.m_red && m_green == ch.m_green && m_blue == ch.m_blue);
086: } else
087: return false;
088: }
089:
090: /**
091: * Returns the color contained by this holder.
092: *
093: * @return the color
094: */
095: public Color getColor() {
096: if (m_color == null) {
097: m_color = new Color(m_red, m_green, m_blue);
098: }
099: return m_color;
100: }
101:
102: /**
103: * Returns the red component in the range 0-255 in the default RGB space.
104: *
105: * @return the red component.
106: */
107: public int getRed() {
108: return m_red;
109: }
110:
111: /**
112: * Returns the green component in the range 0-255 in the default RGB space.
113: *
114: * @return the green component.
115: */
116: public int getGreen() {
117: return m_green;
118: }
119:
120: /**
121: * Returns the blue component in the range 0-255 in the default RGB space.
122: *
123: * @return the blue component.
124: */
125: public int getBlue() {
126: return m_blue;
127: }
128:
129: /**
130: * JETAPersistable Implementation
131: */
132: public void read(JETAObjectInput in) throws ClassNotFoundException,
133: IOException {
134: int version = in.readVersion();
135: m_red = in.readInt("red");
136: m_green = in.readInt("green");
137: m_blue = in.readInt("blue");
138: }
139:
140: /**
141: * JETAPersistable Implementation
142: */
143: public void write(JETAObjectOutput out) throws IOException {
144: out.writeVersion(VERSION);
145: out.writeInt("red", m_red);
146: out.writeInt("green", m_green);
147: out.writeInt("blue", m_blue);
148: }
149:
150: }
|