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.memento;
031:
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035:
036: import com.jeta.forms.store.AbstractJETAPersistable;
037: import com.jeta.forms.store.JETAObjectInput;
038: import com.jeta.forms.store.JETAObjectOutput;
039:
040: /**
041: * Defines a set of rows or columns that belong to the same group in a form. A
042: * <code>FormGroup</code> is used to store a single group of rows or columns.
043: *
044: * @author Jeff Tassin
045: */
046: public class FormGroup extends AbstractJETAPersistable {
047: static final long serialVersionUID = -9169090936449529487L;
048:
049: /**
050: * The version of this class.
051: */
052: public static final int VERSION = 1;
053:
054: /**
055: * An array of Integer objects that define the row or columns in a group on
056: * a form
057: */
058: private ArrayList m_indexes = new ArrayList();
059:
060: /**
061: * Creates an unitialized <code>FormGroup</code> instance.
062: */
063: public FormGroup() {
064: }
065:
066: /**
067: * Add a row or column to this group.
068: *
069: * @param index
070: * the 1-based row or column to add
071: */
072: void assign(int index) {
073: if (!contains(index)) {
074: m_indexes.add(new Integer(index));
075: }
076: }
077:
078: /**
079: * Returns true if this group contains the specified row or column.
080: *
081: * @return true if this group contains the 1-based row or column
082: */
083: public boolean contains(int index) {
084: Iterator iter = m_indexes.iterator();
085: while (iter.hasNext()) {
086: Integer ival = (Integer) iter.next();
087: if (ival.intValue() == index)
088: return true;
089: }
090: return false;
091: }
092:
093: /**
094: * Returns the group as an array of row or column indexes.
095: *
096: * @return the group as an array of row or column indexes.
097: */
098: public int[] toArray() {
099: int[] result = new int[m_indexes.size()];
100: int count = 0;
101: Iterator iter = m_indexes.iterator();
102: while (iter.hasNext()) {
103: Integer ival = (Integer) iter.next();
104: result[count] = ival.intValue();
105: count++;
106: }
107: return result;
108: }
109:
110: /**
111: * Remove a row or column from this group
112: *
113: * @param index
114: * the 1-based row or column to remove
115: */
116: void remove(int index) {
117: Iterator iter = m_indexes.iterator();
118: while (iter.hasNext()) {
119: Integer ival = (Integer) iter.next();
120: if (ival.intValue() == index)
121: iter.remove();
122: }
123: }
124:
125: /**
126: * Returns the number of rows or columns in this group.
127: *
128: * @return the size of this group
129: */
130: public int size() {
131: return m_indexes.size();
132: }
133:
134: /**
135: * Externalizable Implementation
136: */
137: public void read(JETAObjectInput in) throws ClassNotFoundException,
138: IOException {
139: int version = in.readVersion();
140: m_indexes = (ArrayList) in.readObject("indexes");
141: }
142:
143: /**
144: * Externalizable Implementation
145: */
146: public void write(JETAObjectOutput out) throws IOException {
147: out.writeVersion(VERSION);
148: out.writeObject("indexes", m_indexes);
149: }
150:
151: }
|