001    /*
002      Copyright (C) 2002-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, but
010      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 program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui.web;
020    
021    import org.objectweb.jac.aspects.gui.*;
022    import org.objectweb.jac.core.rtti.FieldItem;
023    import java.io.PrintWriter;
024    
025    /**
026     * This class defines a component view for references in
027     * objects.
028     *
029     * <p>By default this view constructs an embedded <code>JLabel</code>
030     * containing the string representation of the referenced object. However,
031     * the field can be attributed to be displayed with a customized
032     * rendering by the GUI aspect component.
033     *
034     * @see GuiAC
035     * @see FieldView 
036     */
037    
038    public class ReferenceView extends AbstractFieldView 
039        implements FieldView, FieldUpdate, ObjectUpdate, 
040                  HTMLViewer, SelectionListener, LinkGenerator 
041    {
042        protected Object object;
043        protected String text;
044        protected String eventURL;
045    
046        /**
047         * Constructs a new reference view.
048         *
049         * @param substance the object the viewed field belongs to */
050    
051        public ReferenceView(Object value, Object substance, FieldItem reference) {
052            super(substance,reference);
053            this.object = value;
054    
055            if (autoUpdate)
056                Utils.registerField(substance,reference,this);
057    
058            refreshView();
059        }
060    
061        public ReferenceView() {
062        }
063    
064        boolean enableLinks = true;
065        public void setEnableLinks(boolean enable) {
066            this.enableLinks = enable;
067        }
068        public boolean areLinksEnabled() {
069            return enableLinks;
070        }
071    
072        public void refreshView() {
073            if (autoUpdate)
074                Utils.registerObject(object,this);
075            if (object!=null) {
076                text = GuiAC.toString(object,contexts);
077            } else {
078                text = "";
079            }
080        }
081    
082        /**
083         * Set the URL to link to.
084         */
085        public void setEventURL(String eventURL) {
086            this.eventURL = eventURL;
087        }
088    
089        // FieldView interface
090    
091        public void setValue(Object value) {
092            if (autoUpdate)
093                Utils.unregisterObject(object,this);
094            this.object = value;
095            refreshView();
096        }
097    
098        public void close(boolean validate) {
099            super.close(validate);
100            if (autoUpdate)
101                Utils.unregisterObject(object,this);
102        }
103    
104        // FieldUpdate interface
105    
106        public void fieldUpdated(Object substance, FieldItem field, Object value, Object param) {
107            setValue(value);
108        }
109    
110        // ObjectUpdate interface
111    
112        public void objectUpdated(Object object, Object param) {
113            refreshView();
114        }
115    
116        // HTMLViewer interface
117        public void genHTML(PrintWriter out) {
118            if (enableLinks)
119                out.print("<a href=\""+
120                          (eventURL!=null?eventURL:eventURL("onSelection"))+"\">"+text+"</a>");
121            else
122                out.print(text);
123        }
124    
125        // SelectionListener interface
126    
127        public void onSelection() {
128            EventHandler.get().onSelection(context,field,object,null,null,true);
129        }
130    
131    }