home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / jfc / Metalworks / src / MetalworksFrame.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  9.0 KB  |  309 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)MetalworksFrame.java    1.17 02/06/13
  38.  */
  39.  
  40. import java.awt.*;
  41. import java.io.*;
  42. import java.awt.event.*;
  43. import java.beans.*;
  44. import javax.swing.*;
  45. import javax.swing.border.*;
  46.  
  47. import javax.swing.plaf.metal.*;
  48.  
  49.  
  50. /**
  51.   * This is the main container frame for the Metalworks demo app
  52.   *
  53.   * @version 1.17 06/13/02
  54.   * @author Steve Wilson
  55.   */
  56. public class MetalworksFrame extends JFrame {
  57.  
  58.     JMenuBar menuBar;
  59.     JDesktopPane desktop;
  60.     JInternalFrame toolPalette;
  61.     JCheckBoxMenuItem showToolPaletteMenuItem;
  62.  
  63.     static final Integer DOCLAYER = new Integer(5);
  64.     static final Integer TOOLLAYER = new Integer(6);
  65.     static final Integer HELPLAYER = new Integer(7);
  66.  
  67.     static final String ABOUTMSG = "Metalworks \n \nAn application written to show off the Java Look & Feel. \n \nWritten by the JavaSoft Look & Feel Team \n  Michael Albers\n  Tom Santos\n  Jeff Shapiro\n  Steve Wilson";
  68.  
  69.  
  70.     public MetalworksFrame() {
  71.         super("Metalworks");
  72.         final int inset = 50;
  73.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  74.     setBounds ( inset, inset, screenSize.width - inset*2, screenSize.height - inset*2 );
  75.     buildContent();
  76.     buildMenus();
  77.     this.addWindowListener(new WindowAdapter() {
  78.                            public void windowClosing(WindowEvent e) {
  79.                    quit();
  80.                    }});
  81.     UIManager.addPropertyChangeListener(new UISwitchListener((JComponent)getRootPane()));
  82.     }
  83.  
  84.     protected void buildMenus() {
  85.         menuBar = new JMenuBar();
  86.     menuBar.setOpaque(true);
  87.     JMenu file = buildFileMenu();
  88.     JMenu edit = buildEditMenu();
  89.     JMenu views = buildViewsMenu();
  90.     JMenu speed = buildSpeedMenu();
  91.     JMenu help = buildHelpMenu();
  92.  
  93.     // load a theme from a text file
  94.     MetalTheme myTheme = null;
  95.     try {
  96.         myTheme =  new PropertiesMetalTheme(new FileInputStream("MyTheme.theme"));
  97.     } catch (IOException e) {System.out.println(e);}
  98.  
  99.     // build an array of themes
  100.     MetalTheme[] themes = { new DefaultMetalTheme(),
  101.                 new GreenMetalTheme(),
  102.                 new AquaMetalTheme(),
  103.                 new KhakiMetalTheme(),
  104.                 new DemoMetalTheme(),
  105.                 new ContrastMetalTheme(),
  106.                 new BigContrastMetalTheme(),
  107.                             myTheme };
  108.  
  109.     // put the themes in a menu
  110.     JMenu themeMenu = new MetalThemeMenu("Theme", themes);
  111.  
  112.     menuBar.add(file);
  113.     menuBar.add(edit);
  114.     menuBar.add(views);
  115.     menuBar.add(themeMenu);
  116.     menuBar.add(speed);
  117.     menuBar.add(help);
  118.     setJMenuBar(menuBar);    
  119.     }
  120.  
  121.     protected JMenu buildFileMenu() {
  122.     JMenu file = new JMenu("File");
  123.     JMenuItem newWin = new JMenuItem("New");
  124.     JMenuItem open = new JMenuItem("Open");
  125.     JMenuItem quit = new JMenuItem("Quit");
  126.  
  127.     newWin.addActionListener(new ActionListener() {
  128.                            public void actionPerformed(ActionEvent e) {
  129.                    newDocument();
  130.                    }});
  131.  
  132.     open.addActionListener(new ActionListener() {
  133.                            public void actionPerformed(ActionEvent e) {
  134.                    openDocument();
  135.                    }});
  136.  
  137.     quit.addActionListener(new ActionListener() {
  138.                            public void actionPerformed(ActionEvent e) {
  139.                    quit();
  140.                    }});
  141.  
  142.     file.add(newWin);
  143.     file.add(open);
  144.     file.addSeparator();
  145.     file.add(quit);
  146.     return file;
  147.     }
  148.  
  149.     protected JMenu buildEditMenu() {
  150.     JMenu edit = new JMenu("Edit");
  151.     JMenuItem undo = new JMenuItem("Undo");
  152.     JMenuItem copy = new JMenuItem("Copy");
  153.     JMenuItem cut = new JMenuItem("Cut");
  154.     JMenuItem paste = new JMenuItem("Paste");
  155.     JMenuItem prefs = new JMenuItem("Preferences...");
  156.  
  157.     undo.setEnabled(false);
  158.     copy.setEnabled(false);
  159.     cut.setEnabled(false);
  160.     paste.setEnabled(false);
  161.  
  162.     prefs.addActionListener(new ActionListener() {
  163.                            public void actionPerformed(ActionEvent e) {
  164.                    openPrefsWindow();
  165.                    }});
  166.  
  167.     edit.add(undo);
  168.     edit.addSeparator();
  169.     edit.add(cut);
  170.     edit.add(copy);
  171.     edit.add(paste);
  172.     edit.addSeparator();
  173.     edit.add(prefs);
  174.     return edit;
  175.     }
  176.  
  177.     protected JMenu buildViewsMenu() {
  178.     JMenu views = new JMenu("Views");
  179.  
  180.     JMenuItem inBox = new JMenuItem("Open In-Box");
  181.     JMenuItem outBox = new JMenuItem("Open Out-Box");
  182.     outBox.setEnabled(false);
  183.  
  184.     inBox.addActionListener(new ActionListener() {
  185.                            public void actionPerformed(ActionEvent e) {
  186.                    openInBox();
  187.                    }});
  188.  
  189.     views.add(inBox);
  190.     views.add(outBox);
  191.     return views;
  192.     }
  193.  
  194.      protected JMenu buildSpeedMenu() {
  195.         JMenu speed = new JMenu("Drag");
  196.  
  197.         JRadioButtonMenuItem live = new JRadioButtonMenuItem("Live");
  198.         JRadioButtonMenuItem outline = new JRadioButtonMenuItem("Outline");
  199.  
  200.         JRadioButtonMenuItem slow = new JRadioButtonMenuItem("Old and Slow");
  201.  
  202.     ButtonGroup group = new ButtonGroup();
  203.  
  204.     group.add(live);
  205.     group.add(outline);
  206.     group.add(slow);
  207.  
  208.     live.setSelected(true);
  209.  
  210.         slow.addActionListener(new ActionListener(){
  211.                                public void actionPerformed(ActionEvent e){
  212.                 // for right now I'm saying if you set the mode
  213.                  // to something other than a specified mode
  214.                  // it will revert to the old way
  215.                  // This is mostly for comparison's sake
  216.                                desktop.setDragMode(-1);}});
  217.  
  218.         live.addActionListener(new ActionListener(){
  219.                          public void actionPerformed(ActionEvent e){
  220.                          desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);}});
  221.       
  222.         outline.addActionListener(new ActionListener(){
  223.                          public void actionPerformed(ActionEvent e){
  224.                          desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);}});
  225.       
  226.  
  227.         speed.add(live);
  228.         speed.add(outline);
  229.         speed.add(slow);
  230.         return speed;
  231.      }
  232.  
  233.     protected JMenu buildHelpMenu() {
  234.     JMenu help = new JMenu("Help");
  235.         JMenuItem about = new JMenuItem("About Metalworks...");
  236.     JMenuItem openHelp = new JMenuItem("Open Help Window");
  237.  
  238.     about.addActionListener(new ActionListener() {
  239.         public void actionPerformed(ActionEvent e) {
  240.             showAboutBox();
  241.         }
  242.     });
  243.  
  244.     openHelp.addActionListener(new ActionListener() {
  245.                            public void actionPerformed(ActionEvent e) {
  246.                    openHelpWindow();
  247.                    }});
  248.  
  249.     help.add(about);
  250.     help.add(openHelp);
  251.  
  252.     return help;
  253.     }
  254.  
  255.     protected void buildContent() {
  256.         desktop = new JDesktopPane();
  257.         getContentPane().add(desktop);
  258.     }
  259.  
  260.     public void quit() {
  261.         System.exit(0);
  262.     }
  263.  
  264.     public void newDocument() {
  265.     JInternalFrame doc = new MetalworksDocumentFrame();
  266.     desktop.add(doc, DOCLAYER);
  267.     try { 
  268.         doc.setVisible(true);
  269.         doc.setSelected(true); 
  270.     } catch (java.beans.PropertyVetoException e2) {}
  271.     }
  272.  
  273.     public void openDocument() {
  274.         JFileChooser chooser = new JFileChooser();
  275.     chooser.showOpenDialog(this);
  276.     }
  277.  
  278.     public void openHelpWindow() {
  279.     JInternalFrame help = new MetalworksHelp();
  280.     desktop.add(help, HELPLAYER);
  281.     try { 
  282.         help.setVisible(true);
  283.         help.setSelected(true); 
  284.     } catch (java.beans.PropertyVetoException e2) {}
  285.     }
  286.  
  287.     public void showAboutBox() {
  288.         JOptionPane.showMessageDialog(this, ABOUTMSG);
  289.     }
  290.  
  291.     public void openPrefsWindow() {
  292.         MetalworksPrefs dialog = new MetalworksPrefs(this);
  293.     dialog.show();
  294.  
  295.     }
  296.  
  297.     public void openInBox() {
  298.     JInternalFrame doc = new MetalworksInBox();
  299.     desktop.add(doc, DOCLAYER);
  300.     try { 
  301.         doc.setVisible(true);
  302.         doc.setSelected(true); 
  303.     } catch (java.beans.PropertyVetoException e2) {}
  304.     }
  305.  
  306. }
  307.  
  308.  
  309.