001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.web.template;
009:
010: //base classes
011: import java.util.ArrayList;
012:
013: //project specific classes
014: import org.jfolder.common.UnexpectedSystemException;
015:
016: //other classes
017:
018: public class ConsoleTable {
019:
020: private int width = 0;
021: private int leftMargin = 0;
022:
023: private boolean rowOpen = false;
024: private boolean tableOpen = false;
025:
026: private ArrayList templateRowWidths = null;
027: private ArrayList currentRowWidths = null;
028:
029: private ConsoleTable(int inWidth, int inLeftMargin) {
030:
031: this .width = inWidth;
032: this .leftMargin = inLeftMargin;
033:
034: this .rowOpen = false;
035: this .tableOpen = true;
036: }
037:
038: public final static ConsoleTable newInstance(int inWidth,
039: int inLeftMargin) {
040: //
041: ConsoleTable outValue = new ConsoleTable(inWidth, inLeftMargin);
042: //MiscHelper.println("startTable = " + outValue);
043: //(new Exception()).printStackTrace();
044: return outValue;
045: }
046:
047: public int getWidth() {
048: return this .width + this .leftMargin;
049: }
050:
051: //public int getRowWidth() {
052: // //TO DO: fix this
053: // return 0;
054: //}
055:
056: public void startRow() {
057:
058: //MiscHelper.println("startRow = " + this);
059:
060: if (this .rowOpen) {
061: throw new UnexpectedSystemException("Row already open");
062: }
063: this .rowOpen = true;
064: this .currentRowWidths = new ArrayList();
065:
066: if (leftMargin > 0) {
067: addCell(this .leftMargin);
068: }
069: }
070:
071: public void endRow() {
072:
073: //MiscHelper.println("endRow = " + this);
074:
075: if (!this .rowOpen) {
076: throw new UnexpectedSystemException("Row already closed");
077: }
078: this .rowOpen = false;
079:
080: if (this .templateRowWidths != null) {
081: if (this .templateRowWidths.size() != this .currentRowWidths
082: .size()) {
083: throw new UnexpectedSystemException(
084: "Row does not have the same cell count as first row");
085: } else {
086: boolean cellWidthsEqual = true;
087:
088: for (int i = 0; i < this .templateRowWidths.size(); i++) {
089: boolean result = this .templateRowWidths.get(i)
090: .equals(this .currentRowWidths.get(i));
091: cellWidthsEqual = cellWidthsEqual && result;
092: }
093:
094: if (!cellWidthsEqual) {
095: throw new UnexpectedSystemException(
096: "Cell widths not equal to cells in first row");
097: }
098: }
099: } else {
100: int rowWidth = 0;
101: for (int i = 0; i < this .currentRowWidths.size(); i++) {
102: //MiscHelper.println(i + ") = " + this.currentRowWidths.get(i));
103: Integer nextRowWidth = (Integer) (this .currentRowWidths
104: .get(i));
105: rowWidth = rowWidth + nextRowWidth.intValue();
106: }
107: if (rowWidth == this .getWidth()) {
108: this .templateRowWidths = this .currentRowWidths;
109: } else {
110: //MiscHelper.println("this.currentRowWidths = "
111: // + this.currentRowWidths);
112: //MiscHelper.println("this.getWidth() = "
113: // + this.getWidth());
114: //MiscHelper.println("this.width = "
115: // + this.width);
116: //MiscHelper.println("this.leftMargin = "
117: // + this.leftMargin);
118: throw new UnexpectedSystemException(
119: "First row does not have same width as table (row = "
120: + rowWidth + ", table = "
121: + this .getWidth() + ")");
122: }
123: }
124: }
125:
126: public void endTable() {
127:
128: //MiscHelper.println("endTable = " + this);
129:
130: if (!this .tableOpen) {
131: throw new UnexpectedSystemException(
132: "Table is already closed");
133: } else if (this .rowOpen) {
134: throw new UnexpectedSystemException("Row is still opened");
135: }
136:
137: this .tableOpen = false;
138: }
139:
140: public void addCell(int inWidth) {
141: if (!this .rowOpen) {
142: throw new UnexpectedSystemException("Row is not opened");
143: }
144: this .currentRowWidths.add(new Integer(inWidth));
145: }
146: }
|