⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configureoption.java

📁 Gmail API for Java 一个gmail信箱的客户端
💻 JAVA
字号:
package siuying.gm.app.gmailer4j.controller;

import java.util.logging.*;
import java.util.prefs.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.Ostermiller.util.Browser;

import siuying.gm.app.gmailer4j.*;
import siuying.gm.app.gmailer4j.ui.*;

public class ConfigureOption
    implements ActionListener, ChangeListener {
  private JFrame parent;
  private GMOptionDialog dialog;
  private Preferences pref;
  private Logger logger = Logger.getLogger(ConfigureOption.class.getName());

  public ConfigureOption(JFrame parent, Preferences pref) {
    this.parent = parent;
    this.pref = pref;

    dialog = new GMOptionDialog(parent, "Option", true);

    /* setup action listeners */
    dialog.btnCancel.addActionListener(this);
    dialog.btnOK.addActionListener(this);
    dialog.chkUseProxy.addActionListener(this);
    dialog.tabbedPane.addChangeListener(this);
    dialog.chkAutoLogon.addActionListener(this);
    dialog.chkAutoMin.addActionListener(this);
    dialog.chkCheckMail.addActionListener(this);
    dialog.panelLAF.btnApply.addActionListener(this);

    loadOption();
  }

  /**
   * add additional action listeners controls
   */
  public void addActionListener(ActionListener l){
    dialog.chkUseProxy.addActionListener(l);
    dialog.chkAutoLogon.addActionListener(l);
    dialog.chkAutoMin.addActionListener(l);
    dialog.chkCheckMail.addActionListener(l);
    dialog.panelLAF.btnApply.addActionListener(l);
    dialog.btnCancel.addActionListener(l);
    dialog.btnOK.addActionListener(l);
  }

  public void applyLAF(){
    dialog.panelLAF.applySelectedLAF();
  }

  public String getSelectedLookAndFeelClassName(){
    return dialog.panelLAF.getSelectedLookAndFeelClassName();
  }

  public String getSelectedThemeClassName(){
    return dialog.panelLAF.getSelectedThemeClassName();
  }

  public void updateComponentTreeUI(){
    SwingUtilities.updateComponentTreeUI(dialog.panelLAF);
    SwingUtilities.updateComponentTreeUI(dialog.panelGeneral);
    SwingUtilities.updateComponentTreeUI(dialog.panelAccount);
    SwingUtilities.updateComponentTreeUI(dialog.panelConnection);
    SwingUtilities.updateComponentTreeUI(dialog.tabbedPane);
    SwingUtilities.updateComponentTreeUI(dialog.getContentPane());
    SwingUtilities.updateComponentTreeUI(dialog);

    dialog.pack();
  }

  private void loadOption(){
    // load preference
    dialog.txtUser.setText(pref.node("Accounts").get("username", "Username"));
    dialog.txtPassword.setText(pref.node("Accounts").get("password", ""));
    dialog.txtServer.setText(pref.node("Proxy").get("server", ""));
    dialog.txtPort.setText(pref.node("Proxy").get("port", ""));
    dialog.chkUseProxy.getModel().setSelected(pref.node("Proxy").getBoolean("enabled", false));
    dialog.chkAutoLogon.getModel().setSelected(pref.node("General").getBoolean("autolog", false));
    dialog.chkAutoMin.getModel().setSelected(pref.node("General").getBoolean("automin", false));
    dialog.chkCheckMail.getModel().setSelected(pref.node("General").getBoolean("checkmail", false));
    ((SpinnerNumberModel)dialog.spiMinutes.getModel()).setValue(new Integer(pref.node("General").getInt("checkinterval", 10)));

    String lookandfeel = pref.node("LAF").get("Look and Feel", null);
    String theme = pref.node("LAF").get("Theme", null);

    if (lookandfeel != null){
      // set look and feel etxt
      try {
        LookAndFeel laf = (LookAndFeel) Class.forName(
            lookandfeel).newInstance();
        dialog.panelLAF.setSelectedLookAndFeel( laf);
      }
      catch (Exception ex) {
      }
    }else{
      dialog.panelLAF.selectDefaultLAF();
    }

    if (theme != null){
      // set theme text
      try {
        dialog.panelLAF.setSelectedTheme( theme );
        logger.warning("LAF THEME: " + theme );
      }
      catch (Exception ex) {
        ex.printStackTrace();
        logger.warning("WARNING LAF THEME: " + theme + ", " + ex.getClass());
      }
    }else{
      dialog.panelLAF.selectDefaultTheme();
    }

    updateLogonSetting();
    updateChkProxySetting();
    updateChkPeriod();
  }

  private void saveOption(){
    Runnable task = new Runnable (){
      public void run(){
        // save preference
        pref.node("Accounts").put("username", dialog.txtUser.getText());
        pref.node("Accounts").put("password",
                                  new String(dialog.txtPassword.getPassword()));
        pref.node("Proxy").put("server", dialog.txtServer.getText());
        pref.node("Proxy").put("port", dialog.txtPort.getText());
        pref.node("Proxy").putBoolean("enabled",
                                      dialog.chkUseProxy.getModel().
                                      isSelected());
        pref.node("General").putBoolean("autolog",
                                        dialog.chkAutoLogon.getModel().
                                        isSelected());
        pref.node("General").putBoolean("automin",
                                        dialog.chkAutoMin.getModel().
                                        isSelected());
        pref.node("General").putBoolean("checkmail",
                                        dialog.chkCheckMail.getModel().
                                        isSelected());
        pref.node("General").putInt("checkinterval", ((Integer)((SpinnerNumberModel)dialog.spiMinutes.getModel()).getValue()).intValue());

        String selectedLAF = dialog.panelLAF.getSelectedLookAndFeelClassName();
        String selectedTheme = dialog.panelLAF.getSelectedThemeClassName();

        if (selectedLAF != null){
          pref.node("LAF").put("Look and Feel",
                               dialog.panelLAF.getSelectedLookAndFeelClassName());
        }else{
          pref.node("LAF").put("Look and Feel", "");
        }

        if (selectedTheme != null){
          pref.node("LAF").put("Theme",
                               dialog.panelLAF.getSelectedThemeClassName());
        }else{
          pref.node("LAF").put("Theme", "");
        }

        Browser.save(pref);
        
        try {
          pref.sync();
          pref.flush();
        }
        catch (Exception e) {
          // warn user
        }
      }
    };
    try{
        ThreadUtils.execute(task);
    }catch(InterruptedException ie){}
  }

  /**
   * Open the option pane, wait for user input
   */
  public void openDialog(){
    loadOption();

    /* centre the option pane */
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension dialogSize = dialog.getSize();
    if (dialogSize.height > screenSize.height) {
      dialogSize.height = screenSize.height;
    }
    if (dialogSize.width > screenSize.width) {
      dialogSize.width = screenSize.width;
    }
    dialog.setLocation( (screenSize.width - dialogSize.width) / 2,
                             (screenSize.height - dialogSize.height) / 2);
    dialog.tabbedPane.setSelectedIndex(0);

    // show the option dialog
    dialog.show();
  }

  /**
   * actionPerformed
   *
   * @param e ActionEvent
   */
  public void actionPerformed(ActionEvent e) {
    // get the string of action command,
    // with intern() we can use == to compare string
    String command = e.getActionCommand().intern();

    if (command == "Use HTTP proxy") {
      // if the check box is clicked, update the enable status of the releted components
      updateChkProxySetting();
    }
    else if (command == "Option Cancel") {
      dialog.hide();
    }
    else if (command == "Option OK") {
      // check if logon setting filled in correctly
      if ( !checkLogonSetting() )
        return;

      // check if proxy setting filled in correctly
      if ( !checkProxySetting() )
        return;
      
      Browser.userOKedPanelChanges();
      
      // save data ...
      saveOption();

      // then hide
      dialog.hide();
    }else if (command == "Check Every"){
      updateChkPeriod();
    }else if (command == "Start Min"){

    }else if (command == "Check Every"){

    }
  }

  /**
   * stateChanged
   *
   * @param e ChangeEvent
   */
  public void stateChanged(ChangeEvent e) {
    if (e.getSource() instanceof JTabbedPane){
      int selected = ((JTabbedPane)e.getSource()).getSelectedIndex();

      // if not selecting "proxy" tab
      if (selected != 2){
        // check if proxy setting s missing
        checkProxySetting();
      }
    }
  }

  private boolean checkProxySetting(){
    // check if missing proxy info
    if (dialog.chkUseProxy.getModel().isSelected() &&
        (dialog.txtServer.getText().equals("") ||
           dialog.txtPort.getText().equals(""))) {
        // warn user must enter something
        Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(dialog,
                                      "You must enter proxy server and port!",
                                      "Missing information",
                                      JOptionPane.WARNING_MESSAGE);

        // force showing proxy tab
        dialog.tabbedPane.setSelectedIndex(2);
        return false;
    }
    return true;
  }

  private void updateLogonSetting(){
    if ( dialog.txtPassword.equals("") ||
         dialog.txtUser.equals("Username")){
      JOptionPane.showMessageDialog(dialog,
                                    "Logon info is not entered, please enter it in Tools > Option > Account!",
                                    "Missing information",
                                    JOptionPane.WARNING_MESSAGE);
    }
  }

  private void updateChkProxySetting(){
      if (dialog.chkUseProxy.getModel().isSelected()) {
        // enable components related to proxy
        dialog.lblAddress.setEnabled(true);
        dialog.lblPort.setEnabled(true);
        dialog.txtServer.setEnabled(true);
        dialog.txtPort.setEnabled(true);
      }
      else {
        // disable components related to proxy
        dialog.lblAddress.setEnabled(false);
        dialog.lblPort.setEnabled(false);
        dialog.txtServer.setEnabled(false);
        dialog.txtPort.setEnabled(false);
      }
  }

  private void updateChkPeriod() {
    if (dialog.chkCheckMail.isSelected()) {
      dialog.spiMinutes.setEnabled(true);
      //dialog.lblMinutes.setEnabled(true);

    }
    else {
      dialog.spiMinutes.setEnabled(false);
      //dialog.lblMinutes.setEnabled(false);

    }
  }

  private boolean checkLogonSetting(){
    char[] password = dialog.txtPassword.getPassword();
    // check if missing proxy info
    if (password.length == 0 ||
        dialog.txtUser.getText().equals("")){
      Toolkit.getDefaultToolkit().beep();
      JOptionPane.showMessageDialog(dialog,
                                    "You must enter user name and password!",
                                    "Missing information",
                                    JOptionPane.WARNING_MESSAGE);
      dialog.tabbedPane.setSelectedIndex(1);
      return false;
    }
    return true;
  }

  public JDialog getDialog(){
    return dialog;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -