001    /*
002     * CRIMSON
003     * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 
004     * University of Pennsylvania.
005     *
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License as
008     * published by the Free Software Foundation; either version 2 of the
009     * License, or (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful, but
012     * WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019     * 02110-1301 USA.
020     *
021     * @(#)ObjectSelectorDialog.java
022     */
023    
024    package edu.upenn.crimson.gui;
025    
026    import java.awt.*;
027    import java.awt.event.*;
028    import javax.swing.*;
029    import java.util.ArrayList;
030    
031    /**
032     * This class is used to present a modal dialog for selecting an item
033     * from the 'objects' array and includes the selected item in the
034     * 'out' arraylist.
035     * 
036     *
037     * @author  Stephen Fisher
038     * @version $Id: ObjectSelectorDialog.java,v 1.4 2007/05/16 18:55:58 fisher Exp $
039     */
040    public class ObjectSelectorDialog extends JDialog {
041             ObjectSelectorDialog thisDialog;
042             ArrayList output;
043    
044             JList objectL;
045             JButton selectB;
046             JButton cancelB;
047    
048             public ObjectSelectorDialog(String title, Object[] objects, ArrayList out) {
049                      super((Frame) null, title, true);
050    
051                      // keep pointer to self so can 'dispose' Dialog below
052                      thisDialog = this;
053                      output = out;
054    
055                      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
056    
057                      JPanel objectP = new JPanel(new BorderLayout());
058                      objectL = new JList(objects);
059                      objectL.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
060                      JScrollPane objectSP = new JScrollPane(objectL);
061                      objectP.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
062                      objectP.setPreferredSize(new Dimension(200, 400));
063                      objectP.setLayout(new BorderLayout());
064                      objectP.add(new Label("Select Item:"), BorderLayout.NORTH);
065                      objectP.add(objectSP, BorderLayout.CENTER);
066    
067                      // select button sub-panel
068                      JPanel buttonP = new JPanel(new GridLayout(1,0,5,5));
069                      buttonP.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
070                      // select objects "select" Button
071                      selectB = new JButton("Select");
072                      selectB.setEnabled(true);
073                      selectB.addActionListener(new ActionListener() {
074                                             public void actionPerformed(ActionEvent e) {
075                                                      output.add(objectL.getSelectedValue());
076                                                      thisDialog.dispose();
077                                             }
078                                    });
079                      buttonP.add(selectB);
080                      // select objects "cancel" Button
081                      cancelB = new JButton("Cancel");
082                      cancelB.setEnabled(true);
083                      cancelB.addActionListener(new ActionListener() {
084                                             public void actionPerformed(ActionEvent e) {
085                                                      thisDialog.dispose();
086                                             }
087                                    });
088                      buttonP.add(cancelB);
089                      
090                      getContentPane().setLayout(new BorderLayout());
091                      getContentPane().add(objectP, BorderLayout.CENTER);
092                      getContentPane().add(buttonP, BorderLayout.SOUTH);
093                      pack();
094                      
095                      // set the default window size
096                      setSize(getSize().width + 100, getSize().height + 30);
097                      
098                      // display the window
099                      setVisible(true);
100             }
101    } // ObjectSelectorDialog.java