Package de.kunysch.tvbrowser.localimdb

Source Code of de.kunysch.tvbrowser.localimdb.SettingsDialog

/* $Id: SettingsDialog.java 124 2008-03-06 07:19:25Z bananeweizen $
* GNU GPL Version 2, Copyright (C) 2005 Paul C. Kunysch */
package de.kunysch.tvbrowser.localimdb;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.TitledBorder;

import util.ui.Localizer;
import de.kunysch.tvbrowser.Settings;
import devplugin.SettingsTab;

/** A SettingsDialog shows one or two panels from SettingsTabs. */
public class SettingsDialog extends JDialog {
  private static Localizer mLocalizer = util.ui.Localizer.getLocalizerFor(SettingsDialog.class);

  private static final long serialVersionUID = 1L;

  private final SettingsTab pluginTab;

  private final SettingsTab programTab;

  private final JPanel iconPanel;

  public SettingsDialog(Plugin plugin, JDialog parent) {
    super(parent, mLocalizer.msg("title", "Settings - LocalImdb {0}", new Object[] { plugin.getInfo().getVersion() }),
        true);
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    final Settings settings = SettingsKeys.getSettings();
    final Point location = new Point(settings.getInt(SettingsKeys.SETTINGS_X), settings.getInt(SettingsKeys.SETTINGS_Y));
    SwingUtilities.convertPointFromScreen(location, this);
    setLocation(location);
    this.programTab = getProgramPanelSettingsTab();
    this.pluginTab = plugin.getSettingsTab();
    iconPanel = extractIconPanel();
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(BorderFactory.createEmptyBorder(7, 7, 7, 7));
    setContentPane(contentPane);
    final JPanel settingsPn = pluginTab.createSettingsPanel();
    getContentPane().add(settingsPn, BorderLayout.CENTER);
    if (null != iconPanel) {
      getContentPane().add(iconPanel, BorderLayout.EAST);
    }
    getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
    settingsPn.setPreferredSize(new Dimension(settingsPn.getPreferredSize().width + 20,
        settingsPn.getMinimumSize().height));
    pack();
  }

  /*
   * This code locates the icon panel in new TVBrowser versions. * / private
   * void printComponents(JComponent comp, String indent) { if (null == comp)
   * return; final int count = comp.getComponentCount(); for ( int i = 0; i <
   * count; ++i ) { final Component child = comp.getComponent(i); if (child
   * instanceof JComponent) { JComponent jc = (JComponent)child;
   * System.out.print(indent + i + ": " + jc.getClass().getName()); if
   * (jc.getBorder() instanceof TitledBorder) System.out.print(" '" +
   * ((TitledBorder)jc.getBorder()).getTitle() + "'"); System.out.println("");
   * printComponents(jc, indent + " "); } else { System.out.println(indent + i + ":
   * skipped"); } } } /*
   */

  /**
   * This returns the "Icon-Panel" from the ProgramPanelSettingsTab. If anything
   * goes wrong null is returned.
   */
  private JPanel extractIconPanel() {
    if (null == programTab) {
      return null;
    }
    final JPanel settingsPn = programTab.createSettingsPanel();
    // printComponents(settingsPn, "");
    if (null == settingsPn || 1 != settingsPn.getComponentCount() || !(settingsPn.getComponent(0) instanceof JPanel)) {
      return null;
    }
    final JPanel programBoxPn = (JPanel) settingsPn.getComponent(0);
    if (2 != programBoxPn.getComponentCount() || !(programBoxPn.getComponent(0) instanceof JPanel)) {
      return null;
    }
    JPanel iconPn = (JPanel) programBoxPn.getComponent(0);
    if (2 != iconPn.getComponentCount()) {
      return null;
    }
    if (!(iconPn.getBorder() instanceof TitledBorder) && (iconPn.getComponent(0) instanceof JPanel)) { // TVB
                                                                                                        // >=
                                                                                                        // 2.0
      iconPn = (JPanel) iconPn.getComponent(0);
      if (2 != iconPn.getComponentCount()) {
        return null;
      }
    }
    if (!(((JComponent) iconPn).getBorder() instanceof TitledBorder)) {
      return null;
    }
    TitledBorder border = (TitledBorder) iconPn.getBorder();
    if (!(border.getTitle().equals("Plugin-Icons") //$NON-NLS-1$
    || border.getTitle().equals("Plugin icons"))) {
      return null;
    }
    border.setTitle(border.getTitle() + ":"); //$NON-NLS-1$
    ((JComponent) programBoxPn).remove(iconPn);
    return iconPn;
  }

  /**
   * This returns a ProgramPanelSettingsTab or null.
   */
  private SettingsTab getProgramPanelSettingsTab() {
    try {
      return (SettingsTab) getClass().getClassLoader()
          .loadClass("tvbrowser.ui.settings.ProgramPanelSettingsTab").newInstance(); //$NON-NLS-1$
    } catch (Exception e) {
      return null;
    }
  }

  /**
   * This returns a Panel with an Ok, Cancel and Apply button.
   */
  private JPanel createButtonPanel() {
    JPanel buttonPn = new JPanel(new GridBagLayout());
    final JButton okBtn = new JButton(Localizer.getLocalization(Localizer.I18N_OK));
    final JButton cancelBtn = new JButton(Localizer.getLocalization(Localizer.I18N_CANCEL));
    final JButton applyBtn = new JButton(mLocalizer.msg("apply", "Apply")); //$NON-NLS-1$
    okBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        final Settings settings = SettingsKeys.getSettings();
        settings.setInt(SettingsKeys.SETTINGS_X, getLocationOnScreen().x);
        settings.setInt(SettingsKeys.SETTINGS_Y, getLocationOnScreen().y);
        applySettings();
        dispose();
      }
    });
    cancelBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    applyBtn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        applySettings();
      }
    });
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1.0;
    c.anchor = GridBagConstraints.EAST;
    c.insets.top = 7;
    if (System.getProperty("os.name").startsWith("Mac OS X")) {
      c.insets.bottom = 7;
    }
    buttonPn.add(okBtn, c);
    c.weightx = 0;
    c.insets.left = 7;
    buttonPn.add(cancelBtn, c);
    buttonPn.add(applyBtn, c);
    getRootPane().setDefaultButton(okBtn);
    return buttonPn;
  }

  /**
   * This tells the SettingsTab-Panels to apply their settings.
   */
  private void applySettings() {
    if (null != iconPanel && null != programTab) {
      programTab.saveSettings();
    }
    pluginTab.saveSettings();
  }
}
TOP

Related Classes of de.kunysch.tvbrowser.localimdb.SettingsDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.