001    /*
002     * cs101 QueryDialog
003     * $Id: QueryDialog.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Copyright (C) 1996 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package cs101.awt;
014    
015    import java.awt.*;
016    import java.awt.event.*;
017    
018    
019    /** 
020     * A class to display a toplevel dialog box with a message, 
021     * a list of "things" and associated textfields, and get answers.
022     *
023     * <P>Copyright (c) 1998 Massachusetts Institute of Technology
024     *
025     * @author Todd C. Parnell, tparnell@ai.mit.edu
026     * @author <nathanw@mit.edu>
027     * @version $Id: QueryDialog.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
028     */
029    public class QueryDialog extends Frame {
030      
031      private String message;
032      private String[] fields,values;
033      private TextComponent[] entries;
034      private Button ok;
035      
036      
037      /** Create a hidden QueryDialog with the appropriate text and defaults. */
038      public QueryDialog(String message, String[] fields, String[] values) {
039    
040        this.message=message;
041        this.fields=fields;
042        this.values=values;
043      }
044      
045      /** Display the dialog box, and block until the OK button is checked. */
046      public synchronized String[] ask() {
047    
048        Label l;
049        int i;
050    
051        this.removeAll();
052    
053        GridBagLayout gridbag = new GridBagLayout();
054        this.setLayout(gridbag); 
055    
056        GridBagConstraints gbc,gblabel,gbarea;
057    
058        gbc = new GridBagConstraints();
059        gblabel = new GridBagConstraints();
060        gbarea = new GridBagConstraints();
061    
062        
063        gbc.gridwidth=1;
064        gbc.gridheight=1;
065        gbc.fill=GridBagConstraints.BOTH;
066        gbc.weightx=1;
067        gbc.weighty=1;
068    
069        gblabel=(GridBagConstraints) gbc.clone();
070        gbarea=(GridBagConstraints) gbc.clone();
071    
072        gbarea.gridwidth=GridBagConstraints.REMAINDER;
073        gblabel.gridwidth=GridBagConstraints.RELATIVE;
074    
075        gbc.gridwidth=GridBagConstraints.REMAINDER;
076    
077        String[] lines = getLines(message);
078        for(i=0;i<lines.length;i++) {
079          l = new Label(lines[i]);
080          gridbag.setConstraints(l,gbc);
081          add(l);
082        }
083        
084        gbarea.gridwidth=GridBagConstraints.REMAINDER;
085    
086        entries = new TextComponent[fields.length];
087    
088        for(i=0;i<fields.length;i++) {
089          l = new Label(fields[i]);
090          gridbag.setConstraints(l,gblabel);
091          add(l);
092          
093          entries[i] = new TextField(values[i],40);
094          entries[i].setEditable(true);
095          gridbag.setConstraints(entries[i],gbarea);
096          add(entries[i]);
097        }
098    
099        gbc.gridheight=1;
100    
101        /* Close the window when OK is checked. */
102        ok = new Button("OK");
103        gridbag.setConstraints(ok,gbc);
104    
105        ok.addActionListener(new ActionListener() {
106          public void actionPerformed(ActionEvent e) {    
107            dispose();
108            synchronized (QueryDialog.this) {
109              QueryDialog.this.notifyAll();
110            }
111          }
112        });
113    
114        add(ok);
115    
116    
117        this.pack();
118        this.show();
119        try {
120          this.wait();
121        } catch (InterruptedException e) {
122        }
123        return this.getValues();
124      }      
125    
126      private String[] getLines(String s) {
127        int i,pos,prev;
128        int lines=countLines(s);
129        String[] ss=new String[lines];
130    
131        i=0;
132        prev=0;
133        pos=s.indexOf('\n');
134        while(pos!=-1) {
135          ss[i]=s.substring(prev,pos);
136          prev=pos+1; // First character after the newline
137          pos=s.indexOf('\n',prev);
138          i++;
139        }
140    
141        ss[i]=s.substring(prev);
142    
143        return ss;
144      }
145    
146      /** Count lines in a string */
147      private int countLines(String s) {
148        char[] c = new char[s.length()];
149        int i,lines=1;
150        
151        s.getChars(0,s.length()-1,c,0);
152        for(i=0;i<c.length;i++) 
153          if(c[i]=='\n') 
154            lines++;
155    
156    
157        return lines;
158      }
159          
160      /** Create a hidden QueryDialog with the appropriate text. */
161      public QueryDialog(String message, String[] fields) {
162        this(message,fields,new String[fields.length]);
163      }
164    
165      public void setValues(String[] newfields) {
166        int i,max;
167    
168        if(fields.length < newfields.length) 
169          max=fields.length;
170        else
171          max=newfields.length;
172        
173        for(i=0;i<max;i++) {
174          fields[i]=newfields[i];
175        }      
176      
177      }
178    
179      /** Return the last entered values for the field. */
180      public String[] getValues() {
181        String[] data=new String[entries.length];
182        int i;
183    
184        for(i=0;i<entries.length;i++)
185          data[i]=entries[i].getText();
186    
187        return data;
188      }
189    
190    }
191        
192    /*
193     * $Log: QueryDialog.java,v $
194     * Revision 1.1.1.1  2002/06/05 21:56:32  root
195     * CS101 comes to Olin finally.
196     *
197     * Revision 1.4  1998/07/24 17:06:30  tparnell
198     * Placate new javadoc behavior
199     *
200     * Revision 1.3  1998/07/22 18:18:40  tparnell
201     * migration from cs101.util to cs101.*
202     *
203     * Revision 1.2  1998/06/03 21:29:02  tparnell
204     * migration from Java 1.0 to 1.1
205     *
206     */