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 java.io.IOException;
021    import java.io.PrintWriter;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Vector;
025    import javax.servlet.http.HttpServletResponse;
026    import org.objectweb.jac.aspects.gui.Constants;
027    import org.objectweb.jac.aspects.gui.FieldEditor;
028    import org.objectweb.jac.aspects.gui.GuiAC;
029    
030    public class EditorContainer extends Container
031        implements org.objectweb.jac.aspects.gui.EditorContainer, DialogListener
032    {
033        Vector editors = new Vector();
034        boolean showButtons;
035    
036        /**
037         * @param showButtons wether to show an OK and a Cancel button
038         */
039        public EditorContainer(boolean showButtons) {
040            super(Constants.VERTICAL);
041            this.showButtons = showButtons;
042        }
043    
044        public void addEditor(Object editor) {
045            editors.add(editor);
046        }
047       
048        public void removeEditor(Object editor) {
049            editors.remove(editor);
050        }
051       
052        public List getEditors() {
053            return (List)editors.clone();
054        }
055    
056        public boolean hasEnabledEditor() {
057            Iterator it = editors.iterator();
058            while (it.hasNext()) {
059                Object view = it.next();
060                if (view instanceof FieldEditor && 
061                    ((FieldEditor)view).isEnabled()) {
062                    return true;
063                }
064            }
065            return false;
066        }
067    
068        public void setShowButtons(boolean value) {
069            this.showButtons = value;
070        }
071        public boolean showButtons() {
072            return showButtons;
073        }
074    
075        public void genHTML(PrintWriter out) throws IOException {
076            out.println("<div class=\""+type+"\">");
077            if (!editors.isEmpty() && showButtons) {
078                out.println("<form action=\""+
079                            ((WebDisplay)context.getDisplay()).getServletName()+"\""+
080                            " accept-charset=\"utf-8\">");
081            }
082            genItemsHTML(out);
083            if (!editors.isEmpty() && showButtons) {
084                out.println("  <div class=\"actions\">");
085                out.println("    <input type=\"hidden\" name=\"source\" value=\""+getId()+"\">");
086                out.println("    <button class=button type=submit name=event "+
087                            "value=\"onOK\">OK</button>");
088                out.println("    <button class=\"button\" type=\"submit\" name=\"event\" "+
089                            "value=\"onCancel\">"+GuiAC.getLabelCancel()+"</button>");
090                out.println("  </div>");
091                out.println("</form>");
092            }
093            out.println("</div>");
094        }
095    
096        // DialogListener interface
097    
098        public void onOK(JacRequest request) {
099            onValidate(request);
100            ((WebDisplay)context.getDisplay()).refresh();
101        }
102    
103        public void onRefresh(JacRequest request) {
104            onValidate(request);
105            ((WebDisplay)context.getDisplay()).refresh();
106        }
107    
108        public void onValidate(JacRequest request){
109            Iterator i = editors.iterator();
110            while (i.hasNext()) {
111                FieldEditor editor = (FieldEditor)i.next();
112                ((HTMLEditor)editor).readValue(request.getParameter(editor.getLabel()));
113                ((HTMLEditor)editor).commit();
114            }
115        }
116    
117        public void onCancel() {
118            ((WebDisplay)context.getDisplay()).refresh();
119        }
120    
121        public void restoreContext() {}
122    
123        public HttpServletResponse getResponse() {
124            return null;
125        }
126    
127        public JacRequest getRequest() {
128            return null;
129        }
130    
131    }