001: /*
002: * Table.java
003: *
004: * Version: $Revision: 1.6 $
005: *
006: * Date: $Date: 2006/03/13 17:19:39 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.wing.element;
042:
043: /**
044: * A class representing a table.
045: *
046: * The table element is a container for information presented in tabular format.
047: * It consists of a set of row elements and an optional header.
048: *
049: * @author Scott Phillips
050: */
051:
052: import java.util.ArrayList;
053: import java.util.List;
054:
055: import org.dspace.app.xmlui.wing.AttributeMap;
056: import org.dspace.app.xmlui.wing.Message;
057: import org.dspace.app.xmlui.wing.WingContext;
058: import org.dspace.app.xmlui.wing.WingException;
059: import org.xml.sax.ContentHandler;
060: import org.xml.sax.SAXException;
061: import org.xml.sax.ext.LexicalHandler;
062: import org.xml.sax.helpers.NamespaceSupport;
063:
064: public class Table extends AbstractWingElement implements
065: StructuralElement {
066: /** The name of the table element */
067: public static final String E_TABLE = "table";
068:
069: /** The name of the rows attribute */
070: public static final String A_ROWS = "rows";
071:
072: /** The name of the cols attribute */
073: public static final String A_COLS = "cols";
074:
075: /** The name assigned to this table */
076: private String name;
077:
078: /** Special rendering instructions for this table */
079: private String rend;
080:
081: /** The number of rows in the table */
082: private int rows;
083:
084: /** The number of cols in the table */
085: private int cols;
086:
087: /** The table's head */
088: private Head head;
089:
090: /** the rows contained in the table */
091: private List<AbstractWingElement> contents = new ArrayList<AbstractWingElement>();
092:
093: /**
094: * Construct a new row.
095: *
096: * @param context
097: * (Required) The context this element is contained in, such as
098: * where to route SAX events and what i18n catalogue to use.
099: *
100: * @param name
101: * (Required) a local identifier used to differentiate the
102: * element from its siblings.
103: *
104: * @param rows
105: * (Required) the number of rows in the table.
106: * @param cols
107: * (Required) the number of columns in the table.
108: * @param rend
109: * (May be null) a rendering hint used to override the default
110: * display of the element.
111: */
112: protected Table(WingContext context, String name, int rows,
113: int cols, String rend) throws WingException {
114: super (context);
115: require(name,
116: "The 'name' parameter is required for all tables.");
117: greater(rows, 0,
118: "The 'rows' parameter must be grater than zero.");
119: greater(cols, 0,
120: "The 'cols' parameter must be grater than zero.");
121:
122: this .name = name;
123: this .rows = rows;
124: this .cols = cols;
125: this .rend = rend;
126: }
127:
128: /**
129: * Set the head element which is the label associated with this table.
130: *
131: * @param characters
132: * (May be null) Unprocessed characters to be included
133: */
134: public Head setHead() throws WingException {
135: this .head = new Head(context, null);
136: return head;
137:
138: }
139:
140: /**
141: * Set the head element which is the label associated with this table.
142: *
143: * @param characters
144: * (May be null) Unprocessed characters to be included
145: */
146: public void setHead(String characters) throws WingException {
147: Head head = this .setHead();
148: head.addContent(characters);
149:
150: }
151:
152: /**
153: * Set the head element which is the label associated with this table.
154: *
155: * @param message
156: * (Required) A key into the i18n catalogue for translation into
157: * the user's preferred language.
158: */
159: public void setHead(Message message) throws WingException {
160: Head head = this .setHead();
161: head.addContent(message);
162: }
163:
164: /**
165: * Add a new row to the table. The row element is contained inside a table
166: * and serves as a container of cell elements. A required 'role' attribute
167: * determines how the row and its cells are used.
168: *
169: *
170: * @param name
171: * (May be null) a local identifier used to differentiate the
172: * element from its siblings.
173: * @param role
174: * (May be null) determine what kind of information the row
175: * carries, either header or data. See row.ROLES
176: * @param rend
177: * (May be null) a rendering hint used to override the default
178: * display of the element.
179: *
180: * @return a new table row
181: */
182: public Row addRow(String name, String role, String rend)
183: throws WingException {
184: Row row = new Row(context, name, role, rend);
185: contents.add(row);
186: return row;
187: }
188:
189: /**
190: * Add a new row to the table. The row element is contained inside a table
191: * and serves as a container of cell elements. A required 'role' attribute
192: * determines how the row and its cells are used.
193: *
194: * @param role
195: * (May be null) determines what kind of information the row
196: * carries, either header or data. See row.ROLES
197: *
198: * @return a new table row
199: */
200: public Row addRow(String role) throws WingException {
201: return this .addRow(null, role, null);
202: }
203:
204: /**
205: * Add a new row to the table. The row element is contained inside a table
206: * and serves as a container of cell elements.
207: *
208: * @return a new table row
209: */
210: public Row addRow() throws WingException {
211: return this .addRow(null, null, null);
212: }
213:
214: /**
215: * Translate this element and all contained elements into SAX events. The
216: * events should be routed to the contentHandler found in the WingContext.
217: *
218: * @param contentHandler
219: * (Required) The registered contentHandler where SAX events
220: * should be routed too.
221: * @param lexicalHandler
222: * (Required) The registered lexicalHandler where lexical
223: * events (such as CDATA, DTD, etc) should be routed too.
224: * @param namespaces
225: * (Required) SAX Helper class to keep track of namespaces able
226: * to determine the correct prefix for a given namespace URI.
227: */
228:
229: public void toSAX(ContentHandler contentHandler,
230: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
231: throws SAXException {
232: AttributeMap attributes = new AttributeMap();
233: attributes.put(A_NAME, name);
234: attributes.put(A_ID, context.generateID(E_TABLE, name));
235: attributes.put(A_ROWS, rows);
236: attributes.put(A_COLS, cols);
237: if (rend != null)
238: attributes.put(A_RENDER, rend);
239:
240: startElement(contentHandler, namespaces, E_TABLE, attributes);
241: if (head != null)
242: head.toSAX(contentHandler, lexicalHandler, namespaces);
243: for (AbstractWingElement content : contents)
244: content.toSAX(contentHandler, lexicalHandler, namespaces);
245: endElement(contentHandler, namespaces, E_TABLE);
246: }
247:
248: /**
249: * dispose
250: */
251: public void dispose() {
252: if (head != null)
253: head.dispose();
254: for (AbstractWingElement content : contents)
255: content.dispose();
256:
257: head = null;
258: contents.clear();
259: contents = null;
260: super.dispose();
261: }
262:
263: }
|