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: import org.jetbrains.annotations.Nullable;
018:
019: import javax.swing.*;
020: import java.awt.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.util.ArrayList;
025:
026: /**
027: * User: Martin
028: * Date: 21.11.2004
029: * Time: 13:15:56
030: */
031: public class CommandToolBar implements CommandComponent {
032: @NotNull
033: private Command command;
034: @NotNull
035: private CommandApplicationRoot commandApplicationRoot;
036: @Nullable
037: private DataProvider dataProvider;
038: @NotNull
039: private String iconKey;
040:
041: @NotNull
042: private ArrayList<Presentation> presentations;
043:
044: @NotNull
045: private Presentation topLevelPresentation;
046:
047: public CommandToolBar(@NotNull
048: CommandApplicationRoot commandApplicationRoot, @Nullable
049: DataProvider dataProvider, @NotNull
050: Command command) {
051: this .commandApplicationRoot = commandApplicationRoot;
052: this .dataProvider = dataProvider;
053: this .iconKey = CommandSettings.getInstance()
054: .getToolbarIconKey();
055: //noinspection ConstantConditions
056: if (command == null) {
057: throw new IllegalArgumentException(
058: "command must not be null");
059: }
060:
061: this .command = command;
062: presentations = new ArrayList<Presentation>();
063:
064: topLevelPresentation = command.getTemplatePresentation()
065: .getCopy(commandApplicationRoot);
066: }
067:
068: @NotNull
069: public JToolBar getToolBar() {
070: //noinspection UNUSED_SYMBOL
071: final JToolBar toolBar = new JToolBar() {
072: //it's unused, but ensures the CommandToolBar 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.
073: @NotNull
074: @SuppressWarnings({"UnusedDeclaration"})
075: private CommandToolBar commandToolBar = CommandToolBar.this ;
076: };
077:
078: toolBar.setFloatable(false);
079: toolBar.setRollover(true);
080:
081: rebuildToolBar(toolBar);
082:
083: topLevelPresentation
084: .addPropertyChangeListener(new PropertyChangeListener() {
085: public void propertyChange(@NotNull
086: PropertyChangeEvent evt) {
087: if ("structure".equals(evt.getPropertyName()))//NON-NLS
088: {
089: rebuildToolBar(toolBar);
090: }
091: }
092: });
093:
094: return toolBar;
095: }
096:
097: private void rebuildToolBar(@NotNull
098: final JToolBar toolBar) {
099: toolBar.removeAll();
100:
101: for (Presentation presentation : presentations) {
102: presentation.clearPropertyChangeListeners();
103: }
104:
105: presentations.clear();
106:
107: addToolBarButtons(toolBar, command,
108: new PropertyChangeListener() {
109: public void propertyChange(@NotNull
110: PropertyChangeEvent evt) {
111: if ("structure".equals(evt.getPropertyName()))//NON-NLS
112: {
113: rebuildToolBar(toolBar);
114: }
115: }
116: });
117:
118: toolBar.revalidate();
119: toolBar.repaint();
120: }
121:
122: private void addToolBarButtons(@NotNull
123: JToolBar toolBar, @NotNull
124: final Command command, @NotNull
125: PropertyChangeListener propertyChangeListener) {
126: if (command instanceof CommandGroup) {
127: CommandGroup commandGroup = (CommandGroup) command;
128: final Presentation presentation = commandGroup
129: .getTemplatePresentation().getCopy(
130: commandApplicationRoot);
131: presentation
132: .addPropertyChangeListener(propertyChangeListener);
133:
134: presentations.add(presentation);
135:
136: if (commandGroup.getType() == CommandGroup.Type.NORMAL) {
137: Command[] commands = commandGroup.getChildren();
138: for (Command c : commands) {
139: addToolBarButtons(toolBar, c,
140: propertyChangeListener);
141: }
142: } else if (commandGroup.getType() == CommandGroup.Type.POPUP) {
143:
144: CommandDropDownToolBarButton toolBarButton = new CommandDropDownToolBarButton(
145: commandGroup, presentation, iconKey);
146: presentations.addAll(toolBarButton.getPresentations());
147: toolBar.add(toolBarButton);
148: toolBarButton.addActionListener(new ActionListener() {
149: public void actionPerformed(@NotNull
150: ActionEvent e) {
151: presentation.getCommandApplicationRoot()
152: .clearStatusText();
153: }
154: });
155: }
156: } else if (command instanceof SeparatorCommand) {
157: toolBar.addSeparator();
158: } else if (command instanceof ToggleCommand) {
159: final Presentation presentation = command
160: .getTemplatePresentation().getCopy(
161: commandApplicationRoot);
162: presentation
163: .addPropertyChangeListener(propertyChangeListener);
164: presentations.add(presentation);
165:
166: ToggleCommandToolBarButton toolBarButton = new ToggleCommandToolBarButton(
167: presentation, iconKey);
168: toolBar.add(toolBarButton);
169: toolBarButton.addActionListener(new ActionListener() {
170: public void actionPerformed(@NotNull
171: ActionEvent e) {
172: DataProvider dataProvider = getCommandProvider();
173: presentation.getCommandApplicationRoot()
174: .clearStatusText();
175: command.execute(new DefaultCommandEvent(null,
176: dataProvider.getDataContext(), dataProvider
177: .getPlace(), presentation));
178: CommandManager.dataProviderChanged();
179: }
180: });
181: } else {
182: final Presentation presentation = command
183: .getTemplatePresentation().getCopy(
184: commandApplicationRoot);
185: presentation
186: .addPropertyChangeListener(propertyChangeListener);
187: presentations.add(presentation);
188:
189: JComponent customComponent = command
190: .getCustomComponent(presentation);
191: if (customComponent != null) {
192: toolBar.add(customComponent);
193: } else {
194: CommandToolBarButton toolBarButton = new CommandToolBarButton(
195: presentation, iconKey);
196: toolBar.add(toolBarButton);
197: toolBarButton.addActionListener(new ActionListener() {
198: public void actionPerformed(@NotNull
199: ActionEvent e) {
200: DataProvider dataProvider = getCommandProvider();
201: presentation.getCommandApplicationRoot()
202: .clearStatusText();
203: command.execute(new DefaultCommandEvent(null,
204: dataProvider.getDataContext(),
205: dataProvider.getPlace(), presentation));
206: CommandManager.dataProviderChanged();
207: }
208: });
209: }
210: }
211: }
212:
213: public void update(@NotNull
214: DataProvider dataProvider) {
215: //noinspection ObjectEquality
216: if (dataProvider.getDataContext().getCommandApplicationRoot() != commandApplicationRoot) {
217: return;
218: }
219:
220: DataProvider actualDataProvider = this .dataProvider;
221: if (actualDataProvider == null) {
222: actualDataProvider = dataProvider;
223: }
224: ArrayList<Presentation> presentations = new ArrayList<Presentation>(
225: this .presentations);
226: for (Presentation commandPresentation : presentations) {
227: CommandEvent event = new DefaultCommandEvent(null,
228: actualDataProvider.getDataContext(),
229: actualDataProvider.getPlace(), commandPresentation);
230: commandPresentation.getCommand().update(event);
231: }
232: }
233:
234: @NotNull
235: private DataProvider getCommandProvider() {
236: if (dataProvider != null) {
237: return dataProvider;
238: }
239:
240: return CommandManager.getCurrentCommandProvider();
241: }
242:
243: }
|