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;
014:
015: import org.wings.io.Device;
016: import org.wings.plaf.MenuBarCG;
017: import org.wings.plaf.MenuCG;
018: import javax.swing.*;
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: /**
026: * Aggregates various {@link SMenuItem}s under a main header entry.
027: *
028: * @author <a href="mailto:andre.lison@general-bytes.com">Andre Lison</a>
029: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
030: */
031: public class SMenu extends SMenuItem {
032: private boolean popupMenuVisible = false;
033: protected final List menuItems = new ArrayList();
034: private double widthScaleFactor = 0.7f;
035:
036: public SMenu(String text) {
037: super (text);
038: }
039:
040: public SMenu() {
041: super ();
042: }
043:
044: public SMenu(SIcon i) {
045: super (i);
046: }
047:
048: public SMenu(String text, SIcon icon) {
049: super (text, icon);
050: }
051:
052: /**
053: * Add a menu item to this menu.
054: */
055: public void add(SMenuItem menuItem) {
056: menuItems.add(menuItem);
057: menuItem.setParentMenu(this );
058: menuItem.putClientProperty("drm:realParentComponent", this );
059: reload();
060: }
061:
062: /**
063: * Add a menu item to this menu.
064: */
065: public void add(SComponent menuItem) {
066: menuItems.add(menuItem);
067: menuItem.setParentFrame(getParentFrame());
068: menuItem.putClientProperty("drm:realParentComponent", this );
069: reload();
070: }
071:
072: /**
073: * Add a separator to this menu.
074: */
075: public void addSeparator() {
076: add(new SSeparator());
077: }
078:
079: public void setParentFrame(SFrame f) {
080: if (getParentFrame() == null && f != null) {
081: reload();
082: }
083: if (f != null
084: || (f == null && !getSession().getMenuManager()
085: .isMenuLinked(this ))) {
086: super .setParentFrame(f);
087: for (int i = 0; i < menuItems.size(); i++) {
088: ((SComponent) menuItems.get(i)).setParentFrame(f);
089: }
090: }
091: }
092:
093: /**
094: * Add a menu item to this menu.
095: */
096: public void add(String menuitem) {
097: this .add(new SMenuItem(menuitem));
098: }
099:
100: public SComponent getMenuComponent(int pos) {
101: return (SComponent) menuItems.get(pos);
102: }
103:
104: /**
105: * Return the number of items on the menu, including separators.
106: */
107: public int getMenuComponentCount() {
108: return menuItems.size();
109: }
110:
111: /**
112: * Remove all {@link SMenuItem} from this menu.
113: */
114: public void removeAll() {
115: while (menuItems.size() > 0) {
116: remove(0);
117: }
118: }
119:
120: /**
121: * Removes the menu item at specified index from the menu.
122: */
123: public void remove(int pos) {
124: remove(getMenuComponent(pos));
125: }
126:
127: /**
128: * removes a specific menu item component.
129: */
130: public void remove(SComponent comp) {
131: if (menuItems.remove(comp)) {
132: reload();
133: }
134: comp.setParentFrame(null);
135: comp.putClientProperty("drm:realParentComponent", "drm:null");
136: }
137:
138: public void setCG(MenuBarCG cg) {
139: super .setCG(cg);
140: }
141:
142: /**
143: * Sets the visibility of the menu's popup.
144: * If the menu is not enabled, this method will have no effect.
145: *
146: * @param b a boolean value -- true to make the menu visible, false to hide it
147: */
148: public void setPopupMenuVisible(boolean b) {
149: if (!isEnabled())
150: return;
151: popupMenuVisible = b;
152: }
153:
154: /**
155: * Returns true if the menu's popup window is visible.
156: *
157: * @return true if the menu is visible, else false.
158: */
159: public boolean isPopupMenuVisible() {
160: return popupMenuVisible;
161: }
162:
163: /**
164: * Returns the scale factor for the width of the Menu components.
165: * The length of the children texts is multiplied by this factor and set as
166: * width (in em) for the children.
167: *
168: * @return Returns the widthScaleFactor.
169: */
170: public double getWidthScaleFactor() {
171: return widthScaleFactor;
172: }
173:
174: /**
175: * Sets the scale factor for the width of the Menu components.
176: * The length of the children texts is multiplied by this factor and set as
177: * width (in em) for the children.
178: *
179: * Default value is 0.8.
180: *
181: * @param widthScaleFactor The widthScaleFactor to set.
182: */
183: public void setWidthScaleFactor(double widthScaleFactor) {
184: this .widthScaleFactor = widthScaleFactor;
185: }
186:
187: /**
188: * @return Returns the amount of children elements.
189: */
190: public int getChildrenCount() {
191: return menuItems.size();
192: }
193:
194: /** gets the n'th child of the menu. If the index is too high, returns
195: * null.
196: * @param index the index of the child to return
197: * @return the n'th child.
198: */
199: public SComponent getChild(int index) {
200: if (getChildrenCount() > index) {
201: return (SComponent) menuItems.get(index);
202: } else {
203: return null;
204: }
205: }
206:
207: public void writePopup(Device device) throws IOException {
208: ((MenuCG) getCG()).writePopup(device, this );
209: }
210:
211: public void setAccelerator(KeyStroke keyStroke) {
212: throw new UnsupportedOperationException(
213: "Only invoke this on SMenuItem.");
214: }
215:
216: public void setEnabled(boolean enabled) {
217: if (enabled != isEnabled()) {
218: Set menuLinks = getSession().getMenuManager()
219: .getMenueLinks(this );
220: for (Iterator i = menuLinks.iterator(); i.hasNext();) {
221: ((SComponent) i.next()).reload();
222: }
223: }
224: super.setEnabled(enabled);
225: }
226:
227: }
|