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.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.RequestBuilder;
import com.coherentlogic.fred.client.core.factories.RequestBuilderFactory;

/**
* The front-end for the FRED Client that allows users to directly work with the
* {@link com.coherentlogic.fred.client.core.builders.RequestBuilder}.
*
* @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",
        FRED_CLIENT_GUI = "fredClientGUI";

    private static final String
        SERIESS_REQUEST_BUILDER_FACTORY = "seriessRequestBuilderFactory",
        CATEGORIES_REQUEST_BUILDER_FACTORY = "categoriesRequestBuilderFactory",
        OBSERVATIONS_REQUEST_BUILDER_FACTORY =
            "observationsRequestBuilderFactory",
        RELEASES_REQUEST_BUILDER_FACTORY = "releasesRequestBuilderFactory",
        VINTAGE_DATES_REQUEST_BUILDER_FACTORY =
            "vintageDatesRequestBuilderFactory",
        RELEASE_DATES_REQUEST_BUILDER_FACTORY =
            "releaseDatesRequestBuilderFactory",
        SOURCES_REQUEST_BUILDER_FACTORY = "sourcesRequestBuilderFactory";

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

    static {
        beanIdMap.put(SERIESS, SERIESS_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(CATEGORIES, CATEGORIES_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(OBSERVATIONS, OBSERVATIONS_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(RELEASES, RELEASES_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(VINTAGE_DATES, VINTAGE_DATES_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(RELEASE_DATES, RELEASE_DATES_REQUEST_BUILDER_FACTORY);
        beanIdMap.put(SOURCES, SOURCES_REQUEST_BUILDER_FACTORY);
    }

    private static final String
        WRAP = "wrap",
        REQUEST_BUILDER = "requestBuilder";

    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, RequestBuilderFactory> requestBuilderFactoryMap;

    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, RequestBuilderFactory> requestBuilderFactoryMap,
        Map<String, String> exampleMap
    ) {
        this.groovyEngine = groovyEngine;
        this.requestBuilderFactoryMap = 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");
        seriess.setSelected(true);
        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");
        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));

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

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

//        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 () {

        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 seriessExample = exampleMap.get (SERIESS);

        inputTextArea.setText(seriessExample);

        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: " + scriptText);

                    ButtonModel buttonModel =
                        requestMenuItemsGroup.getSelection();

                    JRadioButtonMenuItem selectedMenuItem =
                        radioButtonMap.get(buttonModel);

                    String selectedText = selectedMenuItem.getText();

                    RequestBuilderFactory requestBuilderFactory =
                        (RequestBuilderFactory)
                            requestBuilderFactoryMap.get(selectedText);

                    RequestBuilder requestBuilder =
                        requestBuilderFactory.getInstance();

                    groovyEngine.setVariable(REQUEST_BUILDER, requestBuilder);

                    Object result = groovyEngine.evaluate(scriptText);

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

                    String stringifiedResult =
                        objectStringifier.toString(result);

                    outputTextArea.setText(stringifiedResult);
                }
            }
        );
    }

    /**
     * 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.