KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SMenu


1 /*
2  * $Id: SMenu.java,v 1.8 2005/04/13 16:24:52 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;
15
16 import org.wings.io.Device;
17 import org.wings.plaf.MenuBarCG;
18 import org.wings.plaf.MenuCG;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:andre.lison@general-bytes.com">Andre Lison</a>
26  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
27  * @version $Revision: 1.8 $
28  */

29 public class SMenu extends SMenuItem {
30     private boolean popupMenuVisible = false;
31     protected final List JavaDoc menuItems = new ArrayList JavaDoc();
32     private double widthScaleFactor = 0.7f;
33
34     public SMenu(String JavaDoc text) {
35         super(text);
36     }
37
38     public SMenu() {
39         super();
40     }
41
42     public SMenu(SIcon i) {
43         super(i);
44     }
45
46     public SMenu(String JavaDoc text, SIcon icon) {
47         super(text, icon);
48     }
49
50
51     /**
52      * Add a menu item to this menu.
53      */

54     public void add(SMenuItem menuItem) {
55         menuItems.add(menuItem);
56         menuItem.setParentMenu(this);
57     }
58
59     /**
60      * Add a menu item to this menu.
61      */

62     public void add(SComponent menuItem) {
63         menuItems.add(menuItem);
64         menuItem.setParentFrame(getParentFrame());
65     }
66
67     public void setParentFrame(SFrame f) {
68         super.setParentFrame(f);
69         for (int i = 0; i < menuItems.size(); i++)
70             ((SComponent) menuItems.get(i)).setParentFrame(f);
71     }
72
73     /**
74      * Add a menu item to this menu.
75      */

76     public void add(String JavaDoc menuitem) {
77         this.add(new SMenuItem(menuitem));
78     }
79
80     public SComponent getMenuComponent(int pos) {
81         return (SComponent) menuItems.get(pos);
82     }
83
84     /**
85      * Return the number of items on the menu, including separators.
86      */

87     public int getMenuComponentCount() {
88         return menuItems.size();
89     }
90
91     /**
92      * Remove all {@link SMenuItem} from this menu.
93      */

94     public void removeAll() {
95         while (menuItems.size() > 0) {
96             remove(0);
97         }
98     }
99
100     /**
101      * Removes the menu item at specified index from the menu.
102      */

103     public void remove(int pos) {
104         remove(getMenuComponent(pos));
105     }
106
107     /**
108      * removes a specific menu item component.
109      */

110     public void remove(SComponent comp) {
111         menuItems.remove(comp);
112         comp.setParentFrame(null);
113     }
114
115     public void setCG(MenuBarCG cg) {
116         super.setCG(cg);
117     }
118
119     /**
120      * Sets the visibility of the menu's popup.
121      * If the menu is not enabled, this method will have no effect.
122      *
123      * @param b a boolean value -- true to make the menu visible, false to hide it
124      */

125     public void setPopupMenuVisible(boolean b) {
126         if (!isEnabled())
127             return;
128         popupMenuVisible = b;
129     }
130
131     /**
132      * Returns true if the menu's popup window is visible.
133      *
134      * @return true if the menu is visible, else false.
135      */

136     public boolean isPopupMenuVisible() {
137         return popupMenuVisible;
138     }
139
140     /**
141      * Returns the scale factor for the width of the Menu components.
142      * The length of the children texts is multiplied by this factor and set as
143      * width (in em) for the children.
144      *
145      * @return Returns the widthScaleFactor.
146      */

147     public double getWidthScaleFactor() {
148         return widthScaleFactor;
149     }
150     /**
151      * Sets the scale factor for the width of the Menu components.
152      * The length of the children texts is multiplied by this factor and set as
153      * width (in em) for the children.
154      *
155      * Default value is 0.8.
156      *
157      * @param widthScaleFactor The widthScaleFactor to set.
158      */

159     public void setWidthScaleFactor(double widthScaleFactor) {
160         this.widthScaleFactor = widthScaleFactor;
161     }
162
163     /**
164      * @return Returns the amount of children elements.
165      */

166     public int getChildrenCount() {
167         return menuItems.size();
168     }
169     
170     /** gets the n'th child of the menu. If the index is too high, returns
171      * null.
172      * @param index the index of the child to return
173      * @return the n'th child.
174      */

175     public SComponent getChild(int index) {
176         if (getChildrenCount() > index) {
177             return (SComponent)menuItems.get(index);
178         } else {
179             return null;
180         }
181     }
182     
183     public void writePopup(Device device) throws IOException JavaDoc {
184         ((MenuCG)getCG()).writePopup(device, this);
185     }
186 }
187
188
189
Popular Tags