KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MenuBarCG.java,v 1.28 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 org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.wings.SComponent;
20 import org.wings.SFrame;
21 import org.wings.SMenu;
22 import org.wings.SMenuBar;
23 import org.wings.externalizer.ExternalizeManager;
24 import org.wings.event.SParentFrameEvent;
25 import org.wings.event.SParentFrameListener;
26 import org.wings.header.Script;
27 import org.wings.io.Device;
28 import org.wings.resource.ClasspathResource;
29 import org.wings.resource.DefaultURLResource;
30 import org.wings.script.JavaScriptListener;
31 import org.wings.session.SessionManager;
32
33 import java.io.IOException JavaDoc;
34
35 public class MenuBarCG extends AbstractComponentCG implements
36         org.wings.plaf.MenuBarCG, SParentFrameListener {
37
38     private final transient static Log log = LogFactory.getLog(MenuBarCG.class);
39
40     public static final String JavaDoc UTILS_JS = (String JavaDoc) SessionManager
41     .getSession().getCGManager().getObject("JScripts.utils",
42             String JavaDoc.class);
43     private static final String JavaDoc MENU_JS = (String JavaDoc) SessionManager
44     .getSession().getCGManager().getObject("JScripts.menu",
45             String JavaDoc.class);
46     public static final JavaScriptListener BODY_ONCLICK_SCRIPT =
47         new JavaScriptListener("onclick", "wpm_handleBodyClicks(event)");
48
49     public void installCG(final SComponent comp) {
50         super.installCG(comp);
51         SFrame parentFrame = comp.getParentFrame();
52         if (parentFrame != null) {
53             addListenersToParentFrame(parentFrame);
54         }
55         comp.addParentFrameListener(this);
56     }
57
58     public void uninstallCG(final SComponent comp) {
59     }
60
61     public void writeContent(final Device device,
62                              final SComponent _c)
63             throws IOException JavaDoc {
64         final SMenuBar component = (SMenuBar) _c;
65
66 //--- code from write-template.
67
SMenuBar mbar = (SMenuBar) component;
68         int mcount = mbar.getComponentCount();
69
70         printSpacer(device);
71         for (int i = 0; i < mcount; i++) {
72             SComponent menu = mbar.getComponent(i);
73             if (menu.isVisible()) {
74                 if (menu.isEnabled()) {
75                     device.print("<div class=\"SMenu\" onMouseDown=\"javascript:wpm_menu(event,'");
76                     device.print(menu.getName());
77                     device.print("_pop');\">");
78                 } else {
79                     device.print("<div class=\"SMenu_Disabled\">");
80                 }
81                 Utils.write(device, ((SMenu)menu).getText());
82                 device.print("</div>");
83             }
84         }
85         printSpacer(device);
86
87 //--- end code from write-template.
88
}
89
90     /**
91      * @param device
92      * @throws IOException
93      */

94     protected void printSpacer(final Device device) throws IOException JavaDoc {
95         device.print("<div class=\"spacer\">&nbsp;</div>");
96     }
97
98     /* (non-Javadoc)
99      * @see org.wings.event.SParentFrameListener#parentFrameAdded(org.wings.event.SParentFrameEvent)
100      */

101     public void parentFrameAdded(SParentFrameEvent e) {
102         SFrame parentFrame = e.getParentFrame();
103         addListenersToParentFrame(parentFrame);
104     }
105
106     /**
107      * adds the necessary listeners to the parent frame. is called by
108      * parent frame listener or from install.
109      * @param parentFrame
110      */

111     private void addListenersToParentFrame(SFrame parentFrame) {
112         parentFrame.addScriptListener(BODY_ONCLICK_SCRIPT);
113         addExternalizedHeader(parentFrame, UTILS_JS, "text/javascript");
114         addExternalizedHeader(parentFrame, MENU_JS, "text/javascript");
115     }
116
117     /**
118      * adds the file found at the classPath to the parentFrame header with
119      * the specified mimeType
120      * @param parentFrame the parent frame of the component
121      * @param classPath the classPath to look in for the file
122      * @param mimeType the mimetype of the file
123      */

124     private void addExternalizedHeader(SFrame parentFrame, String JavaDoc classPath, String JavaDoc mimeType) {
125         ClasspathResource res = new ClasspathResource(classPath, mimeType);
126         String JavaDoc jScriptUrl = SessionManager.getSession().getExternalizeManager().externalize(res, ExternalizeManager.GLOBAL);
127         parentFrame.addHeader(new Script(mimeType, new DefaultURLResource(jScriptUrl)));
128     }
129
130     /* (non-Javadoc)
131      * @see org.wings.event.SParentFrameListener#parentFrameRemoved(org.wings.event.SParentFrameEvent)
132      */

133     public void parentFrameRemoved(SParentFrameEvent e) {
134         //e.getParentFrame().removeScriptListener(BODY_ONCLICK_SCRIPT);
135
}
136 }
137
Popular Tags