KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > PasswordDialog


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * PasswordDialog.java
28  *
29  * Created on 7 giugno 2004, 11.18
30  *
31  */

32
33 package it.businesslogic.ireport.gui;
34 import it.businesslogic.ireport.util.I18n;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37
38 /**
39  *
40  * @author Administrator
41  */

42 public class PasswordDialog extends javax.swing.JDialog JavaDoc {
43     
44     private String JavaDoc password = "";
45     private int response = javax.swing.JOptionPane.CANCEL_OPTION;
46     /** Creates new form PasswordDialog */
47     public PasswordDialog(java.awt.Frame JavaDoc parent, boolean modal) {
48         super(parent, modal);
49         initComponents();
50         
51         this.setSize( 300, 164 );
52         it.businesslogic.ireport.util.Misc.centerFrame(this);
53         
54         
55         javax.swing.KeyStroke JavaDoc escape = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
56         javax.swing.Action JavaDoc escapeAction = new javax.swing.AbstractAction JavaDoc() {
57             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
58                 jButton2ActionPerformed(e);
59             }
60         };
61        
62         getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
63         getRootPane().getActionMap().put("ESCAPE", escapeAction);
64
65         applyI18n();
66         //to make the default button ...
67
this.getRootPane().setDefaultButton(this.jButton1);
68         
69     }
70     
71     /** This method is called from within the constructor to
72      * initialize the form.
73      * WARNING: Do NOT modify this code. The content of this method is
74      * always regenerated by the Form Editor.
75      */

76     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
77
private void initComponents() {
78         jPanel1 = new javax.swing.JPanel JavaDoc();
79         jButton1 = new javax.swing.JButton JavaDoc();
80         jLabel1 = new javax.swing.JLabel JavaDoc();
81         jLabel2 = new javax.swing.JLabel JavaDoc();
82         jPasswordField1 = new javax.swing.JPasswordField JavaDoc();
83         jSeparator1 = new javax.swing.JSeparator JavaDoc();
84         jButton2 = new javax.swing.JButton JavaDoc();
85
86         getContentPane().setLayout(null);
87
88         setTitle("Database password");
89         setResizable(false);
90         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
91             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
92                 closeDialog(evt);
93             }
94         });
95
96         jPanel1.setLayout(null);
97
98         jPanel1.setMaximumSize(new java.awt.Dimension JavaDoc(290, 130));
99         jButton1.setText("Ok");
100         jButton1.setMaximumSize(new java.awt.Dimension JavaDoc(50, 34));
101         jButton1.setMinimumSize(new java.awt.Dimension JavaDoc(50, 34));
102         jButton1.setPreferredSize(new java.awt.Dimension JavaDoc(50, 34));
103         jButton1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
104             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
105                 jButton1ActionPerformed(evt);
106             }
107         });
108
109         jPanel1.add(jButton1);
110         jButton1.setBounds(70, 90, 70, 24);
111
112         jLabel1.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/password.png")));
113         jPanel1.add(jLabel1);
114         jLabel1.setBounds(10, 0, 90, 80);
115
116         jLabel2.setText("Password");
117         jPanel1.add(jLabel2);
118         jLabel2.setBounds(90, 20, 130, 14);
119
120         jPanel1.add(jPasswordField1);
121         jPasswordField1.setBounds(90, 40, 180, 23);
122
123         jPanel1.add(jSeparator1);
124         jSeparator1.setBounds(10, 80, 260, 2);
125
126         jButton2.setText("Cancel");
127         jButton2.setPreferredSize(new java.awt.Dimension JavaDoc(50, 34));
128         jButton2.setMaximumSize(new java.awt.Dimension JavaDoc(50, 34));
129         jButton2.setMinimumSize(new java.awt.Dimension JavaDoc(50, 34));
130         jButton2.addActionListener(new java.awt.event.ActionListener JavaDoc() {
131             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
132                 jButton2ActionPerformed(evt);
133             }
134         });
135
136         jPanel1.add(jButton2);
137         jButton2.setBounds(150, 90, 80, 24);
138
139         getContentPane().add(jPanel1);
140         jPanel1.setBounds(0, 0, 290, 130);
141
142         pack();
143     }// </editor-fold>//GEN-END:initComponents
144

145     private void jButton2ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton2ActionPerformed
146
response = javax.swing.JOptionPane.CANCEL_OPTION;
147          password="";
148         setVisible(false);
149         dispose();
150     }//GEN-LAST:event_jButton2ActionPerformed
151

152     private void jButton1ActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jButton1ActionPerformed
153

154         response = javax.swing.JOptionPane.OK_OPTION;
155         char chars[] = this.jPasswordField1.getPassword();
156         if (chars != null)
157         {
158             password = new String JavaDoc(chars);
159         }
160         else
161             password = "";
162         chars = null;
163         
164         setVisible(false);
165         dispose();
166         
167     }//GEN-LAST:event_jButton1ActionPerformed
168

169     /** Closes the dialog */
170     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
171
setVisible(false);
172         dispose();
173     }//GEN-LAST:event_closeDialog
174

175     /**
176      * @param args the command line arguments
177      */

178     public static void main(String JavaDoc args[]) {
179         new PasswordDialog(new javax.swing.JFrame JavaDoc(), true).setVisible(true);
180     }
181     
182     /** Getter for property password.
183      * @return Value of property password.
184      *
185      */

186     public java.lang.String JavaDoc getPassword() {
187         return password;
188     }
189     
190     /** Setter for property password.
191      * @param password New value of property password.
192      *
193      */

194     public void setPassword(java.lang.String JavaDoc password) {
195         this.password = password;
196     }
197     
198     /** Getter for property response.
199      * @return Value of property response.
200      *
201      */

202     public int getResponse() {
203         return response;
204     }
205     
206     /** Setter for property response.
207      * @param response New value of property response.
208      *
209      */

210     public void setResponse(int response) {
211         this.response = response;
212     }
213     
214     // Variables declaration - do not modify//GEN-BEGIN:variables
215
private javax.swing.JButton JavaDoc jButton1;
216     private javax.swing.JButton JavaDoc jButton2;
217     private javax.swing.JLabel JavaDoc jLabel1;
218     private javax.swing.JLabel JavaDoc jLabel2;
219     private javax.swing.JPanel JavaDoc jPanel1;
220     private javax.swing.JPasswordField JavaDoc jPasswordField1;
221     private javax.swing.JSeparator JavaDoc jSeparator1;
222     // End of variables declaration//GEN-END:variables
223

224     public void applyI18n(){
225                 // Start autogenerated code ----------------------
226
jButton1.setText(I18n.getString("passwordDialog.button1","Ok"));
227                 jButton2.setText(I18n.getString("passwordDialog.button2","Cancel"));
228                 jLabel2.setText(I18n.getString("passwordDialog.label2","Password"));
229                 // End autogenerated code ----------------------
230
}
231
232     public static String JavaDoc askPassword()
233     {
234         final PasswordDialog pd = new PasswordDialog(it.businesslogic.ireport.gui.MainFrame.getMainInstance(), true);
235         
236         Runnable JavaDoc r = new Runnable JavaDoc() {
237                 public void run() {
238                     pd.setVisible(true);
239                 }
240             };
241
242         if (!SwingUtilities.isEventDispatchThread())
243         {
244             try {
245                 SwingUtilities.invokeAndWait( r );
246             } catch (InvocationTargetException JavaDoc ex) {
247                 ex.printStackTrace();
248             } catch (InterruptedException JavaDoc ex) {
249                 ex.printStackTrace();
250             }
251         }
252         else
253         {
254                 r.run();
255         }
256         
257         return pd.getPassword();
258         
259     }
260 }
261
Popular Tags