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