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.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019:
020: import javax.swing.*;
021: import java.util.ArrayList;
022: import java.util.Arrays;
023:
024: /**
025: * User: Martin
026: * Date: 21.11.2004
027: * Time: 10:59:24
028: */
029: public class DefaultCommandGroup implements CommandGroup {
030: @NotNull
031: private static final Command[] EMPTY_ARRAY = new Command[0];
032:
033: @NotNull
034: private TemplatePresentation templatePresentation;
035:
036: @NotNull
037: private Command[] children;
038:
039: @NotNull
040: private Type type;
041: @NotNull
042: private String name;
043:
044: public DefaultCommandGroup(@NotNull
045: String name) {
046: this .name = name;
047:
048: templatePresentation = new TemplatePresentation(this ,
049: "DefaultCommandGroup", "DefaultCommandGroup", false,
050: true);//NON-NLS
051: templatePresentation.setIcon(CommandSettings.SIZE_16,
052: new ImageIcon(AbstractCommand.class
053: .getResource("Empty16.png")));//NON-NLS
054: templatePresentation.setIcon(CommandSettings.SIZE_32,
055: new ImageIcon(AbstractCommand.class
056: .getResource("Empty32.png")));//NON-NLS
057: templatePresentation.setIcon(CommandSettings.SIZE_64,
058: new ImageIcon(AbstractCommand.class
059: .getResource("Empty64.png")));//NON-NLS
060: children = EMPTY_ARRAY;
061: type = Type.NORMAL;
062: }
063:
064: public DefaultCommandGroup(@NotNull
065: String text, @NotNull
066: String description, @NotNull
067: @NonNls
068: String name, boolean enabled) {
069: this .name = name;
070:
071: templatePresentation = new TemplatePresentation(this , text,
072: description, enabled, true);
073: templatePresentation.setIcon(CommandSettings.SIZE_16,
074: new ImageIcon(AbstractCommand.class
075: .getResource("Empty16.png")));//NON-NLS
076: templatePresentation.setIcon(CommandSettings.SIZE_32,
077: new ImageIcon(AbstractCommand.class
078: .getResource("Empty32.png")));//NON-NLS
079: templatePresentation.setIcon(CommandSettings.SIZE_64,
080: new ImageIcon(AbstractCommand.class
081: .getResource("Empty64.png")));//NON-NLS
082: children = EMPTY_ARRAY;
083: type = Type.NORMAL;
084: }
085:
086: public void addChild(@NotNull
087: Command child, @Nullable
088: CommandConstraint commandConstraint) {
089: ArrayList<Command> commands = new ArrayList<Command>(Arrays
090: .asList(children));
091:
092: if (!commands.contains(child)) {
093: if (commandConstraint == null
094: || CommandConstraint.LAST.equals(commandConstraint)) {
095: commands.add(child);
096: } else if (CommandConstraint.FIRST
097: .equals(commandConstraint)) {
098: commands.add(0, child);
099: } else if (commandConstraint.getConstraintType() == CommandConstraint.ConstraintType.AFTER) {
100: int index = getIndexOfCommand(commandConstraint
101: .getReference(), commands);
102: if (index == -1) {
103: //we did not find the command --> add as last
104: commands.add(child);
105: } else {
106: commands.add(index + 1, child);
107: }
108: } else if (commandConstraint.getConstraintType() == CommandConstraint.ConstraintType.BEFORE) {
109: int index = getIndexOfCommand(commandConstraint
110: .getReference(), commands);
111: if (index == -1) {
112: //we did not find the command --> add as last
113: commands.add(child);
114: } else {
115: commands.add(index, child);
116: }
117: } else {
118: commands.add(child);//nothing that makes sense->add as last command
119: }
120: }
121:
122: children = commands.toArray(new Command[commands.size()]);
123:
124: for (Presentation presentation : templatePresentation
125: .getCreatedPresentations()) {
126: presentation.fireStructureChanged();
127: }
128: }
129:
130: private int getIndexOfCommand(@NotNull
131: String reference, @NotNull
132: ArrayList<Command> commands) {
133: for (int i = 0; i < commands.size(); i++) {
134: Command command = commands.get(i);
135: if (command.getName().equals(reference)) {
136: return i;
137: }
138: }
139: return -1;
140: }
141:
142: public void removeChild(@NotNull
143: Command child) {
144: ArrayList<Command> commands = new ArrayList<Command>(Arrays
145: .asList(children));
146: commands.remove(child);
147:
148: children = commands.toArray(new Command[commands.size()]);
149:
150: for (Presentation presentation : templatePresentation
151: .getCreatedPresentations()) {
152: presentation.fireStructureChanged();
153: }
154: }
155:
156: public void removeAllChildren() {
157: children = EMPTY_ARRAY;
158:
159: for (Presentation presentation : templatePresentation
160: .getCreatedPresentations()) {
161: presentation.fireStructureChanged();
162: }
163: }
164:
165: public void setChildren(@NotNull
166: Command[] commands) {
167: children = commands;
168:
169: for (Presentation presentation : templatePresentation
170: .getCreatedPresentations()) {
171: presentation.fireStructureChanged();
172: }
173: }
174:
175: @NotNull
176: public Command[] getChildren() {
177: return children;
178: }
179:
180: public void setType(@NotNull
181: Type type) {
182: this .type = type;
183: }
184:
185: @NotNull
186: public Type getType() {
187: return type;
188: }
189:
190: public void update(@NotNull
191: CommandEvent event) {
192: }
193:
194: @NotNull
195: public String getName() {
196: return name;
197: }
198:
199: public void execute(@NotNull
200: CommandEvent event) {
201: }
202:
203: @NotNull
204: public TemplatePresentation getTemplatePresentation() {
205: return templatePresentation;
206: }
207:
208: @Nullable
209: public JComponent getCustomComponent(@NotNull
210: Presentation presentation) {
211: return null;
212: }
213: }
|