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.Component;
033: import java.io.IOException;
034: import java.util.Iterator;
035: import java.util.LinkedList;
036:
037: import javax.swing.BorderFactory;
038: import javax.swing.border.Border;
039:
040: import com.jeta.forms.gui.beans.JETABean;
041: import com.jeta.forms.store.JETAObjectInput;
042: import com.jeta.forms.store.JETAObjectOutput;
043:
044: /**
045: * A <code>CompoundBorderProperty</code> is a composite of BorderProperty
046: * instances. It is used to define a compound border.
047: *
048: * @author Jeff Tassin
049: */
050: public class CompoundBorderProperty extends BorderProperty {
051: static final long serialVersionUID = -8513525079430361312L;
052:
053: /**
054: * The version of this class
055: */
056: public static final int VERSION = 1;
057:
058: /**
059: * A list of borders that make up this compound border. The order is from
060: * outer most ---> inner most.
061: */
062: private LinkedList m_borders = new LinkedList();
063:
064: /**
065: * Creates an uninitialized <code>CompoundBorderProperty</code> instance.
066: */
067: public CompoundBorderProperty() {
068:
069: }
070:
071: /**
072: * Creates a <code>CompoundBorderProperty</code> instance with the
073: * specified border.
074: */
075: public CompoundBorderProperty(BorderProperty prop) {
076: addBorder(prop);
077: }
078:
079: /**
080: * Adds a border to the end of the border list.
081: */
082: public void addBorder(BorderProperty bp) {
083: if (bp != null) {
084: m_borders.addLast(bp);
085: }
086: }
087:
088: /**
089: * Creates a Swing Border composed of all the borders specified in this
090: * object.
091: */
092: public Border createBorder(Component comp) {
093: if (m_borders.size() == 0) {
094: return null;
095: } else if (m_borders.size() == 1) {
096: BorderProperty bp = (BorderProperty) m_borders.getFirst();
097: Border b = bp.createBorder(comp);
098: return b;
099: } else {
100: Border last_border = null;
101: Iterator iter = m_borders.iterator();
102: while (iter.hasNext()) {
103: BorderProperty bp = (BorderProperty) iter.next();
104: Border b = bp.createBorder(comp);
105: if (last_border == null) {
106: last_border = b;
107: } else {
108: last_border = BorderFactory.createCompoundBorder(
109: last_border, b);
110: }
111: }
112: return last_border;
113: }
114: }
115:
116: /**
117: * Object equals implementation.
118: */
119: public boolean equals(Object object) {
120: if (object instanceof CompoundBorderProperty) {
121: CompoundBorderProperty bp = (CompoundBorderProperty) object;
122: boolean bresult = super .equals(object);
123: return (bresult && isEqual(m_borders, bp.m_borders));
124: }
125: return false;
126: }
127:
128: /**
129: * Returns the border at the given index.
130: */
131: public BorderProperty getBorder(int index)
132: throws IndexOutOfBoundsException {
133: return (BorderProperty) m_borders.get(index);
134: }
135:
136: /**
137: * @return an iterator to the borders (BorderProperty objects)in this
138: * compound border
139: */
140: public Iterator iterator() {
141: return m_borders.iterator();
142: }
143:
144: /**
145: * Return the number of borders defined by this compound border.
146: */
147: public int size() {
148: return m_borders.size();
149: }
150:
151: /**
152: * Sets this property to that of another property.
153: */
154: public void setValue(Object prop) {
155: if (prop instanceof CompoundBorderProperty) {
156: CompoundBorderProperty cb = (CompoundBorderProperty) prop;
157: m_borders.clear();
158: m_borders.addAll(cb.m_borders);
159: }
160: }
161:
162: /**
163: * Sets the border on the bean
164: */
165: public void updateBean(JETABean jbean) {
166: if (m_borders != null)
167: super .updateBean(jbean);
168: }
169:
170: /**
171: * JETAPersistable Implementation
172: */
173: public void read(JETAObjectInput in) throws ClassNotFoundException,
174: IOException {
175: super .read(in.getSuperClassInput());
176: int version = in.readVersion();
177: m_borders = (LinkedList) in.readObject("borders");
178: }
179:
180: /**
181: * JETAPersistable Implementation
182: */
183: public void write(JETAObjectOutput out) throws IOException {
184: super .write(out.getSuperClassOutput(BorderProperty.class));
185: out.writeVersion(VERSION);
186: out.writeObject("borders", m_borders);
187: }
188:
189: public String toString() {
190: if (m_borders.size() == 0) {
191: return "NO BORDER";
192: } else if (m_borders.size() == 1) {
193: BorderProperty bp = (BorderProperty) m_borders.getFirst();
194: return bp.toString();
195: } else {
196: return "COMPOUND";
197: }
198: }
199:
200: public int getSize() {
201: return m_borders.size();
202: }
203: }
|