KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > wingset > MenuExample


1 /*
2  * $Id: MenuExample.java,v 1.13 2005/05/18 16:06:15 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 wingset;
15
16 import org.wings.*;
17 import org.wings.plaf.MenuBarCG;
18 import org.wings.plaf.MenuCG;
19 import org.wings.plaf.MenuItemCG;
20
21 import javax.swing.*;
22 import javax.swing.tree.TreeNode JavaDoc;
23
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26
27
28 /**
29  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
30  * @version $Revision: 1.13 $
31  */

32 public class MenuExample extends WingSetPane {
33
34     private SLabel selectionLabel;
35     private SMenuBar menuBar;
36
37     private final ActionListener JavaDoc menuItemListener = new ActionListener JavaDoc() {
38         public void actionPerformed(ActionEvent JavaDoc e) {
39             selectionLabel.setText(((SMenuItem) e.getSource()).getText());
40         }
41     };
42
43     SMenuItem createMenuItem(TreeNode JavaDoc node) {
44         SMenuItem item = new SMenuItem(node.toString());
45         item.setToolTipText(node.toString());
46         item.addActionListener(menuItemListener);
47         return item;
48     }
49
50     SMenu createMenu(TreeNode JavaDoc root) {
51         SMenu menu = new SMenu(root.toString());
52         menu.addActionListener(menuItemListener);
53
54         for (int i = 0; i < root.getChildCount(); i++) {
55             TreeNode JavaDoc node = root.getChildAt(i);
56             if (node.isLeaf()) {
57                 menu.add(createMenuItem(node));
58             } else {
59                 menu.add(createMenu(node));
60             }
61         }
62
63         return menu;
64     }
65
66     SMenuBar createMenuBar(TreeNode JavaDoc root) {
67         SMenuBar menuBar = new SMenuBar();
68
69         for (int i = 0; i < root.getChildCount(); i++) {
70             TreeNode JavaDoc node = root.getChildAt(i);
71             if (node.isLeaf()) {
72                 menuBar.add(createMenuItem(node));
73             } else {
74                 menuBar.add(createMenu(node));
75             }
76         }
77
78         return menuBar;
79     }
80
81     public SComponent createExample() {
82         ComponentControls controls = new ComponentControls();
83         SForm panel = new SForm();
84         panel.setLayout(new SBoxLayout(SConstants.VERTICAL));
85         panel.setPreferredSize(new SDimension("100%", null));
86         
87         
88         selectionLabel = new SLabel("nothing selected");
89         menuBar = createMenuBar(HugeTreeModel.ROOT_NODE);
90         controls.addSizable(menuBar);
91         panel.add(controls);
92         panel.add(menuBar, "MenuBar");
93         panel.add(new SLabel("<html><br>Form components are overlayed or hidden (in IE). Selected Menu: "), "Intro");
94         panel.add(selectionLabel, "SelectionLabel");
95         panel.add(new SLabel("<html><hr><br>combobox :"));
96         panel.add(new SComboBox(new DefaultComboBoxModel(ListExample.createElements())), "ComboBox");
97         panel.add(new SLabel("<html><br>list:"));
98         SList list = new SList(ListExample.createListModel());
99         list.setVisibleRowCount(3);
100         panel.add(list, "List");
101         panel.add(new SLabel("<html><br>textfield(stay visible):"));
102         panel.add(new STextField("wingS is great"), "TextField");
103         panel.add(new SLabel("<html><br>textarea(stay visible):"));
104         panel.add(new STextArea("wingS is a great framework for implementing complex web applications"), "TextArea");
105
106         // enable / disable some menuitems
107
final SCheckBox switchEnable = new SCheckBox("disable some menuitems");
108         controls.add(switchEnable);
109         
110         switchEnable.addActionListener(new ActionListener JavaDoc() {
111             public void actionPerformed(ActionEvent JavaDoc e) {
112                 setMenuItemsEnabled(!switchEnable.isSelected());
113             }
114         });
115
116         return panel;
117     }
118
119     protected void setMenuItemsEnabled(boolean enabled) {
120         if (menuBar.getComponentCount() > 1) {
121             SMenuItem first = (SMenuItem)menuBar.getComponent(0);
122             SMenuItem last = (SMenuItem)menuBar.getComponent(menuBar.getComponentCount() - 1);
123             recursiveMenuItemSwitch(first, last, enabled);
124         } else if (menuBar.getComponentCount() == 1) {
125             ((SMenuItem)menuBar.getComponent(0)).setEnabled(enabled);
126         }
127     }
128
129     private void recursiveMenuItemSwitch(SMenuItem first, SMenuItem last, boolean enabled) {
130         last.setEnabled(enabled);
131         if (first instanceof SMenu) {
132             if (((SMenu)first).getChildrenCount() > 1) {
133                 SMenu parent = (SMenu) first;
134                 SMenuItem firstChild = (SMenuItem)parent.getChild(0);
135                 SMenuItem lastChild = (SMenuItem)parent.getChild(parent.getChildrenCount() - 1);
136                 recursiveMenuItemSwitch(firstChild, lastChild, enabled);
137             } else if (((SMenu)first).getChildrenCount() == 1) {
138                 ((SMenuItem)((SMenu)first).getChild(0)).setEnabled(enabled);
139             }
140         }
141     }
142 }
143
144
145
Popular Tags