001: package net.xoetrope.swing;
002:
003: import java.awt.Dimension;
004: import java.awt.ItemSelectable;
005: import java.awt.event.ItemListener;
006:
007: import net.xoetrope.xui.XAttributedComponent;
008: import net.xoetrope.xui.XModelHolder;
009: import net.xoetrope.xui.data.XModel;
010: import net.xoetrope.xui.style.XStyleComponent;
011: import java.awt.Container;
012: import java.awt.event.MouseListener;
013: import java.awt.event.MouseEvent;
014: import net.xoetrope.xui.XHashCode;
015: import net.xoetrope.xui.data.XRowSelector;
016:
017: /**
018: * <p>Provides a simple read-only tables/grid component.</p>
019: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
020: * License: see license.txt
021: * $Revision: 1.17 $
022: */
023: public class XTable extends XScrollPane implements
024: XAttributedComponent, XModelHolder, ItemSelectable,
025: XStyleComponent, XHashCode, XRowSelector {
026: private XTableRenderer content;
027: private XModel model;
028:
029: public XTable() {
030: super ();
031: content = new XTableRenderer(this );
032: setViewportView(content);
033: }
034:
035: /**
036: * Set the XModel which we will be generating the table from
037: * @param xmodel The XModel of data
038: */
039: public void setModel(XModel xmodel) {
040: model = xmodel;
041: if (model != null) {
042: model.get();
043: content.setContent(xmodel);
044: }
045:
046: checkSize();
047: }
048:
049: /**
050: * Get the underlying model.
051: * @return the model
052: */
053: public XModel getModel() {
054: return model;
055: }
056:
057: /**
058: * Recalculate and check the size of the content
059: */
060: public void checkSize() {
061: Dimension d = content.calcSize();
062: content.setPreferredSize(d);
063: content.revalidate();
064:
065: repaint();
066: }
067:
068: /**
069: * Get the container that holds the components
070: * @return the viewport pane
071: */
072: public Container getComponentPanel() {
073: return getViewport();
074: }
075:
076: /**
077: * Set the type of component for a column
078: * @param col the field index
079: * @param className the class name
080: */
081: public void setComponentAt(int col, String className) {
082: content.setComponentAt(col, className);
083: }
084:
085: /**
086: * Set the model's name
087: * @return the name
088: */
089: public String getContent() {
090: return model.getId();
091: }
092:
093: /**
094: * Creates the scroll pane's peer and sizes the child element.
095: */
096: public void addNotify() {
097: super .addNotify();
098: if (content != null) {
099: Dimension d = content.calcSize();
100: content.setBounds(0, 0, Math.max(
101: (this .getSize().width - 20), d.width), d.height);
102: doLayout();
103: }
104: }
105:
106: /**
107: * Set the general style of the XTable
108: * @param style XStyle
109: */
110: public void setStyle(String style) {
111: content.setStyle(style);
112: }
113:
114: /**
115: * Set the style of the header data
116: * @param style XStyle
117: */
118: public void setHeaderStyle(String style) {
119: content.setHeaderStyle(style);
120: }
121:
122: /**
123: * Set the style of the selected row
124: * @param style XStyle
125: */
126: public void setSelectedStyle(String style) {
127: content.setSelectedStyle(style);
128: }
129:
130: /**
131: * Set the style of the border
132: * @param style XStyle
133: */
134: public void setBorderStyle(String style) {
135: content.setBorderStyle(style);
136: }
137:
138: /**
139: * Check the if the table is interactive
140: * @return true if the table supports user interaction
141: */
142: public boolean isInteractiveTable() {
143: return content.isInteractiveTable();
144: }
145:
146: /**
147: * Set the user interaction state
148: * @param state true for an user interactive table.
149: */
150: public void setInteractiveTable(boolean state) {
151: content.setInteractiveTable(state);
152: }
153:
154: /**
155: * Sets the indexof the selected row
156: * @param idx the new selected row
157: */
158: public void setSelectedRow(int idx) {
159: content.setSelectedRow(idx);
160: }
161:
162: /**
163: * Get the index of the selected row
164: * @return the index of the selected row
165: */
166: public int getSelectedRow() {
167: return content.getSelectedRow();
168: }
169:
170: /**
171: * Move to the first row
172: */
173: public void first() {
174: content.setSelectedRow(0);
175: }
176:
177: /**
178: * Move to the previous row
179: */
180: public void prev() {
181: content.setSelectedRow(content.getSelectedRow() - 1);
182: }
183:
184: /**
185: * Move to the next row
186: */
187: public void next() {
188: content.setSelectedRow(content.getSelectedRow() + 1);
189: }
190:
191: /**
192: * Move to the last row
193: */
194: public void last() {
195: content.setSelectedRow(model.getNumChildren() - 1);
196: }
197:
198: /**
199: * Tie the model selection to this table's selection
200: * @param doUpdate true to tie the selections together, false to ignore
201: */
202: public void setUpdateModelSelection(boolean doUpdate) {
203: content.setUpdateModelSelection(doUpdate);
204: }
205:
206: /**
207: * Sets the bounds for this component
208: * @param x
209: * @param y
210: * @param w
211: * @param h
212: */
213: public void setBounds(int x, int y, int w, int h) {
214: Dimension d = content.calcSize();
215: content.setPreferredSize(d);
216: content.revalidate();
217: super .setBounds(x, y, w, h);
218: validate();
219: }
220:
221: /**
222: * Set the table column width;
223: * @param fieldIdx the field index
224: * @param w the new column width
225: */
226: public void setColWidth(int fieldIdx, int w) {
227: content.setColWidth(fieldIdx, w);
228: }
229:
230: /**
231: * Set one or more attributes of the component. Currently this handles the
232: * attributes
233: * <OL>
234: * <LI>headingStyle, value=the table header style</LI>
235: * <LI>selectionStyle, value=the selected row style</LI>
236: * <LI>interactive, value=true|false</LI>
237: * <LI>updateModel, value=true|false update the underlying model selection (the selected row)</LI>
238: * </OL>
239: * @param attribName the attribute name
240: * @param attribValue the attribute value
241: */
242: public void setAttribute(String attribName, String attribValue) {
243: if (attribName.compareTo("headingStyle") == 0)
244: setHeaderStyle(attribValue);
245: else if (attribName.compareTo("selectionStyle") == 0)
246: setSelectedStyle(attribValue);
247: else if (attribName.compareTo("borderStyle") == 0)
248: setBorderStyle(attribValue);
249: else if (attribName.compareTo("interactive") == 0)
250: setInteractiveTable(attribValue.compareTo("true") == 0);
251: else if (attribName.compareTo("updateModel") == 0)
252: setUpdateModelSelection(attribValue.compareTo("true") == 0);
253:
254: super .setAttribute(attribName, attribValue);
255: }
256:
257: /**
258: * Gets a field value object from the currently selected row
259: * @param fieldIdx the field offset
260: * @return the value
261: */
262: public Object getValue(int fieldIdx) {
263: int row = content.getCurrentRow();
264: return getValue(row, fieldIdx);
265: }
266:
267: /**
268: * Gets a field value object from the specified row
269: * @param rowIdx the row offset
270: * @param fieldIdx the field offset
271: * @return the value
272: */
273: public Object getValue(int rowIdx, int fieldIdx) {
274: XModel rowModel = model.get(rowIdx + content.getFirstRow());
275: Object fieldModel = rowModel.get(fieldIdx);
276: if (fieldModel instanceof XModel)
277: return ((XModel) fieldModel).get();
278: return fieldModel;
279: }
280:
281: /**
282: * Gets a field value as a string from the currently selected row
283: * @param fieldIdx the field offset
284: * @return the value
285: */
286: public String getFieldValue(int fieldIdx) {
287: return (String) getValue(fieldIdx);
288:
289: }
290:
291: /**
292: * Gets a field value as a string from the specified row
293: * @param rowIdx the row offset
294: * @param fieldIdx the field offset
295: * @return the value
296: */
297: public String getFieldValue(int rowIdx, int fieldIdx) {
298: return (String) getValue(rowIdx, fieldIdx);
299: }
300:
301: /**
302: * Adds the specified item listener to receive item events from
303: * this list. Item events are sent in response to user input, but not
304: * in response to calls to <code>select</code> or <code>deselect</code>.
305: * If listener <code>l</code> is <code>null</code>,
306: * no exception is thrown and no action is performed.
307: *
308: * @param l the item listener
309: * @see java.awt.event.ItemEvent
310: * @see java.awt.event.ItemListener
311: * @since JDK1.1
312: */
313: public synchronized void addItemListener(ItemListener l) {
314: content.addItemListener(l);
315: }
316:
317: /**
318: * Removes the specified item listener so that it no longer
319: * receives item events from this list.
320: * If listener <code>l</code> is <code>null</code>,
321: * no exception is thrown and no action is performed.
322: *
323: * @param l the item listener
324: * @see java.awt.event.ItemEvent
325: * @see java.awt.event.ItemListener
326: * @since JDK1.1
327: */
328: public synchronized void removeItemListener(ItemListener l) {
329: content.removeItemListener(l);
330: }
331:
332: /**
333: * Returns the selected items on the list in an array of objects.
334: * @see ItemSelectable
335: */
336: public Object[] getSelectedObjects() {
337: return content.getSelectedObjects();
338: }
339:
340: /**
341: * Add a mouse listener
342: * @param l the new mouse listener
343: */
344: public void addMouseListener(MouseListener l) {
345: content.addMouseListener(l);
346: }
347:
348: /**
349: * Get the hash code to be used by the event handler for this component
350: * @return the hash code
351: */
352: public long getComponentHashCode() {
353: return content.hashCode();
354: }
355: }
|