KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > swing > AbstractFieldView


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.swing;
19
20 import java.awt.Component JavaDoc;
21 import javax.swing.JComponent JavaDoc;
22 import javax.swing.JTable JavaDoc;
23 import javax.swing.UIManager JavaDoc;
24 import javax.swing.table.TableCellRenderer JavaDoc;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.aspects.gui.FieldUpdate;
27 import org.objectweb.jac.aspects.gui.Utils;
28 import org.objectweb.jac.core.rtti.FieldItem;
29
30 public abstract class AbstractFieldView extends AbstractView
31     implements FieldUpdate, TableCellRenderer JavaDoc
32 {
33     static Logger logger = Logger.getLogger("gui.table");
34
35     // substance and field are required so that we can register and
36
// unregister ourself from fieldUpdated events on close()
37
Object JavaDoc substance;
38     FieldItem field;
39
40     boolean isCellViewer = false;
41
42     public AbstractFieldView(Object JavaDoc substance, FieldItem field) {
43         this.substance = substance;
44         this.field = field;
45
46         Utils.registerField(substance,field,this);
47     }
48
49     public AbstractFieldView() {
50         substance = null;
51         field = null;
52     }
53
54     /**
55      * Sets the font of the component for use in a table cell
56      */

57     protected void setTableFont() {
58         JComponent JavaDoc component = getComponent();
59         if (component!=null) {
60             component.setFont(UIManager.getFont("Table.font"));
61         }
62     }
63
64     public abstract void setValue(Object JavaDoc value);
65
66     public void setSubstance(Object JavaDoc substance) {
67         Utils.unregisterField(this.substance,field,this);
68         this.substance = substance;
69         Utils.registerField(substance,field,this);
70     }
71
72     public Object JavaDoc getSubstance() {
73         return substance;
74     }
75
76     public void setField(FieldItem field) {
77         Utils.unregisterField(substance,this.field,this);
78         this.field = field;
79         Utils.registerField(substance,field,this);
80     }
81
82     public FieldItem getField() {
83         return field;
84     }
85
86     public void setAutoUpdate(boolean autoUpdate) {
87         // TODO ...
88
}
89
90     public void close(boolean validate) {
91         Utils.unregisterField(substance,field,this);
92     }
93    
94     // FieldUpdate interface
95
public void fieldUpdated(Object JavaDoc substance,
96                              FieldItem field, Object JavaDoc value,
97                              Object JavaDoc param) {
98         setValue(value);
99     }
100
101     // TableCellRenderer
102
public Component JavaDoc getTableCellRendererComponent(
103         JTable table, Object JavaDoc value,
104         boolean isSelected, boolean hasFocus,
105         int row, int column)
106     {
107         logger.debug(
108             this+".getTableCellRendererComponent("+row+","+column+","+isSelected+")");
109         JComponent JavaDoc component = getComponent();
110       
111         if (component!=null) {
112             component.setOpaque(true); // so that the background is really drawn
113
}
114         setOpaque(true); // so that the background is really drawn
115

116         if (isSelected) {
117             if (component!=null) {
118                 component.setForeground(table.getSelectionForeground());
119                 component.setBackground(table.getSelectionBackground());
120             }
121             setForeground(table.getSelectionForeground());
122             setBackground(table.getSelectionBackground());
123         } else {
124             if (component!=null) {
125                 component.setForeground(table.getForeground());
126                 component.setBackground(table.getBackground());
127             }
128             setForeground(table.getForeground());
129             setBackground(table.getBackground());
130         }
131         setValue(value);
132
133         return this;
134     }
135
136     /**
137     * Used by getTableCellRendererComponent. setForeground(),
138     * setBackground() and setFont() will be called on this component
139     * if it is not null; */

140     protected JComponent JavaDoc getComponent() {
141         return null;
142     }
143 }
144
Popular Tags