1 14 package org.wings.plaf.css; 15 16 17 import java.io.IOException ; 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 UTILS_JS = (String ) SessionManager 49 .getSession().getCGManager().getObject("JScripts.utils", 50 String .class); 51 private static final String MENU_JS = (String ) SessionManager 52 .getSession().getCGManager().getObject("JScripts.menu", 53 String .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 { 59 if (menu.isEnabled()) { 60 String 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 124 protected void writeListAttributes(Device device, SPopupMenu menu) throws IOException { 125 } 127 128 protected void writeAnchorAddress(Device d, SAbstractButton abstractButton) 129 throws IOException { 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 { 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 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 165 private void addExternalizedHeader(SFrame parentFrame, String classPath, String mimeType) { 166 ClasspathResource res = new ClasspathResource(classPath, mimeType); 167 String 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 174 } 175 } 176 | Popular Tags |