Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

/**
 * AntShield - A project release tool for ant.
 */
package com.erggroup.mass.ant;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.AbstractSet;
import java.awt.*;
import java.awt.event.*;

/**
 * AntShieldFieldFrame
 * A dialog box that allows users to enter values for
 * specified fields, displaying either text boxes or combo boxes.
 */
public class AntShieldFieldFrame extends JFrame {

    private int numfields = 0;
    private ArrayList fields = null;
    private ArrayList labelArray = new ArrayList();
    private ArrayList fieldArray = new ArrayList();
    private ArrayList comboArray = new ArrayList();
    private boolean ok = false;
    private AntShieldLock lock = new AntShieldLock();

    /**
     * Constructor.  Builds and displays dialog.
     * @param fieldlist
     */
    public AntShieldFieldFrame(ArrayList fieldlist)
    {
        fields = fieldlist;
        init();
        setPosition();
        updateFields();
        pack();
        setVisible(true);
        lock.acquire();
    }

    /**
     * Build this frame from components.
     */
    private void init()
    {
        setTitle("Antshield Option Fields");

        // Proper close if the user closes the window.
        addWindowListener(new AntShieldFieldWindowClose(this));

        // OK button.
        JButton okButton = new JButton();
        okButton.setActionCommand("OK");
        okButton.setText("OK");
        okButton.addActionListener(new AntShieldFieldOKAction(this));

        // Cancel button.
        JButton cancelButton = new JButton();
        cancelButton.setActionCommand("Cancel");
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new AntShieldFieldCancelAction(this));

        // South-east panel - holds the OK and Cancel buttons.
        JPanel sePanel = new JPanel();
        sePanel.add(okButton, null);
        sePanel.add(cancelButton, null);

        // South panel - holds south-east panel
        JPanel southPanel = new JPanel();
        southPanel.setLayout(new BorderLayout());
        southPanel.setBorder(new EtchedBorder());
        southPanel.add(sePanel, BorderLayout.EAST);

        // Fill up arrays of JLabels, JTextFields and JComboBoxes
        numfields = 0;
        Iterator i = fields.iterator();
        while (i.hasNext())
        {
            FieldElement e = (FieldElement)i.next();

            JLabel l = new JLabel();
            l.setText(" " + e.getPrompt() + " ");
            labelArray.add(l);

            String s = e.getDefaultValue();
            if (s == null)
            {
                s = new String("");
            }

            // No options specified - us a JTextField
            if (e.getOptions().size() == 0)
            {
                JTextField f = new JTextField();
                f.setText(s);
                fieldArray.add(f);
                comboArray.add(null);
            }

            // Options specified - use a JComboBox
            else
            {
                JComboBox c = new JComboBox();
                c.setEditable(true);
                c.setSelectedItem(s);
                // Populate combo box with specified options
                AbstractSet options = e.getOptions();
                Iterator j = options.iterator();
                while (j.hasNext())
                {
                    c.addItem(j.next());
                }
                fieldArray.add(null);
                comboArray.add(c);
            }

            numfields++;
        }

        // Panel with n x 2 grid
        JPanel gridPanel = new JPanel();
        gridPanel.setLayout(new GridLayout(numfields, 2));
        for (int n = 0; n < numfields; n++)
        {
            // Left-hand side gets the JLabel
            gridPanel.add((JLabel)labelArray.get(n));

            // Right-hand side gets the JTextField or JComboBox
            if (fieldArray.get(n) != null)
            {
                gridPanel.add((JTextField)fieldArray.get(n));
            }
            else
            {
                gridPanel.add((JComboBox)comboArray.get(n));
            }
        }

        // Keep our grid panel centred and top-justified
        JPanel centrePanel = new JPanel();
        centrePanel.setLayout(new BorderLayout());
        centrePanel.add(gridPanel, BorderLayout.NORTH);

        // A scroll pane for large numbers of fields
        JScrollPane scrollPanel = new JScrollPane();
        scrollPanel.getViewport().add(centrePanel, null);

        // Format everything with nice indentation
        Box box1 = new Box(BoxLayout.X_AXIS);
        box1.add(Box.createHorizontalStrut(20));
        box1.add(scrollPanel);
        box1.add(Box.createHorizontalStrut(20));

        Box box2 = new Box(BoxLayout.Y_AXIS);
        box2.add(Box.createVerticalStrut(20));
        box2.add(box1);
        box2.add(Box.createVerticalStrut(20));

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(southPanel,  BorderLayout.SOUTH);
        getContentPane().add(box2, BorderLayout.CENTER);

        // Hitting ENTER activates the OK button
        getRootPane().setDefaultButton(okButton);

        pack();
    } // init

    /**
     * Position this frame in the centre of the screen,
     * resizing if necessary.
     */
    private void setPosition()
    {
        int scrnWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int scrnHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
        int width = getWidth();
        int height = getHeight();
        if (width > scrnWidth/2)
        {
            width = scrnWidth/2;
        }
        if (width < scrnWidth/8)
        {
            width = scrnWidth/8;
        }
        if (height > scrnHeight/2)
        {
            height = scrnHeight/2;
        }
        if (height < scrnHeight/8)
        {
            height = scrnHeight/8;
        }
        setSize(width, height);
        int xpos = scrnWidth/2 - width/2;
        int ypos = scrnHeight/2 - height/2;
        setLocation(xpos, ypos);
    } // setPosition

    /**
     * Calls setValue() on each FieldElement with values
     * supplied in the text and combo boxes.
     */
    private void updateFields()
    {
        for (int i = 0; i < numfields; i++)
        {
            FieldElement e = (FieldElement)fields.get(i);
            String v;
            if (fieldArray.get(i) != null)
            {
                v = ((JTextField)fieldArray.get(i)).getText();
            }
            else
            {
                v = (String)((JComboBox)comboArray.get(i)).getSelectedItem();
            }
            if (v == null)
            {
                v = new String("");
            }
            e.setValue(v.trim());
        }
    } // updateFields

    /**
     * Did we exit successfully?
     * @return
     */
    public boolean isOk()
    {
        return ok;
    } // isOk

    /**
     * Handle the OK button by checking that all fields have values.
     * If not, pop up a dialog.
     */
    public void handleOk()
    {
        ok = true;
        updateFields();
        Iterator i = fields.iterator();
        while (i.hasNext())
        {
            FieldElement element = (FieldElement)i.next();
            String s = element.getValue();
            if ((s == null) || (s.length() < 1))
            {
                ok = false;
                break;
            }
        }
        if (ok)
        {
            close();
        }
        else
        {
            new AntShieldRetryDialog("Values must be provided for all fields.");
        }
    } // handleOk

    /**
     * Handle the cancel button.
     */
    public void handleCancel()
    {
        ok = false;
        close();
    } // handleCancel

    /**
     * Close this frame.
     */
    public void close()
    {
        lock.release();
        setVisible(false);
    } // close
}

class AntShieldFieldOKAction implements ActionListener
{
    private AntShieldFieldFrame adaptee;

    public AntShieldFieldOKAction(AntShieldFieldFrame a)
    {
        adaptee = a;
    }

    public void actionPerformed(ActionEvent e)
    {
        adaptee.handleOk();
    }
}

class AntShieldFieldCancelAction implements ActionListener
{
    private AntShieldFieldFrame adaptee;

    public AntShieldFieldCancelAction(AntShieldFieldFrame a)
    {
        adaptee = a;
    }

    public void actionPerformed(ActionEvent e)
    {
        adaptee.handleCancel();
    }
}

class AntShieldFieldWindowClose extends WindowAdapter
{
    private AntShieldFieldFrame parent;

    public AntShieldFieldWindowClose(AntShieldFieldFrame a)
    {
        parent = a;
    }

    public void windowClosing(WindowEvent e)
    {
        parent.close();
    }
}