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.property;
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.JLabel;
028: import javax.swing.JTable;
029: import javax.swing.JTextField;
030: import javax.swing.table.TableCellRenderer;
031:
032: import de.finix.contelligent.client.base.ComponentProperty;
033: import de.finix.contelligent.client.base.TypeProperty;
034: import de.finix.contelligent.client.util.SortableTable;
035: import de.finix.contelligent.client.util.TableModelSorter;
036: import de.finix.contelligent.client.util.dnd.JTextFieldDND;
037:
038: public class PropertyTable extends SortableTable {
039:
040: private final static int MIN_ROW_HEIGHT = 22;
041:
042: public PropertyTable(PropertyTableModel ptm) {
043: super (new TableModelSorter(ptm));
044: propertyTableModel = ptm;
045: propertyTable = this ;
046:
047: setDefaultRenderer(ComponentProperty.class,
048: new PropertyRenderer());
049: setDefaultEditor(ComponentProperty.class, new PropertyEditor(
050: this ));
051:
052: setRowHeight(MIN_ROW_HEIGHT);
053: }
054:
055: PropertyTableModel propertyTableModel;
056:
057: public void stopEditing() {
058: if (cellEditor != null)
059: cellEditor.stopCellEditing();
060: }
061:
062: class PropertyEditor extends DefaultCellEditor {
063: private ComponentProperty property;
064:
065: private PropertyTable propertyTable;
066:
067: private java.awt.Component cellEditor;
068:
069: public PropertyEditor(PropertyTable vt) {
070: super (new JCheckBox()); // Bad Swing design
071: propertyTable = vt;
072: }
073:
074: public Object getCellEditorValue() {
075: return property;
076: }
077:
078: public java.awt.Component getTableCellEditorComponent(
079: JTable table, Object object, boolean isSelected,
080: final int row, final int column) {
081: // Property to edit
082: property = (ComponentProperty) object;
083:
084: TypeProperty typeProperty = property.getTypeProperty();
085:
086: if (typeProperty.isEnumerable()) {
087: String[] items = typeProperty.getEnumeration();
088: cellEditor = new JComboBox(items);
089: // preselect the current value
090: ((JComboBox) cellEditor).setSelectedItem(property
091: .getValue());
092: if (typeProperty.isFreeTextAllowed()) {
093: ((JComboBox) cellEditor).setEditable(true);
094: } else {
095: ((JComboBox) cellEditor).setEditable(false);
096: }
097: ((JComboBox) cellEditor)
098: .addActionListener(new ActionListener() {
099: public void actionPerformed(ActionEvent e) {
100: JComboBox cb = (JComboBox) e
101: .getSource();
102: property.setValue((String) cb
103: .getSelectedItem());
104: fireEditingStopped();
105: }
106: });
107: if (property.isFinal()
108: || typeProperty.getMode().equals(
109: TypeProperty.READ_ONLY)
110: || typeProperty.getMode().equals(
111: TypeProperty.READ_ONLY_NOT_PERSISTENT)) {
112: ((JComboBox) cellEditor).setEditable(false);
113: }
114: } else {
115: if (typeProperty.getPropertyType().equals(
116: TypeProperty.PATH)) {
117: cellEditor = new JTextFieldDND(property.getValue());
118: ((JTextFieldDND) cellEditor).setDragEnabled(true);
119: } else {
120: cellEditor = new JTextField(property.getValue());
121: }
122: if (property.isFinal()
123: || typeProperty.getMode().equals(
124: TypeProperty.READ_ONLY)
125: || typeProperty.getMode().equals(
126: TypeProperty.READ_ONLY_NOT_PERSISTENT)) {
127: ((JTextField) cellEditor).setEditable(false);
128: } else {
129: ((JTextField) cellEditor)
130: .addActionListener(new ActionListener() {
131: public void actionPerformed(
132: ActionEvent e) {
133: property
134: .setValue(((JTextField) cellEditor)
135: .getText());
136: fireEditingStopped();
137: }
138: });
139: }
140: }
141: return cellEditor;
142: }
143:
144: /*
145: * (non-Javadoc)
146: *
147: * @see javax.swing.CellEditor#stopCellEditing()
148: */
149: public boolean stopCellEditing() {
150: if (cellEditor != null
151: && (cellEditor instanceof JTextField)) {
152: property.setValue(((JTextField) cellEditor).getText());
153: // fireEditingStopped();
154: }
155: return super .stopCellEditing();
156: }
157:
158: }
159:
160: class PropertyRenderer implements TableCellRenderer {
161: public java.awt.Component getTableCellRendererComponent(
162: JTable table, Object object, boolean isSelected,
163: boolean hasFocus, int row, int column) {
164: JLabel cellRenderer = new JLabel();
165: cellRenderer.setOpaque(true);
166: ComponentProperty property = (ComponentProperty) object;
167: if (column == 0) {
168: cellRenderer.setText(property.getName());
169: } else {
170: cellRenderer.setText(property.getValue());
171: }
172: if (property.getValue() != null
173: && property.getValue().equals(
174: property.getInitialValue())) {
175: cellRenderer.setForeground(Color.gray);
176: } else {
177: cellRenderer.setForeground(Color.black);
178: }
179: if (isSelected) {
180: cellRenderer.setBackground(propertyTable
181: .getSelectionBackground());
182: cellRenderer.setForeground(propertyTable
183: .getSelectionForeground());
184: } else {
185: if (property.isFinal()) {
186: cellRenderer.setBackground(Color.yellow);
187: } else {
188: cellRenderer.setBackground(propertyTable
189: .getBackground());
190: }
191: }
192: cellRenderer.setFont(propertyTable.getFont());
193: return cellRenderer;
194: }
195: }
196:
197: JTable propertyTable;
198: }
|