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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * MainWindow.java
  3.  *
  4.  * Created on April 5, 2005, 8:51 AM
  5.  */
  6. package org.jdesktop.demo;
  7. import com.jgoodies.looks.HeaderStyle;
  8. import com.jgoodies.looks.Options;
  9. import com.jgoodies.looks.plastic.PlasticLookAndFeel;
  10. import com.jgoodies.looks.plastic.PlasticTheme;
  11. import java.awt.EventQueue;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.Map;
  16. import javax.swing.JFrame;
  17. import javax.swing.JMenu;
  18. import javax.swing.UIManager;
  19. import javax.swing.plaf.metal.MetalLookAndFeel;
  20. import javax.swing.plaf.metal.MetalTheme;
  21. import org.jdesktop.swingx.util.WindowUtils;
  22. /**
  23.  *
  24.  * @author  rbair
  25.  */
  26. public class MainWindow extends javax.swing.JFrame {
  27.     private DemoPanel demo;
  28.     private SourceBrowserWindow sourceWindow;
  29.     
  30.     /**
  31.      * Creates new form MainWindow 
  32.      */
  33.     public MainWindow(DemoPanel demo) {
  34.         this.demo = demo;
  35.         sourceWindow = new SourceBrowserWindow(demo.getSourceRootDir());
  36.         sourceWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  37.         initComponents();
  38.         demo.addMenuItems(menuBar);
  39.         setContentPane(demo.getContent());
  40.         setSize(800, 600);
  41.         setLocation(WindowUtils.getPointForCentering(this));
  42.         menuBar.putClientProperty(Options.HEADER_STYLE_KEY, HeaderStyle.BOTH);
  43.     }
  44.     
  45.     /** This method is called from within the constructor to
  46.      * initialize the form.
  47.      * WARNING: Do NOT modify this code. The content of this method is
  48.      * always regenerated by the Form Editor.
  49.      */
  50.     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  51.     private void initComponents() {
  52.         javax.swing.JSeparator jSeparator1;
  53.         menuBar = new javax.swing.JMenuBar();
  54.         menu = new javax.swing.JMenu();
  55.         menuViewSourceMI = new javax.swing.JMenuItem();
  56.         jSeparator1 = new javax.swing.JSeparator();
  57.         menuExitMI = new javax.swing.JMenuItem();
  58.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  59.         menu.setText("Menu");
  60.         menu.addActionListener(new java.awt.event.ActionListener() {
  61.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  62.                 menuActionPerformed(evt);
  63.             }
  64.         });
  65.         menuViewSourceMI.setText("View Source...");
  66.         menuViewSourceMI.addActionListener(new java.awt.event.ActionListener() {
  67.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  68.                 menuViewSourceMIActionPerformed(evt);
  69.             }
  70.         });
  71.         menu.add(menuViewSourceMI);
  72.         menu.add(jSeparator1);
  73.         menuExitMI.setText("Exit");
  74.         menuExitMI.addActionListener(new java.awt.event.ActionListener() {
  75.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  76.                 menuExitMIActionPerformed(evt);
  77.             }
  78.         });
  79.         menu.add(menuExitMI);
  80.         menuBar.add(menu);
  81.         setJMenuBar(menuBar);
  82.         pack();
  83.     }
  84.     // </editor-fold>//GEN-END:initComponents
  85.     private void menuExitMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuExitMIActionPerformed
  86.         System.exit(0);
  87.     }//GEN-LAST:event_menuExitMIActionPerformed
  88.     private void menuViewSourceMIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuViewSourceMIActionPerformed
  89.         //open up the SourceBrowserWindow, non-modally. Load it with the source
  90.         //files for the currently running demo.
  91.         sourceWindow.setSelectedSourceFile(demo.getSourceFile());
  92.         if (!sourceWindow.isVisible()) {
  93.             sourceWindow.setTitle(getTitle() + " Source Browser");
  94.             sourceWindow.setLocation(WindowUtils.getPointForStaggering(this));
  95.             sourceWindow.setVisible(true);
  96.         } else {
  97.             sourceWindow.toFront();
  98.         }
  99.     }//GEN-LAST:event_menuViewSourceMIActionPerformed
  100.     private void menuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuActionPerformed
  101. // TODO add your handling code here:
  102.     }//GEN-LAST:event_menuActionPerformed
  103.     
  104.     // Variables declaration - do not modify//GEN-BEGIN:variables
  105.     private javax.swing.JMenu menu;
  106.     private javax.swing.JMenuBar menuBar;
  107.     private javax.swing.JMenuItem menuExitMI;
  108.     private javax.swing.JMenuItem menuViewSourceMI;
  109.     // End of variables declaration//GEN-END:variables
  110.     
  111.     /**
  112.      * Mapping of all of the THEMES currently supported.  These are PlasticLookAndFeel themes.
  113.      */
  114.     private static Map<String,MetalTheme> THEMES = null;
  115.     public static void main(String[] args) {
  116.      CmdLineParser parser = new CmdLineParser();
  117.      CmdLineParser.Option dOption = parser.addStringOption('d', "Demo");
  118.      try {
  119.      parser.parse(args);
  120.      } catch (CmdLineParser.UnknownOptionException ex) {
  121.      ex.printStackTrace();
  122.      printUsage();
  123.      System.exit(1);
  124.      } catch (CmdLineParser.IllegalOptionValueException ex) {
  125.      ex.printStackTrace();
  126.      printUsage();
  127.      System.exit(2);
  128.      }
  129.      //look for the -d argument
  130.      final String demoClassName = (String)parser.getOptionValue(dOption);
  131.     
  132.      setAppLookAndFeel();
  133.         EventQueue.invokeLater(new Runnable() {
  134.             public void run() {
  135.                 try {
  136.                     DemoPanel demo = (DemoPanel)Class.forName(demoClassName).newInstance();
  137.                     MainWindow mainWindow = new MainWindow(demo);
  138.                     mainWindow.setTitle(demo.getName());
  139.                     mainWindow.setVisible(true);
  140.                 } catch (Exception e) {
  141.                     System.err.println("Failed to find, create, and open the MainWindow. " +
  142.              "The Demo class was '" + demoClassName + "'");
  143.                     e.printStackTrace();
  144.                     printUsage();
  145.                     System.exit(3);
  146.              }
  147.             }
  148.         });
  149.     }
  150.     
  151.     private static void printUsage() {
  152.      System.out.println("Pass in the name of the JFrame that is to be the main window " +
  153.      "for this app, like so:n" +
  154.      "-w org.jdesktop.binding.demo.blar.BlarWindow");
  155.     }
  156.     /**
  157.      * Sets the application look and feel.  Currently, the look and feel and theme 
  158.      * can be changed on the command line by the command line properties
  159.      * "org.jdesktop.demo.BindingDemo.LookAndFeel" and "org.jdesktop.demo.BindingDemo.Theme".
  160.      */
  161.     private static void setAppLookAndFeel() {
  162. //set the look and feel for the app
  163. try {
  164.         //initialize the THEMES map if they have not already been so initialized
  165.         if (THEMES == null) {
  166.                 initThemes();
  167.         }
  168.         //The defaultClassName and defaultThemeName are the look and feel class name and theme name to use if no system property
  169.         //has been set.
  170.         String defaultClassName = "com.jgoodies.looks.plastic.PlasticXPLookAndFeel";
  171.         String defaultThemeName = "com.jgoodies.looks.plastic.theme.ExperienceBlue";
  172.         String className = defaultClassName;
  173.         String themeName = defaultThemeName;
  174.             try {
  175.                 System.getProperty("org.jdesktop.demo.Theme", defaultThemeName);
  176.                 System.getProperty("org.jdesktop.demo.LookAndFeel", defaultClassName);
  177.             } catch (Throwable e) {
  178.                 //didn't work, oh well.
  179.             }
  180.         //set the current plasic look and feel theme, and the current look and feel
  181.         //Object theme = THEMES.get(themeName);
  182.                 Object theme = null;
  183.         if (theme == null) {
  184.                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  185.         } else if (theme instanceof PlasticTheme){
  186.                 PlasticLookAndFeel.setMyCurrentTheme((PlasticTheme)THEMES.get(themeName));
  187.                 UIManager.setLookAndFeel(className);
  188.         } else if (theme instanceof MetalTheme) {
  189.                 MetalLookAndFeel.setCurrentTheme((MetalTheme)THEMES.get(themeName));
  190.                 UIManager.setLookAndFeel(className);
  191.         }
  192.                  
  193. } catch (Exception e) {
  194.         e.printStackTrace();
  195. }
  196.             
  197.     }
  198.     
  199.     /**
  200.      * Initializes the THEMES map.
  201.      */
  202.     private static void initThemes() {
  203.             THEMES = new HashMap<String,MetalTheme>();
  204.             List list = PlasticLookAndFeel.getInstalledThemes();
  205.             for (Iterator itr = list.iterator(); itr.hasNext();) {
  206.                     PlasticTheme element = (PlasticTheme) itr.next();
  207.                     THEMES.put(element.getClass().getName(), element);
  208.             }
  209.             //add the normal swing themes
  210.             THEMES.put("javax.swing.plaf.metal.DefaultMetalTheme", new javax.swing.plaf.metal.DefaultMetalTheme());
  211.             THEMES.put("javax.swing.plaf.metal.OceanTheme", new javax.swing.plaf.metal.OceanTheme());
  212.     }
  213. }