SwingSelectionFrame.java :  » Testing » jacareto » jacareto » examples » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » examples » SwingSelectionFrame.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.examples;


import jacareto.toolkit.awt.RatioLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

/**
 * Class for testing some swing components.
 */
public class SwingSelectionFrame extends JFrame {
    /** The left list. */
    private JList leftList;

    /** The right list. */
    private JList rightList;

    /** The middle button (between the two lists). */
    private JButton middleButton;

    /** The text field. */
    private JTextField inputField;

    /**
     * Creates a new swing selection frame.
     */
    public SwingSelectionFrame () {
        super("Swing Selection Frame");

        //
        // Upper left panel
        //
        JPanel upperLeftPanel = new JPanel();
        upperLeftPanel.setLayout (new RatioLayout());

        //
        // fill the list
        //
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement ("Adams, Jack");
        listModel.addElement ("Miller, John");
        listModel.addElement ("Connor, Diane");
        listModel.addElement ("Smith, Tom");

        leftList = new JList(listModel);

        rightList = new JList(new DefaultListModel());

        middleButton = new JButton("< >");
        middleButton.setName ("middleButton");

        upperLeftPanel.add ("0.05,0.05;0.30,0.90", leftList);
        upperLeftPanel.add ("0.65,0.05;0.30,0.90", rightList);

        //upperLeftPanel.add( "0.45,0.4;0.1,0.2", middleButton );
        upperLeftPanel.add ("0.40,0.05;0.20,0.90", middleButton);

        //
        // Lower left panel
        //
        JPanel lowerLeftPanel = new JPanel();
        lowerLeftPanel.setLayout (new RatioLayout());

        inputField = new JTextField();
        lowerLeftPanel.add ("0.05,0.05;0.9,0.9", inputField);

        //
        // Right panel
        //
        JPanel rightPanel = new JPanel();

        ButtonGroup buttonGroup = new ButtonGroup();

        JRadioButton radioButtonOne = new JRadioButton("one", true);
        JRadioButton radioButtonTwo = new JRadioButton("two", false);
        JRadioButton radioButtonThree = new JRadioButton("three", false);

        JCheckBox checkBoxCheck = new JCheckBox("check", true);

        buttonGroup.add (radioButtonOne);
        buttonGroup.add (radioButtonTwo);
        buttonGroup.add (radioButtonThree);

        rightPanel.add (radioButtonOne);
        rightPanel.add (radioButtonTwo);
        rightPanel.add (radioButtonThree);
        rightPanel.add (checkBoxCheck);

        //
        // Whole frame
        //
        getContentPane ().setLayout (new RatioLayout());

        getContentPane ().add ("0.0,0.0;0.80,0.9", upperLeftPanel);
        getContentPane ().add ("0.0,0.9;0.80,0.1", lowerLeftPanel);
        getContentPane ().add ("0.8,0.0;0.20,1.0", rightPanel);

        //
        // event handler for button
        //
        ButtonListener buttonListener = new ButtonListener();
        middleButton.addActionListener (buttonListener);

        //
        // event handler for left list
        //
        leftList.addMouseListener (new MouseAdapter() {
                public void mouseClicked (MouseEvent e) {
                    if (e.getClickCount () == 2) {
                        SwingSelectionFrame.this.moveListItem ();
                    }
                }
            });

        //
        // event handler for right list
        //
        rightList.addMouseListener (new MouseAdapter() {
                public void mouseClicked (MouseEvent e) {
                    if (e.getClickCount () == 2) {
                        SwingSelectionFrame.this.moveListItem ();
                    }
                }
            });
    }

    /**
     * Moves an item between the two lists.
     */
    private void moveListItem () {
        if (leftList.getSelectedIndex () != -1) {
            addItemToList (rightList, leftList.getSelectedValue ());
            removeItemFromList (leftList, leftList.getSelectedIndex ());
        } else if (rightList.getSelectedIndex () != -1) {
            addItemToList (leftList, rightList.getSelectedValue ());
            removeItemFromList (rightList, rightList.getSelectedIndex ());
        }
    }

    /**
     * This removes the item with the given index from the given JList.
     *
     * @param list JList object from which the item should be removed.
     * @param index Index of the item to remove.
     */
    private void removeItemFromList (JList list, int index) {
        DefaultListModel listModel = (DefaultListModel) list.getModel ();

        if (index <= listModel.getSize ()) {
            listModel.remove (index);

            //removed item in last position
            if (index == listModel.getSize ()) {
                index--;
            }

            //otherwise select same index
            list.setSelectedIndex (index);
        }
    }

    /**
     * Adds the given item to the given list.
     *
     * @param list JList object to which the item should be added.
     * @param item Item which should be added.
     */
    private void addItemToList (JList list, Object item) {
        if (item != null) {
            DefaultListModel listModel = (DefaultListModel) list.getModel ();
            listModel.addElement (item);
        }
    }

    /**
     * The main method which shows the frame.
     *
     * @param args DOCUMENT ME!
     */
    public static void main (String[] args) {
        JFrame f = new SwingSelectionFrame();
        f.setBounds (400, 300, 400, 300);
        f.setVisible (true);
    }

    /**
     * The button listener class.
     */
    class ButtonListener implements ActionListener {
        //~ Methods --------------------------------------------------------------------------------

        public void actionPerformed (ActionEvent event) {
            SwingSelectionFrame.this.moveListItem ();
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.