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 wingset;
014:
015: import org.wings.*;
016: import org.wings.border.*;
017:
018: import javax.swing.*;
019: import javax.swing.tree.TreeNode;
020: import java.awt.*;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.Random;
024:
025: /**
026: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
027: */
028: public class MenuExample extends WingSetPane {
029:
030: private SLabel selectionLabel;
031: private SMenuBar menuBar;
032: private int shortcutKey = java.awt.event.KeyEvent.VK_A;
033:
034: private ComponentControls controls;
035:
036: protected SComponent createControls() {
037: controls = new MenuControls();
038: return controls;
039: }
040:
041: public SComponent createExample() {
042:
043: menuBar = createMenuBar(HugeTreeModel.ROOT_NODE);
044:
045: controls.addControllable(menuBar);
046:
047: SBoxLayout boxLayout = new SBoxLayout(SBoxLayout.HORIZONTAL);
048: boxLayout.setVgap(20);
049: boxLayout.setHgap(20);
050:
051: SPanel formPanel = new SPanel();
052: formPanel.setLayout(boxLayout);
053: formPanel.setPreferredSize(SDimension.FULLWIDTH);
054: formPanel.add(new SComboBox(new DefaultComboBoxModel(
055: ListExample.createElements())));
056:
057: int tColCount = 1;
058: SPanel tListsPanel = new SPanel(new SGridLayout(tColCount));
059: for (int i = 0; i < tColCount; i++) {
060: SList list = new SList(ListExample.createListModel());
061: list.setVisibleRowCount(3);
062: tListsPanel.add(list);
063: }
064:
065: formPanel.add(tListsPanel, "List");
066: formPanel.add(new STextField("wingS is great"));
067: formPanel
068: .add(new STextArea(
069: "wingS is a great framework for implementing complex web applications"));
070:
071: SPanel messagePanel = new SPanel(new SGridLayout(1));
072: messagePanel
073: .add(new SLabel(
074: "Form components are overlayed or hidden (IE bug).\n\nSelected Menu: "));
075: selectionLabel = new SLabel("<No menue selected yet>");
076: selectionLabel.setFont(new SFont(SFont.BOLD));
077: selectionLabel.setForeground(Color.RED);
078: messagePanel.add(selectionLabel, "SelectionLabel");
079: messagePanel
080: .add(new SLabel(
081: "\nTry the menu accelerator keys."
082: + "\nAlt-A to Alt-Z call menuitem actions (doesn't work on Konqueror)"));
083: messagePanel.setBorder(new SEmptyBorder(50, 0, 0, 0));
084:
085: SButton addMenu = new SButton("Add new random menu");
086: addMenu.setHorizontalAlignment(SConstants.LEFT_ALIGN);
087: addMenu.addActionListener(new ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: int nr = new Random().nextInt(100);
090: SMenu menu = new SMenu("Menu " + nr);
091: for (int i = 0; i < 3; ++i) {
092: SMenuItem item = new SMenuItem(new MenuItemAction(
093: "Item " + nr + "-" + (i + 1)));
094: menu.add(item);
095: }
096: menuBar.add(menu);
097: }
098: });
099: SButton removeMenu = new SButton("Remove last added menu");
100: removeMenu.setHorizontalAlignment(SConstants.LEFT_ALIGN);
101: removeMenu.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: int count = menuBar.getMenuCount();
104: if (count > 0) {
105: menuBar.remove(menuBar.getMenu(count - 1));
106: }
107: }
108: });
109: SButton addMenuItem = new SButton("Add new random menu item");
110: addMenuItem.setHorizontalAlignment(SConstants.LEFT_ALIGN);
111: addMenuItem.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent e) {
113: int nr = new Random().nextInt(100);
114: SMenuItem menuItem = new SMenuItem(new MenuItemAction(
115: "Item " + nr));
116: SMenu firstMenu = menuBar.getMenu(0);
117: if (firstMenu != null) {
118: firstMenu.add(menuItem);
119: }
120: }
121: });
122: SButton removeMenuItem = new SButton(
123: "Remove last added menu item");
124: removeMenuItem.setHorizontalAlignment(SConstants.LEFT_ALIGN);
125: removeMenuItem.addActionListener(new ActionListener() {
126: public void actionPerformed(ActionEvent e) {
127: SMenu firstMenu = menuBar.getMenu(0);
128: if (firstMenu != null
129: && firstMenu.getChildrenCount() > 0) {
130: firstMenu.remove(firstMenu.getChildrenCount() - 1);
131: }
132: }
133: });
134: messagePanel.add(new SSpacer(1, 50));
135: messagePanel.add(addMenu);
136: messagePanel.add(new SSpacer(1, 10));
137: messagePanel.add(removeMenu);
138: messagePanel.add(new SSpacer(1, 20));
139: messagePanel.add(addMenuItem);
140: messagePanel.add(new SSpacer(1, 10));
141: messagePanel.add(removeMenuItem);
142:
143: SPanel mainPanel = new SPanel(new SBoxLayout(
144: SBoxLayout.VERTICAL));
145: mainPanel.add(formPanel);
146: mainPanel.add(messagePanel);
147: mainPanel.setPreferredSize(SDimension.FULLWIDTH);
148:
149: SPanel panel = new SPanel(new SBorderLayout());
150: panel.add(menuBar, SBorderLayout.NORTH);
151: panel.add(mainPanel, SBorderLayout.CENTER);
152: panel.setVerticalAlignment(SConstants.TOP_ALIGN);
153:
154: return panel;
155: }
156:
157: protected void setMenuItemsEnabled(boolean enabled) {
158: if (menuBar.getComponentCount() > 1) {
159: SMenuItem first = (SMenuItem) menuBar.getComponent(0);
160: SMenuItem last = (SMenuItem) menuBar.getComponent(menuBar
161: .getComponentCount() - 1);
162: recursiveMenuItemSwitch(first, last, enabled);
163: } else if (menuBar.getComponentCount() == 1) {
164: menuBar.getComponent(0).setEnabled(enabled);
165: }
166: }
167:
168: private void recursiveMenuItemSwitch(SMenuItem first,
169: SMenuItem last, boolean enabled) {
170: last.setEnabled(enabled);
171: if (first instanceof SMenu) {
172: SMenu parent = (SMenu) first;
173: if (parent.getChildrenCount() > 1) {
174: SMenuItem firstChild = (SMenuItem) parent.getChild(0);
175: SMenuItem lastChild = (SMenuItem) parent
176: .getChild(parent.getChildrenCount() - 1);
177: recursiveMenuItemSwitch(firstChild, lastChild, enabled);
178: } else if (((SMenu) first).getChildrenCount() == 1) {
179: parent.getChild(0).setEnabled(enabled);
180: }
181: }
182: }
183:
184: SMenuItem createMenuItem(TreeNode node) {
185: SMenuItem item = new SMenuItem(new MenuItemAction(node
186: .toString()));
187: /* setToolTipText() cannot be used due to JavaScript performance problems,
188: * only occurs when using incremental updates and menu
189: */
190: //item.setToolTipText(node.toString());
191: item.setShowAsFormComponent(true);
192: if (shortcutKey != 0) {
193: item.setAccelerator(KeyStroke.getKeyStroke(shortcutKey,
194: java.awt.Event.ALT_MASK));
195: if (shortcutKey == java.awt.event.KeyEvent.VK_Z) {
196: shortcutKey = 0;
197: } else {
198: shortcutKey++;
199: }
200: }
201: return item;
202: }
203:
204: SMenu createMenu(TreeNode root) {
205: SMenu menu = new SMenu(root.toString());
206: menu.setShowAsFormComponent(false);
207: menu.addActionListener(new MenuItemAction(null));
208:
209: for (int i = 0; i < root.getChildCount(); i++) {
210: TreeNode node = root.getChildAt(i);
211: if (node.isLeaf()) {
212: menu.add(createMenuItem(node));
213: } else {
214: menu.add(createMenu(node));
215: }
216: }
217:
218: return menu;
219: }
220:
221: SMenuBar createMenuBar(TreeNode root) {
222: SMenuBar menuBar = new SMenuBar();
223: SBorder border = new SLineBorder(Color.WHITE, 0);
224: border.setThickness(1, SConstants.TOP);
225: menuBar.setBorder(border);
226:
227: for (int i = 0; i < root.getChildCount(); i++) {
228: TreeNode node = root.getChildAt(i);
229: if (node.isLeaf()) {
230: menuBar.add(createMenuItem(node));
231: } else {
232: menuBar.add(createMenu(node));
233: }
234: }
235:
236: // Test menu separators
237: SMenu groupsMenu = new SMenu("Groups");
238: groupsMenu.add(new SMenuItem(new MenuItemAction(
239: "Item 1 of group 1")));
240: groupsMenu.addSeparator();
241: groupsMenu.add(new SMenuItem(new MenuItemAction(
242: "Item 1 of group 2")));
243: groupsMenu.add(new SMenuItem(new MenuItemAction(
244: "Item 2 of group 2")));
245: groupsMenu.addSeparator();
246: groupsMenu.add(new SMenuItem(new MenuItemAction(
247: "Item 1 of group 3")));
248: groupsMenu.add(new SMenuItem(new MenuItemAction(
249: "Item 2 of group 3")));
250: groupsMenu.add(new SMenuItem(new MenuItemAction(
251: "Item 3 of group 3")));
252: menuBar.add(groupsMenu);
253:
254: // Test right aligned menu
255: SMenu helpMenu = new SMenu("Help");
256: helpMenu.setHorizontalAlignment(RIGHT_ALIGN);
257: SMenuItem helpMenuItem = new SMenuItem("Help on using WingSet");
258: helpMenuItem.addActionListener(new MenuItemAction(
259: "Help on using WingSet"));
260: helpMenu.add(helpMenuItem);
261: menuBar.add(helpMenu);
262:
263: SMenu aboutMenu = new SMenu("About");
264: aboutMenu.setHorizontalAlignment(RIGHT_ALIGN);
265: SMenuItem aboutMenuItem = new SMenuItem("About WingSet");
266: aboutMenuItem.addActionListener(new MenuItemAction(
267: "About WingSet"));
268: aboutMenu.add(aboutMenuItem);
269: menuBar.add(aboutMenu);
270:
271: return menuBar;
272: }
273:
274: class MenuControls extends ComponentControls {
275: public MenuControls() {
276: widthTextField.setText("100%");
277: removeGlobalControl(fontComboBox);
278: removeGlobalControl(foregroundComboBox);
279: removeGlobalControl(backgroundComboBox);
280: removeGlobalControl(formComponentCheckBox);
281:
282: final SCheckBox disableSomeMenus = new SCheckBox(
283: "Disable some Menus ");
284: disableSomeMenus
285: .addActionListener(new java.awt.event.ActionListener() {
286: public void actionPerformed(ActionEvent e) {
287: setMenuItemsEnabled(!disableSomeMenus
288: .isSelected());
289: }
290: });
291: addControl(disableSomeMenus);
292: }
293: }
294:
295: private class MenuItemAction extends AbstractAction {
296: public MenuItemAction(String name) {
297: super (name);
298: }
299:
300: public void actionPerformed(ActionEvent e) {
301: selectionLabel.setText(((SMenuItem) e.getSource())
302: .getText());
303: }
304: }
305: }
|