001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui.project.editors;
021    
022    import java.awt.BorderLayout;
023    import java.awt.FlowLayout;
024    import java.awt.Frame;
025    import java.awt.event.ActionEvent;
026    import java.util.HashMap;
027    
028    import javax.swing.AbstractAction;
029    import javax.swing.BorderFactory;
030    import javax.swing.JButton;
031    import javax.swing.JDialog;
032    import javax.swing.JLabel;
033    import javax.swing.JPanel;
034    import javax.swing.JScrollPane;
035    import javax.swing.JTextArea;
036    
037    import org.dom4j.Element;
038    import org.mactor.brokers.Message;
039    import org.mactor.framework.MactorException;
040    import org.mactor.framework.TestContextImpl;
041    import org.mactor.framework.commandexecutors.ActionCommandExecutor;
042    import org.mactor.framework.commandexecutors.ActionCommandExecutorFactory;
043    import org.mactor.framework.commandexecutors.MessageBuilderCommandExecutor;
044    import org.mactor.framework.commandexecutors.MessageBuilderCommandExecutorFactory;
045    import org.mactor.framework.commandexecutors.MessageSelectorCommandExecutor;
046    import org.mactor.framework.commandexecutors.MessageSelectorCommandExecutorFactory;
047    import org.mactor.framework.commandexecutors.ValueCommandExecutor;
048    import org.mactor.framework.commandexecutors.ValueCommandExecutorFactory;
049    import org.mactor.framework.spec.ActionSpec;
050    import org.mactor.framework.spec.MessageBuilderSpec;
051    import org.mactor.framework.spec.MessageSelectorSpec;
052    import org.mactor.framework.spec.ProjectContext;
053    import org.mactor.framework.spec.TestSpec;
054    import org.mactor.framework.spec.ValueSpec;
055    import org.mactor.framework.spec.XmlUtil;
056    import org.mactor.ui.gui.AsyncAction;
057    import org.mactor.ui.gui.GuiUtil;
058    
059    public class CommandTestDlg extends JDialog {
060            JTextArea resultText = new JTextArea(10, 60);
061            ContextPanel contextPanel = new ContextPanel();
062            Element commandElement;
063            TestContextImpl context;
064            JButton okButton = new JButton(new AbstractAction("Ok") {
065                    public void actionPerformed(ActionEvent arg0) {
066                            dispose();
067                    };
068            });
069            JButton evaluateButton = new JButton(new AsyncAction("Evaluate Command", false, new AsyncAction.AsyncRunnable() {
070                    public void run() {
071                            context.setValues(contextPanel.getContextValues());
072                            resultText.setText(evaluateCommand());
073                    };
074            }));
075            private String evaluateCommand() {
076                    String elementName = commandElement.getName();
077                    String prefix = "The evaluation of:\n" + XmlUtil.getPrettyXML(commandElement) + "\n\nResultet in:\n";
078                    try {
079                            if ("value".equals(elementName))
080                                    return prefix + evaluateValueCommand();
081                            if ("action".equals(elementName))
082                                    return prefix + evaluateActionCommand();
083                            if ("message_selector".equals(elementName))
084                                    return prefix + evaluateMessageSelectorCommand();
085                            if ("message_builder".equals(elementName))
086                                    return prefix + evaluateMessageBuilderCommand();
087                            throw new MactorException("Unsupported command container: '" + elementName + "'");
088                    } catch (Exception e) {
089                            e.printStackTrace();
090                            return prefix + e;
091                    }
092            }
093            private String evaluateValueCommand() throws MactorException {
094                    ValueSpec spec = ValueSpec.loadSpec(commandElement);
095                    ValueCommandExecutor executor = ValueCommandExecutorFactory.createExecutor(context, spec);
096                    return executor.extractValue(context);
097            }
098            private String evaluateActionCommand() throws MactorException {
099                    ActionSpec spec = ActionSpec.loadSpec(commandElement);
100                    ActionCommandExecutor executor = ActionCommandExecutorFactory.createExecutor(context, spec);
101                    executor.perform(context);
102                    return "Success";
103            }
104            private String evaluateMessageSelectorCommand() throws MactorException {
105                    MessageSelectorSpec spec = MessageSelectorSpec.loadSpec(commandElement);
106                    MessageSelectorCommandExecutor executor = MessageSelectorCommandExecutorFactory.createExecutor(context, spec);
107                    Message m = context.getLastIncomingMessage();
108                    if (m == null)
109                            throw new MactorException("There is no incoming message to validate!");
110                    if (executor.isAcceptableMessage(m))
111                            return "The Message was accepted";
112                    else
113                            return "The Message was NOT accepted";
114            }
115            private String evaluateMessageBuilderCommand() throws MactorException {
116                    MessageBuilderSpec spec = MessageBuilderSpec.loadSpec(commandElement);
117                    MessageBuilderCommandExecutor executor = MessageBuilderCommandExecutorFactory.createExecutor(context, spec);
118                    return executor.buildMessage(context).getContent();
119            }
120            public CommandTestDlg(Frame parent, Element commandElement) throws MactorException {
121                    super(parent);
122                    setLayout(new BorderLayout());
123                    setModal(true);
124                    setTitle("Test " + commandElement.getName() + " Command");
125                    this.commandElement = commandElement;
126                    contextPanel.clearContext();
127                    SimpleFormPanel form = new SimpleFormPanel();
128                    form.add(contextPanel);
129                    JPanel bp = new JPanel(new FlowLayout());
130                    bp.add(evaluateButton);
131                    form.add(bp);
132                    SimpleFormPanel resultForm = new SimpleFormPanel();
133                    resultForm.add(new JScrollPane(resultText));
134                    resultForm.setBorder(BorderFactory.createTitledBorder("Evaluation Result"));
135                    form.add(resultForm);
136                    add(form, BorderLayout.CENTER);
137                    JPanel buttonPanel = new JPanel(new FlowLayout());
138                    buttonPanel.add(okButton);
139                    add(buttonPanel, BorderLayout.SOUTH);
140                    pack();
141            }
142            private class ContextPanel extends SimpleFormPanel {
143                    ParamsPanel paramsPanel = new ParamsPanel();
144                    JTextArea messageText = new JTextArea(10, 40);
145                    JButton clearContextButton = new JButton(new AbstractAction("Remove All Messages from Context") {
146                            public void actionPerformed(ActionEvent arg0) {
147                                    try {
148                                            clearContext();
149                                    } catch (MactorException me) {
150                                            GuiUtil.showGuiError(ContextPanel.this, me);
151                                    }
152                            };
153                    });
154                    JButton addParamButton = new JButton(new AbstractAction("Add Variable to Context") {
155                            public void actionPerformed(ActionEvent arg0) {
156                                    paramsPanel.addNewRow();
157                            }
158                    });
159                    JButton addMessageButton = new JButton(new AbstractAction("Add Message to Context") {
160                            public void actionPerformed(ActionEvent arg0) {
161                                    try {
162                                            context.addReceivedMessage("dummy", Message.createMessage(messageText.getText()));
163                                    } catch (MactorException me) {
164                                            GuiUtil.showGuiError(ContextPanel.this, me);
165                                    }
166                            };
167                    });
168                    public ContextPanel() {
169                            setBorder(BorderFactory.createTitledBorder("Test Context"));
170                            add(new JLabel("Incoming Message:"));
171                            add(new JScrollPane(messageText));
172                            JPanel bp = new JPanel(new FlowLayout());
173                            bp.add(addMessageButton);
174                            bp.add(clearContextButton);
175                            bp.add(addParamButton);
176                            add(bp);
177                            add(new JLabel("Context Variables:"));
178                            add(paramsPanel);
179                    }
180                    public HashMap<String, String> getContextValues() {
181                            HashMap<String, String> m = new HashMap<String, String>();
182                            for (ParamsPanel.RowPanel row : paramsPanel.rows)
183                                    m.put(row.nameField.getText(), row.valueField.getText());
184                            return m;
185                    }
186                    private void clearContext() throws MactorException {
187                            context = new TestContextImpl(ProjectContext.getGlobalInstance().loadGlobalConfig(), TestSpec.loadFromDocument(commandElement.getDocument(), "dummy"));
188                    }
189            }
190    }