001package edu.pdx.cs410J.family; 002 003import java.awt.*; 004import java.awt.event.*; 005import javax.swing.*; 006import javax.swing.border.*; 007 008/** 009 * A <code>ChoosePersonDialog</code> is used to select a person from a 010 * family tree. 011 */ 012@SuppressWarnings("serial") 013public class ChoosePersonDialog extends JDialog { 014 015 private Person person; // The chosen person 016 017 /** 018 * Creates a new <code>ChoosePersonDialog</code> that displays the 019 * people in a given <code>FamilyTree</code>. 020 */ 021 public ChoosePersonDialog(FamilyTree tree, JDialog owner) { 022 super(owner, "Select a Person", true /* modal */); 023 024 // A ChoosePersonDialog consists of a FamilyTreeList and a couple 025 // of buttons 026 Container pane = this.getContentPane(); 027 pane.setLayout(new BorderLayout()); 028 029 final FamilyTreeList treeList = new FamilyTreeList(); 030 treeList.fillInList(tree); 031 032 JScrollPane treePane = new JScrollPane(treeList); 033 Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5); 034 treePane.setBorder(border); 035 036 pane.add(treePane, BorderLayout.CENTER); 037 038 JPanel buttons = new JPanel(); 039 buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS)); 040 buttons.add(Box.createHorizontalGlue()); 041 042 JButton okButton = new JButton("OK"); 043 okButton.addActionListener(new ActionListener() { 044 public void actionPerformed(ActionEvent e) { 045 // Get selected person and dispose 046 ChoosePersonDialog.this.person = 047 treeList.getSelectedPerson(); 048 ChoosePersonDialog.this.dispose(); 049 } 050 }); 051 buttons.add(okButton); 052 053 buttons.add(Box.createHorizontalGlue()); 054 055 JButton cancelButton = new JButton("Cancel"); 056 cancelButton.addActionListener(new ActionListener() { 057 public void actionPerformed(ActionEvent e) { 058 // No person selected 059 ChoosePersonDialog.this.person = null; 060 ChoosePersonDialog.this.dispose(); 061 } 062 }); 063 buttons.add(cancelButton); 064 065 buttons.add(Box.createHorizontalGlue()); 066 067 pane.add(buttons, BorderLayout.SOUTH); 068 } 069 070 /** 071 * Returns the choosen person 072 */ 073 public Person getPerson() { 074 return this.person; 075 } 076 077}