summaryrefslogtreecommitdiff
path: root/java/gjt/QuestionDialog.java
blob: 042b491e178057dc71d546cd124956897d2fcbab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gjt;

import java.awt.*;

/**
 * A dialog that presents a prompt and a TextField into which 
 * a reply may be entered.  Comes complete with an Ok button 
 * and a Cancel button, whose uses will be left to the 
 * imagination.<p>
 *
 * @version 1.0, Apr 1 1996
 * @author  David Geary
 * @see     GJTDialog
 * @see     gjt.test.DialogTest
 */
public class QuestionDialog extends GJTDialog {
    static private int  _defaultTextFieldSize = 20;
    private Button      okButton;
    private Button      cancelButton;
    private String      question;
    private TextField   textField;
    private boolean     wasCancelled;
    private ButtonPanel buttonPanel = new ButtonPanel();

    public QuestionDialog(Frame  frame, DialogClient client,
                          String title, String       question,
                          String initialResponse) {
        this(frame, client, title, question, initialResponse, 
             _defaultTextFieldSize);
    }
    public QuestionDialog(Frame  frame, DialogClient client,
                          String title, String       question) {
        this(frame, client, title,
             question, null, _defaultTextFieldSize);
    }
    public QuestionDialog(Frame  frame, DialogClient client,
                          String title, String       question, 
                          int    textFieldSize) {
        this(frame, client, title, 
             question, null, textFieldSize);
    }
    public QuestionDialog(Frame  frame, DialogClient client, 
                          String title, String       question,
                          String initialResponse, 
                          int textFieldSize) {
        super(frame, title, client, true);

        QuestionPanel questionPanel;

        okButton     = buttonPanel.add("Ok");
        cancelButton = buttonPanel.add("Cancel");

        setLayout(new BorderLayout());
        add("North", questionPanel = 
            new QuestionPanel(this, question, 
                              initialResponse, textFieldSize));
        add("South", buttonPanel);
        textField = questionPanel.getTextField();
        pack();
    }
    public boolean action(Event event, Object what) {
        if(event.target == cancelButton) wasCancelled = true;
        else                             wasCancelled = false;

        hide();
        dispose();
        client.dialogDismissed(this);
        return true;
    }
    public void show() {
        textField.requestFocus();
        super.show();
    }
    public void returnInTextField() {
        okButton.requestFocus();
    }
    public TextField getTextField() {
        return textField;
    }
    public String getAnswer() {
        return textField.getText();
    }
    public boolean wasCancelled() {
        return wasCancelled;
    }
    private void setQuestion(String question) {
        this.question = question;
    }
}

class QuestionPanel extends Panel {
    private TextField      field;
    private QuestionDialog dialog;

    public QuestionPanel(QuestionDialog dialog, 
                         String question) {
        this(dialog, question, null, 0);
    }
    public QuestionPanel(QuestionDialog dialog, String question,
                         int columns) {
        this(dialog, question, null, columns);
    }
    public QuestionPanel(QuestionDialog dialog, String question,
                         String initialResponse, int cols) {
        this.dialog = dialog;
        setLayout(new RowLayout());
        add(new Label(question));

        if(initialResponse != null) {
            if(cols != 0) 
                add(field=new TextField(initialResponse, cols));
            else          
                add(field=new TextField(initialResponse));
        }
        else {
            if(cols != 0) add(field = new TextField(cols));
            else          add(field = new TextField());
        }
    }
    public TextField getTextField() {
        return field;
    }
    public boolean action(Event event, Object what) {
        dialog.returnInTextField();
        return false;
    }
    public Insets insets() {
        return new Insets(10,10,10,10);
    }
}