Source code: com/barteo/emulator/app/ui/swing/SwingDialogWindow.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2002 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.swing;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.Toolkit;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.WindowAdapter;
28 import java.awt.event.WindowEvent;
29
30 import javax.swing.JDialog;
31 import javax.swing.JFrame;
32 import javax.swing.JPanel;
33
34 /**
35 * Uniwersalna klasa sluzaca do wyswietlania okienek dialogowych
36 */
37
38 public class SwingDialogWindow
39 {
40
41 /**
42 * Metoda wywolujaca modalne okienko dialogowe
43 *
44 * @param title tytul okienka
45 * @param panel wnetrze okienka dialogowego
46 * @return true jesli zamkniecie okna zostalo wywolane przez przycisk OK
47 */
48 public static boolean show(JFrame parent, String title, final SwingDialogPanel panel)
49 {
50 final JDialog dialog = new JDialog(parent, title, true);
51 dialog.getContentPane().setLayout(new BorderLayout());
52 dialog.getContentPane().add(panel, BorderLayout.CENTER);
53
54 JPanel actionPanel = new JPanel();
55 actionPanel.add(panel.btOk);
56 actionPanel.add(panel.btCancel);
57 dialog.getContentPane().add(actionPanel, BorderLayout.SOUTH);
58
59 dialog.pack();
60 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
61 Dimension frameSize = dialog.getSize();
62 dialog.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
63
64 ActionListener closeListener = new ActionListener()
65 {
66 public void actionPerformed(ActionEvent event)
67 {
68 if (event.getSource() == panel.btOk) {
69 if (panel.check(true)) {
70 panel.state = true;
71 dialog.setVisible(false);
72 panel.hideNotify();
73 }
74 } else {
75 panel.state = false;
76 dialog.setVisible(false);
77 panel.hideNotify();
78 }
79 }
80 };
81
82 WindowAdapter windowAdapter = new WindowAdapter()
83 {
84 public void windowClosing(WindowEvent e)
85 {
86 panel.state = false;
87 panel.hideNotify();
88 }
89 };
90
91 dialog.addWindowListener(windowAdapter);
92 panel.btOk.addActionListener(closeListener);
93 panel.btCancel.addActionListener(closeListener);
94 panel.showNotify();
95 dialog.setVisible(true);
96 panel.btOk.removeActionListener(closeListener);
97 panel.btCancel.removeActionListener(closeListener);
98
99 return panel.state;
100 }
101
102 }
103