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.plaf.PopupMenuCG;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Set;
021:
022: /**
023: * A popup or context menu that can be attached to abitrary components.
024: *
025: * @author hengels
026: */
027: public class SPopupMenu extends SComponent {
028: protected final List menuItems = new ArrayList();
029: private double widthScaleFactor = 0.7f;
030:
031: /**
032: * Add a menu item to this menu.
033: */
034: public void add(SMenuItem menuItem) {
035: menuItems.add(menuItem);
036: menuItem.setParentMenu(this );
037: menuItem.putClientProperty("drm:realParentComponent", this );
038: }
039:
040: /**
041: * Add a menu item to this menu.
042: */
043: public void add(SComponent menuItem) {
044: menuItems.add(menuItem);
045: menuItem.setParentFrame(getParentFrame());
046: menuItem.putClientProperty("drm:realParentComponent", this );
047: }
048:
049: /**
050: * Add a separator to this menu.
051: */
052: public void addSeparator() {
053: add(new SSeparator());
054: }
055:
056: public void setParentFrame(SFrame f) {
057: if (getParentFrame() == null && f != null) {
058: reload();
059: }
060: if (f != null
061: || (f == null && !getSession().getMenuManager()
062: .isMenuLinked(this ))) {
063: super .setParentFrame(f);
064: for (int i = 0; i < menuItems.size(); i++) {
065: ((SComponent) menuItems.get(i)).setParentFrame(f);
066: }
067: }
068: }
069:
070: /**
071: * Add a menu item to this menu.
072: */
073: public void add(String menuitem) {
074: this .add(new SMenuItem(menuitem));
075: }
076:
077: public SComponent getMenuComponent(int pos) {
078: return (SComponent) menuItems.get(pos);
079: }
080:
081: /**
082: * Return the number of items on the menu, including separators.
083: */
084: public int getMenuComponentCount() {
085: return menuItems.size();
086: }
087:
088: /**
089: * Remove all {@link SMenuItem} from this menu.
090: */
091: public void removeAll() {
092: while (menuItems.size() > 0) {
093: remove(0);
094: }
095: }
096:
097: /**
098: * Removes the menu item at specified index from the menu.
099: */
100: public void remove(int pos) {
101: remove(getMenuComponent(pos));
102: }
103:
104: /**
105: * removes a specific menu item component.
106: */
107: public void remove(SComponent comp) {
108: menuItems.remove(comp);
109: comp.setParentFrame(null);
110: comp.putClientProperty("drm:realParentComponent", "drm:null");
111: }
112:
113: public void setCG(PopupMenuCG cg) {
114: super .setCG(cg);
115: }
116:
117: /**
118: * Returns the scale factor for the width of the Menu components.
119: * The length of the children texts is multiplied by this factor and set as
120: * width (in em) for the children.
121: *
122: * @return Returns the widthScaleFactor.
123: */
124: public double getWidthScaleFactor() {
125: return widthScaleFactor;
126: }
127:
128: /**
129: * Sets the scale factor for the width of the Menu components.
130: * The length of the children texts is multiplied by this factor and set as
131: * width (in em) for the children.
132: *
133: * Default value is 0.8.
134: *
135: * @param widthScaleFactor The widthScaleFactor to set.
136: */
137: public void setWidthScaleFactor(double widthScaleFactor) {
138: this .widthScaleFactor = widthScaleFactor;
139: }
140:
141: public void setEnabled(boolean enabled) {
142: if (enabled != isEnabled()) {
143: Set menuLinks = getSession().getMenuManager()
144: .getMenueLinks(this );
145: for (Iterator i = menuLinks.iterator(); i.hasNext();) {
146: ((SComponent) i.next()).reload();
147: }
148: }
149: super.setEnabled(enabled);
150: }
151: }
|