001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.composed;
019:
020: import java.util.List;
021: import java.util.Vector;
022: import java.util.logging.Logger;
023:
024: import javax.swing.table.DefaultTableModel;
025:
026: import de.finix.contelligent.client.base.ComponentProperty;
027: import de.finix.contelligent.client.base.ContelligentComponent;
028: import de.finix.contelligent.client.base.Editable;
029: import de.finix.contelligent.client.i18n.Resources;
030:
031: public class MixedTableModel extends DefaultTableModel {
032:
033: public final static int ROWS = 0;
034:
035: public final static int COLS = 1;
036:
037: public final static int TABLE = 2;
038:
039: private List rows;
040:
041: private boolean editable = false;
042:
043: private int preferredRowHeight = 25;
044:
045: private int mode;
046:
047: private static Logger logger = Logger
048: .getLogger(MixedTableModel.class.getName());
049:
050: public MixedTableModel(List rows, int mode) {
051: this .rows = rows;
052: this .mode = mode;
053: updateModel();
054: }
055:
056: public void updateModel() {
057: setRowCount(0);
058:
059: switch (mode) {
060: case COLS: {
061: setColumnCount(rows.size());
062: List cols = new Vector();
063: for (int i = 0; i < rows.size(); i++) {
064: Object object = rows.get(i);
065: if (object instanceof ContelligentComponent) {
066: ContelligentComponent component = (ContelligentComponent) object;
067: cols.add(component);
068: } else if (object instanceof ComponentProperty) {
069: ComponentProperty componentProperty = (ComponentProperty) object;
070: cols.add(componentProperty);
071: }
072: }
073: addRow(cols.toArray());
074: break;
075: }
076: case TABLE: {
077: setColumnCount(2);
078: setColumnIdentifiers(new String[] {
079: Resources.getLocalString("name"),
080: Resources.getLocalString("value") });
081: for (int i = 0; i < rows.size(); i++) {
082: Object object = rows.get(i);
083: if (object instanceof ContelligentComponent) {
084: ContelligentComponent component = (ContelligentComponent) object;
085: addRow(new Object[] { component.getName(),
086: component });
087: } else if (object instanceof ComponentProperty) {
088: ComponentProperty componentProperty = (ComponentProperty) object;
089: addRow(new Object[] { componentProperty.getName(),
090: componentProperty });
091: }
092: }
093: break;
094: }
095: case ROWS: {
096: setColumnCount(1);
097: for (int i = 0; i < rows.size(); i++) {
098: Object object = rows.get(i);
099: if (object instanceof ContelligentComponent) {
100: ContelligentComponent component = (ContelligentComponent) object;
101: addRow(new Object[] { component });
102: } else if (object instanceof ComponentProperty) {
103: ComponentProperty componentProperty = (ComponentProperty) object;
104: addRow(new Object[] { componentProperty });
105: }
106: }
107: }
108: }
109: }
110:
111: public int getPreferredRowHeight() {
112: return preferredRowHeight;
113: }
114:
115: public Class getColumnClass(int columnIndex) {
116: if (mode == TABLE)
117: return (columnIndex == 0) ? String.class : Editable.class;
118: return Editable.class;
119: }
120:
121: public boolean isCellEditable(int row, int col) {
122: // Note that the data/cell address is constant,
123: // no matter where the cell appears onscreen.
124: if (mode == TABLE && col == 0) {
125: return false;
126: }
127: return editable;
128: }
129:
130: public void setEditable(boolean editable) {
131: this .editable = editable;
132: }
133:
134: public List getComponents() {
135: List components = new Vector();
136: for (int i = 0; i < rows.size(); i++) {
137: Object object = rows.get(i);
138: if (object instanceof ContelligentComponent) {
139: if (!components
140: .contains((ContelligentComponent) object)) {
141: components.add((ContelligentComponent) object);
142: }
143: } else if (object instanceof ComponentProperty) {
144: ContelligentComponent component = ((ComponentProperty) object)
145: .getComponent();
146: if (!components.contains(component)) {
147: components.add(component);
148: }
149: }
150: }
151: return components;
152: }
153: }
|