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.logging.Level;
022: import java.util.logging.Logger;
023:
024: import javax.swing.table.DefaultTableModel;
025:
026: import de.finix.contelligent.client.base.ComponentFactory;
027: import de.finix.contelligent.client.base.ComponentNotFoundException;
028: import de.finix.contelligent.client.base.ContelligentComponent;
029: import de.finix.contelligent.client.gui.ComponentEditor;
030: import de.finix.contelligent.client.gui.GUI;
031: import de.finix.contelligent.client.gui.GUIFactory;
032: import de.finix.contelligent.client.gui.UnsupportedGUIException;
033: import de.finix.contelligent.client.gui.View;
034: import de.finix.contelligent.client.i18n.Resources;
035:
036: public class ComponentTableModel extends DefaultTableModel {
037:
038: public final static int ROWS = 0;
039:
040: public final static int COLS = 1;
041:
042: public final static int TABLE = 2;
043:
044: private static Logger logger = Logger
045: .getLogger(ComponentTableModel.class.getName());
046:
047: private ContelligentComponent component;
048:
049: private View view;
050:
051: private boolean editable = false;
052:
053: private boolean colspanEnabled = false;
054:
055: private int align, cols;
056:
057: private int[] colspan;
058:
059: public ComponentTableModel(ContelligentComponent component,
060: View view, int align) {
061: this .component = component;
062: this .view = view;
063: this .align = align;
064:
065: updateModel();
066: }
067:
068: public void updateModel() {
069: Object[][] data;
070: String[] columnNames;
071:
072: logger.log(Level.FINE, "updating model");
073: if (align == COLS) {
074: setColspanEnabled(true);
075: List subcomponents = component.getSubcomponents();
076: // calculate cols with colspan
077: colspan = new int[subcomponents.size()];
078: cols = subcomponents.size();
079: data = new Object[1][subcomponents.size()];
080:
081: columnNames = new String[subcomponents.size()];
082: for (int i = 0; i < subcomponents.size(); i++) {
083: try {
084: ContelligentComponent subcomponent = ComponentFactory
085: .getInstance().getComponent(
086: component.getPath()
087: + "/"
088: + (String) subcomponents
089: .get(i));
090: // calculate colspan
091: if (subcomponent
092: .hasProperty(ContelligentComponent.COLSPAN)) {
093: int span = Integer
094: .parseInt(subcomponent
095: .getPropertyValue(ContelligentComponent.COLSPAN));
096: if (span > 1) {
097: cols += (span - 1);
098: }
099: colspan[i] = span;
100: } else {
101: colspan[i] = 1;
102: }
103: // get editor/renderer of component...
104: GUI[] gui = GUIFactory.getInstance().getGUI(
105: subcomponent, view);
106: columnNames[i] = "";
107: data[0][i] = gui[0];
108: } catch (ComponentNotFoundException cnfe) {
109: logger.log(Level.SEVERE,
110: "Could not update subcomponent!", cnfe);
111: }
112: }
113: setDataVector(data, columnNames);
114: } else {
115: int contentColumn = 0;
116: setColumnCount(1);
117: cols = getColumnCount();
118: colspan = new int[cols + 1];
119: if (align == TABLE) {
120: setColumnCount(2);
121: contentColumn = 1;
122: // use colspan to create a beautiful table
123: cols = 4;
124: colspan[1] = 3;
125: setColspanEnabled(true);
126: }
127: List subcomponents = component.getSubcomponents();
128: data = new Object[subcomponents.size()][2];
129: for (int i = 0; i < subcomponents.size(); i++) {
130: String subcomponentName = (String) subcomponents.get(i);
131: try {
132: ContelligentComponent subcomponent = ComponentFactory
133: .getInstance().getComponent(
134: component.getPath() + "/"
135: + subcomponentName);
136: if (align == TABLE) {
137: data[i][0] = subcomponent;
138: }
139: // get editor/renderer of component...
140: GUI[] gui = GUIFactory.getInstance().getGUI(
141: subcomponent, view);
142: data[i][contentColumn] = gui[0];
143: } catch (ComponentNotFoundException cnfe) {
144: logger.log(Level.SEVERE,
145: "Could not update subcomponent!", cnfe);
146: }
147: }
148: if (align == TABLE) {
149: columnNames = new String[] {
150: Resources.getLocalString("name"),
151: Resources.getLocalString("value") };
152: setDataVector(data, columnNames);
153: } else if (align == ROWS) {
154: columnNames = new String[] { Resources
155: .getLocalString("content") };
156: setDataVector(data, columnNames);
157: }
158: }
159: }
160:
161: public void addRow(String name, ContelligentComponent component) {
162: int contentColumn = 0;
163: if (align == TABLE) {
164: contentColumn = 1;
165: }
166: // get editor/renderer of component...
167: GUI[] gui = GUIFactory.getInstance().getGUI(component, view);
168: Object[][] rowData = new Object[1][2];
169: rowData[0][0] = component;
170: rowData[0][contentColumn] = gui[0];
171: super .addRow(rowData);
172: }
173:
174: public int getPreferredColumnWidth(int tableWidth, int column) {
175: return (tableWidth / cols) * Math.max(colspan[column], 1);
176: }
177:
178: public Class getColumnClass(int columnIndex) {
179: if (columnIndex == 0 && align == TABLE) {
180: return ContelligentComponent.class;
181: } else {
182: return GUI.class;
183: }
184: }
185:
186: public boolean isCellEditable(int row, int col) {
187: // Note that the data/cell address is constant,
188: // no matter where the cell appears onscreen.
189: if (col == 0 && align == TABLE) {
190: return false;
191: }
192: return editable;
193: }
194:
195: public void rollback() {
196: // save subcomponents that might have been edited in the table
197: if (align == ROWS || align == TABLE) {
198: int contentColumn = 0;
199: if (align == TABLE) {
200: contentColumn = 1;
201: }
202: for (int i = 0; i < getRowCount(); i++) {
203: rollBackEditorAt(i, contentColumn);
204: }
205: } else {
206: for (int i = 0; i < getColumnCount(); i++) {
207: rollBackEditorAt(0, i);
208: }
209: }
210: }
211:
212: private void rollBackEditorAt(int row, int column) {
213: if (getValueAt(row, column) instanceof GUI) {
214: try {
215: ComponentEditor editor = (ComponentEditor) ((GUI) getValueAt(
216: row, column)).getEditor(GUI.TABLE);
217: if (editor != null) {
218: editor.rollback();
219: }
220: } catch (UnsupportedGUIException e) {
221: }
222: }
223: }
224:
225: public void commit() {
226: // save subcomponents that might have been edited in the table
227: if (align == ROWS || align == TABLE) {
228: int contentColumn = 0;
229: if (align == TABLE) {
230: contentColumn = 1;
231: }
232: for (int i = 0; i < getRowCount(); i++) {
233: logger.log(Level.FINEST, "Commiting row " + i);
234: commitEditorAt(i, contentColumn);
235: }
236: } else {
237: for (int i = 0; i < getColumnCount(); i++) {
238: logger.log(Level.FINEST, "Commiting column " + i);
239: commitEditorAt(0, i);
240: }
241: }
242: }
243:
244: private void commitEditorAt(int row, int column) {
245: if (getValueAt(row, column) instanceof GUI) {
246: try {
247: logger.log(Level.FINEST, "Is gui, so get editor...");
248: ComponentEditor editor = (ComponentEditor) ((GUI) getValueAt(
249: row, column)).getEditor(GUI.TABLE);
250: if (editor != null) {
251: logger.log(Level.FINEST, "Commit editor...");
252: editor.commit();
253: }
254: } catch (UnsupportedGUIException e) {
255: }
256: }
257: }
258:
259: public void setEditable(boolean editable) {
260: this .editable = editable;
261: }
262:
263: public boolean isEditable() {
264: return editable;
265: }
266:
267: public void setColspanEnabled(boolean colspanEnabled) {
268: this .colspanEnabled = colspanEnabled;
269: }
270:
271: public boolean isColspanEnabled() {
272: return colspanEnabled;
273: }
274:
275: public int getContentColumn() {
276: if (align == ROWS || align == COLS) {
277: return 0;
278: } else {
279: return 1;
280: }
281: }
282: }
|