KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > css > PopupMenuCG


1 /*
2  * $Id: PopupMenuCG.java,v 1.24 2005/06/07 12:55:59 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf.css;
15
16
17 import java.io.IOException JavaDoc;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.wings.*;
22 import org.wings.event.SParentFrameEvent;
23 import org.wings.event.SParentFrameListener;
24 import org.wings.externalizer.ExternalizeManager;
25 import org.wings.header.Script;
26 import org.wings.io.Device;
27 import org.wings.resource.ClasspathResource;
28 import org.wings.resource.DefaultURLResource;
29 import org.wings.script.JavaScriptListener;
30 import org.wings.session.SessionManager;
31
32 public class PopupMenuCG extends AbstractComponentCG implements
33         org.wings.plaf.MenuBarCG, SParentFrameListener {
34     private final transient static Log log = LogFactory.getLog(PopupMenuCG.class);
35
36     public void installCG(final SComponent comp) {
37         super.installCG(comp);
38         SFrame parentFrame = comp.getParentFrame();
39         if (parentFrame != null) {
40             addListenersToParentFrame(parentFrame);
41         }
42         comp.addParentFrameListener(this);
43     }
44
45     public void uninstallCG(final SComponent comp) {
46     }
47
48     public static final String JavaDoc UTILS_JS = (String JavaDoc) SessionManager
49     .getSession().getCGManager().getObject("JScripts.utils",
50             String JavaDoc.class);
51     private static final String JavaDoc MENU_JS = (String JavaDoc) SessionManager
52     .getSession().getCGManager().getObject("JScripts.menu",
53             String JavaDoc.class);
54     private static final JavaScriptListener BODY_ONCLICK_SCRIPT =
55         new JavaScriptListener("onclick", "wpm_handleBodyClicks(event)");
56
57     protected void writePopup(final Device device, SPopupMenu menu)
58             throws IOException JavaDoc {
59         if (menu.isEnabled()) {
60             String JavaDoc componentId = menu.getName();
61             device.print("<ul");
62             writeListAttributes(device, menu);
63             device.print(" id=\"");
64             device.print(componentId);
65             device.print("_pop\">");
66             for (int i = 0; i < menu.getMenuComponentCount(); i++) {
67                 SComponent menuItem = menu.getMenuComponent(i);
68     
69                 if (menuItem.isVisible()) {
70                     device.print("<li");
71                     if (menuItem instanceof SMenu) {
72                         if (menuItem.isEnabled()) {
73                             device.print(" class=\"SMenu\"");
74                         } else {
75                             device.print(" class=\"SMenu_Disabled\"");
76                         }
77                     } else {
78                         if (menuItem.isEnabled()) {
79                             device.print(" class=\"SMenuItem\"");
80                         } else {
81     
82                             device.print(" class=\"SMenuItem_Disabled\"");
83                         }
84                     }
85                     device.print(">");
86                     if (menuItem instanceof SMenuItem) {
87                             device.print("<a");
88                             if (menuItem.isEnabled()) {
89                                 device.print(" HREF=\"");
90                                 writeAnchorAddress(device, (SMenuItem) menuItem);
91                                 device.print("\"");
92                             }
93                             if (menuItem instanceof SMenu) {
94                                 if (menuItem.isEnabled()) {
95                                     device.print(" class=\"x\"");
96                                 } else {
97                                     device.print(" class=\"y\"");
98                                 }
99                             }
100                             device.print(">");
101                     }
102                     menuItem.write(device);
103                     if (menuItem instanceof SMenuItem) {
104                         device.print("</a>");
105                     }
106                     if (menuItem.isEnabled() && menuItem instanceof SMenu) {
107                         ((SMenu)menuItem).writePopup(device);
108                     }
109                     device.print("</li>\n");
110                 }
111             }
112             device.print("</ul>");
113         }
114         device.print("\n");
115     }
116
117     /**
118      * Convenience method to keep differences between default and msie
119      * implementations small
120      * @param device
121      * @param menu
122      * @throws IOException
123      */

124     protected void writeListAttributes(Device device, SPopupMenu menu) throws IOException JavaDoc {
125         // do nothing...
126
}
127
128     protected void writeAnchorAddress(Device d, SAbstractButton abstractButton)
129             throws IOException JavaDoc {
130         RequestURL addr = abstractButton.getRequestURL();
131         addr.addParameter(abstractButton,
132                 abstractButton.getToggleSelectionParameter());
133         addr.write(d);
134     }
135
136     public void writeContent(final Device device, final SComponent _c)
137             throws IOException JavaDoc {
138         SPopupMenu menu = (SPopupMenu) _c;
139         writePopup(device, menu);
140     }
141
142     public void parentFrameAdded(SParentFrameEvent e) {
143         SFrame parentFrame = e.getParentFrame();
144         addListenersToParentFrame(parentFrame);
145     }
146
147     /**
148      * adds the necessary listeners to the parent frame. is called by
149      * parent frame listener or from install.
150      * @param parentFrame
151      */

152     private void addListenersToParentFrame(SFrame parentFrame) {
153         parentFrame.addScriptListener(BODY_ONCLICK_SCRIPT);
154         addExternalizedHeader(parentFrame, UTILS_JS, "text/javascript");
155         addExternalizedHeader(parentFrame, MENU_JS, "text/javascript");
156     }
157
158     /**
159      * adds the file found at the classPath to the parentFrame header with
160      * the specified mimeType
161      * @param parentFrame the parent frame of the component
162      * @param classPath the classPath to look in for the file
163      * @param mimeType the mimetype of the file
164      */

165     private void addExternalizedHeader(SFrame parentFrame, String JavaDoc classPath, String JavaDoc mimeType) {
166         ClasspathResource res = new ClasspathResource(classPath, mimeType);
167         String JavaDoc jScriptUrl = SessionManager.getSession().getExternalizeManager().externalize(res, ExternalizeManager.GLOBAL);
168         parentFrame.addHeader(new Script(mimeType, new DefaultURLResource(jScriptUrl)));
169     }
170
171     public void parentFrameRemoved(SParentFrameEvent e) {
172         // TODO Auto-generated method stub
173

174     }
175 }
176
Popular Tags