001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets.plaf.html;
006:
007: import java.awt.*;
008: import java.util.*;
009: import java.io.*;
010:
011: import com.javelin.swinglets.*;
012: import com.javelin.swinglets.plaf.*;
013:
014: /**
015: * HTMLMenuBarUI defines a look and feel for default HTML.
016: *
017: * @author Robin Sharp
018: */
019:
020: public class HTMLMenuBarUI extends HTMLContainerUI {
021: public final static String HTML_LINK = "_HTML_LINK";
022:
023: /**
024: * Render the UI on the PrintWriter
025: */
026: public void update(PrintWriter out, SComponent c) {
027: if (!c.isVisible())
028: return;
029:
030: SMenuBar menuBar = (SMenuBar) c;
031:
032: paintMenuBar(out, menuBar);
033: }
034:
035: /**
036: * Paint the menu bar and orient it in a table.
037: */
038: public void paintMenuBar(PrintWriter out, SMenuBar menuBar) {
039: if (!menuBar.isVisible())
040: return;
041:
042: //TABS IN TABLE
043: if (menuBar.getMenuPlacement() == SConstants.TOP
044: || menuBar.getMenuPlacement() == SConstants.BOTTOM) {
045: out.print("<TABLE BORDER=");
046: if (menuBar.isBorderPainted())
047: out.print("1");
048: else
049: out.print("0");
050: out.print(" CELLSPACING=0 CELLPADDING=0 ");
051:
052: } else {
053: out.print("<TABLE BORDER=");
054: if (menuBar.isBorderPainted())
055: out.print("1");
056: else
057: out.print("0");
058: out.print(" CELLSPACING=0 CELLPADDING=0");
059: }
060:
061: if (menuBar.getMenuPlacement() == SConstants.TOP
062: || menuBar.getMenuPlacement() == SConstants.BOTTOM) {
063: if (menuBar.getSize() == null
064: || menuBar.getSize().width == 0) {
065: //AUTO
066: } else if (menuBar.getSize().width > 0) {
067: out.print(" WIDTH=\"");
068: out.print(menuBar.getSize().width);
069: out.print("\"");
070: } else if (menuBar.getSize().width < 0) {
071: out.print(" WIDTH=\"");
072: out.print((0 - menuBar.getSize().width));
073: out.print("%\"");
074: }
075: }
076:
077: if (menuBar.getMenuPlacement() == SConstants.LEFT
078: || menuBar.getMenuPlacement() == SConstants.RIGHT) {
079: if (menuBar.getSize() == null
080: || menuBar.getSize().width == 0) {
081: //AUTO
082: } else if (menuBar.getSize().height > 0) {
083: out.print(" HEIGHT=\"");
084: out.print(menuBar.getSize().height);
085: out.print("\"");
086: } else if (menuBar.getSize().height < 0) {
087: out.print(" HEIGHT=\"");
088: out.print((0 - menuBar.getSize().height));
089: out.print("%\"");
090: }
091: }
092:
093: if (!menuBar.isOpaque()) {
094: } else if (menuBar.getBackground() != null) {
095: out.print(" BGCOLOR=\"#");
096: out.print(SColor.toHexString(menuBar.getBackground()));
097: out.print("\"");
098: } else {
099: out.print(" BGCOLOR=\"#");
100: out.print(SColor
101: .toHexString(getTheme().getMenuBackground()));
102: out.print("\"");
103: }
104:
105: out.println(" >");
106:
107: out.println("<TR>");
108:
109: //If a menu is selected then paint that, otherwise paint the bar.
110: if (menuBar.isSelected()
111: && menuBar.getMenuItem(menuBar.getSelectedIndex()) instanceof SMenu) {
112: //Get the currently selected sub menu
113: paintSubMenu(out, menuBar);
114: } else {
115: paintTopMenu(out, menuBar);
116: }
117:
118: if (menuBar.getMenuPlacement() == SConstants.TOP
119: || menuBar.getMenuPlacement() == SConstants.BOTTOM) {
120: //Print padding cell for % widths
121: if (menuBar.getSize() != null
122: && menuBar.getSize().width != 0) {
123: out.println("<TD WIDTH=100%> </TD>");
124: }
125: }
126: out.println("</TR>");
127:
128: if (menuBar.getMenuPlacement() == SConstants.LEFT
129: || menuBar.getMenuPlacement() == SConstants.RIGHT) {
130: //Print padding cell for % widths
131: if (menuBar.getSize() != null
132: && menuBar.getSize().height != 0) {
133: out.println("<TR><TD HEIGHT=100%> </TD></TR>");
134: }
135: }
136: out.print("</TABLE>");
137: //out.println( "</FORM>" );
138: }
139:
140: /**
141: * Paint the (top) menu bar.
142: */
143: public void paintTopMenu(PrintWriter out, SMenuBar menuBar) {
144: //Record the current link, on the menubar, how to get back to this point
145: if (menuBar.getClientProperty(HTMLMenuBarUI.HTML_LINK) == null) {
146: menuBar.putClientProperty(HTMLMenuBarUI.HTML_LINK,
147: new SLink(menuBar.getComponentUrl()));
148: }
149:
150: //Paint each item in the menu bar
151: for (int index = 0; index < menuBar.getMenuCount(); index++) {
152: menuBar.getMenuItem(index).paint(out);
153:
154: if (menuBar.getMenuPlacement() == SConstants.LEFT
155: || menuBar.getMenuPlacement() == SConstants.RIGHT) {
156: out.println("</TR>");
157: out.println("<TR>");
158: }
159:
160: }
161: }
162:
163: /**
164: * Paint the (sub) menu.
165: */
166: public void paintSubMenu(PrintWriter out, SMenuBar menuBar) {
167: SMenu menu = menuBar.getSelectedMenu();
168:
169: if (menu == null) {
170: return;
171: }
172:
173: //Record the current link, on the menubar, how to get back to this point
174: if (menu.getClientProperty(HTMLMenuBarUI.HTML_LINK) == null) {
175: menu.putClientProperty(HTMLMenuBarUI.HTML_LINK, new SLink(
176: menu.getComponentUrl()));
177: }
178:
179: //If this is the top level SMenu make the back link to the menu bar
180: //otherwise make it to the parent menu.
181: //System.out.println( "MB SELECTED" + menu.getName() );
182: if (menu.getParentMenu() != null) {
183: //System.out.println( "MB SELECTED" + menu.getParentMenu().getName() );
184: backItem.setLink((SLink) menu.getParentMenu()
185: .getClientProperty(HTMLMenuBarUI.HTML_LINK));
186: } else if (menu != null) {
187: //System.out.println( "MENU SELECTED" + menu.getParent().getName() );
188: backItem.setLink((SLink) menu.getParent()
189: .getClientProperty(HTMLMenuBarUI.HTML_LINK));
190: }
191:
192: for (int index = -1; index < menu.getItemCount(); index++) {
193: if (index == -1) {
194: backItem.paint(out);
195: //paintMenuItem( out, menuBar, menu, backItem, index );
196: } else {
197: menu.getItem(index).paint(out);
198: //paintMenuItem( out, menuBar, menu, menu.getItem( index ), index );
199: }
200:
201: if (menuBar.getMenuPlacement() == SConstants.LEFT
202: || menuBar.getMenuPlacement() == SConstants.RIGHT) {
203: out.println("</TR>");
204: out.println("<TR>");
205: }
206:
207: }
208: }
209:
210: private SMenuItem backItem = new SMenuItem("...");
211:
212: }
|