KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > Dialog


1 /*
2   Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import org.apache.log4j.Logger;
26 import org.objectweb.jac.aspects.gui.*;
27 import org.objectweb.jac.core.Collaboration;
28 import org.objectweb.jac.util.Semaphore;
29
30 /**
31  * An HTML page containing a View, and an OK and a close Button
32  */

33 public class Dialog extends AbstractPage
34     implements DialogView, DialogListener
35 {
36     static Logger loggerTimeout = Logger.getLogger("gui.timeout");
37     static Logger loggerEvents = Logger.getLogger("gui.events");
38
39     boolean ok = false;
40     Semaphore semaphore = new Semaphore();
41     String JavaDoc description;
42     HttpServletResponse JavaDoc response;
43     JacRequest jacRequest;
44     /** Stores context attributes at creation time */
45     Map JavaDoc attributes;
46
47     /**
48      * @param view the view to embed in the dialog
49      * @param parent the parent window of the dialog
50      * @param title the title
51      * @param description description of the view
52      */

53     public Dialog(ViewFactory factory, DisplayContext context,
54                   View view, Object JavaDoc parent,
55                   String JavaDoc title, String JavaDoc description)
56     {
57         super(factory,context,view,false);
58         this.description = description;
59         attributes = Collaboration.get().getAttributes();
60         attributes.remove(WebDisplay.REQUEST);
61         attributes.remove(WebDisplay.RESPONSE);
62         //System.out.println("Stored attributes: "+attributes);
63
/*
64         if (view instanceof org.objectweb.jac.aspects.gui.EditorContainer) {
65             Iterator i =
66                 ((org.objectweb.jac.aspects.gui.EditorContainer)view).getEditors().iterator();
67         */

68             Iterator JavaDoc i = context.getEditors().iterator();
69             while (i.hasNext()) {
70                 Object JavaDoc editor = i.next();
71                 if (editor instanceof HTMLEditor) {
72                     ((HTMLEditor)editor).setAttribute(
73                         "onkeypress",
74                         "return commitFormOnEnter(event,this,'event=onRefresh&amp;source="+getId()+"')\"");
75                 }
76             }
77             // }
78
}
79
80     public HttpServletResponse JavaDoc getResponse() {
81         return response;
82     }
83
84     public JacRequest getRequest() {
85         return jacRequest;
86     }
87
88     // DialogView interface
89

90     public boolean waitForClose() throws TimeoutException {
91         loggerTimeout.debug("waiting for "+this+" to be closed "+
92                   GuiAC.dialogTimeout+"ms");
93         if (!semaphore.acquire(GuiAC.dialogTimeout)) {
94             loggerTimeout.debug("Dialog timedout: "+this);
95             throw new TimeoutException(this);
96         }
97         loggerEvents.debug("closed "+this+" -> "+ok);
98         return ok;
99     }
100
101     public View getContentView() {
102         return view;
103     }
104
105     // HTMLViewer interface
106

107     public void genHTML(PrintWriter JavaDoc out) throws IOException JavaDoc {
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 JavaDoc out) throws IOException JavaDoc {
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 }
177
Popular Tags