001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: /**
008: * A SMenuBar is a menu bar for adding SMenu's to.
009: *
010: * @author Robin Sharp
011: */
012:
013: import java.awt.*;
014: import java.awt.event.*;
015: import java.io.*;
016: import java.util.*;
017:
018: import com.javelin.swinglets.event.*;
019:
020: public class SMenuBar extends SContainer {
021: /**
022: * Construct a SMenuBar.
023: */
024: public SMenuBar() {
025: }
026:
027: /**
028: * Returns the name of the L&F class that renders this component.
029: */
030: public Class getUIClass() {
031: return SMenuBar.class;
032: }
033:
034: /**
035: * Get the top level ancestor.
036: */
037: public SContainer getTopLevelAncestor() {
038: return frame;
039: }
040:
041: /**
042: * Get the SFrame.
043: */
044: public SFrame getFrame() {
045: return frame;
046: }
047:
048: /**
049: * Set the SFrame to which this SMenu Belongs.
050: */
051: public SMenuBar setFrame(SFrame frame) {
052: if (this .frame == frame)
053: return this ;
054:
055: //Remove the menubar on the old frame
056: if (this .frame != null) {
057: this .frame.setMenuBar(null);
058: }
059:
060: this .frame = frame;
061:
062: //Add the menubar to the new frame
063: if (frame != null) {
064: frame.setMenuBar(this );
065: }
066:
067: return this ;
068: }
069:
070: /**
071: * Get the placement of the menus.
072: */
073: public int getMenuPlacement() {
074: return menuPlacement;
075: }
076:
077: /**
078: * Sets the placement of menus. <br>
079: * One of SConstants.TOP, SConstants.BOTTOM, SConstants.LEFT, or SConstants.RIGHT.
080: */
081: public SMenuBar setMenuPlacement(int menuPlacement) {
082: this .menuPlacement = menuPlacement;
083: return this ;
084: }
085:
086: /**
087: * Appends the specified SMenuItem to the end of the menu bar.
088: */
089: public SMenuItem add(SMenuItem c) {
090: super .add(c);
091: return c;
092: }
093:
094: /**
095: * Gets the SMenuItem at the specified position in the menu bar.
096: */
097: public SMenuItem getMenuItem(int index) {
098: SComponent c = getComponent(index);
099:
100: if (c instanceof SMenuItem)
101: return (SMenuItem) c;
102:
103: return null;
104: }
105:
106: /**
107: * Returns the number of items in the menu bar.
108: */
109: public int getMenuCount() {
110: return getComponentCount();
111: }
112:
113: /**
114: * Returns the index of the specified component.
115: */
116: public int getComponentIndex(SComponent c) {
117: int count = this .getComponentCount();
118: SComponent[] component = this .getComponents();
119: for (int index = 0; index < count; index++) {
120: SComponent comp = component[index];
121: if (comp == c) {
122: return index;
123: }
124: }
125: return -1;
126: }
127:
128: /**
129: * Sets the currently selected component.
130: */
131: public SMenuBar setSelected(SComponent component) {
132: if (component == null) {
133: selectedIndex = -1;
134: } else {
135: selectedIndex = getComponentIndex(component);
136: }
137:
138: return this ;
139: }
140:
141: /**
142: * Returns true if the MenuBar currently has a component selected
143: */
144: public boolean isSelected() {
145: return selectedIndex >= 0;
146: }
147:
148: /**
149: * Get the selected index
150: */
151: public int getSelectedIndex() {
152: return selectedIndex;
153: }
154:
155: /**
156: * Drill down to find the selected SMenu, or null.
157: */
158: public SMenu getSelectedMenu() {
159: return getSelectedMenu((SMenu) getMenuItem(getSelectedIndex()));
160: }
161:
162: /**
163: * Drill down to find the selected SMenu, or null.
164: */
165: protected SMenu getSelectedMenu(SMenu menu) {
166: if (menu.isSelected()) {
167: SMenuItem subMenu = menu.getItem(menu.getSelectedIndex());
168: if (subMenu != null && subMenu instanceof SMenu) {
169: return getSelectedMenu((SMenu) subMenu);
170: }
171:
172: return null;
173: } else {
174: return menu;
175: }
176: }
177:
178: /**
179: * Returns true if the Menubar's border should be painted.
180: */
181: public boolean isBorderPainted() {
182: return borderPainted;
183: }
184:
185: /**
186: * Sets whether the border should be painted.
187: */
188: public SMenuBar setBorderPainted(boolean borderPainted) {
189: this .borderPainted = borderPainted;
190: return this ;
191: }
192:
193: /**
194: * Processes events occurring on this component.
195: */
196: protected void processEvent(AWTEvent event) {
197: if (event instanceof FormEvent) {
198: processFormEvent((FormEvent) event);
199: }
200: }
201:
202: /**
203: * Process the FormEvent to compute which menu item has been
204: * selected.
205: * <p>
206: * If this is branch node the display the children, by updating
207: * the selected item value.
208: * <p>
209: * If this is a leaf node then notify the node that it has
210: * been selected through an action event.
211: */
212: protected void processFormEvent(FormEvent event) {
213: //System.out.println( "SMenuBar.processFormEvent\n" + event );
214:
215: //Reset the selected index, unless otherwise stated.
216: selectedIndex = -1;
217:
218: //System.out.println( event );
219: for (Enumeration names = event.getParameterNames(); names
220: .hasMoreElements();) {
221: String name = (String) names.nextElement();
222: if (Character.isDigit(name.charAt(0))) {
223: int index = name.indexOf('.');
224: int menuIndex = -1;
225: if (index >= 0) {
226: menuIndex = Integer.parseInt(name.substring(0,
227: index));
228: } else {
229: menuIndex = Integer.parseInt(name);
230: }
231:
232: //Check to see if the selected index was a menu or a menuItem
233: //If it was a menu item then fire an action event and reset the
234: //selected index
235: SMenuItem menuItem = getMenuItem(menuIndex);
236: if (menuItem != null) {
237: if (menuItem instanceof SMenu) {
238: selectedIndex = menuIndex;
239: } else if (menuItem instanceof SMenuItem) {
240: menuItem.dispatchEvent(new ActionEvent(
241: menuItem, ActionEvent.ACTION_PERFORMED,
242: null));
243: }
244: }
245: }
246: }
247: }
248:
249: /**
250: * Paint this component in the header.
251: */
252: public void paintHeaderx(Object out) {
253: getUI().updateHeader(out, this );
254: }
255:
256: /**
257: * Paint this component.
258: */
259: public void paintx(Object out) {
260: getUI().update(out, this );
261: }
262:
263: // PRIVATE //////////////////////////////////////////////////////////
264:
265: protected SFrame frame;
266:
267: protected int menuPlacement = SConstants.TOP;
268: protected boolean borderPainted;
269: protected int selectedIndex = -1;
270:
271: }
|