001 /*
002 * Copyright 2008-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.griffon.runtime.swing.controller;
017
018 import griffon.core.CallableWithArgs;
019 import griffon.core.artifact.GriffonController;
020 import griffon.core.controller.ActionManager;
021 import griffon.core.threading.UIThreadManager;
022 import griffon.swing.support.SwingAction;
023 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
024
025 import javax.annotation.Nonnull;
026 import javax.annotation.Nullable;
027 import javax.swing.Action;
028 import javax.swing.ImageIcon;
029 import javax.swing.KeyStroke;
030 import java.awt.event.ActionEvent;
031 import java.beans.PropertyChangeEvent;
032 import java.beans.PropertyChangeListener;
033
034 import static griffon.util.GriffonNameUtils.isBlank;
035 import static java.util.Objects.requireNonNull;
036
037 /**
038 * @author Andres Almiray
039 * @since 2.0.0
040 */
041 public class SwingGriffonControllerAction extends AbstractAction {
042 public static final String KEY_SHORT_DESCRIPTION = "shortDescription";
043 public static final String KEY_LONG_DESCRIPTION = "longDescription";
044 public static final String KEY_SMALL_ICON = "smallIcon";
045 public static final String KEY_LARGE_ICON = "largeIcon";
046 public static final String KEY_SELECTED = "selected";
047 public static final String KEY_ACCELERATOR = "accelerator";
048 public static final String KEY_MNEMONIC = "mnemonic";
049 public static final String KEY_COMMAND = "command";
050
051 private String shortDescription;
052 private String longDescription;
053 private String smallIcon;
054 private String largeIcon;
055 private String accelerator;
056 private String mnemonic;
057 private String command;
058 private boolean selected;
059 private final SwingAction toolkitAction;
060
061 public SwingGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
062 super(actionManager, controller, actionName);
063 requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
064
065 toolkitAction = new SwingAction(new CallableWithArgs<Void>() {
066 @Nullable
067 public Void call(@Nullable Object... args) {
068 actionManager.invokeAction(controller, actionName, args);
069 return null;
070 }
071 });
072
073 addPropertyChangeListener(new PropertyChangeListener() {
074 public void propertyChange(final PropertyChangeEvent evt) {
075 uiThreadManager.runInsideUIAsync(new Runnable() {
076 public void run() {
077 if (KEY_NAME.equals(evt.getPropertyName())) {
078 toolkitAction.putValue(Action.NAME, evt.getNewValue());
079 } else if (KEY_COMMAND.equals(evt.getPropertyName())) {
080 toolkitAction.putValue(Action.ACTION_COMMAND_KEY, evt.getNewValue());
081 } else if (KEY_SHORT_DESCRIPTION.equals(evt.getPropertyName())) {
082 toolkitAction.putValue(Action.SHORT_DESCRIPTION, evt.getNewValue());
083 } else if (KEY_LONG_DESCRIPTION.equals(evt.getPropertyName())) {
084 toolkitAction.putValue(Action.LONG_DESCRIPTION, evt.getNewValue());
085 } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
086 toolkitAction.setEnabled((Boolean) evt.getNewValue());
087 } else if (KEY_SELECTED.equals(evt.getPropertyName())) {
088 toolkitAction.putValue(Action.SELECTED_KEY, evt.getNewValue());
089 } else if (KEY_MNEMONIC.equals(evt.getPropertyName())) {
090 String mnemonic = (String) evt.getNewValue();
091 if (!isBlank(mnemonic)) {
092 toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
093 }
094 } else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
095 String accelerator = (String) evt.getNewValue();
096 if (!isBlank(accelerator)) {
097 toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
098 }
099 } else if (KEY_SMALL_ICON.equals(evt.getPropertyName())) {
100 String smallIcon = (String) evt.getNewValue();
101 if (!isBlank(smallIcon)) {
102 toolkitAction.putValue(Action.SMALL_ICON, new ImageIcon(smallIcon));
103 }
104 } else if (KEY_LARGE_ICON.equals(evt.getPropertyName())) {
105 String largeIcon = (String) evt.getNewValue();
106 if (!isBlank(largeIcon)) {
107 toolkitAction.putValue(Action.LARGE_ICON_KEY, new ImageIcon(largeIcon));
108 }
109 }
110 }
111 });
112 }
113 });
114 }
115
116 protected void doInitialize() {
117 toolkitAction.putValue(Action.NAME, getName());
118 toolkitAction.putValue(Action.ACTION_COMMAND_KEY, getCommand());
119 toolkitAction.putValue(Action.SHORT_DESCRIPTION, getShortDescription());
120 toolkitAction.putValue(Action.LONG_DESCRIPTION, getLongDescription());
121 toolkitAction.setEnabled(isEnabled());
122 toolkitAction.putValue(Action.SELECTED_KEY, isSelected());
123 String mnemonic = getMnemonic();
124 if (!isBlank(mnemonic)) {
125 toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
126 }
127 String accelerator = getAccelerator();
128 if (!isBlank(accelerator)) {
129 toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
130 }
131 String smallIcon = getSmallIcon();
132 if (!isBlank(smallIcon)) {
133 toolkitAction.putValue(Action.SMALL_ICON, new ImageIcon(smallIcon));
134 }
135 String largeIcon = getLargeIcon();
136 if (!isBlank(largeIcon)) {
137 toolkitAction.putValue(Action.LARGE_ICON_KEY, new ImageIcon(largeIcon));
138 }
139 }
140
141 @Nullable
142 public String getAccelerator() {
143 return accelerator;
144 }
145
146 public void setAccelerator(@Nullable String accelerator) {
147 firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
148 }
149
150 @Nullable
151 public String getLargeIcon() {
152 return largeIcon;
153 }
154
155 public void setLargeIcon(@Nullable String largeIcon) {
156 firePropertyChange(KEY_LARGE_ICON, this.largeIcon, this.largeIcon = largeIcon);
157 }
158
159 @Nullable
160 public String getLongDescription() {
161 return longDescription;
162 }
163
164 public void setLongDescription(@Nullable String longDescription) {
165 firePropertyChange(KEY_LONG_DESCRIPTION, this.longDescription, this.longDescription = longDescription);
166 }
167
168 @Nullable
169 public String getMnemonic() {
170 return mnemonic;
171 }
172
173 public void setMnemonic(@Nullable String mnemonic) {
174 firePropertyChange(KEY_MNEMONIC, this.mnemonic, this.mnemonic = mnemonic);
175 }
176
177 public boolean isSelected() {
178 return selected;
179 }
180
181 public void setSelected(boolean selected) {
182 firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
183 }
184
185 @Nullable
186 public String getShortDescription() {
187 return shortDescription;
188 }
189
190 public void setShortDescription(@Nullable String shortDescription) {
191 firePropertyChange(KEY_SHORT_DESCRIPTION, this.shortDescription, this.shortDescription = shortDescription);
192 }
193
194 @Nullable
195 public String getSmallIcon() {
196 return smallIcon;
197 }
198
199 public void setSmallIcon(@Nullable String smallIcon) {
200 firePropertyChange(KEY_SMALL_ICON, this.smallIcon, this.smallIcon = smallIcon);
201 }
202
203 @Nullable
204 public String getCommand() {
205 return command;
206 }
207
208 public void setCommand(@Nullable String command) {
209 firePropertyChange(KEY_SMALL_ICON, this.command, this.command = command);
210 }
211
212 @Nonnull
213 public Object getToolkitAction() {
214 return toolkitAction;
215 }
216
217 protected void doExecute(Object... args) {
218 ActionEvent event = null;
219 if (args != null && args.length == 1 && args[0] instanceof ActionEvent) {
220 event = (ActionEvent) args[0];
221 }
222 toolkitAction.actionPerformed(event);
223 }
224 }
|