001: /**********************************************************************************
002:
003: Feedzeo!
004: A free and open source RSS/Atom/RDF feed aggregator
005:
006: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
021:
022: ************************************************************************************/package layout;
023:
024: import java.util.*;
025:
026: import util.*;
027: import ui.*;
028:
029: /**
030: *
031: * @author Anand Rao
032: */
033: public class TableLayout {
034:
035: private String Name; // table object name
036: private boolean IsTableList; // if all the cell elements of this table
037: // have the same datatype and form a list of items
038: // then IsTableList is true
039: private int NumRows, NumCols;
040: private CellElement[][] cellArray;
041:
042: private BitSet[] RowStatus; /* bitmap containing status of each row in the table;
043: * Each RowStatus[i] reflects the status of one row;
044: * If a row has a cell with non-empty data,
045: * then the bit corresponding to that cell is set;
046: * If all cells of a particular row are empty, then
047: * all the bits corresponding to those cells are clear;
048: * i.e RowStatus[i] is empty
049: */
050:
051: private BitSet copyBitSet(BitSet s) {
052: return (BitSet) s.clone();
053: }
054:
055: /*
056: * serves to do common processing between normal constructor
057: * and copy constructor
058: */
059: private void CommonConstructor(String Tablename, int nRows,
060: int nCols, TableLayout t) {
061:
062: //System.out.println(" Rows"+NumRows+" Cols"+NumCols);
063: if (t == null) {
064: Name = Tablename;
065: NumRows = nRows;
066: NumCols = nCols;
067: IsTableList = false;
068: cellArray = new CellElement[NumRows][NumCols];
069: RowStatus = new BitSet[NumRows];
070: for (int i = 0; i < NumRows; i++)
071: RowStatus[i] = new BitSet();
072: } else {
073: Name = new String(t.Name);
074: NumRows = t.NumRows;
075: NumCols = t.NumCols;
076: IsTableList = t.IsTableList;
077: cellArray = new CellElement[NumRows][NumCols];
078: for (int i = 0; i < NumRows; i++)
079: for (int j = 0; j < NumCols; j++) {
080: CellElement c = new CellElement(t.cellArray[i][j]);
081: cellArray[i][j] = c;
082: }
083: RowStatus = new BitSet[NumRows];
084: for (int i = 0; i < NumRows; i++)
085: RowStatus[i] = copyBitSet(t.RowStatus[i]);
086: }
087: }
088:
089: /** Creates a new instance of TableLayout */
090: public TableLayout(String Tablename, int NumRows, int NumCols) {
091: CommonConstructor(Tablename, NumRows, NumCols, null);
092: /*
093: Name = Tablename;
094: cellArray = new CellElement[NumRows][NumCols];
095: this.NumRows = NumRows;
096: this.NumCols = NumCols;
097: IsTableList = false;
098: RowStatus = new BitSet[NumRows];
099: for (int i = 0; i < NumRows; i++)
100: RowStatus[i] = new BitSet();
101: */
102: }
103:
104: // copy constructor
105: public TableLayout(TableLayout t) {
106: //System.out.println("COPY Table "+t.Name);
107: CommonConstructor("", 0, 0, t);
108: /*
109: Name = new String(t.Name);
110: NumRows = t.NumRows;
111: NumCols = t.NumCols;
112: //System.out.println(" Rows"+NumRows+" Cols"+NumCols);
113: IsTableList = t.IsTableList;
114: cellArray = new CellElement[NumRows][NumCols];
115: for (int i = 0; i < NumRows; i++)
116: for (int j = 0; j < NumCols; j++) {
117: CellElement c = new CellElement(t.cellArray[i][j]);
118: cellArray[i][j] = c;
119: }
120: */
121: }
122:
123: public void addCell(int row, int col, String DataTypename,
124: String Data, String url) {
125: if ((row >= NumRows) || (col >= NumCols))
126: return;
127: cellArray[row][col] = new CellElement(DataTypename, Data, url);
128: }
129:
130: public CellElement getCell(int row, int col) {
131: if ((row >= NumRows) || (col >= NumCols))
132: return null;
133: return cellArray[row][col];
134: }
135:
136: public void setCellStatus(int row, int col, boolean status) {
137: BitSet s = RowStatus[row];
138: s.set(col, status);
139: }
140:
141: private boolean IsRowEmpty(int row) {
142: BitSet s = RowStatus[row];
143: return s.isEmpty();
144: }
145:
146: public boolean IsTableEmpty() {
147: for (int i = 0; i < NumRows; i++) {
148: if (!IsRowEmpty(i))
149: return false;
150: }
151: return true;
152: }
153:
154: private boolean IsCellEmpty(int row, int col) {
155: BitSet s = RowStatus[row];
156: return !s.get(col);
157: }
158:
159: public void setTableEmpty() {
160: // clear the RowStatus bitset
161: for (int i = 0; i < NumRows; i++) {
162: BitSet s = RowStatus[i];
163: s.clear();
164: }
165: }
166:
167: public int getNumRows() {
168: return NumRows;
169: }
170:
171: public int getNumCols() {
172: return NumCols;
173: }
174:
175: public String getName() {
176: return Name;
177: }
178:
179: public String toHTML() {
180:
181: if (IsTableEmpty())
182: return "";
183:
184: HTMLTag table = new HTMLTag(TagName.TABLE_TAG);
185: table.addAttribute("class", Name);
186:
187: for (int i = 0; i < NumRows; i++) {
188: if (IsRowEmpty(i))
189: continue;
190:
191: HTMLTag trow = new HTMLTag(TagName.TABLE_ROW_TAG);
192: for (int j = 0; j < NumCols; j++) {
193: if (IsCellEmpty(i, j))
194: continue;
195: trow.add(cellArray[i][j].toHTML());
196: }
197: table.add(trow);
198: }
199: return table.toString();
200: }
201:
202: public boolean IsTableListType() {
203: return IsTableList;
204: }
205:
206: public void setTableListType(boolean value) {
207: IsTableList = value;
208: }
209: }
|