1 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 ; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 27 28 32 public class MenuExample extends WingSetPane { 33 34 private SLabel selectionLabel; 35 private SMenuBar menuBar; 36 37 private final ActionListener menuItemListener = new ActionListener () { 38 public void actionPerformed(ActionEvent e) { 39 selectionLabel.setText(((SMenuItem) e.getSource()).getText()); 40 } 41 }; 42 43 SMenuItem createMenuItem(TreeNode 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 root) { 51 SMenu menu = new SMenu(root.toString()); 52 menu.addActionListener(menuItemListener); 53 54 for (int i = 0; i < root.getChildCount(); i++) { 55 TreeNode 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 root) { 67 SMenuBar menuBar = new SMenuBar(); 68 69 for (int i = 0; i < root.getChildCount(); i++) { 70 TreeNode 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 final SCheckBox switchEnable = new SCheckBox("disable some menuitems"); 108 controls.add(switchEnable); 109 110 switchEnable.addActionListener(new ActionListener () { 111 public void actionPerformed(ActionEvent 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 |