KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > config > GeneralOptionsDialog


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.core.gui.config;
18
19 import java.awt.BorderLayout JavaDoc;
20 import java.awt.Font JavaDoc;
21 import java.awt.GridLayout JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.Properties JavaDoc;
27
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.DefaultComboBoxModel JavaDoc;
30 import javax.swing.JButton JavaDoc;
31 import javax.swing.JCheckBox JavaDoc;
32 import javax.swing.JComboBox JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JDialog JavaDoc;
35 import javax.swing.JFrame JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JOptionPane JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.KeyStroke JavaDoc;
40 import javax.swing.SwingConstants JavaDoc;
41
42 import org.columba.api.gui.frame.IContainer;
43 import org.columba.api.plugin.IExtension;
44 import org.columba.api.plugin.IExtensionHandler;
45 import org.columba.api.plugin.IExtensionHandlerKeys;
46 import org.columba.api.plugin.PluginHandlerNotFoundException;
47 import org.columba.api.plugin.PluginLoadingFailedException;
48 import org.columba.core.config.Config;
49 import org.columba.core.config.GuiItem;
50 import org.columba.core.gui.base.ButtonWithMnemonic;
51 import org.columba.core.gui.base.CheckBoxWithMnemonic;
52 import org.columba.core.gui.base.LabelWithMnemonic;
53 import org.columba.core.gui.base.SingleSideEtchedBorder;
54 import org.columba.core.gui.dialog.FontSelectionDialog;
55 import org.columba.core.gui.frame.FrameManager;
56 import org.columba.core.gui.plugin.ConfigurationDialog;
57 import org.columba.core.gui.themes.ThemeSwitcher;
58 import org.columba.core.gui.util.DialogHeaderPanel;
59 import org.columba.core.gui.util.FontProperties;
60 import org.columba.core.help.HelpManager;
61 import org.columba.core.plugin.PluginManager;
62 import org.columba.core.resourceloader.GlobalResourceLoader;
63 import org.columba.core.xml.XmlElement;
64
65 import com.jgoodies.forms.builder.DefaultFormBuilder;
66 import com.jgoodies.forms.layout.FormLayout;
67
68 /**
69  * Shows a dialog for managing general options such as font settings.
70  */

71
72 public class GeneralOptionsDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
73     private static final String JavaDoc RESOURCE_PATH = "org.columba.core.i18n.dialog";
74
75     // button panel
76
protected JButton JavaDoc okButton;
77
78     protected JButton JavaDoc cancelButton;
79
80     protected JButton JavaDoc helpButton;
81
82     // look and feel
83
protected JLabel JavaDoc lfLabel;
84
85     protected JComboBox JavaDoc lfComboBox;
86
87     protected JButton JavaDoc lfButton;
88
89     private String JavaDoc theme = null;
90
91     private IExtensionHandler handler;
92
93     // fonts
94
protected JCheckBox JavaDoc overwriteCheckBox;
95
96     protected JLabel JavaDoc mainFontLabel;
97
98     protected JLabel JavaDoc textFontLabel;
99
100     protected JButton JavaDoc mainFontButton;
101
102     protected JButton JavaDoc textFontButton;
103
104     private Font JavaDoc mainFont;
105
106     private Font JavaDoc textFont;
107
108     // toolbar
109
protected JLabel JavaDoc toolbarLabel;
110
111     protected JComboBox JavaDoc toolbarComboBox;
112
113     // language
114
protected JLabel JavaDoc languageLabel;
115
116     protected JComboBox JavaDoc languageComboBox;
117
118     protected JFrame JavaDoc frame;
119
120     protected IExtensionHandler configHandler;
121
122     // HTTP proxy
123
protected JLabel JavaDoc proxyLabel;
124
125     protected JButton JavaDoc proxyButton;
126
127     protected String JavaDoc proxyHost;
128
129     protected int proxyPort;
130
131     // ID of configuration plugin of this theme plugin
132
protected String JavaDoc configID;
133
134     private boolean restartRequired = false;
135
136     private String JavaDoc oldLookAndFeelName;
137
138     public GeneralOptionsDialog(JFrame JavaDoc frame) {
139         super(frame, GlobalResourceLoader.getString(RESOURCE_PATH, "general",
140                 "dialog_title"), true);
141
142         this.frame = frame;
143
144         try {
145             // get theme plugin-handler
146
handler = PluginManager.getInstance().getExtensionHandler(
147                     IExtensionHandlerKeys.ORG_COLUMBA_CORE_THEME);
148         } catch (PluginHandlerNotFoundException ex) {
149             ex.printStackTrace();
150         }
151
152         try {
153             // get config plugin-handler
154
configHandler = PluginManager.getInstance().getExtensionHandler(
155                     IExtensionHandlerKeys.ORG_COLUMBA_CORE_CONFIG);
156         } catch (PluginHandlerNotFoundException ex) {
157             ex.printStackTrace();
158         }
159
160         initComponents();
161         layoutComponents();
162         updateComponents(true);
163
164         pack();
165         setLocationRelativeTo(null);
166         setVisible(true);
167     }
168
169     public void updateComponents(boolean b) {
170
171         GuiItem item = ((Config) Config.getInstance()).getOptionsConfig()
172                 .getGuiItem();
173
174         if (b) {
175             // look and feel
176
theme = item.getString(GuiItem.THEME, GuiItem.NAME);
177             oldLookAndFeelName = theme;
178
179             lfComboBox.setSelectedItem(theme);
180
181             // fonts
182
String JavaDoc mainName = item.getStringWithDefault(GuiItem.FONT_MAIN,
183                     GuiItem.NAME, "default");
184             int mainSize = item.getIntegerWithDefault(GuiItem.FONT_MAIN,
185                     GuiItem.SIZE_INT, 12);
186             mainFont = new Font JavaDoc(mainName, Font.PLAIN, mainSize);
187
188             String JavaDoc textName = item.getStringWithDefault(GuiItem.FONT_TEXT,
189                     GuiItem.NAME, "default");
190             int textSize = item.getIntegerWithDefault(GuiItem.FONT_TEXT,
191                     GuiItem.SIZE_INT, 12);
192             textFont = new Font JavaDoc(textName, Font.PLAIN, textSize);
193
194             mainFontButton.setText(mainName);
195             mainFontButton.setFont(mainFont);
196             textFontButton.setText(textName);
197             textFontButton.setFont(textFont);
198
199             boolean overwrite = item.getBooleanWithDefault(GuiItem.FONT,
200                     GuiItem.OVERWRITE_BOOL, false);
201
202             overwriteCheckBox.setSelected(overwrite);
203
204             // enable/disable button, too
205
textFontButton.setEnabled(overwrite);
206             textFontLabel.setEnabled(overwrite);
207             mainFontButton.setEnabled(overwrite);
208             mainFontLabel.setEnabled(overwrite);
209
210             // language
211
Locale JavaDoc[] available = GlobalResourceLoader.getAvailableLocales();
212             languageComboBox.setModel(new DefaultComboBoxModel JavaDoc(available));
213
214             // select Locale in ComboBox
215
for (int i = 0; i < available.length; i++) {
216                 if (available[i].equals(Locale.getDefault())) {
217                     languageComboBox.setSelectedIndex(i);
218                     break;
219                 }
220             }
221
222             boolean enableText = item.getBooleanWithDefault(GuiItem.TOOLBAR,
223                     GuiItem.ENABLE_TEXT_BOOL, true);
224
225             boolean alignment = item.getBooleanWithDefault(GuiItem.TOOLBAR,
226                     GuiItem.TEXT_POSITION_BOOL, true);
227
228             if (enableText) {
229                 toolbarComboBox.setSelectedIndex(alignment ? 1 : 2);
230             }
231
232             proxyHost = System.getProperty("http.proxyHost");
233             String JavaDoc proxyPortString = System.getProperty("http.proxyPort", "-1");
234             proxyPort = Integer.parseInt(proxyPortString);
235             updateProxyButtonText();
236         } else {
237             // fonts
238
item.setString(GuiItem.FONT_MAIN, GuiItem.NAME, getMainFont()
239                     .getName());
240             item.setInteger(GuiItem.FONT_MAIN, GuiItem.SIZE_INT, getMainFont()
241                     .getSize());
242
243             item.setString(GuiItem.FONT_TEXT, GuiItem.NAME, getTextFont()
244                     .getName());
245             item.setInteger(GuiItem.FONT_TEXT, GuiItem.SIZE_INT, getTextFont()
246                     .getSize());
247
248             item.setBoolean(GuiItem.FONT, GuiItem.OVERWRITE_BOOL,
249                     overwriteCheckBox.isSelected());
250
251             // notify all listeners
252
// @see org.columba.core.gui.util.FontProperties
253
// @see org.columba.mail.gui.message.TextViewer
254
// @see org.columba.mail.gui.composer.text.TextEditorController
255
item.getElement("fonts").notifyObservers();
256
257             // look and feel
258
item.setString(GuiItem.THEME, GuiItem.NAME, (String JavaDoc) lfComboBox
259                     .getSelectedItem());
260
261             // remember if Look And Feel has been changed
262
if (!oldLookAndFeelName.equals((String JavaDoc) lfComboBox
263                     .getSelectedItem()))
264                 restartRequired = true;
265
266             // get language configuration
267
XmlElement locale = Config.getInstance().get("options").getElement(
268                     "/options/locale");
269
270             // set language config based on selected item
271
Locale JavaDoc l = (Locale JavaDoc) languageComboBox.getSelectedItem();
272             locale.addAttribute("language", l.getLanguage());
273             locale.addAttribute("country", l.getCountry());
274             locale.addAttribute("variant", l.getVariant());
275
276             int state = toolbarComboBox.getSelectedIndex();
277             if (state == 0) {
278                 item.setBoolean(GuiItem.TOOLBAR, GuiItem.ENABLE_TEXT_BOOL,
279                         false);
280
281             } else {
282                 item
283                         .setBoolean(GuiItem.TOOLBAR, GuiItem.ENABLE_TEXT_BOOL,
284                                 true);
285
286                 item.setBoolean(GuiItem.TOOLBAR, GuiItem.TEXT_POSITION_BOOL,
287                         state == 1 ? true : false);
288
289             }
290
291             XmlElement options = Config.getInstance().get("options")
292                     .getElement("/options");
293
294             XmlElement proxy = options.getElement("proxy");
295             if (proxyHost != null && proxyPort > 0) {
296                 System.setProperty("http.proxyHost", proxyHost);
297                 System.setProperty("http.proxyPort", Integer
298                         .toString(proxyPort));
299                 if (proxy == null) {
300                     proxy = options.addSubElement("proxy");
301                 }
302                 proxy.addAttribute("host", proxyHost);
303                 proxy.addAttribute("port", Integer.toString(proxyPort));
304             } else {
305                 Properties JavaDoc properties = System.getProperties();
306                 properties.remove("http.proxyHost");
307                 properties.remove("http.proxyPort");
308                 if (proxy != null) {
309                     options.removeElement(proxy);
310                 }
311             }
312         }
313     }
314
315     protected void layoutComponents() {
316         JPanel JavaDoc contentPane = new JPanel JavaDoc(new BorderLayout JavaDoc());
317         setContentPane(contentPane);
318
319         // Create a FormLayout instance.
320
FormLayout layout = new FormLayout(
321                 "12dlu, pref, 3dlu, max(40dlu;pref), 3dlu, pref",
322
323                 // 3 columns
324
"");
325
326         // create a form builder
327
DefaultFormBuilder builder = new DefaultFormBuilder(layout);
328
329         // create EmptyBorder between components and dialog-frame
330
builder.setDefaultDialogBorder();
331
332         // skip the first column
333
builder.setLeadingColumnOffset(1);
334
335         // Add components to the panel:
336
builder.appendSeparator(GlobalResourceLoader.getString(RESOURCE_PATH,
337                 "general", "general"));
338         builder.nextLine();
339
340         builder.append(languageLabel);
341         builder.append(languageComboBox, 3);
342         builder.nextLine();
343
344         builder.append(lfLabel, lfComboBox, lfButton);
345         builder.nextLine();
346
347         builder.append(toolbarLabel);
348         builder.append(toolbarComboBox, 3);
349         builder.nextLine();
350
351         builder.append(proxyLabel);
352         builder.append(proxyButton, 3);
353         builder.nextLine();
354
355         builder.appendSeparator(GlobalResourceLoader.getString(RESOURCE_PATH,
356                 "general", "fonts"));
357         builder.nextLine();
358
359         builder.append(overwriteCheckBox, 5);
360         builder.nextLine();
361
362         builder.append(mainFontLabel);
363         builder.append(mainFontButton, 3);
364         builder.nextLine();
365
366         builder.append(textFontLabel);
367         builder.append(textFontButton, 3);
368         builder.nextLine();
369
370         contentPane.add(builder.getPanel(), BorderLayout.CENTER);
371
372         // init bottom panel with OK, Cancel buttons
373
JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(0, 0));
374         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
375
376         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 3, 6, 0));
377         buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
378
379         buttonPanel.add(okButton);
380         buttonPanel.add(cancelButton);
381         buttonPanel.add(helpButton);
382
383         bottomPanel.add(buttonPanel, BorderLayout.EAST);
384         contentPane.add(bottomPanel, BorderLayout.SOUTH);
385
386         getRootPane().setDefaultButton(okButton);
387         getRootPane().registerKeyboardAction(this, "CANCEL",
388                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
389                 JComponent.WHEN_IN_FOCUSED_WINDOW);
390
391         contentPane.add(new DialogHeaderPanel(GlobalResourceLoader.getString(
392                 RESOURCE_PATH, "general", "title_header"), GlobalResourceLoader
393                 .getString(RESOURCE_PATH, "general", "title_description")),
394                 BorderLayout.NORTH);
395     }
396
397     protected void initComponents() {
398         lfLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
399                 RESOURCE_PATH, "general", "look_feel"));
400
401         String JavaDoc[] plugins = handler.getPluginIdList();
402         lfComboBox = new JComboBox JavaDoc(plugins);
403         lfComboBox.setRenderer(new ThemeComboBoxRenderer());
404         lfComboBox.setActionCommand("THEME");
405         lfComboBox.addActionListener(this);
406         lfLabel.setLabelFor(lfComboBox);
407
408         lfButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
409                 RESOURCE_PATH, "general", "look_feel_options"));
410         lfButton.setActionCommand("THEME_OPTIONS");
411         lfButton.addActionListener(this);
412
413         overwriteCheckBox = new CheckBoxWithMnemonic(GlobalResourceLoader
414                 .getString(RESOURCE_PATH, "general", "override_fonts"));
415         overwriteCheckBox.addActionListener(this);
416
417         mainFontLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
418                 RESOURCE_PATH, "general", "main_font"));
419         mainFontButton = new JButton JavaDoc("main font");
420         mainFontButton.addActionListener(this);
421         mainFontLabel.setLabelFor(mainFontButton);
422
423         textFontLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
424                 RESOURCE_PATH, "general", "text_font"));
425         textFontButton = new JButton JavaDoc("text font");
426         textFontButton.addActionListener(this);
427         textFontLabel.setLabelFor(textFontButton);
428
429         toolbarLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
430                 RESOURCE_PATH, "general", "toolbar"));
431         toolbarComboBox = new JComboBox JavaDoc(new String JavaDoc[] {
432                 GlobalResourceLoader.getString(RESOURCE_PATH, "general",
433                         "toolbar_icons"),
434                 GlobalResourceLoader.getString(RESOURCE_PATH, "general",
435                         "toolbar_below"),
436                 GlobalResourceLoader.getString(RESOURCE_PATH, "general",
437                         "toolbar_beside") });
438         toolbarLabel.setLabelFor(toolbarComboBox);
439
440         languageLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
441                 RESOURCE_PATH, "general", "locale"));
442         languageComboBox = new JComboBox JavaDoc();
443         languageLabel.setLabelFor(languageComboBox);
444         languageComboBox.setRenderer(new LocaleComboBoxRenderer());
445
446         proxyLabel = new LabelWithMnemonic(GlobalResourceLoader.getString(
447                 RESOURCE_PATH, "general", "proxy"));
448         proxyButton = new JButton JavaDoc();
449         proxyButton.addActionListener(this);
450         proxyLabel.setLabelFor(proxyButton);
451
452         // button panel
453
okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("",
454                 "global", "ok"));
455         okButton.setActionCommand("OK");
456         okButton.addActionListener(this);
457
458         cancelButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
459                 "", "global", "cancel"));
460         cancelButton.setActionCommand("CANCEL");
461         cancelButton.addActionListener(this);
462
463         helpButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("",
464                 "global", "help"));
465
466         // associate with JavaHelp
467
HelpManager.getInstance().enableHelpOnButton(helpButton,
468                 "configuring_columba_8");
469         HelpManager.getInstance().enableHelpKey(getRootPane(),
470                 "configuring_columba_8");
471     }
472
473     public void actionPerformed(ActionEvent JavaDoc event) {
474         String JavaDoc action = event.getActionCommand();
475
476         if (action.equals("OK")) {
477             
478
479             updateComponents(false);
480
481             // TODO (@author fdietz): until we can get all the settings update
482
// immediately
483
// we just open a message box, telling the user to restart
484

485             if (restartRequired) {
486                 JOptionPane
487                         .showMessageDialog(this,
488                                 "You have to restart Columba for the changes to take effect!");
489
490                 // switch to new theme
491
ThemeSwitcher.setTheme();
492
493             }
494
495             setVisible(false);
496             
497             // notify frame to update
498
IContainer[] m = FrameManager.getInstance().getOpenFrames();
499             for (int i = 0; i < m.length; i++) {
500                 JFrame JavaDoc frame = m[i].getFrame();
501                 ThemeSwitcher.updateFrame(frame);
502             }
503
504             // set fonts
505
FontProperties.setFont();
506
507             
508         } else if (action.equals("CANCEL")) {
509             setVisible(false);
510         } else if (action.equals("THEME")) {
511             // theme selection changed
512
String JavaDoc theme = (String JavaDoc) lfComboBox.getSelectedItem();
513
514             IExtension extension = handler.getExtension(theme);
515
516             configID = extension.getMetadata().getAttribute("config");
517
518             lfButton.setEnabled(configID != null);
519         } else if (action.equals("THEME_OPTIONS")) {
520             try {
521                 ConfigurationDialog dialog = new ConfigurationDialog(configID);
522                 dialog.setVisible(true);
523             } catch (PluginHandlerNotFoundException phnfe) {
524             } catch (PluginLoadingFailedException plfe) {
525             }
526         }
527
528         Object JavaDoc source = event.getSource();
529         if (source == mainFontButton) {
530             FontSelectionDialog fontDialog = new FontSelectionDialog(this, null);
531
532             if (fontDialog.showDialog() == FontSelectionDialog.APPROVE_OPTION) {
533                 mainFont = fontDialog.getSelectedFont();
534                 mainFontButton.setFont(mainFont);
535                 mainFontButton.setText(mainFont.getFontName());
536             }
537         } else if (source == textFontButton) {
538             FontSelectionDialog fontDialog = new FontSelectionDialog(this, null);
539
540             if (fontDialog.showDialog() == FontSelectionDialog.APPROVE_OPTION) {
541                 textFont = fontDialog.getSelectedFont();
542                 textFontButton.setFont(textFont);
543                 textFontButton.setText(textFont.getFontName());
544             }
545         } else if (source == overwriteCheckBox) {
546             boolean enabled = overwriteCheckBox.isSelected();
547             mainFontLabel.setEnabled(enabled);
548             mainFontButton.setEnabled(enabled);
549             textFontLabel.setEnabled(enabled);
550             textFontButton.setEnabled(enabled);
551         } else if (source == proxyButton) {
552             ProxyConfigurationDialog dialog = new ProxyConfigurationDialog(this);
553             dialog.setProxyHost(proxyHost);
554             dialog.setProxyPort(proxyPort);
555             if (dialog.showDialog() == ProxyConfigurationDialog.APPROVE_OPTION) {
556                 proxyHost = dialog.getProxyHost();
557                 proxyPort = dialog.getProxyPort();
558                 updateProxyButtonText();
559             }
560         }
561     }
562
563     protected void updateProxyButtonText() {
564         if (proxyHost != null) {
565             proxyButton.setText(proxyHost + ":" + Integer.toString(proxyPort));
566         } else {
567             proxyButton.setText(GlobalResourceLoader.getString(RESOURCE_PATH,
568                     "proxy", "no_proxy"));
569         }
570     }
571
572     private Font JavaDoc getMainFont() {
573         return mainFont;
574     }
575
576     private Font JavaDoc getTextFont() {
577         return textFont;
578     }
579 }
Popular Tags