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.pentaho.reportdesigner.lib.client.components.Category;
018: import org.pentaho.reportdesigner.lib.client.components.favoritespanel.FavoritesPanel;
019:
020: import javax.swing.*;
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: 13:15:56
029: */
030: public class CommandCategories implements CommandComponent {
031: @NotNull
032: private Command command;
033: @NotNull
034: private CommandApplicationRoot commandApplicationRoot;
035: @NotNull
036: private FavoritesPanel favoritesPanel;
037: @NotNull
038: private String iconKey;
039:
040: @NotNull
041: private ArrayList<Presentation> presentations;
042:
043: public CommandCategories(@NotNull
044: CommandApplicationRoot commandApplicationRoot, @NotNull
045: Command command, @NotNull
046: FavoritesPanel favoritesPanel, @NotNull
047: String iconKey) {
048: this .commandApplicationRoot = commandApplicationRoot;
049: this .favoritesPanel = favoritesPanel;
050: this .iconKey = iconKey;
051: //noinspection ConstantConditions
052: if (command == null) {
053: throw new IllegalArgumentException(
054: "command must not be null");
055: }
056:
057: this .command = command;
058: presentations = new ArrayList<Presentation>();
059:
060: rebuildFavoritesPanel();
061: }
062:
063: private void rebuildFavoritesPanel() {
064: for (Presentation presentation : presentations) {
065: presentation.clearPropertyChangeListeners();
066: }
067:
068: presentations.clear();
069:
070: ArrayList<Category<?>> categories = new ArrayList<Category<?>>();
071:
072: addCategories(categories, command,
073: new PropertyChangeListener() {
074: public void propertyChange(@NotNull
075: PropertyChangeEvent evt) {
076: if ("structure".equals(evt.getPropertyName()))//NON-NLS
077: {
078: rebuildFavoritesPanel();
079: }
080: }
081: });
082:
083: Category<?>[] cs = categories.toArray(new Category[categories
084: .size()]);
085: favoritesPanel.setCategories(cs);
086:
087: favoritesPanel.revalidate();
088: favoritesPanel.repaint();
089: }
090:
091: private void addCategories(@NotNull
092: ArrayList<Category<?>> categories, @NotNull
093: final Command command, @NotNull
094: PropertyChangeListener propertyChangeListener) {
095: if (command instanceof CommandGroup) {
096: CommandGroup commandGroup = (CommandGroup) command;
097: final Presentation presentation = commandGroup
098: .getTemplatePresentation().getCopy(
099: commandApplicationRoot);
100: presentation
101: .addPropertyChangeListener(propertyChangeListener);
102:
103: presentations.add(presentation);
104:
105: Command[] commands = commandGroup.getChildren();
106: for (Command c : commands) {
107: addCategories(categories, c, propertyChangeListener);
108: }
109: } else if (command instanceof SeparatorCommand) {
110: throw new IllegalArgumentException(
111: "CommandCategories can not contain Separators");
112: } else if (command instanceof ToggleCommand) {
113: throw new IllegalArgumentException(
114: "CommandCategories can not contain ToggleCommands");
115: } else {
116: final Presentation presentation = command
117: .getTemplatePresentation().getCopy(
118: commandApplicationRoot);
119: presentation
120: .addPropertyChangeListener(propertyChangeListener);
121: presentations.add(presentation);
122:
123: final Category<JComponent> category = new Category<JComponent>(
124: command.getName(), presentation.getIcon(iconKey),
125: presentation.getIcon(iconKey), presentation
126: .getText(), command
127: .getCustomComponent(presentation));
128: if (command instanceof AbstractToolbarDelivererCommand) {
129: AbstractToolbarDelivererCommand abstractToolbarDelivererCommand = (AbstractToolbarDelivererCommand) command;
130: CommandToolBar commandToolBar = CommandManager
131: .createCommandToolBar(commandApplicationRoot,
132: abstractToolbarDelivererCommand
133: .getToolbarPlace());
134: category.setToolBar(commandToolBar.getToolBar());
135: }
136:
137: categories.add(category);
138: category.addPropertyChangeListener("lastAccessedMillis",
139: new PropertyChangeListener()//NON-NLS
140: {
141: public void propertyChange(@NotNull
142: PropertyChangeEvent evt) {
143: DataProvider dataProvider = CommandManager
144: .getCurrentCommandProvider();
145: presentation.getCommandApplicationRoot()
146: .clearStatusText();
147: command.execute(new DefaultCommandEvent(
148: null,
149: dataProvider.getDataContext(),
150: dataProvider.getPlace(),
151: presentation));
152: CommandManager.dataProviderChanged();
153: }
154: });
155:
156: presentation
157: .addPropertyChangeListener(new PropertyChangeListener() {
158: public void propertyChange(@NotNull
159: PropertyChangeEvent evt) {
160: category.setTitle(presentation.getText());
161: category.setIconBig(presentation
162: .getIcon(iconKey));
163: category.setIconSmall(presentation
164: .getIcon(iconKey));
165: category.setMainComponent(command
166: .getCustomComponent(presentation));
167: }
168: });
169:
170: }
171: }
172:
173: public void update(@NotNull
174: DataProvider dataProvider) {
175: //noinspection ObjectEquality
176: if (dataProvider.getDataContext().getCommandApplicationRoot() != commandApplicationRoot) {
177: return;
178: }
179:
180: ArrayList<Presentation> presentations = new ArrayList<Presentation>(
181: this .presentations);
182: for (Presentation commandPresentation : presentations) {
183: CommandEvent event = new DefaultCommandEvent(null,
184: dataProvider.getDataContext(), dataProvider
185: .getPlace(), commandPresentation);
186: commandPresentation.getCommand().update(event);
187: }
188: }
189:
190: }
|