001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * PasswordDialog.java
028: *
029: * Created on 7 giugno 2004, 11.18
030: *
031: */
032:
033: package it.businesslogic.ireport.gui;
034:
035: import it.businesslogic.ireport.util.I18n;
036: import java.lang.reflect.InvocationTargetException;
037: import javax.swing.SwingUtilities;
038:
039: /**
040: *
041: * @author Administrator
042: */
043: public class PasswordDialog extends javax.swing.JDialog {
044:
045: private String password = "";
046: private int response = javax.swing.JOptionPane.CANCEL_OPTION;
047:
048: /** Creates new form PasswordDialog */
049: public PasswordDialog(java.awt.Frame parent, boolean modal) {
050: super (parent, modal);
051: initComponents();
052:
053: this .setSize(300, 164);
054: it.businesslogic.ireport.util.Misc.centerFrame(this );
055:
056: javax.swing.KeyStroke escape = javax.swing.KeyStroke
057: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
058: false);
059: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
060: public void actionPerformed(java.awt.event.ActionEvent e) {
061: jButton2ActionPerformed(e);
062: }
063: };
064:
065: getRootPane().getInputMap(
066: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
067: escape, "ESCAPE");
068: getRootPane().getActionMap().put("ESCAPE", escapeAction);
069:
070: applyI18n();
071: //to make the default button ...
072: this .getRootPane().setDefaultButton(this .jButton1);
073:
074: }
075:
076: /** This method is called from within the constructor to
077: * initialize the form.
078: * WARNING: Do NOT modify this code. The content of this method is
079: * always regenerated by the Form Editor.
080: */
081: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
082: private void initComponents() {
083: jPanel1 = new javax.swing.JPanel();
084: jButton1 = new javax.swing.JButton();
085: jLabel1 = new javax.swing.JLabel();
086: jLabel2 = new javax.swing.JLabel();
087: jPasswordField1 = new javax.swing.JPasswordField();
088: jSeparator1 = new javax.swing.JSeparator();
089: jButton2 = new javax.swing.JButton();
090:
091: getContentPane().setLayout(null);
092:
093: setTitle("Database password");
094: setResizable(false);
095: addWindowListener(new java.awt.event.WindowAdapter() {
096: public void windowClosing(java.awt.event.WindowEvent evt) {
097: closeDialog(evt);
098: }
099: });
100:
101: jPanel1.setLayout(null);
102:
103: jPanel1.setMaximumSize(new java.awt.Dimension(290, 130));
104: jButton1.setText("Ok");
105: jButton1.setMaximumSize(new java.awt.Dimension(50, 34));
106: jButton1.setMinimumSize(new java.awt.Dimension(50, 34));
107: jButton1.setPreferredSize(new java.awt.Dimension(50, 34));
108: jButton1.addActionListener(new java.awt.event.ActionListener() {
109: public void actionPerformed(java.awt.event.ActionEvent evt) {
110: jButton1ActionPerformed(evt);
111: }
112: });
113:
114: jPanel1.add(jButton1);
115: jButton1.setBounds(70, 90, 70, 24);
116:
117: jLabel1
118: .setIcon(new javax.swing.ImageIcon(
119: getClass()
120: .getResource(
121: "/it/businesslogic/ireport/icons/password.png")));
122: jPanel1.add(jLabel1);
123: jLabel1.setBounds(10, 0, 90, 80);
124:
125: jLabel2.setText("Password");
126: jPanel1.add(jLabel2);
127: jLabel2.setBounds(90, 20, 130, 14);
128:
129: jPanel1.add(jPasswordField1);
130: jPasswordField1.setBounds(90, 40, 180, 23);
131:
132: jPanel1.add(jSeparator1);
133: jSeparator1.setBounds(10, 80, 260, 2);
134:
135: jButton2.setText("Cancel");
136: jButton2.setPreferredSize(new java.awt.Dimension(50, 34));
137: jButton2.setMaximumSize(new java.awt.Dimension(50, 34));
138: jButton2.setMinimumSize(new java.awt.Dimension(50, 34));
139: jButton2.addActionListener(new java.awt.event.ActionListener() {
140: public void actionPerformed(java.awt.event.ActionEvent evt) {
141: jButton2ActionPerformed(evt);
142: }
143: });
144:
145: jPanel1.add(jButton2);
146: jButton2.setBounds(150, 90, 80, 24);
147:
148: getContentPane().add(jPanel1);
149: jPanel1.setBounds(0, 0, 290, 130);
150:
151: pack();
152: }// </editor-fold>//GEN-END:initComponents
153:
154: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
155: response = javax.swing.JOptionPane.CANCEL_OPTION;
156: password = "";
157: setVisible(false);
158: dispose();
159: }//GEN-LAST:event_jButton2ActionPerformed
160:
161: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
162:
163: response = javax.swing.JOptionPane.OK_OPTION;
164: char chars[] = this .jPasswordField1.getPassword();
165: if (chars != null) {
166: password = new String(chars);
167: } else
168: password = "";
169: chars = null;
170:
171: setVisible(false);
172: dispose();
173:
174: }//GEN-LAST:event_jButton1ActionPerformed
175:
176: /** Closes the dialog */
177: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
178: setVisible(false);
179: dispose();
180: }//GEN-LAST:event_closeDialog
181:
182: /**
183: * @param args the command line arguments
184: */
185: public static void main(String args[]) {
186: new PasswordDialog(new javax.swing.JFrame(), true)
187: .setVisible(true);
188: }
189:
190: /** Getter for property password.
191: * @return Value of property password.
192: *
193: */
194: public java.lang.String getPassword() {
195: return password;
196: }
197:
198: /** Setter for property password.
199: * @param password New value of property password.
200: *
201: */
202: public void setPassword(java.lang.String password) {
203: this .password = password;
204: }
205:
206: /** Getter for property response.
207: * @return Value of property response.
208: *
209: */
210: public int getResponse() {
211: return response;
212: }
213:
214: /** Setter for property response.
215: * @param response New value of property response.
216: *
217: */
218: public void setResponse(int response) {
219: this .response = response;
220: }
221:
222: // Variables declaration - do not modify//GEN-BEGIN:variables
223: private javax.swing.JButton jButton1;
224: private javax.swing.JButton jButton2;
225: private javax.swing.JLabel jLabel1;
226: private javax.swing.JLabel jLabel2;
227: private javax.swing.JPanel jPanel1;
228: private javax.swing.JPasswordField jPasswordField1;
229: private javax.swing.JSeparator jSeparator1;
230:
231: // End of variables declaration//GEN-END:variables
232:
233: public void applyI18n() {
234: // Start autogenerated code ----------------------
235: jButton1
236: .setText(I18n.getString("passwordDialog.button1", "Ok"));
237: jButton2.setText(I18n.getString("passwordDialog.button2",
238: "Cancel"));
239: jLabel2.setText(I18n.getString("passwordDialog.label2",
240: "Password"));
241: // End autogenerated code ----------------------
242: this .setTitle(I18n.getString("passwordDialog.title",
243: "Database password"));
244: jButton1.setMnemonic(I18n.getString(
245: "passwordDialog.button1Mnemonic", "o").charAt(0));
246: jButton2.setMnemonic(I18n.getString(
247: "passwordDialog.button2Mnemonic", "c").charAt(0));
248: }
249:
250: public static String askPassword() {
251: final PasswordDialog pd = new PasswordDialog(
252: it.businesslogic.ireport.gui.MainFrame
253: .getMainInstance(), true);
254:
255: Runnable r = new Runnable() {
256: public void run() {
257: pd.setVisible(true);
258: }
259: };
260:
261: if (!SwingUtilities.isEventDispatchThread()) {
262: try {
263: SwingUtilities.invokeAndWait(r);
264: } catch (InvocationTargetException ex) {
265: ex.printStackTrace();
266: } catch (InterruptedException ex) {
267: ex.printStackTrace();
268: }
269: } else {
270: r.run();
271: }
272:
273: return pd.getPassword();
274:
275: }
276: }
|