1 17 package org.columba.core.gui.config; 18 19 import java.awt.BorderLayout ; 20 import java.awt.Font ; 21 import java.awt.GridLayout ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.KeyEvent ; 25 import java.util.Locale ; 26 import java.util.Properties ; 27 28 import javax.swing.BorderFactory ; 29 import javax.swing.DefaultComboBoxModel ; 30 import javax.swing.JButton ; 31 import javax.swing.JCheckBox ; 32 import javax.swing.JComboBox ; 33 import javax.swing.JComponent ; 34 import javax.swing.JDialog ; 35 import javax.swing.JFrame ; 36 import javax.swing.JLabel ; 37 import javax.swing.JOptionPane ; 38 import javax.swing.JPanel ; 39 import javax.swing.KeyStroke ; 40 import javax.swing.SwingConstants ; 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 71 72 public class GeneralOptionsDialog extends JDialog implements ActionListener { 73 private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog"; 74 75 protected JButton okButton; 77 78 protected JButton cancelButton; 79 80 protected JButton helpButton; 81 82 protected JLabel lfLabel; 84 85 protected JComboBox lfComboBox; 86 87 protected JButton lfButton; 88 89 private String theme = null; 90 91 private IExtensionHandler handler; 92 93 protected JCheckBox overwriteCheckBox; 95 96 protected JLabel mainFontLabel; 97 98 protected JLabel textFontLabel; 99 100 protected JButton mainFontButton; 101 102 protected JButton textFontButton; 103 104 private Font mainFont; 105 106 private Font textFont; 107 108 protected JLabel toolbarLabel; 110 111 protected JComboBox toolbarComboBox; 112 113 protected JLabel languageLabel; 115 116 protected JComboBox languageComboBox; 117 118 protected JFrame frame; 119 120 protected IExtensionHandler configHandler; 121 122 protected JLabel proxyLabel; 124 125 protected JButton proxyButton; 126 127 protected String proxyHost; 128 129 protected int proxyPort; 130 131 protected String configID; 133 134 private boolean restartRequired = false; 135 136 private String oldLookAndFeelName; 137 138 public GeneralOptionsDialog(JFrame frame) { 139 super(frame, GlobalResourceLoader.getString(RESOURCE_PATH, "general", 140 "dialog_title"), true); 141 142 this.frame = frame; 143 144 try { 145 handler = PluginManager.getInstance().getExtensionHandler( 147 IExtensionHandlerKeys.ORG_COLUMBA_CORE_THEME); 148 } catch (PluginHandlerNotFoundException ex) { 149 ex.printStackTrace(); 150 } 151 152 try { 153 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 theme = item.getString(GuiItem.THEME, GuiItem.NAME); 177 oldLookAndFeelName = theme; 178 179 lfComboBox.setSelectedItem(theme); 180 181 String 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 (mainName, Font.PLAIN, mainSize); 187 188 String 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 (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 textFontButton.setEnabled(overwrite); 206 textFontLabel.setEnabled(overwrite); 207 mainFontButton.setEnabled(overwrite); 208 mainFontLabel.setEnabled(overwrite); 209 210 Locale [] available = GlobalResourceLoader.getAvailableLocales(); 212 languageComboBox.setModel(new DefaultComboBoxModel (available)); 213 214 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 proxyPortString = System.getProperty("http.proxyPort", "-1"); 234 proxyPort = Integer.parseInt(proxyPortString); 235 updateProxyButtonText(); 236 } else { 237 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 item.getElement("fonts").notifyObservers(); 256 257 item.setString(GuiItem.THEME, GuiItem.NAME, (String ) lfComboBox 259 .getSelectedItem()); 260 261 if (!oldLookAndFeelName.equals((String ) lfComboBox 263 .getSelectedItem())) 264 restartRequired = true; 265 266 XmlElement locale = Config.getInstance().get("options").getElement( 268 "/options/locale"); 269 270 Locale l = (Locale ) 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 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 contentPane = new JPanel (new BorderLayout ()); 317 setContentPane(contentPane); 318 319 FormLayout layout = new FormLayout( 321 "12dlu, pref, 3dlu, max(40dlu;pref), 3dlu, pref", 322 323 ""); 325 326 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 328 329 builder.setDefaultDialogBorder(); 331 332 builder.setLeadingColumnOffset(1); 334 335 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 JPanel bottomPanel = new JPanel (new BorderLayout (0, 0)); 374 bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP)); 375 376 JPanel buttonPanel = new JPanel (new GridLayout (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 [] plugins = handler.getPluginIdList(); 402 lfComboBox = new JComboBox (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 ("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 ("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 (new String [] { 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 (); 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 (); 449 proxyButton.addActionListener(this); 450 proxyLabel.setLabelFor(proxyButton); 451 452 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 HelpManager.getInstance().enableHelpOnButton(helpButton, 468 "configuring_columba_8"); 469 HelpManager.getInstance().enableHelpKey(getRootPane(), 470 "configuring_columba_8"); 471 } 472 473 public void actionPerformed(ActionEvent event) { 474 String action = event.getActionCommand(); 475 476 if (action.equals("OK")) { 477 478 479 updateComponents(false); 480 481 485 if (restartRequired) { 486 JOptionPane 487 .showMessageDialog(this, 488 "You have to restart Columba for the changes to take effect!"); 489 490 ThemeSwitcher.setTheme(); 492 493 } 494 495 setVisible(false); 496 497 IContainer[] m = FrameManager.getInstance().getOpenFrames(); 499 for (int i = 0; i < m.length; i++) { 500 JFrame frame = m[i].getFrame(); 501 ThemeSwitcher.updateFrame(frame); 502 } 503 504 FontProperties.setFont(); 506 507 508 } else if (action.equals("CANCEL")) { 509 setVisible(false); 510 } else if (action.equals("THEME")) { 511 String theme = (String ) 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 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 getMainFont() { 573 return mainFont; 574 } 575 576 private Font getTextFont() { 577 return textFont; 578 } 579 } | Popular Tags |