KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > mos > forms > preferences > JRegisterUserPanel


1 /*
2     =========================================================================
3     Package preferences - Preferences management panels.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JRegisterUserPanel.java,v 1.13 2007/02/04 03:15:56 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.13 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.mos.forms.preferences;
40
41 import java.awt.*;
42 import javax.swing.*;
43 import javax.swing.tree.*;
44 import javax.swing.event.*;
45 import javax.swing.text.*;
46 import javax.swing.text.html.*;
47 import java.util.*;
48 import java.awt.Dimension JavaDoc;
49 import java.awt.event.*;
50 import javax.swing.event.*;
51 import javax.swing.border.*;
52 import com.jgoodies.forms.builder.*;
53 import com.jgoodies.forms.layout.*;
54 import com.jgoodies.forms.debug.*;
55 import org.planetamessenger.mos.engine.*;
56 import org.planetamessenger.mos.forms.*;
57 import org.planetamessenger.mos.ui.*;
58 import org.planetamessenger.plugin.*;
59 import org.planetamessenger.ui.*;
60
61
62 /**
63  * Multiple purpose management panel.
64  * 1) Register existing user;
65  * 2) Register new user;
66  * 3) Change password for registered user;
67  */

68 public class JRegisterUserPanel extends JPluginPreferencesContainer implements JUserRegistrationListener {
69
70   static public final int MODE_NONE = 0;
71   static public final int MODE_REGISTER_USER = 1;
72   static public final int MODE_REGISTER_NEW_USER = 2;
73   static public final int MODE_CHANGE_PASSWD = 3;
74   
75   private FormLayout layout = null;
76   private PanelBuilder builder = null;
77   private JButton addButton = null;
78   private JTextField userIdText = null;
79   private JTextField nickNameText = null;
80   private JPasswordField pwdField = null;
81   private JPasswordField confPwdField = null;
82   private JLabel pwdLabel = null;
83   private JLabel confPwdLabel = null;
84   private JLabel userIdLabel = null;
85   private JLabel nickNameLabel = null;
86   private JCheckBox autoConnectCheck = null;
87   private JPlugin plugin = null;
88   private int nPluginId = -1;
89   private JMOSPreferencesDlg parentDlg = null;
90   private JExtendedPopupWindow popupWindow = null;
91   private int nMode = MODE_REGISTER_USER;
92
93
94   /**
95    * Constructor. Initialize all class data.
96    * @parentDlg The Options tree of preferences window;
97    */

98   public JRegisterUserPanel( JMOSPreferencesDlg parentDlg ) {
99
100     super();
101
102     CellConstraints cellConst = new CellConstraints();
103     FormLayout layout = new FormLayout( "pref, 4dlu, 90dlu, 4dlu, min", "pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, pref, pref, pref, pref" );
104
105
106     this.parentDlg = parentDlg;
107     builder = new PanelBuilder( layout );
108     addButton = new JButton( "OK" );
109     userIdText = new JTextField();
110     nickNameText = new JTextField();
111     pwdField = new JPasswordField();
112     confPwdField = new JPasswordField();
113     autoConnectCheck = new JCheckBox();
114     pwdLabel = new JLabel();
115     confPwdLabel = new JLabel();
116     userIdLabel = new JLabel();
117     nickNameLabel = new JLabel();
118     
119     userIdLabel.setFont( JSystemFonts.FONT );
120     pwdLabel.setFont( JSystemFonts.FONT );
121     confPwdLabel.setFont( JSystemFonts.FONT );
122     autoConnectCheck.setFont( JSystemFonts.FONT );
123     userIdText.setFont( JSystemFonts.FONT );
124     pwdField.setFont( JSystemFonts.FONT );
125     confPwdField.setFont( JSystemFonts.FONT );
126     nickNameText.setFont( JSystemFonts.FONT );
127     nickNameLabel.setFont( JSystemFonts.FONT );
128
129     addButton.setFont( JSystemFonts.FONT );
130     addButton.setIcon( JSharedObjects.getResources().getIcon( JResources.OK_BUTTON_ICON ) );
131     addButton.setEnabled( false );
132     
133     addButton.addActionListener( new java.awt.event.ActionListener JavaDoc() {
134       public void actionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
135         addButtonActionPerformed( evt );
136       }
137     } );
138
139     userIdText.addKeyListener( new KeyAdapter() {
140       public void keyReleased( java.awt.event.KeyEvent JavaDoc keyEvt ) {
141         
142         boolean bCompareResult;
143         
144         if( nMode == MODE_REGISTER_USER )
145           bCompareResult = ( ( pwdField.getPassword().length > 0 ) && ( confPwdField.getPassword().length > 0 ) && ( userIdText.getText().length() > 0 ) );
146         else
147           bCompareResult = ( ( pwdField.getPassword().length > 0 ) && ( confPwdField.getPassword().length > 0 ) );
148         
149         if( bCompareResult )
150           addButton.setEnabled( true );
151         else
152           addButton.setEnabled( false );
153       }
154     } );
155     
156     pwdField.addKeyListener( userIdText.getKeyListeners()[0] );
157     confPwdField.addKeyListener( userIdText.getKeyListeners()[0] );
158
159     layout.setRowGroups( new int[][] { { 1, 3, 5, 7, 9 } } );
160
161     builder.setDefaultDialogBorder();
162     
163     builder.add( nickNameLabel, cellConst.xy( 1, 1 ) );
164     builder.add( nickNameText, cellConst.xyw( 3, 1, 3 ) );
165     builder.add( userIdLabel, cellConst.xy( 1, 3 ) );
166     builder.add( userIdText, cellConst.xyw( 3, 3, 3 ) );
167     builder.add( pwdLabel, cellConst.xy( 1, 5 ) );
168     builder.add( pwdField, cellConst.xyw( 3, 5, 3 ) );
169     builder.add( confPwdLabel, cellConst.xy( 1, 7 ) );
170     builder.add( confPwdField, cellConst.xyw( 3, 7, 3 ) );
171     builder.add( autoConnectCheck, cellConst.xyw( 1, 9, 5 ) );
172     builder.add( addButton, cellConst.xyw( 1, 11, 3 ) );
173   }
174   
175   /**
176    * Set the plugin id for selected Panel.
177    * @param nPluginId The new plugin id;
178    */

179   public void setPluginId( int nPluginId ) {
180     
181     this.nPluginId = nPluginId;
182     plugin = JSharedObjects.getPluginEngine().get( nPluginId );
183   }
184   
185   /**
186    * Set kind of operation of this panel.
187    * @param nMode The new mode for panel;
188    */

189   public void setMode( int nMode ) {
190     
191     this.nMode = nMode;
192   }
193
194   /**
195    * Initialize the component's panel to show on
196    * preferences window.
197    */

198   public void onShow() {
199
200     if( plugin != null )
201       plugin.removeRegistrationEngineListener( this );
202     
203     if( getComponentCount() == 0 )
204       add( builder.getPanel() );
205     
206     if( nMode == MODE_CHANGE_PASSWD )
207       nickNameText.setText( plugin.getAliasName() );
208     else
209       nickNameText.setText( JSharedObjects.getProfileManager().getProfileName() );
210
211     userIdText.setText( plugin.getUserId() );
212     pwdField.setText( "" );
213     confPwdField.setText( "" );
214     autoConnectCheck.setSelected( false );
215     addButton.setEnabled( false );
216     
217     if( nMode == MODE_CHANGE_PASSWD ) {
218       userIdLabel.setEnabled( false );
219       userIdText.setEnabled( false );
220       nickNameLabel.setEnabled( false );
221       nickNameText.setEnabled( false );
222       autoConnectCheck.setVisible( false );
223       addButton.setText( JSharedObjects.getLanguageManager().getStringEx( "CHANGE" ) );
224     }
225     else {
226       userIdLabel.setEnabled( true );
227       userIdText.setEnabled( true );
228       nickNameLabel.setEnabled( true );
229       nickNameText.setEnabled( true );
230       autoConnectCheck.setVisible( true );
231       addButton.setText( JSharedObjects.getLanguageManager().getStringEx( "ADD" ) );
232     }
233   }
234
235   /**
236    * Called when Apply/Ok button is triggered.
237    * Store the new configuration to PM database.
238    */

239   public boolean onApply() {
240
241     return true;
242   }
243   
244   /**
245    * Called when cancel button is triggered.
246    */

247   public void onCancel() {
248
249   }
250   
251   /**
252    * Called by plugin manager when LAF is changed
253    * by user;
254    */

255   public void onLookAndFeelChanged() {
256
257     javax.swing.SwingUtilities.updateComponentTreeUI( this );
258   }
259   
260   /**
261    * Called by plugin manager when LAF is changed
262    * by user;
263    */

264   public void onLanguageChanged( String JavaDoc strCountryCode ) {
265
266     nickNameLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "NICK_NAME" ) );
267     userIdLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "USER" ) );
268     pwdLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "PASSWORD" ) );
269     confPwdLabel.setText( JSharedObjects.getLanguageManager().getStringEx( "CONFIRM_PASSWORD" ) );
270     autoConnectCheck.setText( JSharedObjects.getLanguageManager().getStringEx( "AUTOCONNECT_STARTUP" ) );
271
272     if( nMode != MODE_CHANGE_PASSWD )
273       addButton.setText( JSharedObjects.getLanguageManager().getStringEx( "ADD" ) );
274     else
275       addButton.setText( JSharedObjects.getLanguageManager().getStringEx( "CHANGE" ) );
276   }
277   
278   // JUserRegistrationListener listener implementation
279
/**
280    * This method listener is called when
281    * a user registration is completed
282    * with sucess.
283    */

284   public void onRegisterUserSucessfull() {
285
286     JUserInfo userInfo = new JUserInfo( userIdText.getText(), userIdText.getText(), new String JavaDoc( pwdField.getPassword() ), JPlugin.STATUS_ONLINE, ( short ) ( autoConnectCheck.isSelected() ? 1 : 0 ) );
287
288     userInfo.addUserData( "profile_id", Long.toString( JSharedObjects.getProfileManager().getProfileId() ) );
289     pwdField.setText( "" );
290     confPwdField.setText( "" );
291     userIdText.setText( "" );
292     autoConnectCheck.setSelected( false );
293
294     JSharedObjects.getProfileManager().userAdd( nPluginId, userInfo );
295     hidePopupWindow();
296     JOptionPane.showMessageDialog( this, JSharedObjects.getLanguageManager().getStringEx( "SUCCESSFULL_REGISTERED_USER_WARNING" ), JSharedObjects.getLanguageManager().getStringEx( "INFORMATION" ), javax.swing.JOptionPane.INFORMATION_MESSAGE );
297     setEnabledControls( true );
298     parentDlg.setParentOfCurrentSelection();
299     parentDlg.fillInstalledPluginsOptionsTree();
300   }
301
302   /**
303    * This method listener is called when
304    * a user registration is failed.
305    */

306   public void onRegisterUserFailed() {
307
308     hidePopupWindow();
309     JOptionPane.showMessageDialog( this, JSharedObjects.getLanguageManager().getStringEx( "ADD_EXISTING_USER_ERROR_WARNING" ), JSharedObjects.getLanguageManager().getStringEx( "ERROR" ), javax.swing.JOptionPane.ERROR_MESSAGE );
310     setEnabledControls( true );
311   }
312   
313   /**
314    * This method is called when a user
315    * unregistration is completed with
316    * success.
317    */

318   public void onUnregisterUserSucessfull() {
319     
320     hidePopupWindow();
321     setEnabledControls( true );
322   }
323   
324   /**
325    * This method listener is called when
326    * a user password is changed with success.
327    * @param strNewPasswd New changed password;
328    */

329   public void onPasswordChangedSuccessfull( String JavaDoc strNewPasswd ) {
330
331     JUserInfo userInfo = JSharedObjects.getProfileManager().getUserInfo( nPluginId );
332     
333     
334     JSharedObjects.getProfileManager().updatePassword( nPluginId, userInfo, strNewPasswd );
335     plugin.setUserInfo( userInfo );
336     hidePopupWindow();
337     pwdField.setText( "" );
338     confPwdField.setText( "" );
339     JOptionPane.showMessageDialog( this, JSharedObjects.getLanguageManager().getStringEx( "PASSWORD_SUCCESSFULL_CHANGED_WARNING" ), JSharedObjects.getLanguageManager().getStringEx( "INFORMATION" ), javax.swing.JOptionPane.INFORMATION_MESSAGE );
340     setEnabledControls( true );
341     parentDlg.setParentOfCurrentSelection();
342     parentDlg.fillInstalledPluginsOptionsTree();
343   }
344   
345   /**
346    * This method listener is called when
347    * a user password is changed is failed.
348    */

349   public void onPasswordChangedFailed() {
350
351     hidePopupWindow();
352     JOptionPane.showMessageDialog( this, JSharedObjects.getLanguageManager().getStringEx( "CHANGE_PASSWORD_ERROR_WARNING" ), JSharedObjects.getLanguageManager().getStringEx( "ERROR" ), javax.swing.JOptionPane.ERROR_MESSAGE );
353     setEnabledControls( true );
354   }
355   
356   /**
357    * Implements the onClick event of add button.
358    * @param evt The button event object;
359    */

360   private void addButtonActionPerformed( java.awt.event.ActionEvent JavaDoc evt ) {
361     
362     String JavaDoc strPwd = new String JavaDoc( pwdField.getPassword() );
363     String JavaDoc strConfPwd = new String JavaDoc( confPwdField.getPassword() );
364     StringBuffer JavaDoc strUserId = new StringBuffer JavaDoc( 255 );
365     boolean bRet = false;
366     
367     
368     if( ( strPwd.compareTo( "" ) != 0 ) && ( strConfPwd.compareTo( "" ) != 0 ) && ( plugin != null ) ) {
369
370       strUserId.insert( 0, userIdText.getText() );
371       
372       if( strPwd.equals( strConfPwd ) ) {
373
374         boolean bShowPopup = true;
375
376         
377         setEnabledControls( false );
378         plugin.addRegistrationEngineListener( this );
379         
380         switch( nMode ) {
381
382           case MODE_REGISTER_NEW_USER : {
383             StringBuffer JavaDoc strNewUserId = new StringBuffer JavaDoc();
384             plugin.fireOnRegisterNewUser( nickNameText.getText(), strNewUserId, strPwd );
385             userIdText.setText( strNewUserId.toString() );
386           } break;
387
388           case MODE_REGISTER_USER :
389             plugin.fireOnRegisterExistingUser( nickNameText.getText(), strUserId.toString(), strPwd ); break;
390
391           case MODE_CHANGE_PASSWD : {
392             bShowPopup = false;
393             plugin.fireOnChangePassword( strPwd );
394           } break;
395         }
396         
397         if( bShowPopup ) {
398           popupWindow = new JExtendedPopupWindow( JSharedObjects.getLanguageManager().getStringEx( "REGISTER_USER" ), JSharedObjects.getLanguageManager().getStringEx( "REGISTERING_USER" ), JSharedObjects.getResources().getIcon( JResources.LOADING_32_ICON ) );
399
400           try {
401             popupWindow.setShowTimeout( JPopupWindow.SHOW_INFINITE );
402             popupWindow.showPopup();
403           } catch( Exception JavaDoc e ) {
404             System.err.println( "JRegisterUserPanel.addButtonActionPerformed - " + e );
405           }
406         }
407       }
408       else {
409         JOptionPane.showMessageDialog( this, JSharedObjects.getLanguageManager().getStringEx( "INVALID_PASSWORD" ), JSharedObjects.getLanguageManager().getStringEx( "ERROR" ), javax.swing.JOptionPane.ERROR_MESSAGE );
410       }
411     }
412   }
413   
414   /**
415    * Hide the popup message window.
416    */

417   private void hidePopupWindow() {
418     
419     if( nMode != MODE_CHANGE_PASSWD ) {
420       try {
421         popupWindow.hidePopup();
422       } catch( Exception JavaDoc e ) {
423         System.err.println( "JRegisterUserPanel.hidePopupWindow - " + e );
424       }
425     }
426     
427     if( plugin != null )
428       plugin.removeRegistrationEngineListener( this );
429   }
430   
431   /**
432    * Enable/Disable all dialog controls;
433    * @param bStatus The controls state:
434    * true All will be enabled;
435    * false All will be disabled;
436    */

437   private void setEnabledControls( boolean bStatus ) {
438     
439     nickNameText.setEnabled( bStatus );
440     userIdText.setEnabled( bStatus );
441     pwdField.setEnabled( bStatus );
442     confPwdField.setEnabled( bStatus );
443     addButton.setEnabled( bStatus );
444     autoConnectCheck.setEnabled( bStatus );
445     parentDlg.EnableComponents( bStatus );
446   }
447 }
448
449 // JProxySettingsPanel class
450
Popular Tags