001 /*
002 * Copyright 2008-2015 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.RunnableWithArgs;
019 import griffon.core.artifact.GriffonController;
020 import griffon.core.controller.ActionManager;
021 import griffon.core.editors.PropertyEditorResolver;
022 import griffon.core.threading.UIThreadManager;
023 import griffon.swing.support.SwingAction;
024 import org.codehaus.griffon.runtime.core.controller.AbstractAction;
025
026 import javax.annotation.Nonnull;
027 import javax.annotation.Nullable;
028 import javax.swing.Action;
029 import javax.swing.Icon;
030 import javax.swing.KeyStroke;
031 import java.awt.event.ActionEvent;
032 import java.beans.PropertyChangeEvent;
033 import java.beans.PropertyChangeListener;
034 import java.beans.PropertyEditor;
035
036 import static griffon.util.GriffonNameUtils.isBlank;
037 import static java.util.Objects.requireNonNull;
038
039 /**
040 * @author Andres Almiray
041 * @since 2.0.0
042 */
043 public class SwingGriffonControllerAction extends AbstractAction {
044 public static final String KEY_SHORT_DESCRIPTION = "shortDescription";
045 public static final String KEY_LONG_DESCRIPTION = "longDescription";
046 public static final String KEY_SMALL_ICON = "smallIcon";
047 public static final String KEY_LARGE_ICON = "largeIcon";
048 public static final String KEY_SELECTED = "selected";
049 public static final String KEY_ACCELERATOR = "accelerator";
050 public static final String KEY_MNEMONIC = "mnemonic";
051 public static final String KEY_COMMAND = "command";
052 private final SwingAction toolkitAction;
053 private String shortDescription;
054 private String longDescription;
055 private String smallIcon;
056 private String largeIcon;
057 private String accelerator;
058 private String mnemonic;
059 private String command;
060 private boolean selected;
061
062 public SwingGriffonControllerAction(final @Nonnull UIThreadManager uiThreadManager, @Nonnull final ActionManager actionManager, @Nonnull final GriffonController controller, @Nonnull final String actionName) {
063 super(actionManager, controller, actionName);
064 requireNonNull(uiThreadManager, "Argument 'uiThreadManager' must not be null");
065
066 toolkitAction = new SwingAction(new RunnableWithArgs() {
067 public void run(@Nullable Object... args) {
068 actionManager.invokeAction(controller, actionName, args);
069 }
070 });
071
072 addPropertyChangeListener(new PropertyChangeListener() {
073 public void propertyChange(final PropertyChangeEvent evt) {
074 uiThreadManager.runInsideUIAsync(new Runnable() {
075 public void run() {
076 if (KEY_NAME.equals(evt.getPropertyName())) {
077 toolkitAction.putValue(Action.NAME, evt.getNewValue());
078 } else if (KEY_COMMAND.equals(evt.getPropertyName())) {
079 toolkitAction.putValue(Action.ACTION_COMMAND_KEY, evt.getNewValue());
080 } else if (KEY_SHORT_DESCRIPTION.equals(evt.getPropertyName())) {
081 toolkitAction.putValue(Action.SHORT_DESCRIPTION, evt.getNewValue());
082 } else if (KEY_LONG_DESCRIPTION.equals(evt.getPropertyName())) {
083 toolkitAction.putValue(Action.LONG_DESCRIPTION, evt.getNewValue());
084 } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
085 toolkitAction.setEnabled((Boolean) evt.getNewValue());
086 } else if (KEY_SELECTED.equals(evt.getPropertyName())) {
087 toolkitAction.putValue(Action.SELECTED_KEY, evt.getNewValue());
088 } else if (KEY_MNEMONIC.equals(evt.getPropertyName())) {
089 String mnemonic = (String) evt.getNewValue();
090 if (!isBlank(mnemonic)) {
091 toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
092 }
093 } else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
094 String accelerator = (String) evt.getNewValue();
095 if (!isBlank(accelerator)) {
096 toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
097 }
098 } else if (KEY_SMALL_ICON.equals(evt.getPropertyName())) {
099 handleIcon(evt.getNewValue(), Action.SMALL_ICON);
100 } else if (KEY_LARGE_ICON.equals(evt.getPropertyName())) {
101 handleIcon(evt.getNewValue(), Action.LARGE_ICON_KEY);
102 }
103 }
104 });
105 }
106 });
107 }
108
109 protected void handleIcon(@Nullable Object value, @Nonnull String key) {
110 if (value != null) {
111 PropertyEditor editor = PropertyEditorResolver.findEditor(Icon.class);
112 editor.setValue(value);
113 toolkitAction.putValue(key, editor.getValue());
114 }
115 }
116
117 protected void doInitialize() {
118 toolkitAction.putValue(Action.NAME, getName());
119 toolkitAction.putValue(Action.ACTION_COMMAND_KEY, getCommand());
120 toolkitAction.putValue(Action.SHORT_DESCRIPTION, getShortDescription());
121 toolkitAction.putValue(Action.LONG_DESCRIPTION, getLongDescription());
122 toolkitAction.setEnabled(isEnabled());
123 toolkitAction.putValue(Action.SELECTED_KEY, isSelected());
124 String mnemonic = getMnemonic();
125 if (!isBlank(mnemonic)) {
126 toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
127 }
128 String accelerator = getAccelerator();
129 if (!isBlank(accelerator)) {
130 toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
131 }
132 handleIcon(getSmallIcon(), Action.SMALL_ICON);
133 handleIcon(getLargeIcon(), Action.LARGE_ICON_KEY);
134 }
135
136 @Nullable
137 public String getAccelerator() {
138 return accelerator;
139 }
140
141 public void setAccelerator(@Nullable String accelerator) {
142 firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
143 }
144
145 @Nullable
146 public String getLargeIcon() {
147 return largeIcon;
148 }
149
150 public void setLargeIcon(@Nullable String largeIcon) {
151 firePropertyChange(KEY_LARGE_ICON, this.largeIcon, this.largeIcon = largeIcon);
152 }
153
154 @Nullable
155 public String getLongDescription() {
156 return longDescription;
157 }
158
159 public void setLongDescription(@Nullable String longDescription) {
160 firePropertyChange(KEY_LONG_DESCRIPTION, this.longDescription, this.longDescription = longDescription);
161 }
162
163 @Nullable
164 public String getMnemonic() {
165 return mnemonic;
166 }
167
168 public void setMnemonic(@Nullable String mnemonic) {
169 firePropertyChange(KEY_MNEMONIC, this.mnemonic, this.mnemonic = mnemonic);
170 }
171
172 public boolean isSelected() {
173 return selected;
174 }
175
176 public void setSelected(boolean selected) {
177 firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
178 }
179
180 @Nullable
181 public String getShortDescription() {
182 return shortDescription;
183 }
184
185 public void setShortDescription(@Nullable String shortDescription) {
186 firePropertyChange(KEY_SHORT_DESCRIPTION, this.shortDescription, this.shortDescription = shortDescription);
187 }
188
189 @Nullable
190 public String getSmallIcon() {
191 return smallIcon;
192 }
193
194 public void setSmallIcon(@Nullable String smallIcon) {
195 firePropertyChange(KEY_SMALL_ICON, this.smallIcon, this.smallIcon = smallIcon);
196 }
197
198 @Nullable
199 public String getCommand() {
200 return command;
201 }
202
203 public void setCommand(@Nullable String command) {
204 firePropertyChange(KEY_SMALL_ICON, this.command, this.command = command);
205 }
206
207 @Nonnull
208 public Object getToolkitAction() {
209 return toolkitAction;
210 }
211
212 protected void doExecute(Object... args) {
213 ActionEvent event = null;
214 if (args != null && args.length == 1 && args[0] instanceof ActionEvent) {
215 event = (ActionEvent) args[0];
216 }
217 toolkitAction.actionPerformed(event);
218 }
219 }
|