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.MenuBarCG;
016:
017: import javax.swing.*;
018: import java.awt.*;
019: import java.util.ArrayList;
020:
021: /**
022: * Contains SMenu objects to construct a menu.
023: * <p/>
024: * When the user selects a SMenu object, its
025: * associated {@link org.wings.SMenu} is displayed, allowing the
026: * user to select one of the {@link org.wings.SMenuItem}s on it.
027: * <p/>
028: * Component are rendered in the order of the container. If a component is right
029: * aligned, every following components are also right aligned. So you have to
030: * sort the components in the order you want and have to take care that te
031: * components are sorted by their horizontal alignment
032: *
033: * @author <a href="mailto:andre@lison.de">Andre Lison</a>
034: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
035: * @see SMenu
036: */
037: public class SMenuBar extends SContainer {
038: /*
039: * Model for the selected subcontrol
040: */
041: private transient SingleSelectionModel selectionModel;
042:
043: private boolean paintBorder = true;
044: private Insets margin = null;
045:
046: /**
047: * Creates a new menu bar.
048: */
049: public SMenuBar() {
050: super ();
051: setSelectionModel(new DefaultSingleSelectionModel());
052: }
053:
054: /**
055: * Returns the model object that handles single selections.
056: *
057: * @return the SingleSelectionModel in use
058: * @see SingleSelectionModel
059: */
060: public SingleSelectionModel getSelectionModel() {
061: return selectionModel;
062: }
063:
064: /**
065: * Set the model object to handle single selections.
066: *
067: * @param model the SingleSelectionModel to use
068: * description: The selection model, recording which child is selected.
069: * @see SingleSelectionModel
070: */
071: public void setSelectionModel(SingleSelectionModel model) {
072: this .selectionModel = model;
073: }
074:
075: /**
076: * Appends the specified menu to the end of the menu bar.
077: *
078: * @param menu the SMenu component to add
079: */
080: public SMenuItem add(SMenu menu) {
081: getSession().getMenuManager().registerMenuLink(menu, this );
082: super .add(menu);
083: return menu;
084: }
085:
086: /**
087: * Removes the specified menu from the menu bar.
088: *
089: * @param menu the SMenu component to remove
090: */
091: public void remove(SMenu menu) {
092: getSession().getMenuManager().deregisterMenuLink(menu, this );
093: super .remove(menu);
094: }
095:
096: /**
097: * Gets the menu at the specified position in the menu bar.
098: *
099: * @param index an int giving the position in the menu bar, where
100: * 0 is the first position
101: * @return the SMenu at that position
102: */
103: public SMenu getMenu(int index) {
104: SComponent c = super .getComponent(index);
105: if (c instanceof SMenu)
106: return (SMenu) c;
107: return null;
108: }
109:
110: /**
111: * Returns the number of items in the menu bar.
112: *
113: * @return the number of items in the menu bar
114: */
115: public int getMenuCount() {
116: return getComponentCount();
117: }
118:
119: /**
120: * Returns the index of the specified component.
121: *
122: * @param c the <code>SComponent</code> to find
123: * @return an integer giving the component's position, where 0 is first;
124: * or -1 if it can't be found
125: */
126: public int getComponentIndex(SComponent c) {
127: int ncomponents = this .getComponentCount();
128: for (int i = 0; i < ncomponents; i++) {
129: SComponent comp = getComponent(i);
130: if (comp == c)
131: return i;
132: }
133: return -1;
134: }
135:
136: /**
137: * Sets the currently selected component, producing a
138: * a change to the selection model.
139: *
140: * @param sel the SComponent to select
141: */
142: public void setSelected(SComponent sel) {
143: SingleSelectionModel model = getSelectionModel();
144: int index = getComponentIndex(sel);
145: model.setSelectedIndex(index);
146: }
147:
148: /**
149: * Returns true if the MenuBar currently has a component selected
150: *
151: * @return true if a selection has been made, else false
152: */
153: public boolean isSelected() {
154: return selectionModel.isSelected();
155: }
156:
157: /**
158: * Returns true if the Menubar's border should be painted.
159: *
160: * @return true if the border should be painted, else false
161: */
162: public boolean isBorderPainted() {
163: return paintBorder;
164: }
165:
166: /**
167: * Sets whether the border should be painted.
168: *
169: * @param b if true and border property is not null, the border is painted.
170: * attribute: visualUpdate true
171: * description: Whether the border should be painted.
172: * @see #isBorderPainted
173: */
174: public void setBorderPainted(boolean b) {
175: paintBorder = b;
176: }
177:
178: /**
179: * Sets the margin between the menubar's border and
180: * its menus. Setting to null will cause the menubar to
181: * use the default margins.
182: *
183: * @param m an Insets object containing the margin values
184: * attribute: visualUpdate true
185: * description: The space between the menubar's border and its contents
186: * @see Insets
187: */
188: public void setMargin(Insets m) {
189: this .margin = m;
190: }
191:
192: /**
193: * Returns the margin between the menubar's border and
194: * its menus.
195: *
196: * @return an Insets object containing the margin values
197: * @see Insets
198: */
199: public Insets getMargin() {
200: if (margin == null) {
201: return new Insets(0, 0, 0, 0);
202: } else {
203: return margin;
204: }
205: }
206:
207: /**
208: * Returns a string representation of this SMenuBar. This method
209: * is intended to be used only for debugging purposes, and the
210: * content and format of the returned string may vary between
211: * implementations. The returned string may be empty but may not
212: * be <code>null</code>.
213: *
214: * @return a string representation of this SMenuBar.
215: */
216: protected String paramString() {
217: String paintBorderString = (paintBorder ? "true" : "false");
218: String marginString = (margin != null ? margin.toString() : "");
219:
220: return super .paramString() + ",margin=" + marginString
221: + ",paintBorder=" + paintBorderString;
222: }
223:
224: /* doc see SComponent */
225: public void setCG(MenuBarCG cg) {
226: super .setCG(cg);
227: }
228:
229: /* doc see SComponent */
230: public ArrayList getMenus() {
231: ArrayList menus = new ArrayList();
232: if (isVisible()) {
233: SPopupMenu pmenu = getComponentPopupMenu();
234: if (pmenu != null) {
235: menus.add(pmenu);
236: }
237: for (int i = 0; i < getComponentCount(); i++) {
238: SMenu menu = (SMenu) getComponent(i);
239: if (menus.contains(menu)) {
240: remove(menu);
241: }
242: menus.add(menu);
243: }
244: }
245: return menus;
246: }
247: }
|