Example usage for java.awt GridBagConstraints GridBagConstraints

List of usage examples for java.awt GridBagConstraints GridBagConstraints

Introduction

In this page you can find the example usage for java.awt GridBagConstraints GridBagConstraints.

Prototype

public GridBagConstraints() 

Source Link

Document

Creates a GridBagConstraint object with all of its fields set to their default value.

Usage

From source file:caarray.client.test.gui.GuiMain.java

public GuiMain() throws Exception {
    JFrame frame = new JFrame("API Test Suite");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().setLayout(new BorderLayout(6, 6));

    JPanel topPanel = new JPanel();

    /* ###### Text fields for modifiable parameters ##### */

    final JTextField javaHostText = new JTextField(TestProperties.getJavaServerHostname(), 20);
    JLabel javaHostLabel = new JLabel("Java Service Host");
    JButton save = new JButton("Save");
    save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            TestProperties.setJavaServerHostname(javaHostText.getText());
        }/*  w  w w  .j av  a 2  s  .  co  m*/

    });

    JLabel javaPortLabel = new JLabel("Java Port");
    final JTextField javaPortText = new JTextField(Integer.toString(TestProperties.getJavaServerJndiPort()), 5);
    JButton save2 = new JButton("Save");
    save2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                int port = Integer.parseInt(javaPortText.getText());
                TestProperties.setJavaServerJndiPort(port);
            } catch (NumberFormatException e) {
                System.out.println(javaPortText.getText() + " is not a valid port number.");
            }
        }

    });

    JLabel gridHostLabel = new JLabel("Grid Service Host");
    final JTextField gridHostText = new JTextField(TestProperties.getGridServerHostname(), 20);
    JButton save3 = new JButton("Save");
    save3.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            TestProperties.setGridServerHostname(gridHostText.getText());
        }

    });

    JLabel gridPortLabel = new JLabel("Grid Service Port");
    final JTextField gridPortText = new JTextField(Integer.toString(TestProperties.getGridServerPort()), 5);
    JButton save4 = new JButton("Save");
    save4.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try {
                int port = Integer.parseInt(gridPortText.getText());
                TestProperties.setGridServerPort(port);
            } catch (NumberFormatException e) {
                System.out.println(gridPortText.getText() + " is not a valid port number.");
            }

        }

    });

    JLabel excludeLabel = new JLabel("Exclude test cases (comma-separated list):");
    final JTextField excludeText = new JTextField("", 30);
    JButton save5 = new JButton("Save");
    save5.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            String testString = excludeText.getText();
            if (testString != null) {
                String[] testCases = testString.split(",");
                if (testCases != null && testCases.length > 0) {
                    List<Float> tests = new ArrayList<Float>();
                    for (String test : testCases) {
                        try {
                            tests.add(Float.parseFloat(test));
                        } catch (NumberFormatException e) {
                            System.out.println(test + " is not a valid test case.");
                        }
                    }
                    TestProperties.setExcludedTests(tests);
                }

            }

        }

    });

    JLabel includeLabel = new JLabel("Include only (comma-separated list):");
    final JTextField includeText = new JTextField("", 30);
    JButton save6 = new JButton("Save");
    save6.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            String testString = includeText.getText();
            if (testString != null) {
                String[] testCases = testString.split(",");
                if (testCases != null && testCases.length > 0) {
                    List<Float> tests = new ArrayList<Float>();
                    for (String test : testCases) {
                        try {
                            tests.add(Float.parseFloat(test));
                        } catch (NumberFormatException e) {
                            System.out.println(test + " is not a valid test case.");
                        }
                    }
                    TestProperties.setIncludeOnlyTests(tests);
                }

            }

        }

    });

    JLabel threadLabel = new JLabel("Number of threads:");
    final JTextField threadText = new JTextField(Integer.toString(TestProperties.getNumThreads()), 5);
    JButton save7 = new JButton("Save");
    save7.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try {
                int threads = Integer.parseInt(threadText.getText());
                TestProperties.setNumThreads(threads);
            } catch (NumberFormatException e) {
                System.out.println(threadText.getText() + " is not a valid thread number.");
            }

        }

    });
    GridBagLayout topLayout = new GridBagLayout();
    topPanel.setLayout(topLayout);

    JLabel[] labels = new JLabel[] { javaHostLabel, javaPortLabel, gridHostLabel, gridPortLabel, excludeLabel,
            includeLabel, threadLabel };
    JTextField[] textFields = new JTextField[] { javaHostText, javaPortText, gridHostText, gridPortText,
            excludeText, includeText, threadText };
    JButton[] buttons = new JButton[] { save, save2, save3, save4, save5, save6, save7 };
    for (int i = 0; i < labels.length; i++) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = i;
        topPanel.add(labels[i], c);
        c.gridx = 1;
        topPanel.add(textFields[i], c);
        c.gridx = 2;
        topPanel.add(buttons[i], c);
    }

    frame.getContentPane().add(topPanel, BorderLayout.PAGE_START);

    GridLayout bottomLayout = new GridLayout(0, 4);
    selectionPanel.setLayout(bottomLayout);
    JCheckBox selectAll = new JCheckBox("Select/Deselect All");
    selectAll.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JCheckBox box = (JCheckBox) e.getSource();
            selectAll(box.isSelected());
        }
    });
    selectionPanel.add(selectAll);
    JCheckBox excludeLongTests = new JCheckBox("Exclude Long Tests");
    excludeLongTests.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JCheckBox box = (JCheckBox) e.getSource();
            if (box.isSelected()) {
                TestProperties.excludeLongTests();
            } else {
                TestProperties.removeExcludedLongTests();
            }
        }
    });
    TestProperties.excludeLongTests();
    excludeLongTests.setSelected(true);
    selectionPanel.add(excludeLongTests);

    //Initialize check boxes corresponding to test categories
    initializeTests();

    centerPanel.setLayout(new GridLayout(0, 1));
    centerPanel.add(selectionPanel);

    //Redirect System messages to gui     
    JScrollPane textScroll = new JScrollPane();
    textScroll.setViewportView(textDisplay);
    System.setOut(new PrintStream(new JTextAreaOutputStream(textDisplay)));
    System.setErr(new PrintStream(new JTextAreaOutputStream(textDisplay)));
    centerPanel.add(textScroll);
    JScrollPane scroll = new JScrollPane(centerPanel);

    frame.getContentPane().add(scroll, BorderLayout.CENTER);

    JButton runButton = new JButton("Run Tests");
    runButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try {

                runTests();

            } catch (Exception e) {
                System.out.println("An error occured executing the tests: " + e.getClass()
                        + ". Check error log for details.");
                log.error("Exception encountered:", e);
            }
        }

    });

    JPanel bottomPanel = new JPanel();
    bottomPanel.add(runButton);
    frame.getContentPane().add(bottomPanel, BorderLayout.PAGE_END);

    frame.pack();
    frame.setVisible(true);
}