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.awt.Color;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023:
024: import javax.swing.DefaultCellEditor;
025: import javax.swing.JCheckBox;
026: import javax.swing.JComboBox;
027: import javax.swing.JComponent;
028: import javax.swing.JLabel;
029: import javax.swing.JTable;
030: import javax.swing.JTextField;
031: import javax.swing.table.TableCellRenderer;
032:
033: import de.finix.contelligent.client.base.ComponentProperty;
034: import de.finix.contelligent.client.base.ContelligentComponent;
035: import de.finix.contelligent.client.base.Editable;
036: import de.finix.contelligent.client.base.TypeProperty;
037: import de.finix.contelligent.client.gui.ComponentEditor;
038: import de.finix.contelligent.client.gui.ComponentRenderer;
039: import de.finix.contelligent.client.gui.GUI;
040: import de.finix.contelligent.client.gui.GUIFactory;
041: import de.finix.contelligent.client.gui.UnsupportedGUIException;
042: import de.finix.contelligent.client.gui.View;
043: import de.finix.contelligent.client.i18n.Resources;
044:
045: public class MixedTable extends JTable {
046:
047: private int defaultRowHeight = 20;
048:
049: private final static int MAX_ROW_HEIGHT = 250;
050:
051: private final static int MIN_ROW_HEIGHT = 20;
052:
053: private View view;
054:
055: private MixedTable mixedTable;
056:
057: private MixedTableModel mixedTableModel;
058:
059: public MixedTable(MixedTableModel mixedTableModel, View view) {
060: super (mixedTableModel);
061:
062: this .view = view;
063: this .mixedTableModel = mixedTableModel;
064: mixedTable = this ;
065:
066: setRowHeight(defaultRowHeight);
067: setDefaultRenderer(Editable.class, new MixedTableRenderer());
068: setDefaultEditor(Editable.class, new MixedTableEditor());
069: }
070:
071: public void setDefaultRowHeight(int defaultRowHeight) {
072: this .defaultRowHeight = defaultRowHeight;
073: setRowHeight(defaultRowHeight);
074: }
075:
076: public void increaseRowHeight() {
077: defaultRowHeight = Math.min(MAX_ROW_HEIGHT,
078: defaultRowHeight + 5);
079: ;
080: }
081:
082: public void decreaseRowHeight() {
083: defaultRowHeight = Math.max(MIN_ROW_HEIGHT,
084: defaultRowHeight - 5);
085: ;
086: }
087:
088: class MixedTableEditor extends DefaultCellEditor {
089: private ComponentProperty componentProperty;
090:
091: private ContelligentComponent component;
092:
093: private java.awt.Component cellEditor;
094:
095: public MixedTableEditor() {
096: super (new JCheckBox()); // Bad Swing design
097: }
098:
099: public Object getCellEditorValue() {
100: if (componentProperty != null) {
101: return componentProperty;
102: } else {
103: return component;
104: }
105: }
106:
107: public java.awt.Component getTableCellEditorComponent(
108: JTable table, Object object, boolean isSelected,
109: int row, int column) {
110: if (object instanceof ContelligentComponent) {
111: component = (ContelligentComponent) object;
112: componentProperty = null;
113: int preferredRowHeight = defaultRowHeight;
114: final GUI[] gui = GUIFactory.getInstance().getGUI(
115: component, view);
116: if (gui[0].isSupported(GUI.TABLE)) {
117: try {
118: ComponentEditor editor = gui[0]
119: .getEditor(GUI.TABLE);
120: preferredRowHeight = Math
121: .max(
122: preferredRowHeight,
123: (int) ((java.awt.Component) editor)
124: .getPreferredSize()
125: .getHeight());
126: if (mixedTable.getRowHeight(row) != preferredRowHeight) {
127: mixedTable.setRowHeight(row,
128: preferredRowHeight);
129: }
130: editor.addActionListener(new ActionListener() {
131: public void actionPerformed(ActionEvent e) {
132: fireEditingStopped();
133: }
134: });
135: return (java.awt.Component) editor;
136: } catch (UnsupportedGUIException uge) {
137: return new JLabel("["
138: + Resources
139: .getLocalString("not_editable")
140: + "]");
141: }
142: } else {
143: return new JLabel("["
144: + Resources.getLocalString("not_editable")
145: + "]");
146: }
147: }
148: if (object instanceof ComponentProperty) {
149: component = null;
150: componentProperty = (ComponentProperty) object;
151: TypeProperty typeProperty = componentProperty
152: .getTypeProperty();
153: if (typeProperty.isEnumerable()) {
154: String[] items = typeProperty.getEnumeration();
155: cellEditor = new JComboBox(items);
156: // preselect the current value
157: ((JComboBox) cellEditor)
158: .setSelectedItem(componentProperty
159: .getValue());
160: if (typeProperty.isFreeTextAllowed()) {
161: ((JComboBox) cellEditor).setEditable(true);
162: } else {
163: ((JComboBox) cellEditor).setEditable(false);
164: }
165: ((JComboBox) cellEditor)
166: .addActionListener(new ActionListener() {
167: public void actionPerformed(
168: ActionEvent e) {
169: JComboBox cb = (JComboBox) e
170: .getSource();
171: String text = (String) cb
172: .getSelectedItem();
173: if (!componentProperty.getValue()
174: .equals(text)) {
175: componentProperty
176: .setValue(text);
177: componentProperty
178: .getComponent()
179: .setModified(true);
180: }
181: fireEditingStopped();
182: }
183: });
184: if (componentProperty.isFinal()
185: || typeProperty.getMode().equals(
186: TypeProperty.READ_ONLY)
187: || typeProperty
188: .getMode()
189: .equals(
190: TypeProperty.READ_ONLY_NOT_PERSISTENT)) {
191: ((JComboBox) cellEditor).setEditable(false);
192: }
193: } else {
194: cellEditor = new JTextField(componentProperty
195: .getValue());
196: ((JTextField) cellEditor).setOpaque(true);
197: if (componentProperty.isFinal()
198: || typeProperty.getMode().equals(
199: TypeProperty.READ_ONLY)
200: || typeProperty
201: .getMode()
202: .equals(
203: TypeProperty.READ_ONLY_NOT_PERSISTENT)) {
204: ((JTextField) cellEditor)
205: .setBackground(Color.white);
206: ((JTextField) cellEditor).setEditable(false);
207: } else {
208: ((JTextField) cellEditor)
209: .addActionListener(new ActionListener() {
210: public void actionPerformed(
211: ActionEvent e) {
212: String text = ((JTextField) cellEditor)
213: .getText();
214: if (!componentProperty
215: .getValue()
216: .equals(text)) {
217: componentProperty
218: .setValue(text);
219: componentProperty
220: .getComponent()
221: .setModified(true);
222: }
223: fireEditingStopped();
224: }
225: });
226: }
227: }
228: return cellEditor;
229: }
230: return new JLabel("["
231: + Resources.getLocalString("unknown_class") + "]");
232: }
233: }
234:
235: class MixedTableRenderer implements TableCellRenderer {
236: public java.awt.Component getTableCellRendererComponent(
237: JTable table, Object object, boolean isSelected,
238: boolean hasFocus, int row, int column) {
239:
240: java.awt.Component cellRenderer = (java.awt.Component) new JLabel(
241: "[" + Resources.getLocalString("no_preview") + "]");
242: cellRenderer.setBackground(table.getBackground());
243:
244: if (object instanceof ContelligentComponent) {
245: ContelligentComponent component = (ContelligentComponent) object;
246: int preferredRowHeight = defaultRowHeight;
247: GUI[] gui = GUIFactory.getInstance().getGUI(component,
248: view);
249: if (gui[0].isSupported(GUI.TABLE)) {
250: try {
251: ComponentRenderer renderer = gui[0]
252: .getRenderer(GUI.TABLE);
253: preferredRowHeight = Math
254: .max(
255: preferredRowHeight,
256: (int) ((java.awt.Component) renderer)
257: .getPreferredSize()
258: .getHeight());
259: if (mixedTable.getRowHeight(row) != preferredRowHeight) {
260: mixedTable.setRowHeight(row,
261: preferredRowHeight);
262: }
263: cellRenderer = (java.awt.Component) renderer;
264:
265: } catch (UnsupportedGUIException uge) {
266: // do nothing
267: }
268: }
269: }
270: if (object instanceof ComponentProperty) {
271: JLabel cellLabel = new JLabel();
272: ComponentProperty property = (ComponentProperty) object;
273: if (column == 0) {
274: cellLabel.setText(property.getName());
275: } else {
276: cellLabel.setText(property.getValue());
277: }
278: if (property.getValue().equals(
279: property.getInitialValue())) {
280: cellLabel.setForeground(Color.gray);
281: } else {
282: cellLabel.setForeground(Color.black);
283: }
284: if (!isSelected && property.isFinal()) {
285: cellLabel.setBackground(Color.yellow);
286: }
287: if (mixedTable.getRowHeight(row) != defaultRowHeight) {
288: mixedTable.setRowHeight(row, defaultRowHeight);
289: }
290: cellRenderer = (java.awt.Component) cellLabel;
291: }
292:
293: if (isSelected) {
294: cellRenderer.setBackground(table
295: .getSelectionBackground());
296: } else {
297: cellRenderer.setBackground(table.getBackground());
298: }
299: if (cellRenderer instanceof JComponent) {
300: ((JComponent) cellRenderer).setOpaque(true);
301: ((JComponent) cellRenderer).setFont(table.getFont());
302: }
303: return cellRenderer;
304: }
305: }
306: }
|