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

📄 lafpanel.java

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

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import com.jgoodies.plaf.*;
import java.util.List;
import java.util.*;
import java.util.logging.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.plaf.metal.MetalTheme;
import siuying.gm.app.gmailer4j.ThreadUtils;

/**
 * A Look and Feel Chooser Component
 */
public class LAFPanel
    extends JPanel {
  private DefaultListModel lookAndFeelListModel;
  public JButton btnApply = new JButton("Apply");
  private Logger logger = Logger.getLogger(LAFPanel.class.getName());
  private TreeMap lookAndFeelClassList;
  private List installedThemes;
  private Vector installedThemesName;
  private ActionListener action;
  private ListSelectionListener selectListener;

  private JPanel panelCombo;
  private JPanel panelButtons;

  private FlowLayout flowLayout2 = new FlowLayout();
  private BorderLayout borderLayout1 = new BorderLayout();
  private Frame root;
  private JList listLookAndFeel = new JList();
  private JList listTheme = new JList();

  private GridLayout gridLayout1 = new GridLayout();
  private Border border1;
  private Border border2;
  private Border border3;
  private Border border4;
  private TitledBorder titledBorder1;
  private Border border5;
  private TitledBorder titledBorder2;
  private Border border6;
  private JPanel jPanel1 = new JPanel();
  private JScrollPane jScrollPane1 = new JScrollPane();
  private BorderLayout borderLayout2 = new BorderLayout();
  private Border border7;
  private JPanel jPanel2 = new JPanel();
  private BorderLayout borderLayout3 = new BorderLayout();
  private JScrollPane jScrollPane2 = new JScrollPane();
  private Border border8;


  public LAFPanel(Frame root) {
    super();
    lookAndFeelListModel = new DefaultListModel();
    listLookAndFeel = new JList(lookAndFeelListModel);
    lookAndFeelClassList = new TreeMap();
    installedThemes = new ArrayList();

    this.root = root;

    init();

    try {
      jbInit();
      validate();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    initActions();
  }

  public void applySelectedLAF(){
    String selected = getSelectedLookAndFeelClassName();
    String selectedTheme = getSelectedThemeClassName();
    if (selected == null) {
      return;
    }

    // apply look and feel
    try {
      if (selectedTheme != null) {
        LookUtils.setLookAndTheme( (LookAndFeel) Class.forName(selected).
                                  newInstance(),
                                  Class.forName(selectedTheme).
                                  newInstance());
      }

      UIManager.setLookAndFeel(selected);
    }
    catch (Exception ex) {
      ex.printStackTrace();
      logger.info("Failed setting laf ... " + ex.getClass());
    }
  }

  private void initActions() {
    selectListener = new ListSelectionListener(){
      public void valueChanged(ListSelectionEvent e) {
        if (e.getSource().equals(listLookAndFeel)){
          selectedLAF(e.getFirstIndex());
        }
      }
    };

    listLookAndFeel.addListSelectionListener(selectListener);
    listTheme.addListSelectionListener(selectListener);
  }

  public void refreshTheme(){
    String className = this.getSelectedLookAndFeelClassName();
    // generate list of avaliable themes for JGoodies LAF
    LookAndFeel laf = null;
    try {
      laf = (LookAndFeel) Class.forName(className).newInstance();

      installedThemes = LookUtils.getInstalledThemes(laf);
      installedThemesName.clear();

      for (int i = 0; i < installedThemes.size(); i++) {
        try {
          installedThemesName.add( ( (javax.swing.plaf.metal.MetalTheme)
                                    installedThemes.get(i)).getName());
        }
        catch (Exception e) {}
      }

      listTheme.setListData(installedThemesName);
    }
    catch (ClassNotFoundException ex) {
    }
    catch (IllegalAccessException ex) {
    }
    catch (InstantiationException ex) {
    }
  }

  public void selectDefaultLAF(){
    LookAndFeel laf = null;
    try {
      laf = (LookAndFeel) Class.forName(UIManager.
                                          getSystemLookAndFeelClassName()).
            newInstance();
      setSelectedLookAndFeel(laf);
    }
    catch (ClassNotFoundException ex) {
    }
    catch (IllegalAccessException ex) {
    }
    catch (InstantiationException ex) {
    }
  }

  public void selectDefaultTheme(){
    // generate list of avaliable themes for JGoodies LAF
    LookAndFeel laf = null;
    try {
      String name = getSelectedLookAndFeelClassName();
      if (name == null){
        return;
      }else{
        laf = (LookAndFeel)Class.forName(name).newInstance();
      }


      // select default theme
      MetalTheme defaultTheme = (MetalTheme) LookUtils.getDefaultTheme(laf);
      for (int i = 0; i < installedThemesName.size(); i++) {
        if (defaultTheme.getName().equals( (String) installedThemesName.get(i))) {
          listTheme.setSelectedIndex(i);
          break;
        }
      }
    }
    catch (ClassNotFoundException ex) {
    }
    catch (IllegalAccessException ex) {
    }
    catch (InstantiationException ex) {
    }

  }

  private void selectedLAF(int selectedIndex){
    String className = this.getSelectedLookAndFeelClassName();
    try{
      LookAndFeel laf = (LookAndFeel) Class.forName(className).newInstance();

      // refresh look and feel theme
      refreshTheme();

    }catch(Exception e){
      logger.warning("Error check LAF: " + e.getClass());
    }
  }

  private void jbInit() {
    panelCombo = new JPanel();
    panelButtons = new JPanel();

    btnApply = new JButton("Apply");

    border1 = BorderFactory.createEmptyBorder(10, 10, 10, 10);
    border2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,
        new Color(178, 178, 178)), "");
    border3 = BorderFactory.createEtchedBorder(Color.white,
                                               new Color(178, 178, 178));
    border4 = BorderFactory.createEmptyBorder();
    titledBorder1 = new TitledBorder(border4, "Look and Feel");
    border5 = BorderFactory.createEmptyBorder();
    titledBorder2 = new TitledBorder(BorderFactory.createEmptyBorder(),"Color Theme");
    border6 = BorderFactory.createEtchedBorder(Color.white,new Color(178, 178, 178));
    border7 = BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151));
    border8 = BorderFactory.createEtchedBorder(Color.white,new Color(165, 163, 151));
    btnApply.setActionCommand("LAF Apply");

    flowLayout2.setAlignment(FlowLayout.RIGHT);

    this.setLayout(borderLayout1);
    panelCombo.setLayout(gridLayout1);
    panelButtons.setLayout(flowLayout2);

    gridLayout1.setHgap(5);
    gridLayout1.setVgap(0);
    panelCombo.setBorder(border1);
    listLookAndFeel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listLookAndFeel.setVisibleRowCount(5);
    listLookAndFeel.setEnabled(true);
    listLookAndFeel.setDoubleBuffered(true);
    listLookAndFeel.setFixedCellWidth(150);
    jScrollPane1.setBorder(border7);
    jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    jPanel1.setLayout(borderLayout2);
    jPanel1.setBorder(titledBorder1);
    jPanel2.setLayout(borderLayout3);
    jPanel2.setBorder(titledBorder2);
    listTheme.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    listTheme.setVisibleRowCount(5);
    listTheme.setBorder(null);
    listTheme.setDoubleBuffered(true);
    listTheme.setFixedCellWidth(150);
    jScrollPane2.setBorder(border8);
    jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    panelCombo.add(jPanel1, null);
    jPanel1.add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(listLookAndFeel, null);
    panelCombo.add(jPanel2, null);
    jPanel2.add(jScrollPane2,  BorderLayout.CENTER);
    jScrollPane2.getViewport().add(listTheme, null);

    panelButtons.add(btnApply);

    this.add(panelCombo, BorderLayout.CENTER);
    this.add(panelButtons, BorderLayout.SOUTH);
  }

  private void init() {
    UIManager.put("ClassLoader", LookUtils.class.getClassLoader());

    // add JGoodies Looks Look and feels
    try {
      UIManager.installLookAndFeel("JGoodies Windows",
          "com.jgoodies.plaf.windows.ExtWindowsLookAndFeel");
      UIManager.installLookAndFeel("JGoodies Plastic",
          "com.jgoodies.plaf.plastic.PlasticLookAndFeel");
      UIManager.installLookAndFeel("JGoodies Plastic 3D",
          "com.jgoodies.plaf.plastic.Plastic3DLookAndFeel");
      if (LookUtils.IS_OS_WINDOWS_XP) {
        UIManager.installLookAndFeel("JGoodies Plastic XP",
            "com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
      }
    }
    catch (Exception e) {
    }

    lookAndFeelListModel.removeAllElements();

    UIManager.LookAndFeelInfo[] info = UIManager.getInstalledLookAndFeels();

    for (int i = 0; i < info.length; i++) {
      lookAndFeelClassList.put(info[i].getName(), info[i].getClassName());
      lookAndFeelListModel.addElement(info[i].getName());
    }
    logger.info("Avaliable LAF: " + lookAndFeelClassList);

    installedThemesName = new Vector();
  }

  public String getSelectedThemeClassName(){
    String name = null;
    try{
      if (listTheme.getSelectedIndex() > -1){
        name = ( (MetalTheme) installedThemes.get(listTheme.getSelectedIndex())).
            getClass().getName();
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return name;
  }

  public String getSelectedLookAndFeelClassName() {
    String selected = (String) listLookAndFeel.getSelectedValue();
    if (selected == null){
      return null;
    }else{
      return (String) getLookAndFeelClassName(selected);
    }
  }

  public void setSelectedLookAndFeel(LookAndFeel laf){
    for (int i=0; i<listLookAndFeel.getModel().getSize(); i++){
      String name = (String)listLookAndFeel.getModel().getElementAt(i);
      if (laf.getName().equals(name)){
        listLookAndFeel.setSelectedIndex(i);
        selectedLAF(i);
        break;
      }
    }
  }

  public void setSelectedTheme(String theme){
    for(int i=0; i<installedThemesName.size(); i++){
      try{
        MetalTheme objtheme = ( (MetalTheme) installedThemes.get(i));
        String subTheme = objtheme.getClass().getName();
        if (theme.equals(subTheme)) {
          listTheme.setSelectedIndex(i);
          break;
        }
      }catch(Exception e){}
    }
  }

  /**
   * return a look and feel panel that default selected the specified look
   * and feel
   * If the laf does not exist or is null, system look and feel is selected
   */
  public static LAFPanel LAFPanelFactory(JFrame root,
                                         String selectedLookAndFeel) {
    LAFPanel panel = new LAFPanel(root);

    String defaultlaf = panel.getLookAndFeelClassName(selectedLookAndFeel);

    if (defaultlaf == null) {
      // select default look and feel if it is null
      defaultlaf = UIManager.getSystemLookAndFeelClassName();
    }

    return panel;
  }

  private String getLookAndFeelClassName(String lookAndFeelName) {
    return (String) lookAndFeelClassList.get(lookAndFeelName);
  }
}

⌨️ 快捷键说明

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