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