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.Color;
022: import java.awt.Component;
023: import java.awt.Font;
024: import java.awt.FontMetrics;
025: import java.awt.Graphics;
026: import java.awt.Rectangle;
027: import java.beans.PropertyEditor;
028:
029: import javax.swing.JComponent;
030: import javax.swing.JTable;
031: import javax.swing.UIManager;
032: import javax.swing.table.TableCellRenderer;
033:
034: import com.jeta.forms.gui.common.FormUtils;
035:
036: public class PropertyValueRenderer extends JComponent implements
037: TableCellRenderer {
038: /**
039: * @directed
040: */
041: private PropertyTableModel m_model;
042:
043: private PropertyEditor m_editor;
044:
045: /**
046: * A cache of editors for each row
047: */
048: private PropertyEditorCache m_cache;
049:
050: /**
051: * Used for storing bounds of component for painting
052: */
053: private Rectangle m_bounds = new Rectangle();
054:
055: /**
056: * The current object value
057: */
058: private Object m_value;
059:
060: private int m_row;
061:
062: /**
063: * The font for this component
064: */
065: private Font m_font;
066:
067: private Color m_selbg;
068: private Color m_selfg;
069: private Color m_bg;
070: private Color m_fg;
071:
072: /**
073: * ctor
074: */
075: public PropertyValueRenderer(PropertyTableModel tmodel) {
076: m_model = tmodel;
077: m_cache = new PropertyEditorCache(tmodel);
078:
079: }
080:
081: /**
082: * TableCellRenderer implementation
083: */
084: public Component getTableCellRendererComponent(JTable table,
085: Object obj, boolean isSelected, boolean hasFocus, int row,
086: int col) {
087:
088: Component result = this ;
089: m_value = obj;
090:
091: try {
092: m_editor = m_cache.getPropertyEditor(row);
093: if (m_editor != null) {
094: m_editor.setValue(obj);
095: if (m_editor instanceof JETAPropertyEditor) {
096: JETAPropertyEditor jpe = (JETAPropertyEditor) m_editor;
097: if (!jpe.isPaintable()) {
098: result = jpe.getCustomEditor();
099: }
100: }
101: }
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105:
106: return result;
107: }
108:
109: /**
110: * For renderering JComponent
111: */
112: public void paintComponent(Graphics g) {
113: try {
114: if (m_font == null)
115: cacheProperties();
116:
117: if (m_editor != null && m_editor.isPaintable()) {
118: m_bounds.setBounds(0, 0, getWidth(), getHeight());
119: m_editor.paintValue(g, m_bounds);
120: } else {
121: // if we are here, there is no editor available for the given
122: // type
123:
124: int height = getHeight();
125: g.setColor(m_bg);
126:
127: g.fillRect(0, 0, getWidth(), height);
128:
129: Color fg = getForeground();
130: if (fg != null)
131: g.setColor(fg);
132:
133: g.setFont(m_font);
134: FontMetrics fm = getFontMetrics(m_font);
135: assert (fm != null);
136: int line_height = fm.getHeight();
137: int y = height - (height - line_height) / 2
138: - fm.getDescent();
139: if (m_value == null) {
140: g.drawString("null", 0, y);
141: } else {
142: g.drawString(m_value.toString(), 0, y);
143: }
144: }
145: } catch (Exception e) {
146: e.printStackTrace();
147: }
148: }
149:
150: public void updateUI() {
151: super .updateUI();
152: m_cache.updateUI();
153: if (m_editor != null) {
154: java.awt.Component comp = m_editor.getCustomEditor();
155: FormUtils.updateLookAndFeel(comp);
156: }
157:
158: m_font = null;
159: }
160:
161: private void cacheProperties() {
162: m_font = UIManager.getFont("Table.font");
163: m_selbg = UIManager.getColor("Table.selectionBackground");
164: m_selfg = UIManager.getColor("Table.selectionForeground");
165: m_fg = UIManager.getColor("Table.foreground");
166: m_bg = UIManager.getColor("TextField.background");
167: }
168:
169: }
|