001: package org.swingml.event;
002:
003: import java.awt.*;
004: import java.util.*;
005:
006: import javax.swing.*;
007:
008: import org.swingml.*;
009: import org.swingml.model.*;
010: import org.swingml.system.*;
011:
012: public class SwingMLEvent implements ISwingMLEvent {
013:
014: private ListenerModel listener;
015: private String name;
016: private JComponent owner;
017: private SwingMLModel ownerModel;
018:
019: public SwingMLEvent(ListenerModel listener,
020: SwingMLModel eventOwnerModel, String eventName,
021: JComponent eventOwner) {
022: setListener(listener);
023: setOwnerModel(eventOwnerModel);
024: setName(eventName);
025: setOwner(eventOwner);
026: }
027:
028: /**
029: * Notify the listener of the event that occurred, so it can handle it.
030: */
031: public Object execute(Object anObject) {
032: if (anObject != null && anObject instanceof ListenerModel) {
033: ListenerModel theEventListener = (ListenerModel) anObject;
034:
035: int theOption = JOptionPane.YES_OPTION;
036: if (theEventListener.hasConfirmationMessage()) {
037: JOptionPaneModel theOptionPane = theEventListener
038: .getConfirmationMessage();
039: Component theComponent = EventHandler.getInstance()
040: .findActionTarget(
041: getOwner().getTopLevelAncestor(),
042: theOptionPane.getComponent());
043: theOption = JOptionPane.showConfirmDialog(theComponent,
044: theOptionPane.getText(), "Select an Option",
045: JOptionPane.YES_NO_OPTION);
046: }
047:
048: if (theOption == JOptionPane.YES_OPTION) {
049: Iterator theActions = theEventListener.getActions()
050: .iterator();
051: Object theIteratorObject = null;
052: while (theActions.hasNext()) {
053: theIteratorObject = theActions.next();
054: if (theIteratorObject instanceof ActionModel) {
055: ActionModel theAction = (ActionModel) theIteratorObject;
056: Component theActionTarget = EventHandler
057: .getInstance().findActionTarget(
058: getOwner()
059: .getTopLevelAncestor(),
060: theAction.getComponent());
061: if (theActionTarget != null) {
062: EventHandler.getInstance().processAction(
063: theAction, theActionTarget,
064: getOwnerModel().getName(),
065: theEventListener);
066: } else {
067: SwingMLLogger
068: .getInstance()
069: .log(
070: ILogCapable.WARN,
071: "Syntax error: The component "
072: + theAction
073: .getComponent()
074: + " in the action for the listener "
075: + theEventListener
076: .getEvent()
077: + " in the element "
078: + getOwnerModel()
079: .getName()
080: + " wasn't found. Verify the value of COMPONENT.");
081: }
082: } else if (theIteratorObject instanceof ExternalActionModel) {
083:
084: ExternalActionModel eam = (ExternalActionModel) theIteratorObject;
085:
086: EventHandler
087: .getInstance()
088: .handleExternalEvent(
089: (ExternalActionModel) theIteratorObject,
090: getOwner(), getOwnerModel(),
091: theEventListener);
092: } else if (theIteratorObject instanceof ControllerActionModel) {
093:
094: ControllerActionModel eam = (ControllerActionModel) theIteratorObject;
095: EventHandler.getInstance()
096: .handleControllerActionEvent(eam,
097: getOwner(), getOwnerModel(),
098: theEventListener);
099:
100: }
101: }
102: }
103: }
104:
105: return null;
106: }
107:
108: public ListenerModel getListener() {
109: return listener;
110: }
111:
112: public String getName() {
113: return name;
114: }
115:
116: public JComponent getOwner() {
117: return owner;
118: }
119:
120: public SwingMLModel getOwnerModel() {
121: return ownerModel;
122: }
123:
124: public void postExecute(Object anObject) {
125: // Do nothing
126: }
127:
128: /**
129: * return a list of all ListenerModels that are listening on this event.
130: */
131: public Object preExecute() {
132: return getListener();
133: }
134:
135: public void setListener(ListenerModel listener) {
136: this .listener = listener;
137: }
138:
139: public void setName(String string) {
140: name = string;
141: }
142:
143: public void setOwner(JComponent aComponent) {
144: owner = aComponent;
145: }
146:
147: public void setOwnerModel(SwingMLModel model) {
148: ownerModel = model;
149: }
150: }
|