001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: /////////////////////////
023: //$Archive: /JADE/SourceCode/com/salmonllc/util/Vector2D.java $
024: //$Author: Dan $
025: //$Revision: 15 $
026: //$Modtime: 10/30/02 2:59p $
027: /////////////////////////
028:
029: import java.util.*;
030:
031: /**
032: * This class is a two dimensional Vector. It contains rows and columns that can expand as necessary.
033: */
034: public class Vector2D implements java.io.Serializable {
035: Vector _rows = new Vector();
036: int _columnCapacity = 5;
037: int _columnCount;
038: int _columnSize;
039: int _rowSize;
040:
041: /**
042: * Constructs an empty 2 Dimensional vector.
043: */
044: public Vector2D() {
045: super ();
046: }
047:
048: /**
049: * This method adds a specified number of columns to the vector
050: * @param The Number of colums to add.
051: */
052: public void addColumns(int noColumns) {
053: if ((noColumns + _columnCount) >= _columnCapacity) {
054: _columnCapacity = noColumns + _columnCount
055: + _columnCapacity;
056: }
057: for (int i = 0; i < _rows.size(); i++) {
058: Object[] tgt = new Object[_columnCapacity];
059: Object[] src = (Object[]) _rows.elementAt(i);
060: System.arraycopy(src, 0, tgt, 0, _columnCount);
061: _rows.setElementAt(tgt, i);
062: }
063: _columnCount += noColumns;
064: }
065:
066: /**
067: * This method adds a specified number of rows to the vector
068: * @param The Number of rows to add.
069: */
070:
071: public void addRows(int noRows) {
072: for (int i = 0; i < noRows; i++) {
073: Object[] o = new Object[_columnCapacity];
074: _rows.addElement(o);
075: }
076: }
077:
078: /**
079: * This method returns the object at row and column..
080: */
081: public Object elementAt(int index) {
082: if (index < 0 || index >= size())
083: return null;
084:
085: int row = index / _columnCount;
086: int column = index % _columnCount;
087:
088: Object[] o = (Object[]) _rows.elementAt(row);
089: return o[column];
090:
091: }
092:
093: /**
094: * This method returns the object at row and column..
095: */
096: public Object elementAt(int row, int column) {
097: if (row < 0 || row >= _rows.size())
098: return null;
099: else if (column < 0 || column >= _columnCount)
100: return null;
101:
102: Object[] o = (Object[]) _rows.elementAt(row);
103: return o[column];
104:
105: }
106:
107: public void exportData(boolean includeHeaders, java.io.PrintWriter p) {
108: StringBuffer work = new StringBuffer();
109: Object data = null;
110: //
111: work.append("<TABLE BORDER = \"1\">");
112: // include headers
113: if (includeHeaders) {
114: work.append("<TR>");
115: work.append("<TH>");
116: work.append("ROW_NUM");
117: work.append("</TH>");
118: for (int i = 0; i < getColumnCount(); i++) {
119: work.append("<TH>");
120: work.append("COL_" + i);
121: work.append("</TH>");
122: }
123: work.append("</TR>\n");
124: }
125: for (int rows = 0; rows < getRowCount(); rows++) {
126: work.append("<TR>");
127: work.append("<TD>ROW_" + rows);
128: work.append("</TD>");
129: for (int cols = 0; cols < getColumnCount(); cols++) {
130: work.append("<TD>");
131: data = elementAt(rows, cols);
132: if (data == null) {
133: work.append(" ");
134: } else {
135: work.append(data.toString());
136: }
137: work.append("</TD>");
138: }
139: work.append("</TR>\n");
140: }
141: work.append("</TABLE>");
142: p.println(work.toString());
143: p.flush();
144: }
145:
146: /**
147: * This method returns the number of columns in the 2D Vector
148: */
149: public int getColumnCount() {
150: return _columnCount;
151: }
152:
153: /**
154: * This method returns the number of columns that are occupied with data in the 2D Vector
155: */
156: public int getColumnSize() {
157: return _columnSize + 1;
158: }
159:
160: /**
161: * This method returns the number of columns in the 2DVector
162: */
163: public int getRowCount() {
164: return _rows.size();
165: }
166:
167: /**
168: * This method returns the number of rows that are occupied with data in the 2D Vector
169: */
170: public int getRowSize() {
171: return _rowSize + 1;
172: }
173:
174: /**
175: * This method returns the index of the item at row and column or -1 if the element doesn't exist.
176: */
177: public int indexAt(int row, int column) {
178: if (row < 0 || row >= _rows.size())
179: return -1;
180: else if (column < 0 || column >= _columnCount)
181: return -1;
182:
183: return (row * _columnCount) + column;
184:
185: }
186:
187: /**
188: * This method inserts a row immediately before the specified row
189: */
190: public void insertRow(int row) {
191: if (row < 0)
192: return;
193:
194: Object[] o = new Object[_columnCapacity];
195:
196: if (row > _rows.size())
197: _rows.addElement(o);
198: else
199: _rows.insertElementAt(o, row);
200:
201: }
202:
203: /**
204: * This method inserts a row immediately before the specified row
205: */
206: public void removeAll() {
207: _rows.removeAllElements();
208: _columnCapacity = 5;
209: _columnCount = 0;
210: _columnSize = 0;
211: _rowSize = 0;
212: }
213:
214: /**
215: * This method removes a row from the 2DVector
216: * @param row The row to remove.
217: */
218: public void removeRow(int row) {
219: if (row < 0 || row > _rows.size())
220: return;
221: _rows.removeElementAt(row);
222: _rowSize = _rowSize - 1;
223: }
224:
225: /**
226: * This method sets the value of a cell in the 2D Vector
227: * @param row The row position
228: * @param column The row position
229: * @param in The object to set.
230: */
231: public void setElementAt(int row, int column, Object in) {
232: if (row < 0 || row >= _rows.size())
233: return;
234: else if (column < 0 || column >= _columnCount)
235: return;
236: Object[] o = (Object[]) _rows.elementAt(row);
237: o[column] = in;
238:
239: // this is to get only the occupied rows and columns
240: if (_rowSize < row)
241: _rowSize = row;
242: if (_columnSize < column)
243: _columnSize = column;
244: }
245:
246: /**
247: * This method sets the value of a cell in the 2D Vector
248: * @param in The object to set.
249: * @param index The position to put it at
250: */
251: public void setElementAt(int index, Object in) {
252:
253: if (index < 0 || index >= size())
254: return;
255:
256: int row = index / _columnCount;
257: int column = index % _columnCount;
258:
259: Object[] o = (Object[]) _rows.elementAt(row);
260: o[column] = in;
261:
262: // this is to get only the occupied rows and columns
263: if (_rowSize < row)
264: _rowSize = row;
265: if (_columnSize < column)
266: _columnSize = column;
267:
268: }
269:
270: /**
271: * This method was created in VisualAge.
272: * @return int
273: */
274: public int size() {
275: return (_rows.size() * _columnCount);
276: }
277:
278: /**
279: * Returns a string representation of the 2D vector.
280: */
281: public String toString() {
282:
283: StringBuffer work = new StringBuffer();
284: Object data = null;
285: boolean commaFlag = false;
286: for (int rows = 0; rows < getRowCount(); rows++) {
287: work.append("[");
288: for (int cols = 0; cols < getColumnCount(); cols++) {
289: if (commaFlag == true) {
290: work.append(",");
291: }
292:
293: data = elementAt(rows, cols);
294: if (data == null) {
295: work.append("NULL");
296: } else {
297: work.append(data.toString());
298: }
299: commaFlag = true;
300: }
301: work.append("]\n");
302: commaFlag = false;
303: }
304: return work.toString();
305: }
306: }
|