001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.lib.client.commands;
015:
016: import org.jetbrains.annotations.NotNull;
017:
018: import javax.swing.*;
019: import java.awt.*;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.awt.event.MouseEvent;
023: import java.util.ArrayList;
024:
025: /**
026: * User: Martin
027: * Date: 21.11.2004
028: * Time: 11:23:59
029: */
030: public class CommandPopupMenu implements CommandComponent {
031: @NotNull
032: private Command command;
033: @NotNull
034: private CommandApplicationRoot commandApplicationRoot;
035: @NotNull
036: private String iconKey;
037:
038: @NotNull
039: private ArrayList<Presentation> presentations;
040:
041: public CommandPopupMenu(@NotNull
042: CommandApplicationRoot commandApplicationRoot, @NotNull
043: Command command, @NotNull
044: String iconKey) {
045: this .commandApplicationRoot = commandApplicationRoot;
046: this .iconKey = iconKey;
047: //noinspection ConstantConditions
048: if (command == null) {
049: throw new IllegalArgumentException(
050: "command must not be null");
051: }
052:
053: this .command = command;
054: presentations = new ArrayList<Presentation>();
055: }
056:
057: public void showPopupMenu(@NotNull
058: MouseEvent me) {
059: getJPopupMenu().show(me.getComponent(), me.getX(), me.getY());
060: }
061:
062: @NotNull
063: public JPopupMenu getJPopupMenu() {
064: JPopupMenu popupMenu = new MyJPopupMenu(this );
065:
066: addMenuItems(popupMenu, command, true);
067:
068: return popupMenu;
069: }
070:
071: private void addMenuItems(@NotNull
072: JPopupMenu popupMenu, @NotNull
073: final Command command, boolean first) {
074: if (command instanceof CommandGroup) {
075: CommandGroup commandGroup = (CommandGroup) command;
076: if (!first
077: && commandGroup.getType() == CommandGroup.Type.POPUP) {
078: Presentation presentation = command
079: .getTemplatePresentation().getCopy(
080: commandApplicationRoot);
081: presentations.add(presentation);
082:
083: CommandMenu menu = new CommandMenu(presentation, true,
084: iconKey);
085: popupMenu.add(menu);
086:
087: Command[] commands = commandGroup.getChildren();
088: for (Command subCommand : commands) {
089: addMenuItems(menu, subCommand);
090: }
091: } else {
092: Command[] commands = commandGroup.getChildren();
093: for (Command c : commands) {
094: addMenuItems(popupMenu, c, false);
095: }
096: }
097: } else if (command instanceof SeparatorCommand) {
098: popupMenu.addSeparator();
099: } else if (command instanceof ToggleCommand) {
100: final Presentation presentation = command
101: .getTemplatePresentation().getCopy(
102: commandApplicationRoot);
103: presentations.add(presentation);
104:
105: ToggleCommandMenuItem menuItem = new ToggleCommandMenuItem(
106: presentation, iconKey);
107: popupMenu.add(menuItem);
108: menuItem.addActionListener(new ActionListener() {
109: public void actionPerformed(@NotNull
110: ActionEvent e) {
111: DataProvider dataProvider = CommandManager
112: .getCurrentCommandProvider();
113: presentation.getCommandApplicationRoot()
114: .clearStatusText();
115: command.execute(new DefaultCommandEvent(null,
116: dataProvider.getDataContext(), dataProvider
117: .getPlace(), presentation));
118: CommandManager.dataProviderChanged();
119: }
120: });
121: } else {
122: final Presentation presentation = command
123: .getTemplatePresentation().getCopy(
124: commandApplicationRoot);
125: presentations.add(presentation);
126:
127: JComponent customComponent = command
128: .getCustomComponent(presentation);
129: if (customComponent != null) {
130: popupMenu.add(customComponent);
131: } else {
132: CommandMenuItem menuItem = new CommandMenuItem(
133: presentation, iconKey);
134: popupMenu.add(menuItem);
135: menuItem.addActionListener(new ActionListener() {
136: public void actionPerformed(@NotNull
137: ActionEvent e) {
138: DataProvider dataProvider = CommandManager
139: .getCurrentCommandProvider();
140: presentation.getCommandApplicationRoot()
141: .clearStatusText();
142: command.execute(new DefaultCommandEvent(null,
143: dataProvider.getDataContext(),
144: dataProvider.getPlace(), presentation));
145: CommandManager.dataProviderChanged();
146: }
147: });
148: }
149: }
150: }
151:
152: private void addMenuItems(@NotNull
153: CommandMenu popupMenu, @NotNull
154: final Command command) {
155: if (command instanceof CommandGroup) {
156: CommandGroup commandGroup = (CommandGroup) command;
157: if (commandGroup.getType() == CommandGroup.Type.POPUP) {
158: Presentation presentation = command
159: .getTemplatePresentation().getCopy(
160: commandApplicationRoot);
161: presentations.add(presentation);
162:
163: CommandMenu menu = new CommandMenu(presentation, true,
164: iconKey);
165: popupMenu.add(menu);
166:
167: Command[] commands = commandGroup.getChildren();
168: for (Command subCommand : commands) {
169: addMenuItems(menu, subCommand);
170: }
171: } else {
172: Command[] commands = commandGroup.getChildren();
173: for (Command c : commands) {
174: addMenuItems(popupMenu, c);
175: }
176: }
177: } else if (command instanceof SeparatorCommand) {
178: popupMenu.addSeparator();
179: } else if (command instanceof ToggleCommand) {
180: final Presentation presentation = command
181: .getTemplatePresentation().getCopy(
182: commandApplicationRoot);
183: presentations.add(presentation);
184:
185: ToggleCommandMenuItem menuItem = new ToggleCommandMenuItem(
186: presentation, iconKey);
187: popupMenu.add(menuItem);
188: menuItem.addActionListener(new ActionListener() {
189: public void actionPerformed(@NotNull
190: ActionEvent e) {
191: DataProvider dataProvider = CommandManager
192: .getCurrentCommandProvider();
193: presentation.getCommandApplicationRoot()
194: .clearStatusText();
195: command.execute(new DefaultCommandEvent(null,
196: dataProvider.getDataContext(), dataProvider
197: .getPlace(), presentation));
198: CommandManager.dataProviderChanged();
199: }
200: });
201: } else {
202: final Presentation presentation = command
203: .getTemplatePresentation().getCopy(
204: commandApplicationRoot);
205: presentations.add(presentation);
206:
207: JComponent customComponent = command
208: .getCustomComponent(presentation);
209: if (customComponent != null) {
210: popupMenu.add(customComponent);
211: } else {
212: CommandMenuItem menuItem = new CommandMenuItem(
213: presentation, iconKey);
214: popupMenu.add(menuItem);
215: menuItem.addActionListener(new ActionListener() {
216: public void actionPerformed(@NotNull
217: ActionEvent e) {
218: DataProvider dataProvider = CommandManager
219: .getCurrentCommandProvider();
220: presentation.getCommandApplicationRoot()
221: .clearStatusText();
222: command.execute(new DefaultCommandEvent(null,
223: dataProvider.getDataContext(),
224: dataProvider.getPlace(), presentation));
225: CommandManager.dataProviderChanged();
226: }
227: });
228: }
229: }
230: }
231:
232: private void update(@NotNull
233: DataProvider dataProvider) {
234: for (Presentation presentation : presentations) {
235: CommandEvent event = new DefaultCommandEvent(null,
236: dataProvider.getDataContext(), dataProvider
237: .getPlace(), presentation);
238: presentation.getCommand().update(event);
239: }
240: }
241:
242: private static class MyJPopupMenu extends JPopupMenu implements
243: CommandComponent {
244: @NotNull
245: private CommandPopupMenu outer;
246:
247: private MyJPopupMenu(@NotNull
248: CommandPopupMenu outer) {
249: this .outer = outer;
250: }
251:
252: public void show(@NotNull
253: Component invoker, int x, int y) {
254: outer.update(CommandManager.getCurrentCommandProvider());
255: super.show(invoker, x, y);
256: }
257: }
258: }
|