001 /* 002 * Copyright (C) 2003 Adam Olsen 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of the GNU General Public License as published by 005 * the Free Software Foundation; either version 1, or (at your option) 006 * any later version. 007 * This program is distributed in the hope that it will be useful, 008 * but WITHOUT ANY WARRANTY; without even the implied warranty of 009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 010 * GNU General Public License for more details. 011 * You should have received a copy of the GNU General Public License 012 * along with this program; if not, write to the Free Software 013 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 014 */ 015 package com.valhalla.jbother; 016 017 import com.valhalla.gui.*; 018 import java.util.*; 019 import java.awt.event.*; 020 import java.awt.*; 021 import javax.swing.*; 022 import java.io.*; 023 import com.valhalla.jbother.*; 024 import com.valhalla.settings.*; 025 import com.valhalla.misc.*; 026 027 /** 028 * Allows a user to edit a profile 029 * 030 * @author synic 031 * @created November 30, 2004 032 */ 033 public class ProfileEditorDialog extends JDialog 034 { 035 private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() ); 036 037 private JPanel main; 038 private JTabbedPane pane = new JTabbedPane(); 039 private JTextField nameField = new JTextField(); 040 private JTextField usernameField = new JTextField( 20 ); 041 private JTextField serverField = new JTextField( 25 ); 042 private JTextField resourceField = new JTextField( 20 ); 043 private JTextField portField = new JTextField( 5 ); 044 private JPasswordField passwordField = new JPasswordField( 20 ); 045 private JCheckBox sslBox = new JCheckBox(); 046 private SettingsProperties settings = new SettingsProperties(); 047 private JCheckBox autoLoginBox = new JCheckBox(); 048 private JCheckBox defaultBox = new JCheckBox(); 049 private JCheckBox reconnectBox = new JCheckBox(); 050 private JButton createButton = new JButton( resources.getString( "createAccountButton" ) ); 051 private JButton saveButton = new JButton( resources.getString( "saveButton" ) ); 052 private JButton cancelButton = new JButton( resources.getString( "cancelButton" ) ); 053 private ProfileEditorDialog thisPointer = this; 054 private File profDir = new File( JBother.settingsDir, "profiles" ); 055 private JPanel innerPanel = new JPanel(); 056 private String origProf = null; 057 private ProfileManager dialog = null; 058 private boolean exitOnClose = false; 059 private boolean isCurrentProfile = false; 060 061 /** 062 * Contructs the ProfileEditorDialog 063 * 064 * @param dialog the ProfileManager dialog that's calling this editor, or <tt>null</tt> if nothing is calling it 065 * @param profile the profile to edit, or <tt>null</tt> if it's a new profile 066 */ 067 public ProfileEditorDialog( ProfileManager dialog, String profile ) 068 { 069 super( dialog, "Profile Editor", true ); 070 this.dialog = dialog; 071 072 origProf = profile; 073 074 main = (JPanel)getContentPane(); 075 main.setLayout( new BorderLayout() ); 076 main.setBorder( BorderFactory.createTitledBorder( resources.getString( "profileEditor" ) ) ); 077 main.add( pane, BorderLayout.CENTER ); 078 pane.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); 079 080 JPanel topPanel = new JPanel(); 081 topPanel.setLayout( new BoxLayout( topPanel, BoxLayout.Y_AXIS ) ); 082 083 JPanel namePanel = new JPanel(); 084 namePanel.setBorder( BorderFactory.createEmptyBorder( 2, 5, 2, 5 ) ); 085 namePanel.setLayout( new BoxLayout( namePanel, BoxLayout.X_AXIS ) ); 086 namePanel.add( new JLabel( resources.getString( "profileName" ) + ": " ) ); 087 namePanel.add( nameField ); 088 089 topPanel.add( namePanel ); 090 091 JPanel defaultPanel = new JPanel(); 092 defaultPanel.setBorder( BorderFactory.createEmptyBorder( 0, 5, 0, 5 ) ); 093 defaultPanel.setLayout( new BoxLayout( defaultPanel, BoxLayout.X_AXIS ) ); 094 defaultPanel.add( new JLabel( resources.getString( "setAsDefault" ) + ": " ) ); 095 defaultPanel.add( defaultBox ); 096 defaultPanel.add( Box.createHorizontalGlue() ); 097 098 topPanel.add( defaultPanel ); 099 100 main.add( topPanel, BorderLayout.NORTH ); 101 102 createAccountPanel(); 103 createOptionsPanel(); 104 105 JPanel buttonPanel = new JPanel(); 106 buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 5, 2, 5 ) ); 107 buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) ); 108 buttonPanel.add( Box.createHorizontalGlue() ); 109 buttonPanel.add( cancelButton ); 110 buttonPanel.add( saveButton ); 111 112 main.add( buttonPanel, BorderLayout.SOUTH ); 113 114 addListeners(); 115 loadProfile( profile ); 116 117 pack(); 118 setLocationRelativeTo( null ); 119 addWindowListener( 120 new WindowAdapter() 121 { 122 public void windowClosing( WindowEvent e ) 123 { 124 cancelHandler(); 125 } 126 } ); 127 } 128 129 /** 130 * Sets whether or not this dialog should exit the application when it's cancel button has been pressed 131 * 132 * @param e true to close the app 133 */ 134 protected void setExitOnClose( boolean e ) 135 { 136 this.exitOnClose = true; 137 } 138 139 public void setIsCurrentProfile( boolean i ) 140 { 141 this.isCurrentProfile = i; 142 } 143 144 /** 145 * @return the defaultBox 146 */ 147 protected JCheckBox getDefaultBox() 148 { 149 return defaultBox; 150 } 151 152 /** 153 * Adds the event listeners to the buttons 154 */ 155 private void addListeners() 156 { 157 PEDialogListener listener = new PEDialogListener(); 158 createButton.addActionListener( listener ); 159 saveButton.addActionListener( listener ); 160 cancelButton.addActionListener( listener ); 161 } 162 163 /** 164 * Handles events 165 * 166 * @author synic 167 * @created November 30, 2004 168 */ 169 class PEDialogListener implements ActionListener 170 { 171 public void actionPerformed( ActionEvent e ) 172 { 173 if( e.getSource() == createButton ) 174 { 175 createAccountHandler(); 176 } 177 else if( e.getSource() == saveButton ) 178 { 179 saveHandler(); 180 } 181 else if( e.getSource() == cancelButton ) 182 { 183 cancelHandler(); 184 } 185 186 } 187 } 188 189 /** 190 * Cancels the dialog, and quits if exitOnClose is set to true 191 */ 192 private void cancelHandler() 193 { 194 dispose(); 195 if( exitOnClose ) 196 { 197 System.exit( 0 ); 198 } 199 } 200 201 /** 202 * Saves the currently opened profile 203 */ 204 private void saveHandler() 205 { 206 try 207 { 208 Standard.setBundle( resources ); 209 Standard.assure( nameField.getText(), "Profile Name" ); 210 Standard.assure( usernameField.getText(), "Username" ); 211 Standard.assure( new String( passwordField.getPassword() ), "Password" ); 212 Standard.assure( resourceField.getText(), "Resource" ); 213 Standard.assure( serverField.getText(), "Server" ); 214 } 215 catch( Exception e ) 216 { 217 com.valhalla.Logger.logException( e ); 218 return; 219 } 220 221 settings.setProperty( "username", usernameField.getText() ); 222 settings.setProperty( "password", new String( passwordField.getPassword() ) ); 223 settings.setProperty( "resource", resourceField.getText() ); 224 settings.setProperty( "defaultServer", serverField.getText() ); 225 settings.setProperty( "port", portField.getText() ); 226 settings.setBoolean( "useSSL", sslBox.isSelected() ); 227 settings.setBoolean( "autoLogin", autoLoginBox.isSelected() ); 228 settings.setBoolean( "reconnectOnDisconnect", reconnectBox.isSelected() ); 229 230 String profile = nameField.getText(); 231 File profileDir = new File( profDir, profile ); 232 233 if( origProf == null ) 234 { 235 // check to see if the profile already exists 236 if( profileDir.exists() ) 237 { 238 Standard.warningMessage( this, resources.getString( "profileEditor" ), resources.getString( "profileExists" ) ); 239 return; 240 } 241 242 profileDir.mkdirs(); 243 } 244 else if( !profile.equals( origProf ) ) 245 { 246 File origProfDir = new File( profDir, origProf ); 247 origProfDir.renameTo( profileDir ); 248 } 249 250 try 251 { 252 settings.saveSettings( profDir.getPath() + File.separatorChar + profile + File.separatorChar + "settings.properties", 253 "JBother Settings File" ); 254 } 255 catch( IOException ex ) 256 { 257 Standard.warningMessage( this, resources.getString( "profileEditor" ), resources.getString( "errorSavingSettings" ) ); 258 return; 259 } 260 261 if( defaultBox.isSelected() ) 262 { 263 ProfileManager.setDefaultProfile( profile ); 264 } 265 266 if( dialog != null ) 267 { 268 dialog.loadProfileList(); 269 } 270 271 if( isCurrentProfile ) 272 { 273 ProfileManager.setCurrentProfile( profile ); 274 Settings.getInstance().loadSettings( profDir.getPath() + File.separatorChar + profile, "settings.properties" ); 275 } 276 277 if( exitOnClose ) 278 { 279 ProfileManager.loadProfile( nameField.getText() ); 280 } 281 282 dispose(); 283 } 284 285 /** 286 * Calls the NewAccoutDialog to create a new account 287 */ 288 private void createAccountHandler() 289 { 290 if( serverField.getText().equals( "" ) ) 291 { 292 Standard.warningMessage( this, "createAccount", resources.getString( "enterNewAccountServer" ) ); 293 return; 294 } 295 296 int port = -1; 297 boolean ssl = sslBox.isSelected(); 298 299 try 300 { 301 port = Integer.parseInt( portField.getText() ); 302 } 303 catch( NumberFormatException e ) 304 { 305 } 306 307 if( port == -1 ) 308 { 309 if( ssl ) 310 { 311 port = 5223; 312 } 313 else 314 { 315 port = 5222; 316 } 317 } 318 319 NewAccountDialog dialog = new NewAccountDialog( this, serverField.getText(), usernameField.getText(), 320 new String( passwordField.getPassword() ), port, ssl ); 321 dialog.getRegistrationInfo(); 322 } 323 324 /** 325 * Sets the username 326 * 327 * @param username The new username value 328 */ 329 public void setUsername( String username ) 330 { 331 usernameField.setText( username ); 332 } 333 334 /** 335 * Sets the password 336 * 337 * @param password The new password value 338 */ 339 public void setPassword( String password ) 340 { 341 passwordField.setText( password ); 342 } 343 344 /** 345 * Creates the Account Panel 346 */ 347 private void createAccountPanel() 348 { 349 AbstractOptionPanel panel = new AbstractOptionPanel(); 350 panel.addComponent( resources.getString( "username" ), usernameField ); 351 passwordField.setFont( usernameField.getFont() ); 352 panel.addComponent( resources.getString( "password" ), passwordField ); 353 panel.addComponent( resources.getString( "resource" ), resourceField ); 354 panel.addComponent( resources.getString( "server" ), serverField ); 355 panel.addComponent( createButton, 1, GridBagConstraints.EAST ); 356 panel.end(); 357 358 pane.add( resources.getString( "account" ), panel ); 359 } 360 361 /** 362 * Creates the options panel 363 */ 364 private void createOptionsPanel() 365 { 366 AbstractOptionPanel panel = new AbstractOptionPanel(); 367 panel.addComponent( resources.getString( "useSsl" ), sslBox ); 368 369 JPanel portPanel = new JPanel( new BorderLayout() ); 370 portPanel.add( portField, BorderLayout.WEST ); 371 portPanel.add( new JLabel( " " + resources.getString( "leaveBlankForDefault" ) ), BorderLayout.CENTER ); 372 panel.addComponent( resources.getString( "logInAutomatically" ), autoLoginBox ); 373 panel.addComponent( resources.getString( "reconnectOnDisconnect" ), reconnectBox ); 374 panel.addComponent( resources.getString( "connectPort" ), portPanel ); 375 panel.end(); 376 377 pane.add( resources.getString( "options" ), panel ); 378 } 379 380 /** 381 * Loads a certain profile 382 * 383 * @param profile the profile to load, or null to create a new one 384 */ 385 private void loadProfile( String profile ) 386 { 387 if( profile != null ) 388 { 389 nameField.setText( profile ); 390 391 try 392 { 393 settings.loadSettings( profDir.getPath() + File.separatorChar + profile + File.separatorChar + "settings.properties" ); 394 } 395 catch( Exception ex ) 396 { 397 return; 398 } 399 } 400 else 401 { 402 // copy the default file in to place 403 InputStream stream = getClass().getClassLoader().getResourceAsStream( "defaultsettings.properties" ); 404 try 405 { 406 settings.load( stream ); 407 } 408 catch( Exception ex ) 409 { 410 return; 411 } 412 } 413 414 usernameField.setText( settings.getProperty( "username" ) ); 415 passwordField.setText( settings.getProperty( "password" ) ); 416 resourceField.setText( settings.getProperty( "resource" ) ); 417 serverField.setText( settings.getProperty( "defaultServer" ) ); 418 portField.setText( settings.getProperty( "port" ) ); 419 420 sslBox.setSelected( settings.getBoolean( "useSSL" ) ); 421 autoLoginBox.setSelected( settings.getBoolean( "autoLogin" ) ); 422 reconnectBox.setSelected( settings.getBoolean( "reconnectOnDisconnect" ) ); 423 424 String defaultProf = ProfileManager.getDefaultProfile(); 425 com.valhalla.Logger.debug( defaultProf ); 426 427 if( defaultProf != null && defaultProf.equals( profile ) ) 428 { 429 defaultBox.setSelected( true ); 430 } 431 } 432 } 433