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