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,
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.Map;
024    import javax.servlet.http.HttpServletResponse;
025    import org.apache.log4j.Logger;
026    import org.objectweb.jac.aspects.gui.*;
027    import org.objectweb.jac.core.Collaboration;
028    import org.objectweb.jac.util.Semaphore;
029    
030    /**
031     * An HTML page containing a View, and an OK and a close Button
032     */
033    public class Dialog extends AbstractPage 
034        implements DialogView, DialogListener
035    {
036        static Logger loggerTimeout = Logger.getLogger("gui.timeout");
037        static Logger loggerEvents = Logger.getLogger("gui.events");
038    
039        boolean ok = false;
040        Semaphore semaphore = new Semaphore();
041        String description;
042        HttpServletResponse response;
043        JacRequest jacRequest;
044        /** Stores context attributes at creation time */
045        Map attributes;
046    
047        /**
048         * @param view the view to embed in the dialog
049         * @param parent the parent window of the dialog
050         * @param title the title
051         * @param description description of the view
052         */
053        public Dialog(ViewFactory factory, DisplayContext context,
054                      View view, Object parent,
055                      String title, String description) 
056        {
057            super(factory,context,view,false);
058            this.description = description;
059            attributes = Collaboration.get().getAttributes();   
060            attributes.remove(WebDisplay.REQUEST);
061            attributes.remove(WebDisplay.RESPONSE);
062            //System.out.println("Stored attributes: "+attributes);
063            /*
064            if (view instanceof org.objectweb.jac.aspects.gui.EditorContainer) {
065                Iterator i = 
066                    ((org.objectweb.jac.aspects.gui.EditorContainer)view).getEditors().iterator();
067            */
068                Iterator i = context.getEditors().iterator();
069                while (i.hasNext()) {
070                    Object editor = i.next();
071                    if (editor instanceof HTMLEditor) {
072                        ((HTMLEditor)editor).setAttribute(
073                            "onkeypress",
074                            "return commitFormOnEnter(event,this,'event=onRefresh&source="+getId()+"')\"");
075                    }
076                }            
077                //        }
078        }
079    
080        public HttpServletResponse getResponse() {
081            return response;
082        }
083    
084        public JacRequest getRequest() {
085            return jacRequest;
086        }
087    
088        // DialogView interface
089    
090        public boolean waitForClose() throws TimeoutException {
091            loggerTimeout.debug("waiting for "+this+" to be closed "+
092                      GuiAC.dialogTimeout+"ms");
093            if (!semaphore.acquire(GuiAC.dialogTimeout)) {
094                loggerTimeout.debug("Dialog timedout: "+this);
095                throw new TimeoutException(this);
096            }
097            loggerEvents.debug("closed "+this+" -> "+ok);
098            return ok;
099        }
100    
101        public View getContentView() {
102            return view;
103        }
104    
105        // HTMLViewer interface
106    
107        public void genHTML(PrintWriter out) throws IOException {
108            Collaboration c = Collaboration.get();
109            // pressing "enter" in an editor should call "refresh"
110            c.addAttribute(WebDisplay.ON_ENTER_ACTION, "event=onRefresh");
111            try {
112                super.genHTML(out);
113            } finally {
114                c.removeAttribute(WebDisplay.ON_ENTER_ACTION);
115            }
116        }
117    
118        public void genBody(PrintWriter out) throws IOException {
119            out.println("<div class=\""+type+"\">");
120            if (description!=null)
121                out.println("<div class=\"description\">"+description+"</div>");
122            openForm(out);
123            ((HTMLViewer)view).genHTML(out);
124            showFormButtons(out);
125            closeForm(out);
126            out.println("</div>");
127        }
128    
129        // DialogListener interface
130    
131        public void restoreContext() {
132            loggerEvents.debug("Restoring attributes: "+attributes.keySet());
133            Collaboration.get().setAttributes(attributes);
134        }
135    
136        public void onOK(JacRequest request) {
137            loggerEvents.debug(this+".onOK");
138            restoreContext();
139            WebDisplay display = (WebDisplay)context.getDisplay();
140            response = WebDisplay.getResponse();
141            jacRequest = WebDisplay.getRequest();
142            display.closeWindow(this,true);
143            ok = true;
144            semaphore.release();
145        }
146    
147        public void onRefresh(JacRequest request) {
148            loggerEvents.debug(this+".onRefresh");
149            WebDisplay display = (WebDisplay)context.getDisplay();
150            restoreContext();
151            response = WebDisplay.getResponse();
152            jacRequest = WebDisplay.getRequest();
153            WebDisplay.readValuesAndRefresh(context,request,true);
154        }
155    
156        public void onValidate(JacRequest request) {
157            restoreContext();
158            WebDisplay.readValues(context,request,true);
159        }
160    
161        public void onCancel() {
162            loggerEvents.debug(this+".onCancel");
163            ok = false;
164            Collaboration collab = Collaboration.get();
165            try {
166                WebDisplay display = (WebDisplay)context.getDisplay();
167                response = WebDisplay.getResponse();
168                jacRequest = WebDisplay.getRequest();
169                //collab.addAttribute(GuiAC.NO_COMMIT,Boolean.TRUE);
170                display.closeWindow(this,false);
171            } finally {
172                //collab.addAttribute(GuiAC.NO_COMMIT,null);
173                semaphore.release();
174            }
175        }
176    }