KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MenuCG.java,v 1.19 2005/05/26 13:18:09 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.wings.*;
18 import org.wings.io.Device;
19 import org.wings.session.SessionManager;
20
21 import java.io.IOException JavaDoc;
22
23 public class MenuCG extends org.wings.plaf.css.MenuItemCG implements
24         org.wings.plaf.MenuCG {
25
26     public void installCG(final SComponent comp) {
27         super.installCG(comp);
28     }
29
30     public void uninstallCG(final SComponent comp) {
31     }
32
33     public void writePopup(final Device device, SMenu menu)
34             throws IOException JavaDoc {
35         String JavaDoc componentId = menu.getName();
36         if (menu.isEnabled()) {
37             device.print("<ul");
38             writeListAttributes(device, menu);
39             device.print(" class=\"SMenu\">");
40             for (int i = 0; i < menu.getMenuComponentCount(); i++) {
41                 SComponent menuItem = menu.getMenuComponent(i);
42     
43                 if (menuItem.isVisible()) {
44                     device.print("<li");
45                     if (menuItem instanceof SMenu) {
46                         if (menuItem.isEnabled()) {
47                             device.print(" class=\"SMenu\"");
48                         } else {
49                             device.print(" class=\"SMenu_Disabled\"");
50                         }
51                     } else {
52                         if (menuItem.isEnabled()) {
53                             device.print(" class=\"SMenuItem\"");
54                         } else {
55                             device.print(" class=\"SMenuItem_Disabled\"");
56                         }
57                     }
58                     device.print(">");
59                     if (menuItem instanceof SMenuItem) {
60                             device.print("<a");
61                             if (menuItem.isEnabled()) {
62                                 device.print(" HREF=\"");
63                                 writeAnchorAddress(device, (SMenuItem) menuItem);
64                                 device.print("\"");
65                             }
66                             if (menuItem instanceof SMenu) {
67                                 if (menuItem.isEnabled()) {
68                                     device.print(" class=\"x\"");
69                                 } else {
70                                     device.print(" class=\"y\"");
71                                 }
72                             }
73                             device.print(">");
74                     }
75                     menuItem.write(device);
76                     if (menuItem instanceof SMenuItem) {
77                         device.print("</a>");
78                     }
79                     if (menuItem.isEnabled() && menuItem instanceof SMenu) {
80                         ((SMenu)menuItem).writePopup(device);
81                     }
82                     device.print("</li>\n");
83                 }
84             }
85             device.print("</ul>");
86         }
87         device.print("\n");
88     }
89
90     /**
91      * Convenience method to keep differences between default and msie
92      * implementations small
93      * @param device
94      * @param menu
95      * @throws IOException
96      */

97     protected void writeListAttributes(final Device device, SMenu menu) throws IOException JavaDoc {
98         if (menu.getParentMenu() != null) {
99             // calculate max length of children texts for sizing of layer
100
int maxLength = 0;
101             for (int i = 0; i < menu.getMenuComponentCount(); i++) {
102                 if (!(menu.getMenuComponent(i) instanceof SMenuItem))
103                     continue;
104                 String JavaDoc text = ((SMenuItem)menu.getMenuComponent(i)).getText();
105                 if (text != null && text.length() > maxLength) {
106                     maxLength = text.length();
107                     if (menu.getMenuComponent(i) instanceof SMenu) {
108                             maxLength = maxLength + 2; //graphics
109
}
110                 }
111             }
112             device.print(" style=\"width:");
113             String JavaDoc stringLength = String.valueOf(maxLength * menu.getWidthScaleFactor());
114             device.print(stringLength.substring(0,stringLength.lastIndexOf('.')+2));
115             device.print("em;\"");
116         } else {
117             device.print(" id=\"");
118             device.print(menu.getName());
119             device.print("_pop\"");
120         }
121     }
122
123     protected void writeItem(final Device device, SMenuItem menu)
124             throws IOException JavaDoc {
125             writeItemContent(device, menu);
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,
137                              final SComponent _c)
138             throws IOException JavaDoc {
139         SMenu menu = (SMenu) _c;
140         boolean hasParent = menu.getParentMenu() != null;
141         if (hasParent) {
142         writeItem(device, menu);
143         } else {
144             SessionManager.getSession().getCGManager().getPrefixSuffixDelegate().writePrefix(device, _c);
145             writePopup(device, menu);
146             SessionManager.getSession().getCGManager().getPrefixSuffixDelegate().writeSuffix(device, _c);
147         }
148         
149     }
150 }
151
Popular Tags