001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.plaf.css;
014:
015: import org.wings.SConstants;
016: import org.wings.SMenu;
017: import org.wings.SMenuBar;
018: import org.wings.event.SParentFrameEvent;
019: import org.wings.event.SParentFrameListener;
020: import org.wings.header.Header;
021: import org.wings.header.SessionHeaders;
022: import org.wings.io.Device;
023: import org.wings.script.JavaScriptEvent;
024: import org.wings.script.JavaScriptListener;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.List;
029: import java.util.Collections;
030:
031: /**
032: * This is the Default XHTML CSS plaf for the SMenuBar Component.
033: * @author ole
034: */
035: public class MenuBarCG extends AbstractComponentCG<SMenuBar> implements
036: org.wings.plaf.MenuBarCG, SParentFrameListener {
037:
038: protected final static List<Header> MENU_HEADERS;
039: static {
040: ArrayList<Header> tmpHeaders = new ArrayList<Header>();
041: tmpHeaders
042: .add(Utils
043: .createExternalizedJSHeaderFromProperty(Utils.JS_ETC_MENU));
044: MENU_HEADERS = Collections.unmodifiableList(tmpHeaders);
045: }
046:
047: public static final JavaScriptListener BODY_ONCLICK_SCRIPT = new JavaScriptListener(
048: JavaScriptEvent.ON_CLICK, "wpm_handleBodyClicks(event)");
049:
050: public MenuBarCG() {
051: }
052:
053: @Override
054: public void installCG(final SMenuBar comp) {
055: super .installCG(comp);
056: comp.addParentFrameListener(this );
057: }
058:
059: public void parentFrameAdded(SParentFrameEvent e) {
060: SessionHeaders.getInstance().registerHeaders(MENU_HEADERS);
061: //e.getParentFrame().addScriptListener(BODY_ONCLICK_SCRIPT);
062: }
063:
064: public void parentFrameRemoved(SParentFrameEvent e) {
065: SessionHeaders.getInstance().deregisterHeaders(MENU_HEADERS);
066: //e.getParentFrame().removeScriptListener(BODY_ONCLICK_SCRIPT);
067: }
068:
069: /* (non-Javadoc)
070: * @see org.wings.plaf.css.AbstractComponentCG#writeContent(org.wings.io.Device, org.wings.SComponent)
071: */
072: @Override
073: public void writeInternal(final Device device,
074: final SMenuBar menuBar) throws IOException {
075: final int mcount = menuBar.getComponentCount();
076: writeTablePrefix(device, menuBar);
077:
078: printSpacer(device); /* clear:both to ensuer menubar surrounds all SMenu entries */
079:
080: // Left-aligned menues must rendered first in natural order
081: for (int i = 0; i < mcount; i++) {
082: final SMenu menu = menuBar.getMenu(i);
083: if (menu != null
084: && menu.isVisible()
085: && menu.getHorizontalAlignment() != SConstants.RIGHT_ALIGN) {
086: renderSMenu(device, menu, false);
087: }
088: }
089: // Right-aligned menues must rendered first in revers order due to float:right
090: for (int i = mcount - 1; i >= 0; i--) {
091: final SMenu menu = menuBar.getMenu(i);
092: if (menu != null
093: && menu.isVisible()
094: && menu.getHorizontalAlignment() == SConstants.RIGHT_ALIGN) {
095: renderSMenu(device, menu, true);
096: }
097: }
098:
099: printSpacer(device); /* clear:both to ensuer menubar surrounds all SMenu entries */
100:
101: writeTableSuffix(device, menuBar);
102: }
103:
104: /* Renders the anchor representing a top SMenu item inside the menu bar. */
105: protected void renderSMenu(final Device device, final SMenu menu,
106: boolean rightAligned) throws IOException {
107: if (menu.isEnabled()) {
108: device
109: .print("<a class=\"SMenu\" onmousedown=\"wpm_menu(event,'");
110: device.print(menu.getName());
111: device
112: .print("_pop');\" onmouseover=\"if (typeof wpm_changeMenu != 'undefined') {wpm_changeMenu(event,'");
113: device.print(menu.getName());
114: device.print("_pop');}\"");
115: } else {
116: device.print("<a class=\"SMenu_Disabled\"");
117: }
118: if (rightAligned)
119: device.print(" style=\"float:right\"");
120: device.print(">");
121: Utils.write(device, menu.getText());
122: device.print("</a>");
123: }
124:
125: /**
126: * Prints a spacer if necessary, depending on browser compatibility.
127: * Is inserted here for possible overwriting in inheriting plafs for
128: * other browsers.
129: * @param device the device to print on
130: * @throws IOException
131: */
132: protected void printSpacer(final Device device) throws IOException {
133: device.print("<div class=\"spacer\"></div>");
134: }
135:
136: }
|