Path: blob/master/src/demo/share/jfc/Metalworks/MetalworksFrame.java
41149 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.Dimension;42import java.awt.Toolkit;43import java.awt.event.ActionEvent;44import java.awt.event.ActionListener;45import java.awt.event.WindowAdapter;46import java.awt.event.WindowEvent;47import java.io.InputStream;48import javax.swing.ButtonGroup;49import javax.swing.JCheckBoxMenuItem;50import javax.swing.JComponent;51import javax.swing.JDesktopPane;52import javax.swing.JFileChooser;53import javax.swing.JFrame;54import javax.swing.JInternalFrame;55import javax.swing.JMenu;56import javax.swing.JMenuBar;57import javax.swing.JMenuItem;58import javax.swing.JOptionPane;59import javax.swing.JRadioButtonMenuItem;60import javax.swing.UIManager;61import javax.swing.plaf.metal.DefaultMetalTheme;62import javax.swing.plaf.metal.MetalTheme;63import javax.swing.plaf.metal.OceanTheme;646566/**67* This is the main container frame for the Metalworks demo app68*69* @author Steve Wilson70* @author Alexander Kouznetsov71*/72@SuppressWarnings("serial")73public final class MetalworksFrame extends JFrame {7475JMenuBar menuBar;76JDesktopPane desktop;77JInternalFrame toolPalette;78JCheckBoxMenuItem showToolPaletteMenuItem;79static final Integer DOCLAYER = 5;80static final Integer TOOLLAYER = 6;81static final Integer HELPLAYER = 7;82static final String ABOUTMSG = "Metalworks \n \nAn application written to "83+ "show off the Java Look & Feel. \n \nWritten by the JavaSoft "84+ "Look & Feel Team \n Michael Albers\n Tom Santos\n "85+ "Jeff Shapiro\n Steve Wilson";8687public MetalworksFrame() {88super("Metalworks");89final int inset = 50;90Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();91setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset92* 2);93buildContent();94buildMenus();95this.addWindowListener(new WindowAdapter() {9697@Override98public void windowClosing(WindowEvent e) {99quit();100}101});102UIManager.addPropertyChangeListener(new UISwitchListener(103(JComponent) getRootPane()));104}105106protected void buildMenus() {107menuBar = new JMenuBar();108menuBar.setOpaque(true);109JMenu file = buildFileMenu();110JMenu edit = buildEditMenu();111JMenu views = buildViewsMenu();112JMenu speed = buildSpeedMenu();113JMenu help = buildHelpMenu();114115// load a theme from a text file116MetalTheme myTheme = null;117try {118InputStream istream = getClass().getResourceAsStream(119"/resources/MyTheme.theme");120myTheme = new PropertiesMetalTheme(istream);121} catch (NullPointerException e) {122System.out.println(e);123}124125// build an array of themes126MetalTheme[] themes = { new OceanTheme(),127new DefaultMetalTheme(),128new GreenMetalTheme(),129new AquaMetalTheme(),130new KhakiMetalTheme(),131new DemoMetalTheme(),132new ContrastMetalTheme(),133new BigContrastMetalTheme(),134myTheme };135136// put the themes in a menu137JMenu themeMenu = new MetalThemeMenu("Theme", themes);138139menuBar.add(file);140menuBar.add(edit);141menuBar.add(views);142menuBar.add(themeMenu);143menuBar.add(speed);144menuBar.add(help);145setJMenuBar(menuBar);146}147148protected JMenu buildFileMenu() {149JMenu file = new JMenu("File");150JMenuItem newWin = new JMenuItem("New");151JMenuItem open = new JMenuItem("Open");152JMenuItem quit = new JMenuItem("Quit");153154newWin.addActionListener(new ActionListener() {155156public void actionPerformed(ActionEvent e) {157newDocument();158}159});160161open.addActionListener(new ActionListener() {162163public void actionPerformed(ActionEvent e) {164openDocument();165}166});167168quit.addActionListener(new ActionListener() {169170public void actionPerformed(ActionEvent e) {171quit();172}173});174175file.add(newWin);176file.add(open);177file.addSeparator();178file.add(quit);179return file;180}181182protected JMenu buildEditMenu() {183JMenu edit = new JMenu("Edit");184JMenuItem undo = new JMenuItem("Undo");185JMenuItem copy = new JMenuItem("Copy");186JMenuItem cut = new JMenuItem("Cut");187JMenuItem paste = new JMenuItem("Paste");188JMenuItem prefs = new JMenuItem("Preferences...");189190undo.setEnabled(false);191copy.setEnabled(false);192cut.setEnabled(false);193paste.setEnabled(false);194195prefs.addActionListener(new ActionListener() {196197public void actionPerformed(ActionEvent e) {198openPrefsWindow();199}200});201202edit.add(undo);203edit.addSeparator();204edit.add(cut);205edit.add(copy);206edit.add(paste);207edit.addSeparator();208edit.add(prefs);209return edit;210}211212protected JMenu buildViewsMenu() {213JMenu views = new JMenu("Views");214215JMenuItem inBox = new JMenuItem("Open In-Box");216JMenuItem outBox = new JMenuItem("Open Out-Box");217outBox.setEnabled(false);218219inBox.addActionListener(new ActionListener() {220221public void actionPerformed(ActionEvent e) {222openInBox();223}224});225226views.add(inBox);227views.add(outBox);228return views;229}230231protected JMenu buildSpeedMenu() {232JMenu speed = new JMenu("Drag");233234JRadioButtonMenuItem live = new JRadioButtonMenuItem("Live");235JRadioButtonMenuItem outline = new JRadioButtonMenuItem("Outline");236237JRadioButtonMenuItem slow = new JRadioButtonMenuItem("Old and Slow");238239ButtonGroup group = new ButtonGroup();240241group.add(live);242group.add(outline);243group.add(slow);244245live.setSelected(true);246247slow.addActionListener(new ActionListener() {248249public void actionPerformed(ActionEvent e) {250// for right now I'm saying if you set the mode251// to something other than a specified mode252// it will revert to the old way253// This is mostly for comparison's sake254desktop.setDragMode(-1);255}256});257258live.addActionListener(new ActionListener() {259260public void actionPerformed(ActionEvent e) {261desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);262}263});264265outline.addActionListener(new ActionListener() {266267public void actionPerformed(ActionEvent e) {268desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);269}270});271272273speed.add(live);274speed.add(outline);275speed.add(slow);276return speed;277}278279protected JMenu buildHelpMenu() {280JMenu help = new JMenu("Help");281JMenuItem about = new JMenuItem("About Metalworks...");282JMenuItem openHelp = new JMenuItem("Open Help Window");283284about.addActionListener(new ActionListener() {285286public void actionPerformed(ActionEvent e) {287showAboutBox();288}289});290291openHelp.addActionListener(new ActionListener() {292293public void actionPerformed(ActionEvent e) {294openHelpWindow();295}296});297298help.add(about);299help.add(openHelp);300301return help;302}303304protected void buildContent() {305desktop = new JDesktopPane();306getContentPane().add(desktop);307}308309public void quit() {310System.exit(0);311}312313public void newDocument() {314JInternalFrame doc = new MetalworksDocumentFrame();315desktop.add(doc, DOCLAYER);316try {317doc.setVisible(true);318doc.setSelected(true);319} catch (java.beans.PropertyVetoException e2) {320}321}322323public void openDocument() {324JFileChooser chooser = new JFileChooser();325chooser.showOpenDialog(this);326}327328public void openHelpWindow() {329JInternalFrame help = new MetalworksHelp();330desktop.add(help, HELPLAYER);331try {332help.setVisible(true);333help.setSelected(true);334} catch (java.beans.PropertyVetoException e2) {335}336}337338public void showAboutBox() {339JOptionPane.showMessageDialog(this, ABOUTMSG);340}341342public void openPrefsWindow() {343MetalworksPrefs dialog = new MetalworksPrefs(this);344dialog.setVisible(true);345346}347348public void openInBox() {349JInternalFrame doc = new MetalworksInBox();350desktop.add(doc, DOCLAYER);351try {352doc.setVisible(true);353doc.setSelected(true);354} catch (java.beans.PropertyVetoException e2) {355}356}357}358359360