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     * @(#)FieldEditDialog.java
022     */
023    
024    package edu.upenn.crimson.gui;
025    
026    import edu.upenn.crimson.*;
027    import java.awt.*;
028    import java.awt.event.*;
029    import javax.swing.*;
030    import java.util.ArrayList;
031    
032    /**
033     * This class is used to present a modal dialog for editing text in
034     * the 'text' HashMap.  The HashMap should contain label:value pairs.
035     *
036     * @author  Stephen Fisher
037     * @version $Id: FieldEditDialog.java,v 1.5 2007/05/16 18:55:58 fisher Exp $
038     */
039    public class FieldEditDialog extends JDialog {
040             FieldEditDialog thisDialog;
041             ArrayList save = new ArrayList();
042             ArrayList textFields;
043    
044             JButton okB;
045             JButton cancelB;
046    
047             public FieldEditDialog(String title, String[] labels, ArrayList orig) {
048                      super((Frame) null, title, true);
049    
050                      // keep pointer to self so can 'dispose' Dialog below
051                      thisDialog = this;
052    
053                      this.textFields = orig;
054    
055                      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
056                      addWindowListener(new WindowAdapter() {
057                                             public void windowClosing(WindowEvent e) { 
058                                                      // revert to original values
059                                                      for (int i = 0; i < textFields.size(); i++) {
060                                                                    JTextField textField = (JTextField) textFields.get(i);
061                                                                    textField.setText((String) save.get(i));
062                                                      }
063                                                      textFields.add(new Boolean(false));
064                                                      thisDialog.dispose();
065                                             }
066                                    });
067    
068    
069                      // entry panel
070                      JPanel labelP = new JPanel(new GridLayout(0,1,5,5));
071                      labelP.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
072                      JPanel editP = new JPanel(new GridLayout(0,1,5,5));
073                      
074                      if (labels.length != textFields.size()) {
075                                    CrimsonUtils.printMsg("Labels and textFields must be same size for FieldEditDialog.", 
076                                                                                    CrimsonUtils.ERROR);
077                                    return;
078                      }
079    
080                      for (int i = 0; i < labels.length; i++) {
081                                    labelP.add(new JLabel(labels[i]));
082                                    JTextField textField = (JTextField) textFields.get(i);
083                                    editP.add(textField);
084                                    save.add(textField.getText());
085                      }
086    
087                      JPanel contentP = new JPanel(new BorderLayout());
088                      contentP.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
089                      contentP.add(labelP, BorderLayout.WEST);
090                      contentP.add(editP, BorderLayout.CENTER);
091    
092                      // select button sub-panel
093                      JPanel buttonP = new JPanel(new GridLayout(1,0,5,5));
094                      buttonP.setBorder(BorderFactory.createEmptyBorder(0, 20, 20, 20));
095                      // select objects "save" Button
096                      okB = new JButton("Ok");
097                      okB.setEnabled(true);
098                      okB.addActionListener(new ActionListener() {
099                                             public void actionPerformed(ActionEvent e) {
100                                                      textFields.add(new Boolean(true));
101                                                      thisDialog.dispose();
102                                             }
103                                    });
104                      buttonP.add(okB);
105                      // select objects "cancel" Button
106                      cancelB = new JButton("Cancel");
107                      cancelB.setEnabled(true);
108                      cancelB.addActionListener(new ActionListener() {
109                                             public void actionPerformed(ActionEvent e) {
110                                                      // revert to original values
111                                                      for (int i = 0; i < textFields.size(); i++) {
112                                                                    JTextField textField = (JTextField) textFields.get(i);
113                                                                    textField.setText((String) save.get(i));
114                                                      }
115    
116                                                      textFields.add(new Boolean(false));
117                                                      thisDialog.dispose();
118                                             }
119                                    });
120                      buttonP.add(cancelB);
121                      
122                      getContentPane().setLayout(new BorderLayout());
123                      getContentPane().add(contentP, BorderLayout.NORTH);
124                      getContentPane().add(buttonP, BorderLayout.SOUTH);
125                      pack();
126                      
127                      // set the default window size
128                      setSize(getSize().width + 100, getSize().height + 30);
129                      
130                      // display the window
131                      setVisible(true);
132             }
133    } // FieldEditDialog.java