KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Dimension JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.apache.log4j.Logger;
29 import org.objectweb.jac.aspects.cache.MethodCache;
30 import org.objectweb.jac.aspects.gui.*;
31 import org.objectweb.jac.aspects.gui.web.html.*;
32 import org.objectweb.jac.aspects.timestamp.Timestamps;
33 import org.objectweb.jac.core.rtti.FieldItem;
34 import org.objectweb.jac.core.rtti.MethodItem;
35 import org.objectweb.jac.util.Images;
36 import org.objectweb.jac.util.ObjectArray;
37 import org.objectweb.jac.util.Strings;
38
39 public abstract class AbstractView implements View {
40     static Logger logger = Logger.getLogger("web");
41     static Logger loggerClose = Logger.getLogger("gui.close");
42     static Logger loggerDisplay = Logger.getLogger("display");
43     static Logger loggerContext = Logger.getLogger("display.context");
44    
45     protected String JavaDoc label;
46     protected DisplayContext context;
47     protected Length width;
48     protected Length height;
49     ViewFactory factory;
50
51     // style used to change display (css for web)
52
String JavaDoc style;
53
54     Object JavaDoc[] parameters;
55     String JavaDoc type;
56
57     /** row number, for TableCellViewer */
58     protected boolean isCellViewer = false;
59     protected int row;
60     protected int column;
61     protected View table;
62
63     public AbstractView() {
64     }
65
66     public AbstractView(ViewFactory factory, DisplayContext context) {
67         this.factory = factory;
68         this.context = context;
69     }
70
71     Border viewBorder;
72    
73     /**
74      * Get the value of viewBorder.
75      * @return value of viewBorder.
76      */

77     public Border getViewBorder() {
78         return viewBorder;
79     }
80    
81     /**
82      * Set the value of viewBorder.
83      * @param v Value to assign to viewBorder.
84      */

85     public void setViewBorder(Border v) {
86         this.viewBorder = v;
87     }
88    
89     MethodItem message;
90    
91     /**
92      * Get the value of message.
93      * @return value of message.
94      */

95     public MethodItem getMessage() {
96         return message;
97     }
98    
99     /**
100      * Set the value of message.
101      * @param v Value to assign to message.
102      */

103     public void setMessage(MethodItem v) {
104         this.message = v;
105     }
106
107     protected String JavaDoc description;
108    
109     /**
110      * Get the value of description.
111      * @return value of description.
112      */

113     public String JavaDoc getDescription() {
114         return description;
115     }
116    
117     /**
118      * Set the value of description.
119      * @param v Value to assign to description.
120      */

121     public void setDescription(String JavaDoc v) {
122         this.description = v;
123     }
124
125     protected View parentView;
126    
127     /**
128      * Get the value of parent.
129      * @return value of parent.
130      */

131     public View getParentView() {
132         return parentView;
133     }
134    
135     /**
136      * Set the value of parent.
137      * @param v Value to assign to parent.
138      */

139     public void setParentView(View v) {
140         this.parentView = v;
141     }
142
143     public View getRootView() {
144         if (parentView==null)
145             return this;
146         return parentView.getRootView();
147     }
148
149     public boolean isDescendantOf(View ancestor) {
150         if (this==ancestor)
151             return true;
152         else if (parentView==null)
153             return false;
154         else
155             return parentView.isDescendantOf(ancestor);
156     }
157
158     public void setContext(DisplayContext context) {
159         loggerContext.debug("setContext on "+this);
160         this.context = context;
161     }
162
163     public DisplayContext getContext() {
164         return context;
165     }
166
167     public void setFactory(ViewFactory factory) {
168         this.factory = factory;
169     }
170
171     public ViewFactory getFactory() {
172         return factory;
173     }
174
175     public void setLabel(String JavaDoc label) {
176         this.label = label;
177     }
178
179     public String JavaDoc getLabel() {
180         return label;
181     }
182
183     public void setSize(Length width, Length height) {
184         this.width = width;
185         this.height = height;
186     }
187
188     public void setType(String JavaDoc type) {
189         this.type = type;
190     }
191
192     public String JavaDoc getType() {
193         return type;
194     }
195
196     public void setStyle(String JavaDoc style) {
197         this.style = style;
198     }
199
200     public String JavaDoc getStyle() {
201         return style;
202     }
203
204     public void setParameters(Object JavaDoc[] parameters) {
205         this.parameters = parameters;
206     }
207    
208     public Object JavaDoc[] getParameters() {
209         return parameters;
210     }
211
212     public void setFocus(FieldItem field, Object JavaDoc option) {
213     }
214
215     public void close(boolean validate) {
216         loggerClose.debug("closing "+this);
217         closed = true;
218         ((WebDisplay)context.getDisplay()).unregisterView(this);
219     }
220
221     boolean closed = false;
222
223     public boolean isClosed() {
224         return closed;
225     }
226
227     public boolean equalsView(ViewIdentity view) {
228         return
229             ( ( type!=null &&
230                 type.equals(view.getType()) )
231               || (type==null && view.getType()==null ) )
232             && ( ( parameters!=null &&
233                    Arrays.equals(parameters,view.getParameters()) )
234                  || (parameters==null && view.getParameters()==null) );
235     }
236
237     public boolean equalsView(String JavaDoc type, Object JavaDoc[] parameters) {
238         return this.type.equals(type)
239             && Arrays.equals(this.parameters,parameters);
240     }
241
242     /**
243      * Are we in a <FORM> element ?
244      */

245     protected boolean isInForm() {
246         return true;
247     }
248    
249     /**
250      * Build an HTML element for an event. It takes into account if we
251      * are in a form, and if the browser is MS-IE.
252      * @param text text to display for the link
253      * @param event the name of the event
254      * @param params additional parameters for the link URL
255      * @return an HTML element */

256     protected Composite eventURL(String JavaDoc text, String JavaDoc event, String JavaDoc params) {
257         JacRequest request = WebDisplay.getRequest();
258         String JavaDoc parameters = "event="+event+"&amp;source="+getId()+params;
259         if (request.isIEUserAgent()) {
260             // workaround for MSIE which does not handle <button> the way it should
261
logger.debug("user-agent: "+request.getUserAgent());
262             Link link = new Link(
263                 ((WebDisplay)context.getDisplay()).getServletName()+
264                 "?"+parameters,
265                 text);
266             link.attribute("onclick","return commitForm(this,'"+parameters+"')");
267             return link;
268         } else {
269             if (isInForm()) {
270                 Button button =
271                     new Button("submit","eventAndAction",parameters);
272                 button.add(text);
273                 return button;
274             } else {
275                 return new Link(
276                     ((WebDisplay)context.getDisplay()).getServletName()+
277                     "?event="+event+"&amp;source="+getId()+params,
278                     text);
279             }
280         }
281     }
282
283     /**
284      * Write HTML code for a button
285      *
286      * @param out where to write the HTML
287      * @param icon resource name of an icon
288      * @param label text of the button
289      * @param event the event linked to the button
290      */

291     protected void showButton(PrintWriter JavaDoc out, String JavaDoc icon, String JavaDoc label, String JavaDoc event) {
292         JacRequest request = WebDisplay.getRequest();
293         if (request.isIEUserAgent()) {
294             out.println(
295                 "<table class=\"method\"><td>"+
296                 (icon!=null?iconElement(ResourceManager.getResource(icon),"").toString():"")+
297                 eventURL(label,event,"").toString()+
298                 "</td></table>");
299         } else {
300             out.println(
301                 eventURL(label,event,"")
302                 .add(0,(icon!=null?iconElement(ResourceManager.getResource(icon),"").toString():""))
303                 .cssClass("method")
304                 .toString());
305         }
306     }
307
308     public String JavaDoc getOpenBorder() {
309         String JavaDoc s = "";
310         if (viewBorder==null)
311             return s;
312         s += "<div class=\"BORDER_"+Border.i2aStyle(viewBorder.getStyle())+"\">";
313         if (viewBorder.hasTitle()) {
314             s += "<div class=\"label\">"+viewBorder.getTitle()+"</div>";
315         }
316         return s;
317     }
318
319     public String JavaDoc getCloseBorder() {
320         if (viewBorder==null)
321             return "";
322         return "</div>";
323     }
324
325     protected String JavaDoc getBaseURL() {
326         return ((WebDisplay)context.getDisplay()).getServletName();
327     }
328
329     /**
330      * Build the base URL for an event
331      * @param event the name of the event
332      */

333     protected String JavaDoc eventURL(String JavaDoc event) {
334         String JavaDoc base = getBaseURL()+
335             "?event="+event+"&amp;source="+getId();
336         if (isCellViewer)
337             return base+"&amp;tableEventSource="+getId(table)+
338                 "&amp;row="+row+"&amp;col="+column;
339         else
340             return base;
341     }
342
343     /**
344      * Builds an <img> tag for an icon
345      *
346      * @param icon resource path of icon
347      * @param alt alt string for <img> HTML tag
348      */

349     protected Element iconElement(String JavaDoc icon, String JavaDoc alt) {
350         return iconElement(icon,alt,true);
351     }
352
353     static MethodCache iconCache = new MethodCache(null/*new Timestamps()*/);
354
355     /**
356      * Builds an <img> tag for an icon
357      *
358      * @param icon resource path of icon
359      * @param alt alt string for <img> HTML tag
360      * @param showAlt if true, return alt if icon is null
361      */

362     protected Element iconElement(String JavaDoc icon, String JavaDoc alt, boolean showAlt) {
363         if (Strings.isEmpty(icon)) {
364             return new Text(showAlt?alt:"");
365         } else {
366             Dimension JavaDoc size = null;
367             File JavaDoc file = new File JavaDoc(icon);
368             ObjectArray args = new ObjectArray(new Object JavaDoc[]{file});
369             MethodCache.Entry entry = iconCache.getEntry(args,null);
370             if (entry!=null) {
371                 size = (Dimension JavaDoc)entry.value;
372             } else {
373                 try {
374                     size = Images.getImageFileSize(file);
375                     logger.debug("size of "+icon+": "+size.width+"x"+size.height);
376                } catch (Exception JavaDoc e) {
377                     logger.warn("Could not determine size of icon "+icon,e);
378                 }
379             }
380             if (size==null) {
381                 logger.warn("Could not determine size of icon "+icon);
382             } else {
383                  iconCache.putEntry(args,size);
384             }
385             return new Image("resources/"+icon,alt,size).cssClass("icon");
386         }
387     }
388
389     protected String JavaDoc getId() {
390         return ((WebDisplay)context.getDisplay()).registerView(this);
391     }
392
393     protected String JavaDoc getId(View view) {
394         return ((WebDisplay)context.getDisplay()).registerView(view);
395     }
396
397     // HTMLViewer interface
398

399     /* the style sheets */
400     String JavaDoc styleSheet = "resources/org/objectweb/jac/aspects/gui/web/style.css";
401     String JavaDoc styleSheetIE = "resources/org/objectweb/jac/aspects/gui/web/ie.css";
402     String JavaDoc styleSheetKonqueror = "resources/org/objectweb/jac/aspects/gui/web/konqueror.css";
403
404     String JavaDoc javascript = "resources/org/objectweb/jac/aspects/gui/web/script.js";
405
406     public void setStyleSheet(String JavaDoc styleSheet) {
407         this.styleSheet = styleSheet;
408     }
409
410     /**
411      * Generate an HTML page, with full headers
412      * @see #genBody(PrintWriter)
413      */

414     protected void genPage(PrintWriter JavaDoc out) throws IOException JavaDoc {
415         out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
416         out.println("<html>");
417         out.println(" <head>");
418         out.println(" <title>"+label+"</title>");
419         out.println(" <meta name=\"Author\" content=\"JAC web-gui server\">" );
420         out.println(" <script type=\"text/javascript\" SRC=\""+javascript+"\"></script>");
421         genStyleSheets(out,context.getCustomizedView());
422         out.println(" </head>");
423         out.println(" <body>");
424         genBody(out);
425         out.println(" </body>");
426         out.println("</html>");
427     }
428
429     protected void genStyleSheets(PrintWriter JavaDoc out, CustomizedView customized) {
430         out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+
431                     "href=\""+styleSheet+"\" title=\"JAC\">");
432         JacRequest request = WebDisplay.getRequest();
433         if (request.isIEUserAgent()) {
434             out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+
435                         "href=\""+styleSheetIE+"\">");
436         }
437         if (request.userAgentMatch("Konqueror")) {
438             out.println(" <link rel=\"stylesheet\" type=\"text/css\" "+
439                         "href=\""+styleSheetKonqueror+"\">");
440         }
441
442         out.println(" <style type=\"text/css\">");
443         Iterator JavaDoc it;
444         if (customized!=null) {
445             it = customized.getCustomizedGUI().getStyleSheetURLs().iterator();
446             while (it.hasNext()) {
447                 String JavaDoc url = (String JavaDoc)it.next();
448                 out.println(" @import \""+url+"\";");
449             }
450         }
451         it = GuiAC.getStyleSheetURLs().iterator();
452         while (it.hasNext()) {
453             String JavaDoc url = (String JavaDoc)it.next();
454             out.println(" @import \""+url+"\";");
455         }
456         out.println(" </style>");
457     }
458
459     /**
460      * Override this method to generate the body of an HTML page.
461      * @see #genPage(PrintWriter)
462      */

463     protected void genBody(PrintWriter JavaDoc out) throws IOException JavaDoc {
464         out.println(" <p>Empty page</p>");
465     }
466
467     // TableCellViewer interface
468

469     public void setTable(View table) {
470         this.table = table;
471     }
472
473     public void setRow(int row) {
474         this.row = row;
475     }
476
477     public void setColumn(int column) {
478         this.column = column;
479     }
480
481     Hashtable JavaDoc attributes = new Hashtable JavaDoc();
482     public void setAttribute(String JavaDoc name, String JavaDoc value) {
483         attributes.put(name,value);
484     }
485     protected void printAttributes(PrintWriter JavaDoc out) {
486         Iterator JavaDoc it = attributes.entrySet().iterator();
487         while (it.hasNext()) {
488             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
489             out.write(" "+entry.getKey()+"=\""+entry.getValue()+"\"");
490         }
491     }
492
493     protected void openForm(PrintWriter JavaDoc out) {
494         out.println(" <form action=\""+
495                     ((WebDisplay)context.getDisplay()).getServletName()+"\" "+
496                     "method=\"post\" accept-charset=\""+GuiAC.getEncoding()+"\" "+
497                     "enctype=\"multipart/form-data\">");
498     }
499
500     protected void closeForm(PrintWriter JavaDoc out) {
501         out.println(" </form>");
502     }
503
504     protected void showFormButtons(PrintWriter JavaDoc out, boolean dialog) {
505         out.println(" <div class=\"actions\">");
506         out.println(" <input type=\"hidden\" name=\"source\" value=\""+getId()+"\">");
507         if (dialog) {
508             showButton(out,null,GuiAC.getLabelOK(),"onOK");
509             showButton(out,null,GuiAC.getLabelCancel(),"onCancel");
510         } else {
511             showButton(out,null,GuiAC.getLabelClose(),"onOK");
512         }
513         if (context.hasEnabledEditor()) {
514             showButton(out,null,"Refresh","onRefresh");
515         }
516         out.println(" </div>");
517     }
518
519     protected void showFormButtons(PrintWriter JavaDoc out) {
520         showFormButtons(out,true);
521     }
522
523     protected void genEventAndActionButton(PrintWriter JavaDoc out, String JavaDoc event) {
524         showButton(out,null,"Refresh",event);
525     }
526 }
527
Popular Tags