summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/Listing3111.java
blob: 22c3b5e9116b8bba0cc5839c4b9f83f0e41c2efc (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
/* Listing3111.java */

import java.awt.*;
import java.awt.event.*;
import java.util.*;

class ModalDialog
extends Dialog
implements ActionListener
{
  String result;

  public static String OKDialog(Frame owner, String msg)
  {
    ModalDialog dlg;
    dlg = new ModalDialog(owner,"Nachricht",msg,"OK");
    dlg.setVisible(true);
    return dlg.getResult();
  }

  public static String YesNoDialog(Frame owner, String msg)
  {
    ModalDialog dlg;
    dlg = new ModalDialog(owner,"Frage",msg,"Ja,Nein");
    dlg.setVisible(true);
    return dlg.getResult();
  }

  public static String YesNoCancelDialog(Frame owner,String msg)
  {
    ModalDialog dlg;
    dlg = new ModalDialog(owner,"Frage",msg,"Ja,Nein,Abbruch");
    dlg.setVisible(true);
    return dlg.getResult();
  }

  public ModalDialog(
    Frame owner,
    String title,
    String msg,
    String buttons
  )
  {
    super(owner, title, true);
    //Fenster
    setBackground(Color.lightGray);
    setLayout(new BorderLayout());
    setResizable(false);
    Point parloc = owner.getLocation();
    setLocation(parloc.x + 30, parloc.y + 30);
    //Message
    add(new Label(msg), BorderLayout.CENTER);
    //Buttons
    Panel panel = new Panel();
    panel.setLayout(new FlowLayout(FlowLayout.CENTER));
    StringTokenizer strtok = new StringTokenizer(buttons,",");
    while (strtok.hasMoreTokens()) {
      Button button = new Button(strtok.nextToken());
      button.addActionListener(this);
      panel.add(button);
    }
    add(panel, BorderLayout.SOUTH);
    pack();
  }

  public void actionPerformed(ActionEvent event)
  {
    result = event.getActionCommand();
    setVisible(false);
    dispose();
  }

  public String getResult()
  {
    return result;
  }
}

public class Listing3111
extends Frame
implements ActionListener
{
  public static void main(String[] args)
  {
    Listing3111 wnd = new Listing3111();
    wnd.setVisible(true);
  }

  public Listing3111()
  {
    super("Drei modale Standarddialoge");
    setLayout(new FlowLayout());
    setBackground(Color.lightGray);
    Button button = new Button("OKDialog");
    button.addActionListener(this);
    add(button);
    button = new Button("YesNoDialog");
    button.addActionListener(this);
    add(button);
    button = new Button("YesNoCancelDialog");
    button.addActionListener(this);
    add(button);
    setLocation(100,100);
    setSize(400,200);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event)
  {
    String cmd = event.getActionCommand();
    if (cmd.equals("OKDialog")) {
      ModalDialog.OKDialog(this,"Dream, dream, dream, ...");
    } else if (cmd.equals("YesNoDialog")) {
      String ret = ModalDialog.YesNoDialog(
        this,
        "Programm beenden?"
      );
      if (ret.equals("Ja")) {
        setVisible(false);
        dispose();
        System.exit(0);
      }
    } else if (cmd.equals("YesNoCancelDialog")) {
      String msg = "Verzeichnis erstellen?";
      String ret = ModalDialog.YesNoCancelDialog(this,msg);
      ModalDialog.OKDialog(this,"R�ckgabe: " + ret);
    }
  }
}