01: package org.uispec4j;
02:
03: import junit.framework.Assert;
04: import org.uispec4j.assertion.Assertion;
05: import org.uispec4j.utils.ArrayUtils;
06:
07: import javax.swing.*;
08: import java.awt.*;
09:
10: /**
11: * Wrapper for JMenuBar components.<p/>
12: * A MenuBar is a container for top-level menu items represented by {@link MenuItem} components.
13: */
14: public class MenuBar extends AbstractUIComponent {
15: public static final String TYPE_NAME = "menuBar";
16: public static final Class[] SWING_CLASSES = { JMenuBar.class };
17:
18: private JMenuBar jMenuBar;
19:
20: public MenuBar(JMenuBar menuBar) {
21: Assert.assertNotNull(menuBar);
22: this .jMenuBar = menuBar;
23: }
24:
25: public Component getAwtComponent() {
26: return jMenuBar;
27: }
28:
29: public String getDescriptionTypeName() {
30: return TYPE_NAME;
31: }
32:
33: /** Returns a {@link MenuItem} component representing a top-level menu (for instance File/Edit/etc.).
34: * That MenuItem can be used then to access the individual menu commands, or other submenus.
35: */
36: public MenuItem getMenu(String menuName) {
37: int menuIndex = getMenuIndex(menuName);
38: Assert.assertFalse("Menu '" + menuName + "' does not exist",
39: menuIndex == -1);
40: JMenu menu = jMenuBar.getMenu(menuIndex);
41: return new MenuItem(menu);
42: }
43:
44: /** Checks the names displayed in the menu, ommiting separators. */
45: public Assertion contentEquals(final String[] menuNames) {
46: return new Assertion() {
47: public void check() {
48: String[] actual = new String[jMenuBar.getMenuCount()];
49: for (int i = 0; i < actual.length; i++) {
50: actual[i] = jMenuBar.getMenu(i).getText();
51: }
52: ArrayUtils.assertEquals(menuNames, actual);
53: }
54: };
55: }
56:
57: /** Returns the index of a menu item given its name, or -1 if it was not found. */
58: private int getMenuIndex(String menuName) {
59: for (int i = 0; i < jMenuBar.getMenuCount(); i++) {
60: JMenu menu = jMenuBar.getMenu(i);
61: if (menu == null) {
62: return -1;
63: }
64: String text = menu.getText();
65: if (text == null) {
66: continue;
67: }
68: if (text.equals(menuName)) {
69: return i;
70: }
71: }
72: return -1;
73: }
74: }
|