001    /*
002      Copyright (C) 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 java.io.IOException;
021    import java.io.PrintWriter;
022    import java.util.List;
023    import org.objectweb.jac.aspects.gui.EventHandler;
024    import org.objectweb.jac.aspects.gui.FieldEditor;
025    import org.objectweb.jac.aspects.gui.InvokeEvent;
026    import org.objectweb.jac.aspects.gui.MethodView;
027    import org.objectweb.jac.core.rtti.AbstractMethodItem;
028    import org.objectweb.jac.core.rtti.MethodItem;
029    
030    public class EmbeddedMethod extends AbstractCompositeView
031        implements MethodView, MethodListener
032    {
033        Object substance;
034        AbstractMethodItem method;
035        String icon;
036        EditorContainer parameters;
037    
038        public EmbeddedMethod(Object substance, AbstractMethodItem method, 
039                              EditorContainer parameters) {
040            this.substance = substance;
041            this.method = method;
042            this.parameters = parameters;
043        }
044    
045        // MethodView interface
046    
047        public void setMethod(AbstractMethodItem method) {
048            this.method = method;
049        }
050    
051        public void setIcon(String icon) {
052            this.icon = icon;
053        }
054    
055        boolean onlyIcon = false;
056        public void setOnlyIcon(boolean onlyIcon) {
057            this.onlyIcon = onlyIcon;
058        }
059    
060        /**
061         * Returns the text of the button
062         */
063        protected String getText() {
064            if (method instanceof MethodItem && 
065                ((MethodItem)method).isSetter() && icon!=null)
066                return "";
067            else
068                return label;
069        }
070    
071        // HTMLViewer interface
072    
073        public void genHTML(PrintWriter out) throws IOException {
074            JacRequest request = WebDisplay.getRequest();
075            parameters.genHTML(out);
076            if (request.isIEUserAgent()) {
077                out.print("<table class=\"method\"><tr><td>"+
078                          iconElement(icon,label,false)+
079                          eventURL(getText(),"onInvoke","")+
080                          "</td></tr></table>");
081            } else {
082                out.print("<span class=\"method\">"+
083                          iconElement(icon,label,false)+
084                          eventURL(getText(),"onInvoke","")+
085                          "</span>");
086            }
087        }
088    
089        // MethodListener interface
090    
091        public void onInvoke() {
092            List editors = parameters.getEditors();
093            Object[] params = new Object[editors.size()];
094            JacRequest request = WebDisplay.getRequest();
095            for (int i=0; i<params.length; i++) {
096                FieldEditor editor = (FieldEditor)editors.get(i);
097                ((HTMLEditor)editor).readValue(request.getParameter(editor.getLabel()));
098                params[i] = editor.getValue();
099            }
100            EventHandler.get().onInvoke(
101                context,
102                new InvokeEvent(this,substance,method,params),
103                false,null,null);
104        }
105    }