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.web;
019    
020    import org.apache.log4j.Logger;
021    import org.objectweb.jac.aspects.gui.FieldUpdate; 
022    import org.objectweb.jac.aspects.gui.FieldView; 
023    import org.objectweb.jac.aspects.gui.GuiAC;
024    import org.objectweb.jac.aspects.gui.Utils; 
025    import org.objectweb.jac.core.rtti.FieldItem;
026    import org.objectweb.jac.util.Stack;
027    
028    /**
029     * Base class for field views
030     */
031    public abstract class AbstractFieldView extends AbstractView 
032       implements FieldUpdate, FieldView
033    {
034        static Logger logger = Logger.getLogger("gui.autoupdate");
035    
036        // substance and field are required so that we can register and
037        // unregister ourself from fieldUpdated events on close()
038        protected Object substance;
039        protected FieldItem field;
040        protected boolean autoUpdate = true;
041    
042        public AbstractFieldView(Object substance, FieldItem field) {
043            this.substance = substance;
044            this.field = field;
045    
046            if (autoUpdate)
047                Utils.registerField(substance,field,this);
048    
049            if (GuiAC.getGraphicContext()!=null)
050                contexts.addAll(GuiAC.getGraphicContext());
051            if (field!=null)
052                contexts.push(field);
053        }
054    
055        Stack contexts = new Stack();
056    
057        public AbstractFieldView() {
058            if (GuiAC.getGraphicContext()!=null)
059                contexts.addAll(GuiAC.getGraphicContext());
060        }
061    
062        public abstract void setValue(Object value);
063    
064        public void setSubstance(Object substance) {
065            logger.debug(this+".setSubstance,  autoUpdate="+autoUpdate);
066            if (autoUpdate)
067                Utils.unregisterField(this.substance,field,this);
068            this.substance = substance;
069            if (autoUpdate)
070                Utils.registerField(substance,field,this);
071        }
072    
073        public Object getSubstance() {
074            return substance;
075        }    
076    
077        public void setField(FieldItem field) {
078            logger.debug(this+".setField,  autoUpdate="+autoUpdate);
079            if (autoUpdate)
080                Utils.unregisterField(substance,this.field,this);
081            this.field = field;
082            if (autoUpdate)
083                Utils.registerField(substance,field,this);
084            if (field!=null)
085                contexts.push(field);
086        }
087    
088        public FieldItem getField() {
089            return field;
090        }
091    
092        public void setAutoUpdate(boolean autoUpdate) {
093            logger.debug(this+".setAutoUpdate "+autoUpdate);
094            if (this.autoUpdate!=autoUpdate) {
095                if (this.autoUpdate)
096                    Utils.unregisterField(substance,field,this);
097                this.autoUpdate = autoUpdate;
098                if (autoUpdate)
099                    Utils.registerField(substance,field,this);            
100            }
101        }
102    
103        public void close(boolean validate) {
104            if (autoUpdate)
105                Utils.unregisterField(substance,field,this);
106        }
107       
108        // FieldUpdate interface
109        public void fieldUpdated(Object substance, FieldItem field, Object value, Object param) {
110            setValue(value);
111        }
112    
113    }