001: package org.gui4j.core.call;
002:
003: import java.awt.Component;
004: import java.awt.event.ActionEvent;
005: import java.util.Collections;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import javax.swing.Action;
010: import javax.swing.ActionMap;
011: import javax.swing.JComponent;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: import org.gui4j.Gui4jCallBase;
017: import org.gui4j.core.Gui4jCall;
018: import org.gui4j.core.Gui4jComponent;
019: import org.gui4j.core.Gui4jComponentInstance;
020: import org.gui4j.core.Gui4jMap1;
021: import org.gui4j.core.Gui4jThreadManager;
022:
023: public final class Gui4jInternalActionCall implements Gui4jCall {
024: private static final Log log = LogFactory
025: .getLog(Gui4jInternalActionCall.class);
026:
027: private final Gui4jComponent mGui4jComponent;
028: private final String mActionKey;
029:
030: public Gui4jInternalActionCall(Gui4jComponent gui4jComponent,
031: String actionKey) {
032: this .mGui4jComponent = gui4jComponent;
033: this .mActionKey = actionKey;
034:
035: log.debug("Gui4jInternalActionCall created for: " + actionKey);
036: }
037:
038: public Gui4jCall[] getDependantProperties() {
039: return null;
040: }
041:
042: public boolean hasTriggerEvents() {
043: return false;
044: }
045:
046: public Class getResultClass() {
047: return null;
048: }
049:
050: public Object getValue(Gui4jCallBase gui4jController, Map paramMap,
051: Object defaultValue) {
052: return getValueNoErrorChecking(gui4jController, paramMap, null);
053: }
054:
055: public Class getValueClass() {
056: return null;
057: }
058:
059: public Class getValueClass(String paramName) {
060: return null;
061: }
062:
063: public Set getUsedParams() {
064: return Collections.EMPTY_SET;
065: }
066:
067: public Object getValueNoParams(Gui4jCallBase baseInstance,
068: Object defaultValue) {
069: return getValueNoErrorChecking(baseInstance, null, null);
070: }
071:
072: public Object getValueUseDefaultParam(Gui4jCallBase baseInstance,
073: Object defaultParamValue, Object defaultValue) {
074: return getValueNoErrorChecking(baseInstance, new Gui4jMap1("",
075: defaultParamValue), null);
076: }
077:
078: public String getConfigurationName() {
079: return mGui4jComponent.getGui4jComponentContainer()
080: .getConfigurationName();
081: }
082:
083: public Object getValueNoErrorChecking(Gui4jCallBase callBase,
084: Map paramMap, Gui4jComponentInstance componentInstance) {
085: return performAction(componentInstance);
086: }
087:
088: private Object performAction(Gui4jComponentInstance instance) {
089: if (instance == null) {
090: log
091: .warn("Cannot perform internal action because Gui4jComponentInstance is not supplied.");
092: return null;
093: }
094: final Action action = findAction(instance);
095: if (action != null) {
096: Gui4jThreadManager
097: .executeInSwingThreadAndContinue(new Runnable() {
098:
099: public void run() {
100: action.actionPerformed(new ActionEvent(
101: this , 0, mActionKey));
102: }
103: });
104: }
105: return null;
106: }
107:
108: private Action findAction(Gui4jComponentInstance instance) {
109: // Find named action in swing container hierarchy
110: Component component = instance.getComponent();
111: while (component != null) {
112: if (component instanceof JComponent) {
113: JComponent jcomponent = (JComponent) component;
114: ActionMap actionMap = jcomponent.getActionMap();
115: Action action = actionMap.get(mActionKey);
116: if (action != null) {
117: log.debug("Action found: " + action);
118: return action;
119: }
120: }
121: component = component.getParent();
122: }
123: return null;
124: }
125:
126: public String toString() {
127: return "Internal Action for: " + mActionKey;
128: }
129:
130: }
|