001 /* 002 Copyright (C) 2003 Adam Olsen 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU General Public License as published by 006 the Free Software Foundation; either version 1, or (at your option) 007 any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU General Public License for more details. 013 014 You should have received a copy of the GNU General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 017 */ 018 019 package com.valhalla.jbother.preferences; 020 021 import java.awt.Dimension; 022 import java.awt.GridLayout; 023 import java.awt.event.ActionEvent; 024 import java.awt.event.ActionListener; 025 import java.lang.reflect.Constructor; 026 import java.util.*; 027 028 import javax.swing.BorderFactory; 029 import javax.swing.Box; 030 import javax.swing.BoxLayout; 031 import javax.swing.JButton; 032 import javax.swing.JDialog; 033 import javax.swing.JPanel; 034 035 import com.valhalla.gui.*; 036 import com.valhalla.pluginmanager.*; 037 import com.valhalla.jbother.*; 038 import com.valhalla.settings.*; 039 040 /** 041 * This is the preferences dialog. It is basically a container for the different 042 * PreferencesPanels, which are swapped in and out as the user selects items 043 * from the preferences tree. 044 * @author Adam Olsen 045 * @version 1.0 046 **/ 047 public class PreferencesDialog extends JDialog 048 { 049 private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() ); 050 private PreferencesTree tree; 051 private JPanel container = new JPanel(); 052 private JPanel rightSide = new JPanel(); 053 private JPanel buttonPanel = new JPanel(); 054 private JPanel prefsPanel = new JPanel( new GridLayout( 0, 1 ) ); 055 056 private JButton cancelButton = new JButton( resources.getString( "cancelButton" ) ), 057 okButton = new JButton( resources.getString( "okButton" ) ), 058 applyButton = new JButton( resources.getString( "applyButton" ) ); 059 060 private boolean ok = true; 061 private JPanel currentPrefsPanel[]; 062 private int currentIndex = 0; 063 private PreferencesDialog thisPointer = this; 064 065 /** 066 * Creates the PreferencesDialog 067 */ 068 public PreferencesDialog() 069 { 070 super( BuddyList.getInstance(), "Preferences", false ); 071 setTitle( resources.getString( "preferences" ) ); 072 initComponents(); 073 074 container.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) ); 075 pack(); 076 setLocationRelativeTo( null ); 077 DialogTracker.addDialog( this, false, true ); 078 } 079 080 /** 081 * Sets up the visual components 082 */ 083 private void initComponents() 084 { 085 container.setLayout( new BoxLayout( container, BoxLayout.X_AXIS ) ); 086 087 tree = new PreferencesTree( this ); 088 currentPrefsPanel = new JPanel[tree.getRowCount()]; 089 container.add( tree ); 090 091 rightSide.setLayout( new BoxLayout( rightSide, BoxLayout.Y_AXIS ) ); 092 container.add( rightSide ); 093 094 // set the general preferences panel as the default selected panel 095 currentPrefsPanel[0] = new GeneralPreferencesPanel( this ); 096 prefsPanel.add( currentPrefsPanel[0] ); 097 rightSide.add( prefsPanel ); 098 099 buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) ); 100 buttonPanel.add( Box.createHorizontalGlue() ); 101 buttonPanel.add( cancelButton ); 102 buttonPanel.add( applyButton ); 103 buttonPanel.add( okButton ); 104 105 rightSide.add( Box.createRigidArea( new Dimension( 0, 5 ) ) ); 106 rightSide.add( buttonPanel ); 107 rightSide.setPreferredSize( new Dimension( 520, 400 ) ); 108 109 //set up the handlers 110 PrefsActionHandler handler = new PrefsActionHandler(); 111 cancelButton.addActionListener( handler ); 112 okButton.addActionListener( handler ); 113 applyButton.addActionListener( handler ); 114 115 setContentPane( container ); 116 } 117 118 /** 119 * Listens for events on the different buttons 120 */ 121 class PrefsActionHandler implements ActionListener 122 { 123 public void actionPerformed( ActionEvent e ) 124 { 125 if( e.getSource() == cancelButton ) 126 { 127 DialogTracker.removeDialog( thisPointer ); 128 } 129 130 if( e.getSource() == okButton ) writeSettings(); 131 if( e.getSource() == applyButton ) applyHandler(); 132 } 133 } 134 135 /** 136 * Applies the settings 137 */ 138 private void applyHandler() 139 { 140 PreferencesPanel panel = (PreferencesPanel)currentPrefsPanel[currentIndex]; 141 applySettings( panel.getSettings() ); 142 Standard.noticeMessage( this, resources.getString( "applySettings" ), 143 resources.getString( "settingsHaveBeenApplied" ) ); 144 } 145 146 /** 147 * Applies the specified Settings 148 * @param settings the settings to apply 149 */ 150 private void applySettings( TempSettings settings ) 151 { 152 Iterator iterator = settings.keySet().iterator(); 153 154 while( iterator.hasNext() ) 155 { 156 String key = (String)iterator.next(); 157 158 if( settings.getProperty( key ).equals( "!!REMOVED!!" ) ) 159 Settings.getInstance().setBoolean( key, false ); 160 else Settings.getInstance().setProperty( key, settings.getProperty( key ) ); 161 162 } 163 164 BuddyList.getInstance().updateIcons(); 165 166 // check the away timer 167 if( Settings.getInstance().getBoolean( "autoAway" ) ) 168 BuddyList.getInstance().getAwayTimer().start(); 169 else 170 BuddyList.getInstance().getAwayTimer().stop(); 171 172 } 173 174 /** 175 * Switches the currently displayed preferences panel to the specified one 176 * @param string the panel to switch to 177 */ 178 protected void switchPanel( String string ) 179 { 180 prefsPanel.remove( currentPrefsPanel[currentIndex] ); 181 currentIndex = tree.getSelectedIndex(); 182 183 // if there has already been a panel assigned to the selected index, 184 // use it. 185 if( currentPrefsPanel[currentIndex] != null ) 186 { 187 prefsPanel.add( currentPrefsPanel[currentIndex] ); 188 prefsPanel.repaint(); 189 validate(); 190 return; 191 } 192 193 // otherwise, dynamically get the name of the selected panel and create an instance 194 // matching the name of the class 195 try { 196 Class def = null; 197 198 if( Arguments.getInstance().getProperty( "webstart" ) == null ) 199 { 200 def = PluginLoader.getInstance().loadClass( 201 "com.valhalla.jbother.preferences." + string + "PreferencesPanel" ); 202 } 203 else { 204 def = getClass().getClassLoader().loadClass( 205 "com.valhalla.jbother.preferences." + string + "PreferencesPanel" ); 206 } 207 208 Class[] thisArgsClass = new Class[] { PreferencesDialog.class }; 209 Object[] thisArgs = new Object[] { this }; 210 211 Constructor con = def.getConstructor( thisArgsClass ); 212 currentPrefsPanel[currentIndex] = (JPanel)con.newInstance( thisArgs ); 213 prefsPanel.add( currentPrefsPanel[currentIndex] ); 214 validate(); 215 } 216 catch( ClassNotFoundException ex ) 217 { 218 com.valhalla.Logger.debug( "Cannot find that preferences class." ); 219 } 220 catch( NoSuchMethodException ex ) 221 { 222 com.valhalla.Logger.debug( "Could not find constructor." ); 223 } 224 catch( Exception e ) 225 { 226 com.valhalla.Logger.logException( e ); 227 com.valhalla.Logger.debug( "Could not create preferences class." ); 228 } 229 } 230 231 /** 232 * Writes the settings to the settings file 233 */ 234 private void writeSettings() 235 { 236 for( int i = 0; i < currentPrefsPanel.length; i++ ) 237 { 238 JPanel panel = currentPrefsPanel[i]; 239 if( panel != null ) 240 { 241 PreferencesPanel prefsPanel = (PreferencesPanel)panel; 242 applySettings( prefsPanel.getSettings() ); 243 } 244 } 245 246 DialogTracker.removeDialog( thisPointer ); 247 } 248 249 250 }