Package com.coherentlogic.fred.client.webstart.application

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

package com.coherentlogic.fred.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.fred.client.core.builders.QueryBuilder;
import com.coherentlogic.fred.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 FREDClientGUI extends JFrame {

    private static final long serialVersionUID = 1L;

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

    private static final String
        SERIESS = "Seriess",
        CATEGORIES = "Categories",
        OBSERVATIONS = "Observations",
        RELEASES = "Releases",
        VINTAGE_DATES = "VintageDates",
        RELEASE_DATES = "ReleaseDates",
        SOURCES = "Sources",
        TAGS = "Tags",
        FRED_CLIENT_GUI = "fredClientGUI";

    private static final String
        SERIESS_QUERY_BUILDER_FACTORY = "seriessQueryBuilderFactory",
        CATEGORIES_QUERY_BUILDER_FACTORY = "categoriesQueryBuilderFactory",
        OBSERVATIONS_QUERY_BUILDER_FACTORY =
            "observationsQueryBuilderFactory",
        RELEASES_QUERY_BUILDER_FACTORY = "releasesQueryBuilderFactory",
        VINTAGE_DATES_QUERY_BUILDER_FACTORY =
            "vintageDatesQueryBuilderFactory",
        RELEASE_DATES_QUERY_BUILDER_FACTORY =
            "releaseDatesQueryBuilderFactory",
        SOURCES_QUERY_BUILDER_FACTORY = "sourcesQueryBuilderFactory",
        TAGS_QUERY_BUILDER_FACTORY = "tagsQueryBuilderFactory";

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

    static {
        beanIdMap.put(SERIESS, SERIESS_QUERY_BUILDER_FACTORY);
        beanIdMap.put(CATEGORIES, CATEGORIES_QUERY_BUILDER_FACTORY);
        beanIdMap.put(OBSERVATIONS, OBSERVATIONS_QUERY_BUILDER_FACTORY);
        beanIdMap.put(RELEASES, RELEASES_QUERY_BUILDER_FACTORY);
        beanIdMap.put(VINTAGE_DATES, VINTAGE_DATES_QUERY_BUILDER_FACTORY);
        beanIdMap.put(RELEASE_DATES, RELEASE_DATES_QUERY_BUILDER_FACTORY);
        beanIdMap.put(SOURCES, SOURCES_QUERY_BUILDER_FACTORY);
        beanIdMap.put(TAGS, TAGS_QUERY_BUILDER_FACTORY);
    }

    private static final String
        WRAP = "wrap",
        QUERY_BUILDER = "queryBuilder";

    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 FREDClientGUI(
        GroovyEngine groovyEngine,
        Map<String, QueryBuilderFactory> requestBuilderFactoryMap,
        Map<String, String> exampleMap
    ) {
        this.groovyEngine = groovyEngine;
        this.queryBuilderFactoryMap = requestBuilderFactoryMap;
        this.exampleMap = exampleMap;

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

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

//        JMenu mnFile = new JMenu("File");
//        menuBar.add(mnFile);
//
//        JMenuItem mntmExit = new JMenuItem("Exit");
//        mnFile.add(mntmExit);

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

        JRadioButtonMenuItem seriess = new JRadioButtonMenuItem("Seriess");
        mnRequest.add(seriess);

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

        JRadioButtonMenuItem categories =
            new JRadioButtonMenuItem("Categories");
        mnRequest.add(categories);

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

        JRadioButtonMenuItem observations =
            new JRadioButtonMenuItem("Observations");
        observations.setSelected(true);
        mnRequest.add(observations);

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

        JRadioButtonMenuItem releases = new JRadioButtonMenuItem("Releases");
        mnRequest.add(releases);

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

        JRadioButtonMenuItem vintageDates =
            new JRadioButtonMenuItem("VintageDates");
        mnRequest.add(vintageDates);

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

        JRadioButtonMenuItem releaseDates =
            new JRadioButtonMenuItem("ReleaseDates");
        mnRequest.add(releaseDates);

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

        JRadioButtonMenuItem sources = new JRadioButtonMenuItem("Sources");
        mnRequest.add(sources);

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

        JRadioButtonMenuItem tags = new JRadioButtonMenuItem(TAGS);
        mnRequest.add(tags);

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

        requestMenuItemsGroup.add(seriess);
        requestMenuItemsGroup.add(categories);
        requestMenuItemsGroup.add(observations);
        requestMenuItemsGroup.add(releases);
        requestMenuItemsGroup.add(vintageDates);
        requestMenuItemsGroup.add(releaseDates);
        requestMenuItemsGroup.add(sources);
        requestMenuItemsGroup.add(tags);

        radioButtonMap.put(seriess.getModel(), seriess);
        radioButtonMap.put(categories.getModel(), categories);
        radioButtonMap.put(observations.getModel(), observations);
        radioButtonMap.put(releases.getModel(), releases);
        radioButtonMap.put(vintageDates.getModel(), vintageDates);
        radioButtonMap.put(releaseDates.getModel(), releaseDates);
        radioButtonMap.put(sources.getModel(), sources);
        radioButtonMap.put(tags.getModel(), tags);

//        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 (OBSERVATIONS);

        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();

                    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 FREDClientGUI} and then displays this object.
     */
    public static void main (String[] unused) throws IOException {

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

        FREDClientGUI applet = (FREDClientGUI)
            applicationContext.getBean(FRED_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.fred.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.