1:
31:
32: package ;
33:
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50:
55: public class AutoTableLayoutController extends ElementLayoutController
56: {
57: public static final int HANDLING_HEADER = 0;
58: public static final int HANDLING_DATA = 1;
59: public static final int HANDLING_FOOTER = 2;
60:
61: private int currentColumn;
62: private int processingState;
63: private int columnCount;
64:
65: public AutoTableLayoutController()
66: {
67: }
68:
69: public void initialize(final Object node, final FlowController flowController, final LayoutController parent)
70: throws DataSourceException, ReportDataFactoryException, ReportProcessingException
71: {
72: super.initialize(node, flowController, parent);
73: final ReportDataRow reportDataRow =
74: flowController.getMasterRow().getReportDataRow();
75: this.columnCount = reportDataRow.getColumnCount();
76: }
77:
78: protected LayoutController processContent(final ReportTarget target)
79: throws DataSourceException, ReportProcessingException, ReportDataFactoryException
80: {
81: switch (processingState)
82: {
83: case AutoTableLayoutController.HANDLING_HEADER:
84: return processHeader(target);
85: case AutoTableLayoutController.HANDLING_FOOTER:
86: return processFooter(target);
87: case AutoTableLayoutController.HANDLING_DATA:
88: return processData(target);
89: default:
90: throw new ReportProcessingException("No such state.");
91: }
92:
93: }
94:
95: private LayoutController processData(final ReportTarget target)
96: throws ReportProcessingException, DataSourceException, ReportDataFactoryException
97: {
98:
99: final AutoTableElement node = (AutoTableElement) getElement();
100: if (node.getContentCount() == 0)
101: {
102: throw new ReportProcessingException
103: ("An Auto-Table must have at least one defined column.");
104: }
105:
106: if (currentColumn == 0)
107: {
108:
109: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
110: (AutoTableModule.AUTOTABLE_NAMESPACE, "data-row");
111: target.startElement(elementMap);
112: }
113:
114: if (currentColumn < columnCount)
115: {
116:
117: final FlowController flowController = getFlowController();
118: final ReportContext reportContext = flowController.getReportContext();
119: final LayoutControllerFactory layoutControllerFactory =
120: reportContext.getLayoutControllerFactory();
121:
122: final int idx = currentColumn % node.getContentCount();
123: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
124: return layoutControllerFactory.create
125: (flowController, node.getContentCell(idx), derived);
126: }
127:
128:
129: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
130: (AutoTableModule.AUTOTABLE_NAMESPACE, "data-row");
131: target.endElement(elementMap);
132:
133: final FlowController flowController =
134: getFlowController().performOperation(FlowControlOperation.ADVANCE);
135: final FlowController cfc = tryRepeatingCommit(flowController);
136: if (cfc != null)
137: {
138:
139:
140: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
141: derived.setFlowController(cfc);
142: derived.currentColumn = 0;
143: return derived;
144: }
145:
146:
147:
148: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
149: derived.currentColumn = 0;
150: derived.processingState = AutoTableLayoutController.HANDLING_FOOTER;
151: return derived;
152: }
153:
154: private LayoutController processFooter(final ReportTarget target)
155: throws ReportProcessingException, DataSourceException, ReportDataFactoryException
156: {
157: final AutoTableElement node = (AutoTableElement) getElement();
158: if (node.getFooterCount() == 0)
159: {
160: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
161: derived.currentColumn = 0;
162: derived.processingState = -1;
163: derived.setProcessingState(ElementLayoutController.FINISHING);
164: return derived;
165: }
166:
167: if (currentColumn == 0)
168: {
169:
170: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
171: (AutoTableModule.AUTOTABLE_NAMESPACE, "footer-row");
172: target.startElement(elementMap);
173: }
174:
175: if (currentColumn < columnCount)
176: {
177:
178: final FlowController flowController = getFlowController();
179: final ReportContext reportContext = flowController.getReportContext();
180: final LayoutControllerFactory layoutControllerFactory =
181: reportContext.getLayoutControllerFactory();
182:
183: final int idx = currentColumn % node.getFooterCount();
184: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
185: return layoutControllerFactory.create
186: (flowController, node.getFooterCell(idx), derived);
187: }
188:
189:
190: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
191: (AutoTableModule.AUTOTABLE_NAMESPACE, "footer-row");
192: target.endElement(elementMap);
193:
194: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
195: derived.currentColumn = 0;
196: derived.processingState = -1;
197: derived.setProcessingState(ElementLayoutController.FINISHING);
198: return derived;
199: }
200:
201: private LayoutController processHeader(final ReportTarget target)
202: throws ReportProcessingException, DataSourceException, ReportDataFactoryException
203: {
204: final AutoTableElement node = (AutoTableElement) getElement();
205: if (node.getHeaderCount() == 0)
206: {
207: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
208: derived.currentColumn = 0;
209: derived.processingState = AutoTableLayoutController.HANDLING_DATA;
210: return derived;
211: }
212:
213: if (currentColumn == 0)
214: {
215:
216: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
217: (AutoTableModule.AUTOTABLE_NAMESPACE, "header-row");
218: target.startElement(elementMap);
219: }
220:
221: if (currentColumn < columnCount)
222: {
223:
224: final FlowController flowController = getFlowController();
225: final ReportContext reportContext = flowController.getReportContext();
226: final LayoutControllerFactory layoutControllerFactory =
227: reportContext.getLayoutControllerFactory();
228:
229: final int idx = currentColumn % node.getHeaderCount();
230: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
231: return layoutControllerFactory.create
232: (flowController, node.getHeaderCell(idx), derived);
233: }
234:
235:
236: final AttributeMap elementMap = LayoutControllerUtil.createEmptyMap
237: (AutoTableModule.AUTOTABLE_NAMESPACE, "header-row");
238: target.endElement(elementMap);
239:
240: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
241: derived.currentColumn = 0;
242: derived.processingState = AutoTableLayoutController.HANDLING_DATA;
243: return derived;
244: }
245:
246:
253: public LayoutController join(final FlowController flowController)
254: {
255: final AutoTableLayoutController derived = (AutoTableLayoutController) clone();
256: derived.setFlowController(flowController);
257: derived.currentColumn += 1;
258: return derived;
259: }
260:
261: public int getCurrentColumn()
262: {
263: return currentColumn;
264: }
265: }