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.actions;
021    
022    import java.awt.BorderLayout;
023    import java.awt.FlowLayout;
024    
025    import javax.swing.JButton;
026    import javax.swing.JFrame;
027    import javax.swing.JPanel;
028    import javax.swing.JScrollPane;
029    import javax.swing.JTextArea;
030    
031    import org.dom4j.Element;
032    import org.mactor.brokers.Message;
033    import org.mactor.brokers.mqseries.MqSeriesQueueBrowser;
034    import org.mactor.framework.MactorException;
035    import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig.ChannelConfig;
036    import org.mactor.ui.gui.AsyncAction;
037    import org.mactor.ui.gui.GuiUtil;
038    import org.mactor.ui.gui.project.GuiAction;
039    import org.mactor.ui.gui.project.ProjectController;
040    import org.mactor.ui.gui.project.ProjectNodeType;
041    import org.mactor.ui.gui.project.ProjectTreeNode;
042    
043    public class BrowseMQSeriesChannelAction implements GuiAction {
044            public boolean isPermitted(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) {
045                    if (selectedTreeNode == null || !selectedTreeNode.getNodeType().equals(ProjectNodeType.MBC_CHANNEL))
046                            return false;
047                    String bc = ((Element) selectedTreeNode.getParentNode().getModelObject()).attributeValue("broker_class");
048                    if (bc != null && bc.endsWith("MqSeriesMessageBroker"))
049                            return true;
050                    return false;
051            }
052            public void perform(ProjectTreeNode selectedTreeNode, ProjectController projectController, String[] parameters) throws MactorException {
053                    try {
054                            new BrowseMQSeriesChannelDlg(ChannelConfig.loadConfig(((Element) selectedTreeNode.getModelObject()))).setVisible(true);
055                    } catch (Exception e) {
056                            GuiUtil.showGuiError(projectController.getControllerFrame(), e);
057                    }
058            }
059            private class BrowseMQSeriesChannelDlg extends JFrame {
060                    MqSeriesQueueBrowser browser;
061                    JTextArea messageText = new JTextArea(40, 80);
062                    JButton okButton = new JButton(new AsyncAction("Ok", false, new AsyncAction.AsyncRunnable() {
063                            public void run() throws MactorException {
064                                    browser.close();
065                                    dispose();
066                            }
067                    }));
068                    JButton firstButton = new JButton(new AsyncAction("First Message", false, new AsyncAction.AsyncRunnable() {
069                            public void run() throws MactorException {
070                                    Message message = browser.browseFirstMessage();
071                                    if (message == null)
072                                            messageText.setText("The channel is empty");
073                                    else
074                                            messageText.setText(message.getContent());
075                                    consumeButton.setEnabled(message != null);
076                            }
077                    }));
078                    JButton consumeButton = new JButton(new AsyncAction("Consume Message", false, new AsyncAction.AsyncRunnable() {
079                            public void run() throws MactorException {
080                                    browser.consumeMessage();
081                                    messageText.setText("The message was consumed");
082                            }
083                    }));
084                    JButton nextButton = new JButton(new AsyncAction("Next Message", false, new AsyncAction.AsyncRunnable() {
085                            public void run() throws MactorException {
086                                    Message message = browser.browseNextMessage();
087                                    if (message == null)
088                                            messageText.setText("There is no more messages on the channel");
089                                    else
090                                            messageText.setText(message.getContent());
091                                    consumeButton.setEnabled(message != null);
092                            }
093                    }));
094                    public BrowseMQSeriesChannelDlg(ChannelConfig cc) throws MactorException {
095                            super("MActor - Browse MQSeries Channel '" + cc.getName() + "'");
096                            setLayout(new BorderLayout());
097                            this.browser = new MqSeriesQueueBrowser(cc);
098                            JPanel topButtonPanel = new JPanel(new FlowLayout());
099                            topButtonPanel.add(firstButton);
100                            topButtonPanel.add(nextButton);
101                            // topButtonPanel.add(consumeButton);
102                            add(topButtonPanel, BorderLayout.NORTH);
103                            add(new JScrollPane(messageText), BorderLayout.CENTER);
104                            messageText.setEditable(false);
105                            JPanel bottomButtonPanel = new JPanel(new FlowLayout());
106                            bottomButtonPanel.add(okButton);
107                            add(bottomButtonPanel, BorderLayout.SOUTH);
108                            consumeButton.setEnabled(false);
109                            pack();
110                    }
111            }
112    }