summaryrefslogtreecommitdiff
path: root/java/gjt/test/DialogTest.java
blob: f92069b7c4966ddf28bfa9cfbd502db005ba6d5d (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
131
132
133
134
135
136
137
138
139
140
package gjt.test;

import java.awt.*;
import java.applet.Applet;

import gjt.Util;
import gjt.DialogClient;
import gjt.MessageDialog;
import gjt.ProgressDialog;
import gjt.QuestionDialog;
import gjt.YesNoDialog;

/**
 * Tests 4 gjt custom dialogs:
 * <dl>
 * <dd> MessageDialog  (a dialog which displays a message)
 * <dd> QuestionDialog (a dialog which asks a question)
 * <dd> YesNoDialog    (a dialog with yes/no buttons)
 * <dd> ProgressDialog (a dialog which records progress of task)
 * </dl>
 * @version 1.0, April 25, 1996
 * @author  David Geary
 * @see     gjt.test.UnitTest
 * @see     gjt.DialogClient
 * @see     gjt.MessageDialog
 * @see     gjt.ProgressDialog;
 * @see     gjt.QuestionDialog;
 * @see     gjt.YesNoDialog;
 */
public class DialogTest extends UnitTest {
    public String title() { 
        return "Graphic Java Toolkit Dialog Test"; 
    }
    public Panel centerPanel() { 
        return new DialogLauncher();
    }
}

class DialogLauncher extends Panel implements DialogClient {
    private MessageDialog  messageDialog;
    private QuestionDialog questionDialog;
    private YesNoDialog    yesNoDialog;
    private ProgressDialog progressDialog;

    private Button messageDialogButton, questionDialogButton,
                   yesNoDialogButton,   progressDialogButton;

    public DialogLauncher() {
        setLayout(new GridLayout(0,1));

        add(messageDialogButton  = 
            new Button("Message Dialog"));

        add(questionDialogButton = 
            new Button("Question Dialog"));

        add(yesNoDialogButton    = 
            new Button("YesNo Dialog"));

        add(progressDialogButton = 
                new Button("Progress Dialog"));
    }
    public boolean action(Event event, Object what) {
        if(event.target == messageDialogButton) {
            messageDialog = MessageDialog.getMessageDialog(
                Util.getFrame(this), this, 
                "Example Message Dialog", 
                "This is an example of a message dialog.");

            messageDialog.show();
        }
        else if(event.target == questionDialogButton) {
            questionDialog = 
                new QuestionDialog(Util.getFrame(this), this, 
                                   "Example Question Dialog", 
                                   "Name:  ", "Gumby", 45);
            questionDialog.show();
        }
        else if(event.target == yesNoDialogButton) {
            yesNoDialog = 
                YesNoDialog.getYesNoDialog(Util.getFrame(this), 
                                       this, 
                                       "Example YesNo Dialog",
                                       "Another cup of Java?");
            yesNoDialog.show();
        }
        else if(event.target == progressDialogButton) {
            progressDialog = 
                ProgressDialog.getProgressDialog(
                    Util.getFrame(this), 
                    "Example Progress Dialog",
                    Color.blue);

            progressDialog.show();

            ProgressThread thread = 
                new ProgressThread(progressDialog);
            thread.start();
        }

        return true;
    }
    public void dialogDismissed(Dialog d) {
        if(d == messageDialog)  {
            System.out.println("MessageDialog Down");
        }
        if(d == questionDialog) {
            if(questionDialog.wasCancelled())
                System.out.println("CANCELLED"); 
            else 
            System.out.println(
                "Name:  " + 
                questionDialog.getTextField().getText());
        }
        if(d == yesNoDialog) {
            if(yesNoDialog.answeredYes())
                System.out.println("YES");
            else
                System.out.println("NO");
        }
    }
}

class ProgressThread extends Thread {
    private ProgressDialog dialog;
    private double         percentComplete = 0;

    public ProgressThread(ProgressDialog dialog) {
        this.dialog = dialog;
    }
    public void run() {
        while(percentComplete <= 100) {
            try { Thread.currentThread().sleep(500); }
            catch(InterruptedException e) { }

            dialog.setPercentComplete(percentComplete);
            percentComplete += 10;
        }
    }
}