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

xml/soap/webservice

开发平台:

Java

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