001    /*
002      Copyright (C) 2002 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 javax.swing.JLabel;
021    import org.apache.log4j.Logger;
022    import org.objectweb.jac.aspects.gui.FieldUpdate;
023    import org.objectweb.jac.aspects.gui.FieldView;
024    import org.objectweb.jac.aspects.gui.GuiAC;
025    import org.objectweb.jac.core.rtti.FieldItem;
026    import org.objectweb.jac.util.Enum;
027    import org.objectweb.jac.util.InvalidIndexException;
028    
029    public class EnumViewer extends AbstractFieldView 
030        implements FieldView, FieldUpdate
031    {
032        static Logger logger = Logger.getLogger("gui");
033    
034        Object value;
035        Enum enum;
036        JLabel label = new JLabel ();
037    
038        public EnumViewer(Object value,
039                          Object substance, FieldItem field) {
040            super(substance,field);
041            if (field!=null)
042                this.enum = (Enum)field.getAttribute(GuiAC.FIELD_ENUM);
043            else
044                logger.warn("EnumViewer: Cannot determine enum because field is null");        
045            setValue(value);
046            add(label);
047        }
048    
049        public EnumViewer() {
050            label.setFont(null);
051            add(label);
052        }
053    
054        public void setField(FieldItem field) {
055            super.setField(field);
056            if (field!=null)
057                this.enum = (Enum)field.getAttribute(GuiAC.FIELD_ENUM);
058            else
059                logger.warn("EnumViewer: Cannot determine enum because field is null");        
060        }
061    
062        public void setValue(Object value) {
063            this.value = value;
064            try {
065                label.setText(value!=null?enum.int2string(((Integer)value).intValue()):"null");
066            } catch (InvalidIndexException e) {
067                label.setText(value.toString());
068            }
069        }
070    }