KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > chateverywhere > InputDialog


1 /* InputDialog
2  * Copyright (C) 1998-2004 Alexis de Bernis <alexis@bernis.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */

19
20 package org.chateverywhere;
21
22 import java.lang.*;
23 import java.awt.*;
24 import java.awt.event.*;
25
26
27 public class InputDialog extends Dialog implements ActionListener
28 {
29     private TextField password;
30     private Button ok, cancel;
31     private boolean data_ok = false;
32     private String JavaDoc data;
33
34     public InputDialog(Frame owner, String JavaDoc title, String JavaDoc text,
35      Color fg_color, Color bg_color, Color c_fg_color, Color c_bg_color)
36     {
37         super(owner, title, true);
38         
39         draw_dialog(text, fg_color, bg_color, c_fg_color, c_bg_color);
40     
41         center_on_screen();
42         this.setVisible(true);
43         password.requestFocus();
44     }
45
46
47     private void draw_dialog(String JavaDoc text,
48      Color fg_color, Color bg_color, Color c_fg_color, Color c_bg_color)
49     {
50         Panel buttons;
51
52         this.setLayout(new GridLayout(3, 1));
53         
54         this.setBackground(bg_color);
55         this.setForeground(fg_color);
56         
57         ok = new Button("OK");
58          ok.addActionListener(this);
59          ok.setForeground(c_fg_color);
60          ok.setBackground(c_bg_color);
61         cancel = new Button("Cancel");
62          cancel.addActionListener(this);
63          cancel.setForeground(c_fg_color);
64          cancel.setBackground(c_bg_color);
65         password = new TextField(10);
66          password.setEchoChar('*');
67          password.addActionListener(this);
68          password.setForeground(c_fg_color);
69          password.setBackground(c_bg_color);
70         buttons = new Panel();
71          buttons.add(ok);
72          buttons.add(cancel);
73         
74         this.add(new Label(text));
75         this.add(password);
76         this.add(buttons);
77
78         setSize(new Dimension(250, 130));
79         validate();
80     }
81
82
83     public void actionPerformed(ActionEvent evt)
84     {
85         if(evt.getSource().equals(ok) || evt.getSource().equals(password)) {
86             data = password.getText();
87             data_ok = true;
88             dispose();
89         }
90
91         if(evt.getSource().equals(cancel)) {
92             data = "";
93             data_ok = false;
94             dispose();
95         }
96     }
97
98     public boolean is_data_ok()
99     {
100         return data_ok;
101     }
102
103     public String JavaDoc get_data()
104     {
105         return data;
106     }
107
108     private void center_on_screen()
109     {
110         Dimension screen;
111         Dimension w = this.getSize();
112         
113 // try {
114
screen = this.getToolkit().getScreenSize();
115 // } catch(Exception e) {
116
// getToolkit() can cause a security exception on IE
117
// screen = new Dimension(800, 600);
118
// }
119

120         /* Center the dialog on the screen */
121         this.setBounds(screen.width/2 - w.width/2,
122                        screen.height/2 - w.height/2,
123                        w.width, w.height);
124     }
125
126
127
128
129 }
130
131
132
Popular Tags