001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.action.swing;
014:
015: import java.awt.event.ActionEvent;
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018:
019: import javax.swing.AbstractAction;
020: import javax.swing.Action;
021:
022: import com.eviware.soapui.SoapUI;
023: import com.eviware.soapui.model.ModelItem;
024: import com.eviware.soapui.support.UISupport;
025: import com.eviware.soapui.support.action.SoapUIAction;
026: import com.eviware.soapui.support.action.SoapUIActionMapping;
027: import com.eviware.soapui.support.action.support.StandaloneActionMapping;
028:
029: /**
030: * Delegates a SwingAction to a SoapUIActionMapping
031: *
032: * @author ole.matzura
033: */
034:
035: public class SwingActionDelegate<T extends ModelItem> extends
036: AbstractAction implements PropertyChangeListener {
037: private final T target;
038: private final SoapUIActionMapping<T> mapping;
039:
040: public SwingActionDelegate(SoapUIActionMapping<T> mapping, T target) {
041: super (mapping.getName());
042: this .mapping = mapping;
043: this .target = target;
044:
045: if (mapping.getDescription() != null)
046: putValue(Action.SHORT_DESCRIPTION, mapping.getDescription());
047:
048: if (mapping.getIconPath() != null)
049: putValue(Action.SMALL_ICON, UISupport
050: .createImageIcon(mapping.getIconPath()));
051:
052: if (mapping.getKeyStroke() != null)
053: putValue(Action.ACCELERATOR_KEY, UISupport
054: .getKeyStroke(mapping.getKeyStroke()));
055:
056: setEnabled(mapping.getAction().isEnabled());
057:
058: // mapping.getAction().addPropertyChangeListener( this );
059: }
060:
061: public void actionPerformed(ActionEvent e) {
062: mapping.getAction().perform(target, mapping.getParam());
063: }
064:
065: public void propertyChange(PropertyChangeEvent evt) {
066: if (evt.getPropertyName().equals(SoapUIAction.ENABLED_PROPERTY))
067: setEnabled(((Boolean) evt.getNewValue()).booleanValue());
068: }
069:
070: public SoapUIAction<T> getAction() {
071: return mapping.getAction();
072: }
073:
074: public T getTarget() {
075: return target;
076: }
077:
078: public static <T extends ModelItem> SwingActionDelegate<T> createDelegate(
079: SoapUIAction<T> action, T target, String keyStroke,
080: String iconPath) {
081: return new SwingActionDelegate<T>(
082: new StandaloneActionMapping<T>(action, keyStroke,
083: iconPath), target);
084: }
085:
086: public static <T extends ModelItem> SwingActionDelegate<T> createDelegate(
087: SoapUIAction<T> action, T target, String keyStroke) {
088: return new SwingActionDelegate<T>(
089: new StandaloneActionMapping<T>(action, keyStroke),
090: target);
091: }
092:
093: public static <T extends ModelItem> SwingActionDelegate<T> createDelegate(
094: SoapUIAction<T> action, T target) {
095: return new SwingActionDelegate<T>(
096: new StandaloneActionMapping<T>(action), target);
097: }
098:
099: public static <T extends ModelItem> SwingActionDelegate<T> createDelegate(
100: SoapUIAction<T> action) {
101: return new SwingActionDelegate<T>(
102: new StandaloneActionMapping<T>(action), null);
103: }
104:
105: public static AbstractAction createDelegate(String soapUIActionId) {
106: return createDelegate(SoapUI.getActionRegistry().getAction(
107: soapUIActionId));
108: }
109:
110: public static <T extends ModelItem> AbstractAction createDelegate(
111: String soapUIActionId, T target) {
112: return createDelegate(SoapUI.getActionRegistry().getAction(
113: soapUIActionId), target);
114: }
115:
116: public static <T extends ModelItem> AbstractAction createDelegate(
117: String soapUIActionId, T target, String keyStroke) {
118: return createDelegate(SoapUI.getActionRegistry().getAction(
119: soapUIActionId), target, keyStroke);
120: }
121:
122: public static <T extends ModelItem> AbstractAction createDelegate(
123: String soapUIActionId, T target, String keyStroke,
124: String iconPath) {
125: return createDelegate(SoapUI.getActionRegistry().getAction(
126: soapUIActionId), target, keyStroke, iconPath);
127: }
128: }
|