SwingXDeveloperDemo.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:13k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: SwingXDeveloperDemo.java,v 1.13 2005/06/24 13:20:03 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.demo.swingx_dev;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.LinkedHashMap;
  11. import java.util.List;
  12. import java.util.Vector;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.swing.AbstractListModel;
  16. import javax.swing.DefaultComboBoxModel;
  17. import javax.swing.JFrame;
  18. import javax.swing.SwingUtilities;
  19. import javax.swing.UIManager;
  20. import org.jdesktop.demo.DemoPanel;
  21. import org.jdesktop.demo.MainWindow;
  22. import org.jdesktop.demo.swingx.DecoratorDemoPanel;
  23. import org.jdesktop.demo.swingx.JXHyperlinkDemoPanel;
  24. import org.jdesktop.demo.swingx.JXTableDemoPanel;
  25. import com.jgoodies.looks.LookUtils;
  26. /**
  27.  * A JPanel that demonstrates the use of various SwingX components, intended for
  28.  * developers, as some components may not work correctly or may be used in experimental
  29.  * contexts. Each SwingX Component is shown in its own independent panel, and the user
  30.  * can navigate between the different demos from a list.
  31.  *
  32.  * @author  rbair
  33.  */
  34. public class SwingXDeveloperDemo extends DemoPanel {
  35.     private Logger LOG = Logger.getLogger(SwingXDeveloperDemo.class.getName());
  36.     
  37.     /** A List of each component we're demonstrating--all of which are DemoPanel
  38.      * instances. */
  39.     private List<DemoPanel> demoPanels = new ArrayList<DemoPanel>();
  40.     
  41.     /**
  42.      * Creates new form SwingXDemo
  43.      */
  44.     public SwingXDeveloperDemo() {
  45.         demoPanels.add(new JXChooserDemoPanel());
  46.         demoPanels.add(new DevDropShadowBorderPanel());
  47.         demoPanels.add(new JXPanelTranslucencyDemoPanel());
  48.         
  49.         demoPanels.add(new JXHyperlinkDemoPanel());
  50.         demoPanels.add(new DecoratorDemoPanel());
  51.         demoPanels.add(new JXTableDemoPanel());
  52.         initComponents();
  53.         
  54.         // JGoodies: next line is necessary if running in webstart
  55.         UIManager.put("ClassLoader", LookUtils.class.getClassLoader());
  56.         
  57.         LinkedHashMap lnf = new LinkedHashMap();
  58.         lnf.put("Metal", "javax.swing.plaf.metal.MetalLookAndFeel");
  59.         lnf.put("Motif", "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  60.         lnf.put("Windows", "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  61.   //      lnf.put("Synth - GTK", "javax.swing.plaf.synth.SynthLookAndFeel");
  62.         lnf.put("Ocean", "plasticxp");
  63.         lnf.put("{platform}", UIManager.getSystemLookAndFeelClassName());
  64.         lnf.put("{cross-platform}", UIManager.getCrossPlatformLookAndFeelClassName());
  65.         
  66.         lnf.put("JGoodies Windows", "com.jgoodies.looks.windows.WindowsLookAndFeel");
  67.         lnf.put("JGoodies Plastic", "com.jgoodies.looks.plastic.PlasticLookAndFeel");
  68.         lnf.put("JGoodies Plastic XP", "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
  69.         lnf.put("JGoodies Plastic 3D", "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");
  70.         lnf.put("Kunststoff", "com.incors.plaf.kunststoff.KunststoffLookAndFeel");
  71.         lnf.put("Liquid", "com.birosoft.liquid.LiquidLookAndFeel");
  72.         lnf.put("Napkin", "napkin.NapkinLookAndFeel");
  73.         lnf.put("Substance", "org.jvnet.substance.SubstanceLookAndFeel");
  74.         lnfList.setModel(new MappedComboBoxModel(lnf));
  75.         lnfList.setSelectedItem("{platform}");
  76.         changePLAF();
  77.     }
  78.     
  79.     private static Logger getLogger() { return Logger.getLogger(SwingXDeveloperDemo.class.getName()); }
  80.     
  81.     /** This method is called from within the constructor to
  82.      * initialize the form.
  83.      * WARNING: Do NOT modify this code. The content of this method is
  84.      * always regenerated by the Form Editor.
  85.      */
  86.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  87.     private void initComponents() {
  88.         java.awt.GridBagConstraints gridBagConstraints;
  89.         jSplitPane1 = new javax.swing.JSplitPane();
  90.         jSplitPane2 = new javax.swing.JSplitPane();
  91.         jXTitledPanel1 = new org.jdesktop.swingx.JXTitledPanel();
  92.         jScrollPane2 = new javax.swing.JScrollPane();
  93.         descriptionPane = new javax.swing.JEditorPane();
  94.         demoContainer = new org.jdesktop.swingx.JXTitledPanel();
  95.         jXTitledPanel3 = new org.jdesktop.swingx.JXTitledPanel();
  96.         jScrollPane1 = new javax.swing.JScrollPane();
  97.         demoList = new javax.swing.JList();
  98.         jPanel1 = new javax.swing.JPanel();
  99.         jLabel1 = new javax.swing.JLabel();
  100.         lnfList = new javax.swing.JComboBox();
  101.         applyLnF = new javax.swing.JButton();
  102.         priorPLAF = new javax.swing.JButton();
  103.         nextPLAF = new javax.swing.JButton();
  104.         setLayout(new java.awt.GridBagLayout());
  105.         jSplitPane1.setBorder(null);
  106.         jSplitPane1.setDividerLocation(200);
  107.         jSplitPane2.setBorder(null);
  108.         jSplitPane2.setDividerLocation(175);
  109.         jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
  110.         jXTitledPanel1.getContentContainer().setLayout(new java.awt.BorderLayout());
  111.         jXTitledPanel1.setTitle("Information");
  112.         jScrollPane2.setBorder(null);
  113.         descriptionPane.setEditable(false);
  114.         descriptionPane.setContentType("text/html");
  115.         jScrollPane2.setViewportView(descriptionPane);
  116.         jXTitledPanel1.getContentContainer().add(jScrollPane2, java.awt.BorderLayout.CENTER);
  117.         jSplitPane2.setLeftComponent(jXTitledPanel1);
  118.         demoContainer.getContentContainer().setLayout(new java.awt.CardLayout());
  119.         demoContainer.setTitle("Demo");
  120.         jSplitPane2.setRightComponent(demoContainer);
  121.         jSplitPane1.setRightComponent(jSplitPane2);
  122.         jXTitledPanel3.getContentContainer().setLayout(new java.awt.BorderLayout());
  123.         jXTitledPanel3.setTitle("Table of Contents");
  124.         jScrollPane1.setBorder(null);
  125.         demoList.setModel(new AbstractListModel() {
  126.             public Object getElementAt(int index) {
  127.                 return demoPanels.get(index).getName();
  128.             }
  129.             public int getSize() {
  130.                 return demoPanels.size();
  131.             }
  132.         });
  133.         demoList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
  134.         demoList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
  135.             public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
  136.                 demoListValueChanged(evt);
  137.             }
  138.         });
  139.         jScrollPane1.setViewportView(demoList);
  140.         jXTitledPanel3.getContentContainer().add(jScrollPane1, java.awt.BorderLayout.CENTER);
  141.         jSplitPane1.setLeftComponent(jXTitledPanel3);
  142.         gridBagConstraints = new java.awt.GridBagConstraints();
  143.         gridBagConstraints.gridx = 0;
  144.         gridBagConstraints.gridy = 0;
  145.         gridBagConstraints.gridwidth = 2;
  146.         gridBagConstraints.gridheight = 2;
  147.         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
  148.         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
  149.         gridBagConstraints.weightx = 1.0;
  150.         gridBagConstraints.weighty = 1.0;
  151.         gridBagConstraints.insets = new java.awt.Insets(12, 12, 17, 11);
  152.         add(jSplitPane1, gridBagConstraints);
  153.         jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
  154.         jLabel1.setText("Look and Feel:");
  155.         jPanel1.add(jLabel1);
  156.         jPanel1.add(lnfList);
  157.         applyLnF.setMnemonic('A');
  158.         applyLnF.setText("Apply");
  159.         applyLnF.addActionListener(new java.awt.event.ActionListener() {
  160.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  161.                 applyLnFActionPerformed(evt);
  162.             }
  163.         });
  164.         jPanel1.add(applyLnF);
  165.         priorPLAF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/navigation/Back16.gif")));
  166.         priorPLAF.addActionListener(new java.awt.event.ActionListener() {
  167.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  168.                 priorPLAFActionPerformed(evt);
  169.             }
  170.         });
  171.         jPanel1.add(priorPLAF);
  172.         nextPLAF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/navigation/Forward16.gif")));
  173.         nextPLAF.addActionListener(new java.awt.event.ActionListener() {
  174.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  175.                 nextPLAFActionPerformed(evt);
  176.             }
  177.         });
  178.         jPanel1.add(nextPLAF);
  179.         gridBagConstraints = new java.awt.GridBagConstraints();
  180.         gridBagConstraints.gridx = 0;
  181.         gridBagConstraints.gridy = 2;
  182.         gridBagConstraints.gridwidth = 2;
  183.         gridBagConstraints.gridheight = 2;
  184.         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
  185.         gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
  186.         add(jPanel1, gridBagConstraints);
  187.     }
  188.     // </editor-fold>//GEN-END:initComponents
  189.     
  190.     private void priorPLAFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priorPLAFActionPerformed
  191.         int prior = lnfList.getSelectedIndex() - 1;
  192.         prior = ( prior < 0 ? lnfList.getItemCount() - 1 : prior );
  193.         lnfList.setSelectedIndex(prior);
  194.         changePLAF();
  195.     }//GEN-LAST:event_priorPLAFActionPerformed
  196.     
  197.     private void nextPLAFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextPLAFActionPerformed
  198.         int next = lnfList.getSelectedIndex() + 1;
  199.         next = ( next == lnfList.getItemCount() ? 0 : next );
  200.         lnfList.setSelectedIndex(next);
  201.         changePLAF();
  202.     }//GEN-LAST:event_nextPLAFActionPerformed
  203.     
  204.     private void applyLnFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyLnFActionPerformed
  205.         changePLAF();
  206.     }//GEN-LAST:event_applyLnFActionPerformed
  207.     
  208.     private void changePLAF() {
  209.         MappedComboBoxModel map = (MappedComboBoxModel)lnfList.getModel();
  210.         String newLNF = (String)map.getValueForSelection();
  211.         
  212.         try {
  213.             UIManager.setLookAndFeel(newLNF);
  214.             JFrame frame = (JFrame)SwingUtilities.getWindowAncestor(this);
  215.             if ( frame != null ) {
  216.                 SwingUtilities.updateComponentTreeUI(frame);
  217.                 LOG.info("Look and feel changed to " + newLNF);
  218.             }
  219.         } catch ( Exception x ) {
  220.             LOG.warning("Could not change look and feel to '" + newLNF + "': " + x.getMessage());
  221.         }
  222.     }
  223.     
  224.     private void demoListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_demoListValueChanged
  225.         DemoPanel dp = demoPanels.get(demoList.getSelectedIndex());
  226.         descriptionPane.setText(dp.getHtmlDescription());
  227.         demoContainer.setContentContainer(dp.getContent());
  228.         SwingUtilities.updateComponentTreeUI(dp.getContent());
  229.         demoContainer.revalidate();
  230.         demoContainer.repaint();
  231.         
  232.         // In the regular SwingX demo, the right component is a titled panel, and
  233.         // we just set contents on that panel--but that doesn't work if we also want
  234.         // to change the plaf dynamically--looks like a titled panel bug (PW 12/05/05)
  235. //        jSplitPane2.setRightComponent(dp.getContents());
  236. //        SwingUtilities.updateComponentTreeUI(dp.getContents());
  237. //        jSplitPane2.resetToPreferredSizes();
  238.         
  239.     }//GEN-LAST:event_demoListValueChanged
  240.     
  241.     /**
  242.      * @param args the command line arguments
  243.      */
  244.     public static void main(String args[])  {
  245.         try {
  246.             MainWindow.main(new String[]{"-d", SwingXDeveloperDemo.class.getName()});
  247.         } catch ( Exception e ) {
  248.             SwingXDeveloperDemo.getLogger().log(Level.WARNING, "Could not launch demo window and pluggable panel.", e);
  249.         }
  250.     }
  251.     
  252.     public java.io.File getSourceRootDir() {
  253.         try {
  254.             LOG.info("Source root directory: " + getClass().getResource("/sources"));
  255.             return new File(getClass().getResource("/sources").toURI());
  256.         } catch (Exception e) {
  257.             LOG.log(Level.WARNING, "Could not determine source code root directory", e);
  258.             return null;
  259.         }
  260.     }
  261.     
  262.     public javax.swing.Icon getIcon() {
  263.         return null;
  264.     }
  265.     
  266.     public java.awt.Container getContent() {
  267.         return this;
  268.     }
  269.     
  270.     // Variables declaration - do not modify//GEN-BEGIN:variables
  271.     private javax.swing.JButton applyLnF;
  272.     private org.jdesktop.swingx.JXTitledPanel demoContainer;
  273.     private javax.swing.JList demoList;
  274.     private javax.swing.JEditorPane descriptionPane;
  275.     private javax.swing.JLabel jLabel1;
  276.     private javax.swing.JPanel jPanel1;
  277.     private javax.swing.JScrollPane jScrollPane1;
  278.     private javax.swing.JScrollPane jScrollPane2;
  279.     private javax.swing.JSplitPane jSplitPane1;
  280.     private javax.swing.JSplitPane jSplitPane2;
  281.     private org.jdesktop.swingx.JXTitledPanel jXTitledPanel1;
  282.     private org.jdesktop.swingx.JXTitledPanel jXTitledPanel3;
  283.     private javax.swing.JComboBox lnfList;
  284.     private javax.swing.JButton nextPLAF;
  285.     private javax.swing.JButton priorPLAF;
  286.     // End of variables declaration//GEN-END:variables
  287.     
  288.     class MappedComboBoxModel extends DefaultComboBoxModel {
  289.         private LinkedHashMap data;
  290.         
  291.         MappedComboBoxModel(LinkedHashMap data) {
  292.             super(new Vector(data.keySet()));
  293.             this.data = data;
  294.         }
  295.         
  296.         public Object getValueForSelection() {
  297.             return data.get(getSelectedItem());
  298.         }
  299.     }
  300. }