KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > common > LoginDialog


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.common;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.Dimension JavaDoc;
34 import java.awt.Toolkit JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.FocusEvent JavaDoc;
37 import java.awt.event.KeyEvent JavaDoc;
38 import java.awt.event.WindowEvent JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.InputStream JavaDoc;
43 import java.io.OutputStream JavaDoc;
44
45 import javax.swing.BorderFactory JavaDoc;
46 import javax.swing.JButton JavaDoc;
47 import javax.swing.JDialog JavaDoc;
48 import javax.swing.JFrame JavaDoc;
49 import javax.swing.JLabel JavaDoc;
50 import javax.swing.JPanel JavaDoc;
51 import javax.swing.JPasswordField JavaDoc;
52 import javax.swing.JTextField JavaDoc;
53 import javax.swing.border.Border JavaDoc;
54
55 import com.genimen.djeneric.language.Messages;
56 import com.genimen.djeneric.repository.DjRepositoryDescriptor;
57 import com.genimen.djeneric.repository.exceptions.LogonException;
58 import com.genimen.djeneric.ui.DjVerticalFlowLayout;
59 import com.genimen.djeneric.ui.Util;
60 import com.genimen.djeneric.util.DjEnvironment;
61 import com.genimen.djeneric.util.DjLogger;
62 import com.genimen.djeneric.util.DjProperties;
63
64 public class LoginDialog extends JDialog JavaDoc
65 {
66   private static final long serialVersionUID = 1L;
67   public static String JavaDoc _propertiesFileName = DjEnvironment
68                                                       .getAbsoluteFileName("djeneric credentials.properties");
69   private final static String JavaDoc USER_ID = "logon.userid";
70   private final static String JavaDoc PASSWORD = "logon.password";
71
72   DjProperties _props = new DjProperties();
73
74   JPanel JavaDoc panel1 = new JPanel JavaDoc();
75   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
76   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
77   JPanel JavaDoc jPanel2 = new JPanel JavaDoc();
78   JPanel JavaDoc jPanel3 = new JPanel JavaDoc();
79   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
80   JPanel JavaDoc jPanel4 = new JPanel JavaDoc();
81   JButton JavaDoc _butOk = new JButton JavaDoc();
82   JButton JavaDoc _butCancel = new JButton JavaDoc();
83   JLabel JavaDoc jLabel1 = new JLabel JavaDoc();
84   JLabel JavaDoc jLabel2 = new JLabel JavaDoc();
85   DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout();
86   JTextField JavaDoc _edtUserId = new JTextField JavaDoc();
87   DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout();
88   JPasswordField JavaDoc _edtPassword = new JPasswordField JavaDoc();
89   private boolean _wasCancelled = true;
90   Border JavaDoc border1;
91   JLabel JavaDoc jLabel3 = new JLabel JavaDoc();
92   JLabel JavaDoc _lblRepositoryName = new JLabel JavaDoc();
93   DjRepositoryDescriptor _repository;
94
95   public LoginDialog(JFrame JavaDoc mainFrame, DjRepositoryDescriptor repository) throws LogonException
96   {
97     super(mainFrame, Messages.getString("LoginDialog.EnterCredentials"), true);
98     try
99     {
100       _repository = repository;
101
102       jbInit();
103       _lblRepositoryName.setText(repository.getName());
104       pack();
105       setSize(306, getHeight());
106       Dimension JavaDoc screenSize = Toolkit.getDefaultToolkit().getScreenSize();
107       Dimension JavaDoc frameSize = getSize();
108       setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
109
110       loadProps();
111       _edtUserId.setText(_props.getProperty(USER_ID + getRepositoryPrefix(), ""));
112       _edtPassword.setText(_props.getProperty(PASSWORD + getRepositoryPrefix(), ""));
113
114       setVisible(true);
115     }
116     catch (Exception JavaDoc ex)
117     {
118       throw new LogonException(ex);
119     }
120     if (isCancelled()) throw new LogonException(Messages.getString("LoginDialog.LoginCancelled"));
121   }
122
123   private String JavaDoc getRepositoryPrefix()
124   {
125     return "." + _repository.getName();
126   }
127
128   public String JavaDoc getUserId()
129   {
130     return _edtUserId.getText();
131   }
132
133   public String JavaDoc getPassword()
134   {
135     return new String JavaDoc(_edtPassword.getPassword());
136   }
137
138   void jbInit() throws Exception JavaDoc
139   {
140     border1 = BorderFactory.createEmptyBorder(0, 5, 0, 0);
141     panel1.setLayout(borderLayout1);
142     jPanel3.setLayout(borderLayout2);
143     _butOk.setText(Messages.getString("global.Ok"));
144     _butOk.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
145     {
146       public void keyPressed(KeyEvent JavaDoc e)
147       {
148         _edtUserId_keyPressed(e);
149       }
150     });
151     _butOk.addActionListener(new java.awt.event.ActionListener JavaDoc()
152     {
153       public void actionPerformed(ActionEvent JavaDoc e)
154       {
155         _butOk_actionPerformed(e);
156       }
157     });
158     _butCancel.setText(Messages.getString("global.Cancel"));
159     _butCancel.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
160     {
161       public void keyPressed(KeyEvent JavaDoc e)
162       {
163         _edtUserId_keyPressed(e);
164       }
165     });
166     _butCancel.addActionListener(new java.awt.event.ActionListener JavaDoc()
167     {
168       public void actionPerformed(ActionEvent JavaDoc e)
169       {
170         _butCancel_actionPerformed(e);
171       }
172     });
173     jLabel1.setText(Messages.getString("LoginDialog.UserID"));
174     jLabel2.setText(Messages.getString("global.Password"));
175     jPanel1.setLayout(verticalFlowLayout1);
176     jPanel2.setLayout(verticalFlowLayout2);
177     _edtUserId.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
178     {
179       public void keyPressed(KeyEvent JavaDoc e)
180       {
181         _edtUserId_keyPressed(e);
182       }
183     });
184     _edtPassword.addKeyListener(new java.awt.event.KeyAdapter JavaDoc()
185     {
186       public void keyPressed(KeyEvent JavaDoc e)
187       {
188         _edtUserId_keyPressed(e);
189       }
190     });
191     jLabel3.setText(Messages.getString("LoginDialog.Repository"));
192     _lblRepositoryName.setText(Messages.getString("LoginDialog.RepositoryName"));
193     _edtUserId.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
194     {
195       public void focusGained(FocusEvent JavaDoc e)
196       {
197         _edtUserId_focusGained(e);
198       }
199     });
200     _edtPassword.addFocusListener(new java.awt.event.FocusAdapter JavaDoc()
201     {
202       public void focusGained(FocusEvent JavaDoc e)
203       {
204         _edtPassword_focusGained(e);
205       }
206     });
207     this.addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
208     {
209       public void windowOpened(WindowEvent JavaDoc e)
210       {
211         this_windowOpened(e);
212       }
213     });
214     getContentPane().add(panel1);
215     panel1.add(jPanel1, BorderLayout.WEST);
216     jPanel1.add(jLabel3, null);
217     jPanel1.add(jLabel1, null);
218     jPanel1.add(jLabel2, null);
219     panel1.add(jPanel2, BorderLayout.CENTER);
220     jPanel2.add(_lblRepositoryName, null);
221     jPanel2.add(_edtUserId, null);
222     jPanel2.add(_edtPassword, null);
223     panel1.add(jPanel3, BorderLayout.SOUTH);
224     jPanel3.add(jPanel4, BorderLayout.EAST);
225     jPanel4.add(_butCancel, null);
226     jPanel4.add(_butOk, null);
227     Util.sizeButtons(jPanel4);
228     Util.sizeLabels(getContentPane());
229   }
230
231   void _butOk_actionPerformed(ActionEvent JavaDoc e)
232   {
233     setCancelled(false);
234     saveProps();
235     setVisible(false);
236   }
237
238   void _butCancel_actionPerformed(ActionEvent JavaDoc e)
239   {
240     setVisible(false);
241   }
242
243   private void loadProps()
244   {
245     String JavaDoc absFileName = new File JavaDoc(_propertiesFileName).getAbsoluteFile().toString();
246
247     try
248     {
249       InputStream JavaDoc ios = new FileInputStream JavaDoc(absFileName);
250       _props.load(ios);
251       ios.close();
252
253     }
254     catch (Exception JavaDoc x)
255     {
256       DjLogger.log(x);
257     }
258   }
259
260   private void saveProps()
261   {
262     String JavaDoc absFileName = new File JavaDoc(_propertiesFileName).getAbsoluteFile().toString();
263
264     try
265     {
266       OutputStream JavaDoc os = new FileOutputStream JavaDoc(absFileName);
267
268       _props.setProperty(USER_ID + getRepositoryPrefix(), getUserId());
269       _props.setProperty(PASSWORD + getRepositoryPrefix(), getPassword());
270
271       _props.store(os, Messages.getString("global.CachedLoginCredentials"));
272       os.close();
273     }
274     catch (Exception JavaDoc x)
275     {
276       System.err.println(Messages.getString("LoginDialog.CouldNotWriteUnPw", absFileName));
277     }
278   }
279
280   void _edtUserId_keyPressed(KeyEvent JavaDoc e)
281   {
282     if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null);
283     if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null);
284   }
285
286   void _edtUserId_focusGained(FocusEvent JavaDoc e)
287   {
288     _edtUserId.selectAll();
289   }
290
291   void _edtPassword_focusGained(FocusEvent JavaDoc e)
292   {
293     _edtPassword.selectAll();
294   }
295
296   void this_windowOpened(WindowEvent JavaDoc e)
297   {
298     if (_edtUserId.getText().trim().length() == 0)
299     {
300       _edtUserId.requestFocus();
301     }
302     else
303     {
304       _edtPassword.requestFocus();
305     }
306
307   }
308
309   void setCancelled(boolean _wasCancelled)
310   {
311     this._wasCancelled = _wasCancelled;
312   }
313
314   public boolean isCancelled()
315   {
316     return _wasCancelled;
317   }
318
319 }
Popular Tags