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.util.Iterator;
026 import java.util.Locale;
027 import java.util.ResourceBundle;
028 import java.util.TreeMap;
029
030 import javax.swing.BorderFactory;
031 import javax.swing.Box;
032 import javax.swing.BoxLayout;
033 import javax.swing.JButton;
034 import javax.swing.JDialog;
035 import javax.swing.JPanel;
036
037 import com.valhalla.gui.DialogTracker;
038 import com.valhalla.gui.Standard;
039 import com.valhalla.jbother.BuddyList;
040 import com.valhalla.jbother.JBotherLoader;
041 import com.valhalla.settings.Settings;
042 import com.valhalla.settings.TempSettings;
043
044 /**
045 * This is the preferences dialog. It is basically a container for the different
046 * PreferencesPanels, which are swapped in and out as the user selects items
047 * from the preferences tree.
048 *
049 * @author Adam Olsen
050 * @version 1.0
051 */
052 public class PreferencesDialog extends JDialog {
053 private ResourceBundle resources = ResourceBundle.getBundle(
054 "JBotherBundle", Locale.getDefault());
055
056 private PreferencesTree tree;
057
058 private JPanel container = new JPanel();
059
060 private JPanel rightSide = new JPanel();
061
062 private JPanel buttonPanel = new JPanel();
063
064 private JPanel prefsPanel = new JPanel(new GridLayout(0, 1));
065
066 private JButton cancelButton = new JButton(resources
067 .getString("cancelButton")), okButton = new JButton(resources
068 .getString("okButton")), applyButton = new JButton(resources
069 .getString("applyButton"));
070
071 private boolean ok = true;
072
073 private TreeMap panels = new TreeMap();
074
075 private JPanel current;
076
077 private static TreeMap pluginPanels = new TreeMap();
078
079 /**
080 * Creates the PreferencesDialog
081 */
082 public PreferencesDialog() {
083 super(JBotherLoader.getParentFrame(), "Preferences", false);
084 setTitle(resources.getString("preferences"));
085
086 // add the panels
087 current = new GeneralPreferencesPanel(this);
088 panels.put("a01 " + resources.getString("general"), current);
089 panels.put("a02 " + resources.getString("eventPrefs"), new NotificationPreferencesPanel(this));
090 panels.put("a03 " + resources.getString("applications"),
091 new ApplicationsPreferencesPanel(this));
092 panels.put("a04 " + resources.getString("sounds"),
093 new SoundsPreferencesPanel(this));
094 panels.put("a05 " + resources.getString("appearance"),
095 new AppearancePreferencesPanel(this));
096 panels.put("a06 " + resources.getString("tabs"),
097 new TabsPreferencesPanel(this));
098 panels.put("a07 " + resources.getString("privacy"),
099 new PrivacyPreferencesPanel(this));
100 // this is commented because I've upgraded JBother to use smack's
101 // built in file transfer, and I can't see any way of setting a proxy
102 // or anything
103 //panels.put("a08 " + resources.getString("dataTransfer"),
104 // new DataTransferPreferencesPanel( this ) );
105 panels.put("a09 " + resources.getString("pluginsMirrorSettings"),
106 new PluginDownloaderPreferencesPanel( this ));
107 initComponents();
108
109 container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
110 pack();
111 setLocationRelativeTo(null);
112 DialogTracker.addDialog(this, false, true);
113 }
114
115 public static void registerPluginPanel(String name, JPanel panel) {
116 pluginPanels.put(name, panel);
117 }
118
119 public static TreeMap getPluginPanels() {
120 return pluginPanels;
121 }
122
123 public TreeMap getPanels() {
124 return panels;
125 }
126
127 public static void removePluginPanel(String name) {
128 pluginPanels.remove(name);
129 }
130
131 public JPanel getTree() {
132 return tree;
133 }
134
135 /**
136 * Sets up the visual components
137 */
138 private void initComponents() {
139 container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
140
141 tree = new PreferencesTree(this);
142 container.add(tree);
143
144 rightSide.setLayout(new BoxLayout(rightSide, BoxLayout.Y_AXIS));
145 container.add(rightSide);
146
147 // set the general preferences panel as the default selected panel
148 prefsPanel.add(current);
149 rightSide.add(prefsPanel);
150
151 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
152 buttonPanel.add(Box.createHorizontalGlue());
153 buttonPanel.add(cancelButton);
154 buttonPanel.add(applyButton);
155 buttonPanel.add(okButton);
156
157 rightSide.add(Box.createRigidArea(new Dimension(0, 5)));
158 rightSide.add(buttonPanel);
159 rightSide.setPreferredSize(new Dimension(520, 400));
160
161 //set up the handlers
162 PrefsActionHandler handler = new PrefsActionHandler();
163 cancelButton.addActionListener(handler);
164 okButton.addActionListener(handler);
165 applyButton.addActionListener(handler);
166
167 setContentPane(container);
168 }
169
170 /**
171 * Listens for events on the different buttons
172 */
173 class PrefsActionHandler implements ActionListener {
174 public void actionPerformed(ActionEvent e) {
175 if (e.getSource() == cancelButton) {
176 DialogTracker.removeDialog(PreferencesDialog.this);
177 }
178
179 if (e.getSource() == okButton)
180 writeSettings();
181 if (e.getSource() == applyButton)
182 applyHandler();
183 }
184 }
185
186 /**
187 * Applies the settings
188 */
189 private void applyHandler() {
190 PreferencesPanel panel = (PreferencesPanel) current;
191 applySettings(panel.getSettings());
192 Standard.noticeMessage(this, resources.getString("applySettings"),
193 resources.getString("settingsHaveBeenApplied"));
194 }
195
196 /**
197 * Applies the specified Settings
198 *
199 * @param settings
200 * the settings to apply
201 */
202 private void applySettings(TempSettings temp) {
203 Iterator iterator = temp.keySet().iterator();
204
205 while (iterator.hasNext()) {
206 String key = (String) iterator.next();
207
208 if (temp.getProperty(key).equals("!!REMOVED!!"))
209 Settings.getInstance().setBoolean(key, false);
210 else if (temp.getProperty(key).equals("!!EMPTY!!"))
211 Settings.getInstance().remove(key);
212 else
213 Settings.getInstance().setProperty(key, temp.getProperty(key));
214
215 }
216
217 BuddyList.getInstance().updateIcons();
218 }
219
220 /**
221 * Switches the currently displayed preferences panel to the specified one
222 *
223 * @param string
224 * the panel to switch to
225 */
226 protected void switchPanel(String string) {
227 com.valhalla.Logger.debug("Switching to panel " + string);
228 prefsPanel.remove(current);
229
230 JPanel panel = (JPanel) panels.get(string);
231 if (panel == null) {
232 switchToPluginPanel(string);
233 return;
234 }
235
236 prefsPanel.add(panel);
237 current = panel;
238 validate();
239 repaint();
240 }
241
242 private void switchToPluginPanel(String string) {
243 com.valhalla.Logger.debug("Switching to plugin panel " + string);
244 JPanel panel = (JPanel) pluginPanels.get(string);
245 if (panel == null)
246 return;
247 prefsPanel.add(panel);
248 current = panel;
249 validate();
250 repaint();
251 }
252
253 /**
254 * Writes the settings to the settings file
255 */
256 private void writeSettings() {
257 Iterator i = panels.keySet().iterator();
258
259 while (i.hasNext()) {
260 JPanel panel = (JPanel) panels.get(i.next());
261 if (panel != null) {
262 PreferencesPanel prefsPanel = (PreferencesPanel) panel;
263 applySettings(prefsPanel.getSettings());
264 }
265 }
266 JBotherLoader.checkGPG();
267
268 if (pluginPanels.size() > 0) {
269 i = pluginPanels.keySet().iterator();
270
271 while (i.hasNext()) {
272 JPanel panel = (JPanel) pluginPanels.get(i.next());
273 if (panel != null) {
274 PreferencesPanel prefsPanel = (PreferencesPanel) panel;
275 applySettings(prefsPanel.getSettings());
276 }
277 }
278 }
279
280 DialogTracker.removeDialog(this);
281 }
282
283 }