1 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 ; 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 UTILS_JS = (String ) SessionManager 41 .getSession().getCGManager().getObject("JScripts.utils", 42 String .class); 43 private static final String MENU_JS = (String ) SessionManager 44 .getSession().getCGManager().getObject("JScripts.menu", 45 String .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 { 64 final SMenuBar component = (SMenuBar) _c; 65 66 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 } 89 90 94 protected void printSpacer(final Device device) throws IOException { 95 device.print("<div class=\"spacer\"> </div>"); 96 } 97 98 101 public void parentFrameAdded(SParentFrameEvent e) { 102 SFrame parentFrame = e.getParentFrame(); 103 addListenersToParentFrame(parentFrame); 104 } 105 106 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 124 private void addExternalizedHeader(SFrame parentFrame, String classPath, String mimeType) { 125 ClasspathResource res = new ClasspathResource(classPath, mimeType); 126 String jScriptUrl = SessionManager.getSession().getExternalizeManager().externalize(res, ExternalizeManager.GLOBAL); 127 parentFrame.addHeader(new Script(mimeType, new DefaultURLResource(jScriptUrl))); 128 } 129 130 133 public void parentFrameRemoved(SParentFrameEvent e) { 134 } 136 } 137 | Popular Tags |