Package com.coherentlogic.wb.client.webstart.application

Source Code of com.coherentlogic.wb.client.webstart.application.MenuItemSelectedActionListener

package com.coherentlogic.wb.client.webstart.application;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import net.miginfocom.swing.MigLayout;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.coherentlogic.wb.client.core.builders.QueryBuilder;
import com.coherentlogic.wb.client.core.factories.QueryBuilderFactory;

/**
* The front-end for the FRED Client that allows users to directly work with the
* {@link com.coherentlogic.fred.client.core.builders.QueryBuilder}.
*
* @author <a href="support@coherentlogic.com">Support</a>
*/
public class WBClientGUI extends JFrame {

    private static final long serialVersionUID = 1L;

    private static final Logger log =
        LoggerFactory.getLogger(WBClientGUI.class);

    private static final String
        COUNTRIES = "Countries",
        CATALOG_SOURCES = "CatalogSources",
        INVALID_REQUEST = "InvalidRequest",
        INCOME_LEVELS = "IncomeLevels",
        INDICATORS = "Indicators",
        WB_CLIENT_GUI = "wbClientGUI",
        WRAP = "wrap",
        QUERY_BUILDER = "queryBuilder";

    private static final String
        COUNTRIES_QUERY_BUILDER_FACTORY = "countriesQueryBuilderFactory";

    private static final Map<String, String> beanIdMap =
        new HashMap<String, String> ();

    static {
        beanIdMap.put(COUNTRIES, COUNTRIES_QUERY_BUILDER_FACTORY);
    }

    private final JTextArea outputTextArea = new JTextArea();

    private final JButton runScriptButton = new JButton("Run script");

    private final ButtonGroup requestMenuItemsGroup = new ButtonGroup();

    private final Map<ButtonModel, JRadioButtonMenuItem> radioButtonMap =
        new HashMap<ButtonModel, JRadioButtonMenuItem> ();

    private final GroovyEngine groovyEngine;

    private final Map<String, QueryBuilderFactory> queryBuilderFactoryMap;

    private final Map<String, String> exampleMap;

    private final ObjectStringifier objectStringifier =
        new ObjectStringifier ();

    /**
     * @todo Remove the init method from the constructor.
     */
    public WBClientGUI(
        GroovyEngine groovyEngine,
        Map<String, QueryBuilderFactory> queryBuilderFactoryMap,
        Map<String, String> exampleMap
    ) {
        this.groovyEngine = groovyEngine;
        this.queryBuilderFactoryMap = queryBuilderFactoryMap;
        this.exampleMap = exampleMap;

        setTitle("World Bank Client GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initialize ();
    }

    /**
     * @see #initialize()
     */
    void initializeMenu (JTextArea inputTextArea) {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnRequest = new JMenu("Examples");
        menuBar.add(mnRequest);

        JRadioButtonMenuItem countries = new JRadioButtonMenuItem(COUNTRIES);
        countries.setSelected(true);
        mnRequest.add(countries);

        countries.addActionListener(
            new MenuItemSelectedActionListener (exampleMap, inputTextArea));

        JRadioButtonMenuItem catalogSources =
            new JRadioButtonMenuItem(CATALOG_SOURCES);
        mnRequest.add(catalogSources);

        catalogSources.addActionListener(
            new MenuItemSelectedActionListener (exampleMap, inputTextArea));

        JRadioButtonMenuItem invalidRequest =
            new JRadioButtonMenuItem(INVALID_REQUEST);
        mnRequest.add(invalidRequest);

        invalidRequest.addActionListener(
            new MenuItemSelectedActionListener (exampleMap, inputTextArea));

        JRadioButtonMenuItem incomeLevels =
            new JRadioButtonMenuItem(INCOME_LEVELS);
        mnRequest.add(incomeLevels);

        incomeLevels.addActionListener(
            new MenuItemSelectedActionListener (exampleMap, inputTextArea));

        JRadioButtonMenuItem indicators =
            new JRadioButtonMenuItem(INDICATORS);
        mnRequest.add(indicators);

        indicators.addActionListener(
            new MenuItemSelectedActionListener (exampleMap, inputTextArea));

        requestMenuItemsGroup.add(countries);
        requestMenuItemsGroup.add(catalogSources);
        requestMenuItemsGroup.add(invalidRequest);
        requestMenuItemsGroup.add(incomeLevels);
        requestMenuItemsGroup.add(indicators);

        radioButtonMap.put(countries.getModel(), countries);
        radioButtonMap.put(catalogSources.getModel(), catalogSources);
        radioButtonMap.put(invalidRequest.getModel(), invalidRequest);
        radioButtonMap.put(incomeLevels.getModel(), incomeLevels);
        radioButtonMap.put(indicators.getModel(), indicators);

//        JMenu mnAbout = new JMenu("Help");
//        menuBar.add(mnAbout);
//
//        JMenuItem mntmAbout = new JMenuItem("About");
//        mnAbout.add(mntmAbout);
    }

    /**
     * Method configures the Swing components that are added to this object's
     * JFrame.
     */
    public void initialize () {

        final JPanel panel = new JPanel();
        getContentPane().add(panel, BorderLayout.CENTER);
        setExtendedState(Frame.MAXIMIZED_BOTH);
        panel.setLayout(new MigLayout());

        JLabel enterYourQueryLabel = new JLabel(
            "Enter your query here (context contains references to: " +
            "requestBuilder):");

        panel.add(enterYourQueryLabel, WRAP);

        final JTextArea inputTextArea = new JTextArea();

        JScrollPane inputTextAreaScrollPane = new JScrollPane(inputTextArea);

        inputTextAreaScrollPane.
            setVerticalScrollBarPolicy(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        initializeMenu(inputTextArea);

        String exampleText = exampleMap.get (COUNTRIES);

        inputTextArea.setText(exampleText);

        inputTextArea.setColumns(80);
        inputTextArea.setRows(40);
        panel.add(inputTextAreaScrollPane, WRAP);

        panel.add(runScriptButton, WRAP);

        JLabel outputAppearsBelowLabel = new JLabel(
            "The output appears below:");

        panel.add(outputAppearsBelowLabel, WRAP);

        outputTextArea.setColumns(80);
        outputTextArea.setRows(40);

        JScrollPane outputTextAreaScrollPane = new JScrollPane(outputTextArea);

        outputTextAreaScrollPane.
            setVerticalScrollBarPolicy(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        panel.add(outputTextAreaScrollPane, WRAP);

        GraphicsEnvironment env =
            GraphicsEnvironment.getLocalGraphicsEnvironment();

        Rectangle bounds = env.getMaximumWindowBounds();

        setBounds(bounds);

        runScriptButton.addActionListener(
            new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent actionEvent) {

                    String scriptText = inputTextArea.getText();

                    log.info("scriptText:\n\n" + scriptText);

                    ButtonModel buttonModel =
                        requestMenuItemsGroup.getSelection();

                    JRadioButtonMenuItem selectedMenuItem =
                        radioButtonMap.get(buttonModel);

                    String selectedText = selectedMenuItem.getText();

                    log.info("selectedText: " + selectedText);

                    QueryBuilderFactory queryBuilderFactory =
                        (QueryBuilderFactory)
                            queryBuilderFactoryMap.get(selectedText);

                    QueryBuilder requestBuilder =
                        queryBuilderFactory.getInstance();

                    groovyEngine.setVariable(QUERY_BUILDER, requestBuilder);

                    Object result = null;

                    try {
                        result = groovyEngine.evaluate(scriptText);
                    } catch (Throwable throwable) {

                        log.error("Evaluation failed for the script:\n\n" +
                            scriptText, throwable);

                        JOptionPane.showMessageDialog(
                            panel,
                            throwable.getMessage(),
                            "Evaluation failed!",
                            JOptionPane.ERROR_MESSAGE);

                        return;
                    }

                    log.info("result: " + result);

                    if (result != null) {
                        String stringifiedResult =
                            objectStringifier.toString(result);

                        String fullResult =
                            "// Note that null values are not indicative of a " +
                            "problem, per se, for \n" +
                            "// example the PrimaryKey is only ever assigned " +
                            "when the object has been \n" +
                            "// saved to a database and since this does not " +
                            "happen in this example.\n\n\n" +
                            stringifiedResult;

                        outputTextArea.setText(fullResult);
                    }
                }
            }
        );
    }

    /**
     * The main method uses the Spring application context to get an instance of
     * {@link WBClientGUI} and then displays this object.
     */
    public static void main (String[] unused) throws IOException {

        ApplicationContext applicationContext
            = new ClassPathXmlApplicationContext (
                "application-context.xml");

        WBClientGUI applet = (WBClientGUI)
            applicationContext.getBean(WB_CLIENT_GUI);

        applet.setVisible(true);
    }
}

/**
* An {@link java.awt.event ActionListener} implementation that adds a given
* example to the inputTextArea when the user selects a given
* {@link javax.swing.JMenuItem}.
*
* @author <a href="support@coherentlogic.com">Support</a>
*/
class MenuItemSelectedActionListener implements ActionListener {

    private final Map<String, String> exampleMap;

    private final JTextArea inputTextArea;

    public MenuItemSelectedActionListener(
        Map<String, String> exampleMap,
        JTextArea inputTextArea
    ) {
        super();
        this.exampleMap = exampleMap;
        this.inputTextArea = inputTextArea;
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {

        JMenuItem menuItem = (JMenuItem) actionEvent.getSource();

        String selectedMenu = menuItem.getText();

        String example = exampleMap.get(selectedMenu);

        inputTextArea.setText(example);
    }
}
TOP

Related Classes of com.coherentlogic.wb.client.webstart.application.MenuItemSelectedActionListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.