001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: import java.awt.*;
008: import java.awt.event.*;
009: import java.util.*;
010: import java.io.*;
011:
012: import javax.swing.table.*;
013: import com.javelin.swinglets.table.*;
014: import com.javelin.swinglets.event.*;
015: import com.javelin.swinglets.plaf.*;
016:
017: /**
018: * STable defines a table.
019: *
020: * @author Robin Sharp
021: */
022:
023: public class STable extends SComponent {
024: /**
025: * Creates a STable.
026: */
027: public STable(int numRows, int numColumns) {
028: this (new DefaultTableModel(numRows, numColumns));
029:
030: for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
031: for (int columnIndex = 0; columnIndex < model
032: .getColumnCount(); columnIndex++) {
033: model.setValueAt(new Cell(), rowIndex, columnIndex);
034: }
035: }
036:
037: }
038:
039: /**
040: * Creates a STable with a swing table model, with no model.
041: */
042: public STable() {
043: }
044:
045: /**
046: * Creates a STable with a swing table model
047: */
048: public STable(TableModel model) {
049: this .model = model;
050: }
051:
052: /**
053: * Returns the name of the class for L&F of this component.
054: */
055: public Class getUIClass() {
056: return STable.class;
057: }
058:
059: /**
060: * Set Model.
061: */
062: public STable setModel(TableModel model) {
063: firePropertyChange("model", this .model, this .model = model);
064: return this ;
065: }
066:
067: /**
068: * Get Model.
069: */
070: public TableModel getModel() {
071: return model;
072: }
073:
074: /**
075: * Set the look and Feel on this table
076: */
077: public SComponent setLookAndFeel(SLookAndFeel lookAndFeel) {
078: super .setLookAndFeel(lookAndFeel);
079:
080: if (tableHeader != null)
081: tableHeader.setLookAndFeel(lookAndFeel);
082: if (tableFooter != null)
083: tableFooter.setLookAndFeel(lookAndFeel);
084:
085: //Go through the whole table looking for SComponents
086: Object object;
087: for (int row = 0; row < getRowCount(); row++) {
088: for (int column = 0; column < getColumnCount(); column++) {
089: object = getValueAt(row, column);
090: if (object != null && object instanceof SComponent) {
091: ((SComponent) object).setLookAndFeel(lookAndFeel);
092: }
093: }
094: }
095:
096: return this ;
097: }
098:
099: /**
100: * Get a sub component by name. This will descend the component
101: * hierarchy and return the SComponent, or null.
102: */
103: public SComponent getComponent(String name) {
104: if (getName().equals(name))
105: return this ;
106:
107: Object object = null;
108: SComponent component = null;
109: for (int row = 0; row < getRowCount(); row++) {
110: for (int column = 0; column < getColumnCount(); column++) {
111: object = getValueAt(row, column);
112: if (object != null && object instanceof SComponent) {
113: component = ((SComponent) object)
114: .getComponent(name);
115: if (component != null)
116: return component;
117: }
118: }
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Get the column count
126: */
127: public int getColumnCount() {
128: if (model == null)
129: return 0;
130:
131: return model.getColumnCount();
132: }
133:
134: /**
135: * Set Table cell renderer for the current look and feel.
136: */
137: public STable setTableCellRenderer(
138: STableCellRenderer tableCellRenderer) {
139: tableCellRenderers.put(getLookAndFeel().getClass().getName(),
140: tableCellRenderer);
141:
142: return this ;
143: }
144:
145: /**
146: * Set Table cell renderer for each look and feel.
147: */
148: public STable setTableCellRenderer(String lookAndFeel,
149: STableCellRenderer tableCellRenderer) {
150: tableCellRenderers.put(lookAndFeel, tableCellRenderer);
151:
152: return this ;
153: }
154:
155: /**
156: * Get Table cell renderer, for the current look and feel.
157: */
158: public STableCellRenderer getTableCellRenderer() {
159: return getTableCellRenderer(getLookAndFeel().getClass()
160: .getName());
161: }
162:
163: /**
164: * Get Table cell renderer, or null.
165: * <P>
166: * If a renderer has not been installed for the look and feel, it is.
167: * picked up from the LookAndFeel as SLookAndFeel.TABLE_DEFAULT_CELL_RENDERER.
168: */
169: public STableCellRenderer getTableCellRenderer(String lookAndFeel) {
170: STableCellRenderer renderer = (STableCellRenderer) tableCellRenderers
171: .get(lookAndFeel);
172:
173: if (renderer == null) {
174: Class clazz = getLookAndFeel().getUIDefaults()
175: .getTableCellRenderer(
176: SLookAndFeel.TABLE_DEFAULT_CELL_RENDERER);
177:
178: //System.out.println( lookAndFeel + " " + clazz.getName() );
179:
180: if (clazz != null) {
181: try {
182: renderer = (STableCellRenderer) clazz.newInstance();
183: } catch (Exception e) {
184: throw new IllegalArgumentException(
185: "Cannot load cell renderer for class "
186: + clazz.getName());
187: }
188: }
189:
190: if (renderer != null) {
191: tableCellRenderers.put(lookAndFeel, renderer);
192: }
193: }
194:
195: return renderer;
196: }
197:
198: /**
199: * Get the table header. This defaults to null, and a table header will
200: * not show in the table.
201: */
202: public STableHeader getTableHeader() {
203: return tableHeader;
204: }
205:
206: /**
207: * Get the table footer. This defaults to null, and a table footer will
208: * not show in the table.
209: */
210: public STableFooter getTableFooter() {
211: return tableFooter;
212: }
213:
214: /**
215: * Set the table header.
216: */
217: public STable setTableHeader(STableHeader tableHeader) {
218: if (getTableHeader() == tableHeader)
219: return this ;
220:
221: if (getTableHeader() != null) {
222: getTableHeader().setTable(null);
223: }
224:
225: if (tableHeader != null) {
226: tableHeader.setTable(this );
227: tableHeader.setLookAndFeel(getLookAndFeel());
228: }
229:
230: firePropertyChange("tableHeader", this .tableHeader,
231: this .tableHeader = tableHeader);
232:
233: return this ;
234: }
235:
236: /**
237: * Set the table footer.
238: */
239: public STable setTableFooter(STableFooter tableFooter) {
240: if (getTableFooter() == tableFooter)
241: return this ;
242:
243: if (getTableFooter() != null) {
244: getTableFooter().setTable(null);
245: }
246:
247: if (tableFooter != null) {
248: tableFooter.setTable(this );
249: tableFooter.setLookAndFeel(getLookAndFeel());
250: }
251:
252: this .tableFooter = tableFooter;
253:
254: return this ;
255: }
256:
257: /**
258: * Set the background icon.
259: */
260: public STable setBackgroundIcon(SIcon backgroundIcon) {
261: firePropertyChange("backgroundIcon", this .backgroundIcon,
262: this .backgroundIcon = backgroundIcon);
263: return this ;
264: }
265:
266: /**
267: * Get Table cell renderer.
268: */
269: public SIcon getBackgroundIcon() {
270: return backgroundIcon;
271: }
272:
273: /**
274: * Get the row count
275: */
276: public int getRowCount() {
277: if (model == null)
278: return 0;
279:
280: return model.getRowCount();
281: }
282:
283: /**
284: * Get the value at the specified row and column.
285: */
286: public Object getValueAt(int row, int column) {
287: if (model == null)
288: return null;
289:
290: Object cell = model.getValueAt(row, column);
291:
292: if (cell instanceof Cell)
293: return null;
294:
295: return cell;
296: }
297:
298: /**
299: * Set the value at the specified row and column.
300: */
301: public STable setValueAt(Object value, int row, int column) {
302: if (model == null)
303: return this ;
304:
305: model.setValueAt(value, row, column);
306: return this ;
307: }
308:
309: /**
310: * Get the horizontal alignment at the specified row and column.
311: */
312: public int getHorizontalAlignmentAt(int column) {
313: return getHorizontalAlignmentAt(-1, column);
314: }
315:
316: /**
317: * Get the horizontal alignment at the specified row and column.
318: */
319: public int getHorizontalAlignmentAt(int row, int column) {
320: if (horizontalAlignments == null)
321: return SConstants.DEFAULT;
322:
323: point.setLocation(row, column);
324: Integer i = (Integer) horizontalAlignments.get(point);
325:
326: if (i == null && row != -1) {
327: point.setLocation(-1, column);
328: i = (Integer) horizontalAlignments.get(point);
329: }
330:
331: if (i == null)
332: return SConstants.DEFAULT;
333: else
334: return i.intValue();
335: }
336:
337: /**
338: * Set the horizontal alignment at the specified column.
339: * One of SConstants DEFAULT, LEFT, CENTER, RIGHT.
340: */
341: public STable setHorizontalAlignmentAt(int alignment, int column) {
342: setHorizontalAlignmentAt(alignment, -1, column);
343: return this ;
344: }
345:
346: /**
347: * Set the horizontal alignment at the specified row and column.
348: * One of SConstants DEFAULT, LEFT, CENTER, RIGHT.
349: */
350: public STable setHorizontalAlignmentAt(int alignment, int row,
351: int column) {
352: if (horizontalAlignments == null)
353: horizontalAlignments = new Hashtable();
354:
355: horizontalAlignments.put(new Point(row, column), new Integer(
356: alignment));
357:
358: return this ;
359: }
360:
361: /**
362: * Get the vertical lignment at the specified row and column.
363: */
364: public int getVerticalAlignmentAt(int row, int column) {
365: if (verticalAlignments == null)
366: return SConstants.DEFAULT;
367:
368: point.setLocation(row, column);
369: Integer i = (Integer) verticalAlignments.get(point);
370:
371: if (i == null)
372: return SConstants.DEFAULT;
373: else
374: return i.intValue();
375: }
376:
377: /**
378: * Set the vertical alignment at the specified row and column.
379: * One of SConstants DEFAULT, TOP, MIDDLE, BOTTOM.
380: */
381: public STable setVerticalAlignmentAt(int alignment, int row,
382: int column) {
383: if (verticalAlignments == null)
384: verticalAlignments = new Hashtable();
385:
386: verticalAlignments.put(new Point(row, column), new Integer(
387: alignment));
388:
389: return this ;
390: }
391:
392: /**
393: * Get the cell span at the specified row and column.
394: */
395: public Dimension getCellSpanAt(int row, int column) {
396: if (cellSpans == null)
397: return null;
398:
399: point.setLocation(row, column);
400: return (Dimension) cellSpans.get(point);
401: }
402:
403: /**
404: * Set the cellspans at the specified row and column.
405: */
406: public STable setCellSpanAt(Dimension dimension, int row, int column) {
407: if (cellSpans == null)
408: cellSpans = new Hashtable();
409:
410: cellSpans.put(new Point(row, column), dimension);
411:
412: return this ;
413: }
414:
415: /**
416: * Check whether this table not wrapped.
417: */
418: public boolean isNoWrap() {
419: return noWrap;
420: }
421:
422: /**
423: * Set whether this table not wrapped.
424: */
425: public STable setNoWrap(boolean noWrap) {
426: this .noWrap = noWrap;
427: return this ;
428: }
429:
430: /**
431: * Get the no wrap at the specified row and column.
432: */
433: public boolean getNoWrapAt(int row, int column) {
434: if (noWraps == null)
435: return false;
436:
437: point.setLocation(row, column);
438: return Boolean.TRUE.equals(noWraps.get(point));
439: }
440:
441: /**
442: * Set the no wrap at the specified row and column.
443: */
444: public STable setNoWrapAt(boolean noWrap, int row, int column) {
445: if (noWraps == null)
446: noWraps = new Hashtable();
447:
448: if (noWrap == false) {
449: noWraps.remove(new Point(row, column));
450: } else {
451: noWraps.put(new Point(row, column), Boolean.TRUE);
452: }
453:
454: return this ;
455: }
456:
457: /**
458: * Get the background icon at the specified row and column.
459: */
460: public SIcon getBackgroundIconAt(int row, int column) {
461: if (backgroundIcons == null)
462: return null;
463:
464: point.setLocation(row, column);
465: return (SIcon) backgroundIcons.get(point);
466: }
467:
468: /**
469: * Set the background icon at the specified row and column.
470: */
471: public STable setBackgroundIconAt(SIcon icon, int row, int column) {
472: if (backgroundIcons == null)
473: backgroundIcons = new Hashtable();
474:
475: backgroundIcons.put(new Point(row, column), icon);
476:
477: return this ;
478: }
479:
480: /**
481: * Set the grid width.
482: */
483: public STable setGridWidth(int gridWidth) {
484: firePropertyChange("gridWidth", this .gridWidth,
485: this .gridWidth = gridWidth);
486:
487: return this ;
488: }
489:
490: /**
491: * Get the grid width.
492: */
493: public int getGridWidth() {
494: return gridWidth;
495: }
496:
497: /**
498: * Set the intercell spacing.
499: */
500: public STable setIntercellSpacing(Dimension intercellSpacing) {
501: firePropertyChange("intercellSpacing", this .intercellSpacing,
502: this .intercellSpacing = intercellSpacing);
503: return this ;
504: }
505:
506: /**
507: * Get the intercell spacing.
508: */
509: public Dimension getIntercellSpacing() {
510: return intercellSpacing;
511: }
512:
513: /**
514: * Set the intercell padding.
515: */
516: public STable setIntercellPadding(Dimension intercellPadding) {
517: firePropertyChange("intercellPadding", this .intercellPadding,
518: this .intercellPadding = intercellPadding);
519: return this ;
520: }
521:
522: /**
523: * Get the intercell padding.
524: */
525: public Dimension getIntercellPadding() {
526: return intercellPadding;
527: }
528:
529: /**
530: * Processes events occurring on this component.
531: *
532: * If the property STableHeader.SOURCE_COLUMN is set
533: * then pass an ActionEvent to the tableHeader, with
534: * an id set to the column number.
535: */
536: protected void processEvent(AWTEvent event) {
537: if (event instanceof FormEvent) {
538: String sourcetype = ((FormEvent) event)
539: .getParameter("_TYPE");
540:
541: String column = ((FormEvent) event)
542: .getParameter(STableHeader.SOURCE_COLUMN);
543:
544: if (column != null && "H".equals(sourcetype)
545: && getTableHeader() != null) {
546: try {
547: ActionEvent actionEvent = new ActionEvent(
548: getTableHeader(), Integer.parseInt(column),
549: "");
550: getTableHeader().processActionEvent(actionEvent);
551: } catch (NumberFormatException nfe) {
552: nfe.printStackTrace();
553: }
554: }
555:
556: String command = ((FormEvent) event)
557: .getParameter(STableFooter.COMMAND);
558:
559: if (command != null && "F".equals(sourcetype)
560: && getTableFooter() != null) {
561: ActionEvent actionEvent = new ActionEvent(
562: getTableFooter(), 0, command);
563: getTableFooter().processActionEvent(actionEvent);
564:
565: }
566: }
567: }
568:
569: // PRIVATE //////////////////////////////////////////////////////////////
570:
571: protected Dimension intercellSpacing;
572: protected Dimension intercellPadding;
573: protected SIcon backgroundIcon;
574:
575: protected int gridWidth = 1;
576: protected boolean noWrap;
577:
578: public class Cell extends Object {
579: };
580:
581: protected Cell cell = new Cell();
582:
583: protected TableModel model;
584:
585: protected Hashtable tableCellRenderers = new Hashtable();
586:
587: protected STableHeader tableHeader;
588: protected STableFooter tableFooter;
589:
590: //Assume
591: protected Hashtable cellSpans;
592: protected Hashtable verticalAlignments;
593: protected Hashtable horizontalAlignments;
594: protected Hashtable noWraps;
595: protected Hashtable backgroundIcons;
596: protected Point point = new Point();
597: }
|