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.beans.PropertyChangeEvent;
021: import java.beans.PropertyChangeListener;
022:
023: /**
024: * User: Martin
025: * Date: 19.05.2005
026: * Time: 07:50:28
027: */
028: public class CommandButton extends JButton implements CommandComponent {
029: @NotNull
030: private Presentation presentation;
031: @Nullable
032: private Command command;
033: @Nullable
034: private String iconKey;
035: @Nullable
036: private ButtonType buttonType;
037: @Nullable
038: @SuppressWarnings({"UnusedDeclaration"})
039: private CommandApplicationRoot commandApplicationRoot;//probably used in future
040: @Nullable
041: private DataProvider dataProvider;
042:
043: public CommandButton(@NotNull
044: Presentation presentation) {
045: //noinspection ConstantConditions
046: if (presentation == null) {
047: throw new IllegalArgumentException(
048: "presentation must not be null");
049: }
050:
051: this .presentation = presentation;
052:
053: presentation
054: .addPropertyChangeListener(new PropertyChangeListener() {
055: public void propertyChange(@NotNull
056: PropertyChangeEvent evt) {
057: update();
058: }
059: });
060: }
061:
062: public CommandButton(@NotNull
063: CommandApplicationRoot commandApplicationRoot, @Nullable
064: DataProvider dataProvider, @NotNull
065: Command command, @NotNull
066: String iconKey, @NotNull
067: ButtonType buttonType) {
068: //noinspection ConstantConditions
069: if (commandApplicationRoot == null) {
070: throw new IllegalArgumentException(
071: "commandApplicationRoot must not be null");
072: }
073: //noinspection ConstantConditions
074: if (command == null) {
075: throw new IllegalArgumentException(
076: "command must not be null");
077: }
078: //noinspection ConstantConditions
079: if (iconKey == null) {
080: throw new IllegalArgumentException(
081: "iconKey must not be null");
082: }
083: //noinspection ConstantConditions
084: if (buttonType == null) {
085: throw new IllegalArgumentException(
086: "buttonType must not be null");
087: }
088:
089: this .commandApplicationRoot = commandApplicationRoot;
090: this .dataProvider = dataProvider;
091:
092: this .command = command;
093: this .iconKey = iconKey;
094: this .buttonType = buttonType;
095:
096: presentation = command.getTemplatePresentation().getCopy(
097: commandApplicationRoot);
098: }
099:
100: @NotNull
101: protected Presentation getPresentation() {
102: return presentation;
103: }
104:
105: protected void update() {
106: ButtonType type = buttonType;
107:
108: if (type != null) {
109: Presentation presentation = getPresentation();
110:
111: //noinspection EnumSwitchStatementWhichMissesCases
112: switch (type) {
113: case ICON:
114: setIcon(presentation.getIcon(iconKey));
115: setToolTipText(presentation.getText());
116: break;
117: case TEXT:
118: setIcon(null);
119: setToolTipText(presentation.getText());
120: break;
121: }
122: setEnabled(presentation.isEnabled());
123: setVisible(presentation.isVisible());
124: }
125: }
126:
127: public void update(@NotNull
128: DataProvider dataProvider) {
129: //noinspection ObjectEquality
130: if (dataProvider.getDataContext().getCommandApplicationRoot() != commandApplicationRoot) {
131: return;
132: }
133:
134: DataProvider actualDataProvider = this .dataProvider;
135: //if!=null -> we have a fixed command provider -> not context sensitive
136:
137: if (actualDataProvider == null) {
138: actualDataProvider = dataProvider;
139: }
140:
141: Command c = command;
142: if (c != null) {
143: c.update(new DefaultCommandEvent(null, actualDataProvider
144: .getDataContext(), actualDataProvider.getPlace(),
145: presentation));
146: }
147: }
148: }
|