001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.properties;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.beans.PropertyEditor;
025:
026: import javax.swing.JPanel;
027: import javax.swing.JScrollPane;
028: import javax.swing.JTable;
029: import javax.swing.event.TableModelEvent;
030: import javax.swing.event.TableModelListener;
031:
032: import com.jeta.forms.gui.beans.JETABean;
033:
034: /**
035: * This pane displays the properties for a Java Bean
036: *
037: * @author Jeff Tassin
038: */
039: public class PropertyPane extends JPanel implements TableModelListener {
040: private JETABean m_bean; // Current Bean.
041:
042: private JTable m_table;
043: private PropertyColumnModel m_columnModel;
044: private PropertyTableModel m_tablemodel;
045: private PropertyValueEditor m_editor;
046: private PropertyValueRenderer m_renderer;
047:
048: private static final int ROW_HEIGHT = 20;
049:
050: /**
051: * Constructor
052: *
053: * @param basic
054: * set to true if you only want to show preferred properties
055: */
056: public PropertyPane(boolean basic) {
057: super (new BorderLayout());
058:
059: m_tablemodel = new PropertyTableModel();
060: if (basic)
061: m_tablemodel.setFilter(PropertyTableModel.VIEW_PREFERRED);
062:
063: m_tablemodel.addTableModelListener(this );
064:
065: m_columnModel = new PropertyColumnModel();
066: m_table = new JTable(m_tablemodel, m_columnModel);
067: m_table.setShowGrid(true);
068: m_table.setRowHeight(ROW_HEIGHT);
069: m_table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
070:
071: m_renderer = new PropertyValueRenderer(m_tablemodel);
072: m_editor = new PropertyValueEditor(m_tablemodel);
073: m_columnModel.getColumn(PropertyTableModel.COL_VALUE)
074: .setCellRenderer(m_renderer);
075: m_columnModel.getColumn(PropertyTableModel.COL_VALUE)
076: .setCellEditor(m_editor);
077:
078: add(new JScrollPane(m_table), BorderLayout.CENTER);
079: }
080:
081: public JETABean getBean() {
082: return m_bean;
083: }
084:
085: PropertyTableModel getTableModel() {
086: return m_tablemodel;
087: }
088:
089: /**
090: * Sets the state of the up and down buttons based on the contents of the
091: * stack.
092: */
093: private void setButtonState() {
094: }
095:
096: /**
097: * Sets the PropertyPane to show the properties of the named bean.
098: */
099: protected void setBean(JETABean bean) {
100: if (m_table.isEditing()) {
101: m_editor.stopCellEditing();
102: }
103: m_bean = bean;
104: m_tablemodel.setBean(bean);
105: }
106:
107: /**
108: * Cancels any editing in the property table
109: */
110: public void cancelEditing() {
111: try {
112: if (m_table.isEditing()) {
113: m_editor.cancelCellEditing();
114: }
115: } catch (Exception e) {
116: e.printStackTrace();
117: }
118: }
119:
120: /**
121: * Stops any editing in the property table
122: */
123: public void stopEditing() {
124: try {
125: if (m_table.isEditing()) {
126: m_editor.stopCellEditing();
127: }
128: } catch (Exception e) {
129: e.printStackTrace();
130: }
131: }
132:
133: /**
134: * TableModelListener Implementation
135: */
136: public void tableChanged(TableModelEvent evt) {
137: m_table.setRowHeight(ROW_HEIGHT);
138:
139: for (int i = 0; i < m_table.getRowCount(); i++) {
140: PropertyEditor editor = m_tablemodel.getPropertyEditor(i);
141: if (editor != null) {
142: Component comp = editor.getCustomEditor();
143: if (comp != null) {
144: Dimension prefsize = comp.getPreferredSize();
145: if (prefsize.height != m_table.getRowHeight(i)) {
146: m_table.setRowHeight(i, prefsize.height);
147: }
148: }
149: }
150: }
151: }
152:
153: public void updateUI() {
154: super .updateUI();
155:
156: if (m_table != null) {
157: m_table.updateUI();
158: m_table.getTableHeader().updateUI();
159: }
160:
161: if (m_renderer != null)
162: m_renderer.updateUI();
163:
164: if (m_editor != null)
165: m_editor.updateUI();
166: }
167:
168: public class DeletePropertyAction extends
169: javax.swing.AbstractAction {
170: public void actionPerformed(java.awt.event.ActionEvent evt) {
171: System.out
172: .println("PropertyPane.delete key hit on table.. ");
173: }
174: }
175: }
|