Source code: com/barteo/emulator/app/ui/awt/AwtDialogWindow.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2002-2003 Bartek Teodorczyk <barteo@it.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 package com.barteo.emulator.app.ui.awt;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dialog;
24 import java.awt.Dimension;
25 import java.awt.Frame;
26 import java.awt.Panel;
27 import java.awt.Toolkit;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.WindowAdapter;
31 import java.awt.event.WindowEvent;
32
33 /**
34 * Uniwersalna klasa sluzaca do wyswietlania okienek dialogowych
35 */
36
37 public class AwtDialogWindow
38 {
39
40 /**
41 * Metoda wywolujaca modalne okienko dialogowe
42 *
43 * @param title tytul okienka
44 * @param panel wnetrze okienka dialogowego
45 * @return true jesli zamkniecie okna zostalo wywolane przez przycisk OK
46 */
47 public static boolean show(String title, final AwtDialogPanel panel)
48 {
49 final Dialog dialog = new Dialog(new Frame(), title, true);
50 dialog.setLayout(new BorderLayout());
51 dialog.add(panel, BorderLayout.CENTER);
52
53 Panel actionPanel = new Panel();
54 actionPanel.add(panel.btOk);
55 actionPanel.add(panel.btCancel);
56 dialog.add(actionPanel, BorderLayout.SOUTH);
57
58 dialog.pack();
59 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
60 Dimension frameSize = dialog.getSize();
61 dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
62
63 ActionListener closeListener = new ActionListener()
64 {
65 public void actionPerformed(ActionEvent event)
66 {
67 if (event.getSource() == panel.btOk) {
68 if (panel.check(true)) {
69 panel.state = true;
70 dialog.setVisible(false);
71 panel.hideNotify();
72 }
73 } else {
74 panel.state = false;
75 dialog.setVisible(false);
76 panel.hideNotify();
77 }
78 }
79 };
80
81 WindowAdapter windowAdapter = new WindowAdapter()
82 {
83 public void windowClosing(WindowEvent e)
84 {
85 panel.state = false;
86 panel.hideNotify();
87 }
88 };
89
90 dialog.addWindowListener(windowAdapter);
91 panel.btOk.addActionListener(closeListener);
92 panel.btCancel.addActionListener(closeListener);
93 panel.showNotify();
94 dialog.setVisible(true);
95 panel.btOk.removeActionListener(closeListener);
96 panel.btCancel.removeActionListener(closeListener);
97
98 return panel.state;
99 }
100
101 }
102