001: /*
002: * FieldHierachy.java
003: *
004: * Created on June 24, 2004, 10:48 AM
005: */
006:
007: package jimm.datavision.layout.excel;
008:
009: import java.util.*;
010: import jimm.datavision.*;
011: import jimm.datavision.field.Rectangle;
012:
013: /**
014: *
015: * @author dbeeler
016: */
017: public class FieldMap {
018:
019: public ArrayList reportRows;
020: public double[] colWidths = new double[256];
021: public boolean[] colAlloc = new boolean[256];
022: public static final int MAXCOL = 256;
023:
024: /** Creates a new instance of FieldHierachy */
025: public FieldMap() {
026: // 8.00 pels or 61 pixels
027: this (8.00);
028: }
029:
030: public FieldMap(double defaultColWidth) {
031: int i;
032: for (i = 0; i < MAXCOL; i++) {
033: colWidths[i] = defaultColWidth * 7.625;
034: colAlloc[i] = false;
035: }
036: reportRows = new ArrayList();
037: }
038:
039: public RowContainer createRow() {
040: RowContainer tmpRow = new RowContainer(this );
041: reportRows.add(tmpRow);
042: return tmpRow;
043: }
044:
045: /* Transverses all rows and columns and establishes the best column allotment */
046: public void realignColumns() {
047: int i;
048: Iterator it = reportRows.iterator();
049: while (it.hasNext()) {
050: RowContainer tmpRow = (RowContainer) it.next();
051: /* Iterate through the fields in this line and find acceptable column allocations */
052: Iterator rowit = tmpRow.reportFields.iterator();
053: while (rowit.hasNext()) {
054: PermField tmpField = (PermField) rowit.next();
055: Rectangle fbound = tmpField.getBounds();
056: /* Find a column that fits or has a position near it */
057:
058: for (i = 0; i < MAXCOL; i++) {
059: int curvalue = getColOffset(i);
060: if (fbound.x == curvalue) {
061: /* Found a perfect match. Lets make sure its allocated. */
062: if (!colAlloc[i])
063: colAlloc[i] = true;
064: break;
065: }
066: if (fbound.x < curvalue) {
067: /* We've passed our ideal column. Lets allocate this one and change
068: * its size to fit, if its not already allocated. If it is, we'll
069: * shift this column and the others to the right and insert ours here */
070: if (!colAlloc[i]) {
071: colAlloc[i] = true;
072: double prePosition = getColOffset(i - 1);
073: double postPosition = getColOffset(i + 1);
074: colWidths[i - 1] = fbound.x - prePosition;
075: colWidths[i] = postPosition - fbound.x;
076: break;
077: } else {
078: /* Column is allocated. Shift it to the right */
079: double prePosition = getColOffset(i - 1);
080: double postPosition = getColOffset(i);
081: shiftColsRight(i);
082: colAlloc[i] = true;
083: colWidths[i - 1] = fbound.x - prePosition;
084: colWidths[i] = postPosition - fbound.x;
085: break;
086: }
087: }
088: }
089: }
090: }
091: }
092:
093: /**
094: * Calculates the column offset given the stored array of widths
095: */
096: public int getColOffset(int colNum) {
097: int offset = 0;
098: int i = 0;
099: for (i = 0; i < colNum; i++)
100: offset += colWidths[i];
101: return offset;
102: }
103:
104: public void shiftColsRight(int startColNum) {
105: int i;
106: for (i = MAXCOL - 1; i > startColNum; --i) {
107: colWidths[i] = colWidths[i - 1];
108: colAlloc[i] = colAlloc[i - 1];
109: }
110:
111: }
112:
113: /**
114: * Routine to prepare for garbage collection
115: */
116: public void delete() {
117: reportRows.clear();
118:
119: }
120: }
|