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.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.beans.PropertyChangeEvent;
022: import java.beans.PropertyChangeListener;
023: import java.util.ArrayList;
024:
025: /**
026: * User: Martin
027: * Date: 21.11.2004
028: * Time: 11:23:59
029: */
030: public class CommandMenuBar 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 CommandMenuBar(@NotNull
042: CommandApplicationRoot commandApplicationRoot, @NotNull
043: Command command) {
044: this .commandApplicationRoot = commandApplicationRoot;
045: this .iconKey = CommandSettings.getInstance().getMenuIconKey();
046: //noinspection ConstantConditions
047: if (command == null) {
048: throw new IllegalArgumentException(
049: "command must not be null");
050: }
051:
052: this .command = command;
053: presentations = new ArrayList<Presentation>();
054: }
055:
056: @NotNull
057: public JMenuBar getJMenuBar() {
058: //noinspection UNUSED_SYMBOL
059: final JMenuBar menuBar = new JMenuBar() {
060: //it's unused, but ensures the CommandMenuBar is not GCed while the menuBar is still alive (see usage of WeakReferences). Fix this if you know a more elegant way to achieve this.
061: @NotNull
062: @SuppressWarnings({"UnusedDeclaration"})
063: private CommandMenuBar commandMenuBar = CommandMenuBar.this ;
064: };
065:
066: rebuildMenuBar(menuBar);
067:
068: return menuBar;
069: }
070:
071: private void rebuildMenuBar(@NotNull
072: final JMenuBar menuBar) {
073: menuBar.removeAll();
074:
075: for (Presentation presentation : presentations) {
076: presentation.clearPropertyChangeListeners();
077: }
078:
079: presentations.clear();
080:
081: PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
082: public void propertyChange(@NotNull
083: PropertyChangeEvent evt) {
084: if ("structure".equals(evt.getPropertyName()))//NON-NLS
085: {
086: rebuildMenuBar(menuBar);
087: }
088: }
089: };
090:
091: if (command instanceof CommandGroup) {
092: CommandGroup commandGroup = (CommandGroup) command;
093: Presentation presentation = commandGroup
094: .getTemplatePresentation().getCopy(
095: commandApplicationRoot);
096: presentation
097: .addPropertyChangeListener(propertyChangeListener);
098: presentations.add(presentation);
099:
100: Command[] commands = commandGroup.getChildren();
101:
102: for (Command c : commands) {
103: addMenuItems(menuBar, c, propertyChangeListener);
104: }
105: }
106:
107: menuBar.revalidate();
108: menuBar.repaint();
109: }
110:
111: private void addMenuItems(@NotNull
112: JMenuBar menuBar, @NotNull
113: final Command command, @NotNull
114: PropertyChangeListener propertyChangeListener) {
115: if (command instanceof CommandGroup) {
116: CommandGroup commandGroup = (CommandGroup) command;
117:
118: Presentation presentation = command
119: .getTemplatePresentation().getCopy(
120: commandApplicationRoot);
121: presentation
122: .addPropertyChangeListener(propertyChangeListener);
123: presentations.add(presentation);
124:
125: CommandMenu menu = new CommandMenu(presentation, false,
126: iconKey);
127: menuBar.add(menu);
128:
129: Command[] commands = commandGroup.getChildren();
130: for (Command subCommand : commands) {
131: addMenuItems(menu, subCommand, propertyChangeListener);
132: }
133: } else if (command instanceof ToggleCommand) {
134: final Presentation presentation = command
135: .getTemplatePresentation().getCopy(
136: commandApplicationRoot);
137: presentation
138: .addPropertyChangeListener(propertyChangeListener);
139: presentations.add(presentation);
140:
141: ToggleCommandMenuItem menuItem = new ToggleCommandMenuItem(
142: presentation, iconKey);
143: menuBar.add(menuItem);
144: menuItem.addActionListener(new ActionListener() {
145: public void actionPerformed(@NotNull
146: ActionEvent e) {
147: DataProvider dataProvider = CommandManager
148: .getCurrentCommandProvider();
149: presentation.getCommandApplicationRoot()
150: .clearStatusText();
151: command.execute(new DefaultCommandEvent(null,
152: dataProvider.getDataContext(), dataProvider
153: .getPlace(), presentation));
154: CommandManager.dataProviderChanged();
155: }
156: });
157: } else {
158: final Presentation presentation = command
159: .getTemplatePresentation().getCopy(
160: commandApplicationRoot);
161: presentation
162: .addPropertyChangeListener(propertyChangeListener);
163: presentations.add(presentation);
164:
165: JComponent customComponent = command
166: .getCustomComponent(presentation);
167: if (customComponent != null) {
168: menuBar.add(customComponent);
169: } else {
170: CommandMenuItem menuItem = new CommandMenuItem(
171: presentation, iconKey);
172: menuBar.add(menuItem);
173: menuItem.addActionListener(new ActionListener() {
174: public void actionPerformed(@NotNull
175: ActionEvent e) {
176: DataProvider dataProvider = CommandManager
177: .getCurrentCommandProvider();
178: presentation.getCommandApplicationRoot()
179: .clearStatusText();
180: command.execute(new DefaultCommandEvent(null,
181: dataProvider.getDataContext(),
182: dataProvider.getPlace(), presentation));
183: CommandManager.dataProviderChanged();
184: }
185: });
186: }
187: }
188: }
189:
190: private void addMenuItems(@NotNull
191: CommandMenu popupMenu, @NotNull
192: final Command command, @NotNull
193: PropertyChangeListener propertyChangeListener) {
194: if (command instanceof CommandGroup) {
195: CommandGroup commandGroup = (CommandGroup) command;
196: if (commandGroup.getType() == CommandGroup.Type.POPUP) {
197: Presentation presentation = command
198: .getTemplatePresentation().getCopy(
199: commandApplicationRoot);
200: presentation
201: .addPropertyChangeListener(propertyChangeListener);
202: presentations.add(presentation);
203:
204: CommandMenu menu = new CommandMenu(presentation, true,
205: iconKey);
206: popupMenu.add(menu);
207:
208: Command[] commands = commandGroup.getChildren();
209: for (Command subCommand : commands) {
210: addMenuItems(menu, subCommand,
211: propertyChangeListener);
212: }
213:
214: } else {
215: Command[] commands = commandGroup.getChildren();
216: for (Command c : commands) {
217: addMenuItems(popupMenu, c, propertyChangeListener);
218: }
219: }
220: } else if (command instanceof SeparatorCommand) {
221: popupMenu.addSeparator();
222: } else if (command instanceof ToggleCommand) {
223: final Presentation presentation = command
224: .getTemplatePresentation().getCopy(
225: commandApplicationRoot);
226: presentation
227: .addPropertyChangeListener(propertyChangeListener);
228: presentations.add(presentation);
229:
230: ToggleCommandMenuItem menuItem = new ToggleCommandMenuItem(
231: presentation, iconKey);
232: popupMenu.add(menuItem);
233: menuItem.addActionListener(new ActionListener() {
234: public void actionPerformed(@NotNull
235: ActionEvent e) {
236: DataProvider dataProvider = CommandManager
237: .getCurrentCommandProvider();
238: presentation.getCommandApplicationRoot()
239: .clearStatusText();
240: command.execute(new DefaultCommandEvent(null,
241: dataProvider.getDataContext(), dataProvider
242: .getPlace(), presentation));
243: CommandManager.dataProviderChanged();
244: }
245: });
246: } else {
247: final Presentation presentation = command
248: .getTemplatePresentation().getCopy(
249: commandApplicationRoot);
250: presentations.add(presentation);
251:
252: JComponent customComponent = command
253: .getCustomComponent(presentation);
254: if (customComponent != null) {
255: popupMenu.add(customComponent);
256: } else {
257: CommandMenuItem menuItem = new CommandMenuItem(
258: presentation, iconKey);
259: popupMenu.add(menuItem);
260: menuItem.addActionListener(new ActionListener() {
261: public void actionPerformed(@NotNull
262: ActionEvent e) {
263: DataProvider dataProvider = CommandManager
264: .getCurrentCommandProvider();
265: presentation.getCommandApplicationRoot()
266: .clearStatusText();
267: command.execute(new DefaultCommandEvent(null,
268: dataProvider.getDataContext(),
269: dataProvider.getPlace(), presentation));
270: CommandManager.dataProviderChanged();
271: }
272: });
273: }
274: }
275: }
276:
277: public void update(@NotNull
278: DataProvider dataProvider) {
279: //noinspection ObjectEquality
280: if (dataProvider.getDataContext().getCommandApplicationRoot() != commandApplicationRoot) {
281: return;
282: }
283:
284: ArrayList<Presentation> presentations = new ArrayList<Presentation>(
285: this .presentations);
286: for (Presentation presentation : presentations) {
287: CommandEvent event = new DefaultCommandEvent(null,
288: dataProvider.getDataContext(), dataProvider
289: .getPlace(), presentation);
290: presentation.getCommand().update(event);
291: }
292: }
293:
294: }
|