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.SBoxLayout;
016: import org.wings.SButton;
017: import org.wings.SComponent;
018: import org.wings.SConstants;
019: import org.wings.SFont;
020: import org.wings.SLabel;
021: import org.wings.SMenu;
022: import org.wings.SMenuItem;
023: import org.wings.SPanel;
024: import org.wings.SPopupMenu;
025: import org.wings.SSpacer;
026: import org.wings.STextField;
027: import org.wings.SURLIcon;
028: import org.wings.border.SEmptyBorder;
029:
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032:
033: /**
034: * @author Holger Engels
035: */
036: public class PopupMenuExample extends WingSetPane {
037:
038: private SLabel selection;
039:
040: private final ActionListener menuItemListener = new java.awt.event.ActionListener() {
041: public void actionPerformed(ActionEvent e) {
042: selection.setText(((SMenuItem) e.getSource()).getText());
043: }
044: };
045:
046: protected SComponent createControls() {
047: return null;
048: }
049:
050: public SComponent createExample() {
051:
052: final SPopupMenu menu = new SPopupMenu();
053: final SMenuItem cutMenuItem = createMenuItem("Cut");
054: cutMenuItem.setShowAsFormComponent(true);
055: menu.add(cutMenuItem);
056: menu.add(createMenuItem("Copy"));
057: menu.add(createMenuItem("Paste"));
058: menu.addSeparator();
059:
060: SMenu subMenu = new SMenu("Help");
061: subMenu.add(createMenuItem("About"));
062: subMenu.add(createMenuItem("Topics"));
063: menu.add(subMenu);
064:
065: final SPopupMenu menu2 = new SPopupMenu();
066: menu2.add(createMenuItem("Open"));
067: menu2.add(createMenuItem("Save"));
068: menu2.add(createMenuItem("Close"));
069:
070: final SLabel testLabel = new SLabel(
071: "This label has a context menu.");
072: testLabel.setComponentPopupMenu(menu);
073: final STextField testLabel2 = new STextField(
074: "This textfield has the same context menu.");
075: testLabel2.setColumns(testLabel2.getText().length());
076: testLabel2.setComponentPopupMenu(menu);
077: final SLabel testLabel3 = new SLabel(
078: " This label has another context menu.", new SURLIcon(
079: "../icons/cowSmall.gif"));
080: testLabel3.setComponentPopupMenu(menu2);
081: SLabel selectionLabel = new SLabel("Selected Menu: ");
082: selectionLabel.setBorder(new SEmptyBorder(20, 0, 0, 0));
083: selection = new SLabel("none");
084: selection.setFont(new SFont(SFont.BOLD));
085:
086: SBoxLayout boxLayout = new SBoxLayout(SBoxLayout.VERTICAL);
087: boxLayout.setVgap(10);
088:
089: final SPanel all = new SPanel(boxLayout);
090:
091: final String titleMenueEnabled = " first context menu";
092: final SButton toggleMenuEnabled = new SButton("Disable"
093: + titleMenueEnabled);
094: toggleMenuEnabled.setHorizontalAlignment(SConstants.LEFT_ALIGN);
095: toggleMenuEnabled.addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: if (menu.isEnabled()) {
098: menu.setEnabled(false);
099: toggleMenuEnabled.setText("Enable"
100: + titleMenueEnabled);
101: } else {
102: menu.setEnabled(true);
103: toggleMenuEnabled.setText("Disable"
104: + titleMenueEnabled);
105: }
106: }
107: });
108: final String titleContextMenu = " context menu from first label";
109: final SButton toggleContextMenu = new SButton("Remove"
110: + titleContextMenu);
111: toggleContextMenu.setHorizontalAlignment(SConstants.LEFT_ALIGN);
112: toggleContextMenu.addActionListener(new ActionListener() {
113: public void actionPerformed(ActionEvent e) {
114: if (testLabel.getComponentPopupMenu() != null) {
115: testLabel.setComponentPopupMenu(null);
116: toggleContextMenu.setText("Add" + titleContextMenu);
117: } else {
118: testLabel.setComponentPopupMenu(menu);
119: toggleContextMenu.setText("Remove"
120: + titleContextMenu);
121: }
122: }
123: });
124: final String titleContextMenu2 = " context menu from textfield";
125: final SButton toggleContextMenu2 = new SButton("Remove"
126: + titleContextMenu2);
127: toggleContextMenu2
128: .setHorizontalAlignment(SConstants.LEFT_ALIGN);
129: toggleContextMenu2.addActionListener(new ActionListener() {
130: public void actionPerformed(ActionEvent e) {
131: if (testLabel2.getComponentPopupMenu() != null) {
132: testLabel2.setComponentPopupMenu(null);
133: toggleContextMenu2.setText("Add"
134: + titleContextMenu2);
135: } else {
136: testLabel2.setComponentPopupMenu(menu);
137: toggleContextMenu2.setText("Remove"
138: + titleContextMenu2);
139: }
140: }
141: });
142: final String titleContextMenu3 = " context menu second label";
143: final SButton toggleContextMenu3 = new SButton("Remove"
144: + titleContextMenu2);
145: toggleContextMenu3
146: .setHorizontalAlignment(SConstants.LEFT_ALIGN);
147: toggleContextMenu3.addActionListener(new ActionListener() {
148: public void actionPerformed(ActionEvent e) {
149: if (testLabel3.getComponentPopupMenu() != null) {
150: testLabel3.setComponentPopupMenu(null);
151: toggleContextMenu3.setText("Add"
152: + titleContextMenu3);
153: } else {
154: testLabel3.setComponentPopupMenu(menu2);
155: toggleContextMenu3.setText("Remove"
156: + titleContextMenu3);
157: }
158: }
159: });
160:
161: all.add(testLabel);
162: all.add(testLabel2);
163: all.add(testLabel3);
164: all.add(new SSpacer(1, 10));
165: all.add(toggleMenuEnabled);
166: all.add(toggleContextMenu);
167: all.add(toggleContextMenu2);
168: all.add(toggleContextMenu3);
169: all.add(selectionLabel);
170: all.add(selection);
171:
172: return all;
173: }
174:
175: private SMenuItem createMenuItem(String string) {
176: SMenuItem result = new SMenuItem(string);
177: result.addActionListener(menuItemListener);
178: return result;
179: }
180: }
|