| 
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.CallableWithArgs;
 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 CallableWithArgs<Void>() {
 067             @Nullable
 068             public Void call(@Nullable Object... args) {
 069                 actionManager.invokeAction(controller, actionName, args);
 070                 return null;
 071             }
 072         });
 073
 074         addPropertyChangeListener(new PropertyChangeListener() {
 075             public void propertyChange(final PropertyChangeEvent evt) {
 076                 uiThreadManager.runInsideUIAsync(new Runnable() {
 077                     public void run() {
 078                         if (KEY_NAME.equals(evt.getPropertyName())) {
 079                             toolkitAction.putValue(Action.NAME, evt.getNewValue());
 080                         } else if (KEY_COMMAND.equals(evt.getPropertyName())) {
 081                             toolkitAction.putValue(Action.ACTION_COMMAND_KEY, evt.getNewValue());
 082                         } else if (KEY_SHORT_DESCRIPTION.equals(evt.getPropertyName())) {
 083                             toolkitAction.putValue(Action.SHORT_DESCRIPTION, evt.getNewValue());
 084                         } else if (KEY_LONG_DESCRIPTION.equals(evt.getPropertyName())) {
 085                             toolkitAction.putValue(Action.LONG_DESCRIPTION, evt.getNewValue());
 086                         } else if (KEY_ENABLED.equals(evt.getPropertyName())) {
 087                             toolkitAction.setEnabled((Boolean) evt.getNewValue());
 088                         } else if (KEY_SELECTED.equals(evt.getPropertyName())) {
 089                             toolkitAction.putValue(Action.SELECTED_KEY, evt.getNewValue());
 090                         } else if (KEY_MNEMONIC.equals(evt.getPropertyName())) {
 091                             String mnemonic = (String) evt.getNewValue();
 092                             if (!isBlank(mnemonic)) {
 093                                 toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
 094                             }
 095                         } else if (KEY_ACCELERATOR.equals(evt.getPropertyName())) {
 096                             String accelerator = (String) evt.getNewValue();
 097                             if (!isBlank(accelerator)) {
 098                                 toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
 099                             }
 100                         } else if (KEY_SMALL_ICON.equals(evt.getPropertyName())) {
 101                             handleIcon(evt.getNewValue(), Action.SMALL_ICON);
 102                         } else if (KEY_LARGE_ICON.equals(evt.getPropertyName())) {
 103                             handleIcon(evt.getNewValue(), Action.LARGE_ICON_KEY);
 104                         }
 105                     }
 106                 });
 107             }
 108         });
 109     }
 110
 111     protected void handleIcon(@Nullable Object value, @Nonnull String key) {
 112         if (value != null) {
 113             PropertyEditor editor = PropertyEditorResolver.findEditor(Icon.class);
 114             editor.setValue(value);
 115             toolkitAction.putValue(key, editor.getValue());
 116         }
 117     }
 118
 119     protected void doInitialize() {
 120         toolkitAction.putValue(Action.NAME, getName());
 121         toolkitAction.putValue(Action.ACTION_COMMAND_KEY, getCommand());
 122         toolkitAction.putValue(Action.SHORT_DESCRIPTION, getShortDescription());
 123         toolkitAction.putValue(Action.LONG_DESCRIPTION, getLongDescription());
 124         toolkitAction.setEnabled(isEnabled());
 125         toolkitAction.putValue(Action.SELECTED_KEY, isSelected());
 126         String mnemonic = getMnemonic();
 127         if (!isBlank(mnemonic)) {
 128             toolkitAction.putValue(Action.MNEMONIC_KEY, KeyStroke.getKeyStroke(mnemonic).getKeyCode());
 129         }
 130         String accelerator = getAccelerator();
 131         if (!isBlank(accelerator)) {
 132             toolkitAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(accelerator));
 133         }
 134         handleIcon(getSmallIcon(), Action.SMALL_ICON);
 135         handleIcon(getLargeIcon(), Action.LARGE_ICON_KEY);
 136     }
 137
 138     @Nullable
 139     public String getAccelerator() {
 140         return accelerator;
 141     }
 142
 143     public void setAccelerator(@Nullable String accelerator) {
 144         firePropertyChange(KEY_ACCELERATOR, this.accelerator, this.accelerator = accelerator);
 145     }
 146
 147     @Nullable
 148     public String getLargeIcon() {
 149         return largeIcon;
 150     }
 151
 152     public void setLargeIcon(@Nullable String largeIcon) {
 153         firePropertyChange(KEY_LARGE_ICON, this.largeIcon, this.largeIcon = largeIcon);
 154     }
 155
 156     @Nullable
 157     public String getLongDescription() {
 158         return longDescription;
 159     }
 160
 161     public void setLongDescription(@Nullable String longDescription) {
 162         firePropertyChange(KEY_LONG_DESCRIPTION, this.longDescription, this.longDescription = longDescription);
 163     }
 164
 165     @Nullable
 166     public String getMnemonic() {
 167         return mnemonic;
 168     }
 169
 170     public void setMnemonic(@Nullable String mnemonic) {
 171         firePropertyChange(KEY_MNEMONIC, this.mnemonic, this.mnemonic = mnemonic);
 172     }
 173
 174     public boolean isSelected() {
 175         return selected;
 176     }
 177
 178     public void setSelected(boolean selected) {
 179         firePropertyChange(KEY_SELECTED, this.selected, this.selected = selected);
 180     }
 181
 182     @Nullable
 183     public String getShortDescription() {
 184         return shortDescription;
 185     }
 186
 187     public void setShortDescription(@Nullable String shortDescription) {
 188         firePropertyChange(KEY_SHORT_DESCRIPTION, this.shortDescription, this.shortDescription = shortDescription);
 189     }
 190
 191     @Nullable
 192     public String getSmallIcon() {
 193         return smallIcon;
 194     }
 195
 196     public void setSmallIcon(@Nullable String smallIcon) {
 197         firePropertyChange(KEY_SMALL_ICON, this.smallIcon, this.smallIcon = smallIcon);
 198     }
 199
 200     @Nullable
 201     public String getCommand() {
 202         return command;
 203     }
 204
 205     public void setCommand(@Nullable String command) {
 206         firePropertyChange(KEY_SMALL_ICON, this.command, this.command = command);
 207     }
 208
 209     @Nonnull
 210     public Object getToolkitAction() {
 211         return toolkitAction;
 212     }
 213
 214     protected void doExecute(Object... args) {
 215         ActionEvent event = null;
 216         if (args != null && args.length == 1 && args[0] instanceof ActionEvent) {
 217             event = (ActionEvent) args[0];
 218         }
 219         toolkitAction.actionPerformed(event);
 220     }
 221 }
 |