001 /* 002 Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program 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 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.gui.swing; 019 020 import java.awt.Component; 021 import javax.swing.JComponent; 022 import javax.swing.JTable; 023 import javax.swing.UIManager; 024 import javax.swing.table.TableCellRenderer; 025 import org.apache.log4j.Logger; 026 import org.objectweb.jac.aspects.gui.FieldUpdate; 027 import org.objectweb.jac.aspects.gui.Utils; 028 import org.objectweb.jac.core.rtti.FieldItem; 029 030 public abstract class AbstractFieldView extends AbstractView 031 implements FieldUpdate, TableCellRenderer 032 { 033 static Logger logger = Logger.getLogger("gui.table"); 034 035 // substance and field are required so that we can register and 036 // unregister ourself from fieldUpdated events on close() 037 Object substance; 038 FieldItem field; 039 040 boolean isCellViewer = false; 041 042 public AbstractFieldView(Object substance, FieldItem field) { 043 this.substance = substance; 044 this.field = field; 045 046 Utils.registerField(substance,field,this); 047 } 048 049 public AbstractFieldView() { 050 substance = null; 051 field = null; 052 } 053 054 /** 055 * Sets the font of the component for use in a table cell 056 */ 057 protected void setTableFont() { 058 JComponent component = getComponent(); 059 if (component!=null) { 060 component.setFont(UIManager.getFont("Table.font")); 061 } 062 } 063 064 public abstract void setValue(Object value); 065 066 public void setSubstance(Object substance) { 067 Utils.unregisterField(this.substance,field,this); 068 this.substance = substance; 069 Utils.registerField(substance,field,this); 070 } 071 072 public Object getSubstance() { 073 return substance; 074 } 075 076 public void setField(FieldItem field) { 077 Utils.unregisterField(substance,this.field,this); 078 this.field = field; 079 Utils.registerField(substance,field,this); 080 } 081 082 public FieldItem getField() { 083 return field; 084 } 085 086 public void setAutoUpdate(boolean autoUpdate) { 087 // TODO ... 088 } 089 090 public void close(boolean validate) { 091 Utils.unregisterField(substance,field,this); 092 } 093 094 // FieldUpdate interface 095 public void fieldUpdated(Object substance, 096 FieldItem field, Object value, 097 Object param) { 098 setValue(value); 099 } 100 101 // TableCellRenderer 102 public Component getTableCellRendererComponent( 103 JTable table, Object value, 104 boolean isSelected, boolean hasFocus, 105 int row, int column) 106 { 107 logger.debug( 108 this+".getTableCellRendererComponent("+row+","+column+","+isSelected+")"); 109 JComponent 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 getComponent() { 141 return null; 142 } 143 }