1 /*
2 * Copyright 2002-2004 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16 package org.springframework.richclient.command;
17
18 import java.awt.event.ActionEvent;
19 import java.beans.PropertyChangeEvent;
20 import java.beans.PropertyChangeListener;
21
22 import javax.swing.AbstractAction;
23 import javax.swing.Action;
24
25 import org.springframework.richclient.command.config.CommandFaceDescriptor;
26 import org.springframework.richclient.util.Assert;
27
28 /**
29 * An adapter between a Spring Rich Client {@link ActionCommand} and the Swing
30 * {@link Action} interface.
31 *
32 * <p>
33 * This adheres to the standard GoF {@code Adapter} pattern whereby this class acts as
34 * a wrapper around an underlying {@link ActionCommand} to give it the appearance of
35 * being an {@link Action}.
36 * </p>
37 *
38 * <p>
39 * The {@link PropertyChangeListener} interface is also implemented so that
40 * instances can be notified of property change events being fired by their underlying command.
41 * </p>
42 */
43 public class SwingActionAdapter extends AbstractAction implements PropertyChangeListener {
44
45 private ActionCommand command;
46
47 /**
48 * Creates a new {@code SwingActionAdapter} with the given underlying action command. The
49 * newly created instance will add itself as a property change listener of the command.
50 *
51 * @param command The underlying action command.
52 *
53 * @throws IllegalArgumentException if {@code command} is null.
54 */
55 public SwingActionAdapter(ActionCommand command) {
56 super();
57
58 Assert.required(command, "command");
59 this.command = command;
60 command.addPropertyChangeListener(this);
61 command.addEnabledListener(this);
62 update();
63
64 }
65
66 /**
67 * Delegates the handling of the given event to the underlying command.
68 * @param event The action event to be handled.
69 */
70 public void actionPerformed(ActionEvent event) {
71 command.actionPerformedHandler.actionPerformed(event);
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 public void propertyChange(PropertyChangeEvent event) {
78 update();
79 }
80
81 /**
82 * Updates this instance according to the properties provided by the
83 * underlying command.
84 */
85 protected void update() {
86 putValue(Action.ACTION_COMMAND_KEY, command.getActionCommand());
87 CommandFaceDescriptor face = command.getFaceDescriptor();
88 if (face != null) {
89 face.configure(this);
90 }
91 setEnabled(command.isEnabled());
92 }
93
94 }