KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MetalworksFrame


1 /*
2  * @(#)MetalworksFrame.java 1.24 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)MetalworksFrame.java 1.24 05/11/17
39  */

40
41 import java.awt.*;
42 import java.io.*;
43 import java.awt.event.*;
44 import java.beans.*;
45 import javax.swing.*;
46 import javax.swing.border.*;
47
48 import javax.swing.plaf.metal.*;
49
50
51 /**
52   * This is the main container frame for the Metalworks demo app
53   *
54   * @version 1.24 11/17/05
55   * @author Steve Wilson
56   */

57 public class MetalworksFrame extends JFrame {
58
59     JMenuBar menuBar;
60     JDesktopPane desktop;
61     JInternalFrame toolPalette;
62     JCheckBoxMenuItem showToolPaletteMenuItem;
63
64     static final Integer JavaDoc DOCLAYER = new Integer JavaDoc(5);
65     static final Integer JavaDoc TOOLLAYER = new Integer JavaDoc(6);
66     static final Integer JavaDoc HELPLAYER = new Integer JavaDoc(7);
67
68     static final String JavaDoc 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";
69
70
71     public MetalworksFrame() {
72         super("Metalworks");
73         final int inset = 50;
74         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
75     setBounds ( inset, inset, screenSize.width - inset*2, screenSize.height - inset*2 );
76     buildContent();
77     buildMenus();
78     this.addWindowListener(new WindowAdapter() {
79                            public void windowClosing(WindowEvent e) {
80                    quit();
81                    }});
82     UIManager.addPropertyChangeListener(new UISwitchListener((JComponent)getRootPane()));
83     }
84
85     protected void buildMenus() {
86         menuBar = new JMenuBar();
87     menuBar.setOpaque(true);
88     JMenu file = buildFileMenu();
89     JMenu edit = buildEditMenu();
90     JMenu views = buildViewsMenu();
91     JMenu speed = buildSpeedMenu();
92     JMenu help = buildHelpMenu();
93
94     // load a theme from a text file
95
MetalTheme myTheme = null;
96     try {
97         InputStream istream = getClass().getResourceAsStream("/resources/MyTheme.theme");
98         myTheme = new PropertiesMetalTheme(istream);
99     } catch (NullPointerException JavaDoc e) {System.out.println(e);}
100
101     // build an array of themes
102
MetalTheme[] themes = { new OceanTheme(),
103                                 new DefaultMetalTheme(),
104                 new GreenMetalTheme(),
105                 new AquaMetalTheme(),
106                 new KhakiMetalTheme(),
107                 new DemoMetalTheme(),
108                 new ContrastMetalTheme(),
109                 new BigContrastMetalTheme(),
110                             myTheme };
111
112     // put the themes in a menu
113
JMenu themeMenu = new MetalThemeMenu("Theme", themes);
114
115     menuBar.add(file);
116     menuBar.add(edit);
117     menuBar.add(views);
118     menuBar.add(themeMenu);
119     menuBar.add(speed);
120     menuBar.add(help);
121     setJMenuBar(menuBar);
122     }
123
124     protected JMenu buildFileMenu() {
125     JMenu file = new JMenu("File");
126     JMenuItem newWin = new JMenuItem("New");
127     JMenuItem open = new JMenuItem("Open");
128     JMenuItem quit = new JMenuItem("Quit");
129
130     newWin.addActionListener(new ActionListener() {
131                            public void actionPerformed(ActionEvent e) {
132                    newDocument();
133                    }});
134
135     open.addActionListener(new ActionListener() {
136                            public void actionPerformed(ActionEvent e) {
137                    openDocument();
138                    }});
139
140     quit.addActionListener(new ActionListener() {
141                            public void actionPerformed(ActionEvent e) {
142                    quit();
143                    }});
144
145     file.add(newWin);
146     file.add(open);
147     file.addSeparator();
148     file.add(quit);
149     return file;
150     }
151
152     protected JMenu buildEditMenu() {
153     JMenu edit = new JMenu("Edit");
154     JMenuItem undo = new JMenuItem("Undo");
155     JMenuItem copy = new JMenuItem("Copy");
156     JMenuItem cut = new JMenuItem("Cut");
157     JMenuItem paste = new JMenuItem("Paste");
158     JMenuItem prefs = new JMenuItem("Preferences...");
159
160     undo.setEnabled(false);
161     copy.setEnabled(false);
162     cut.setEnabled(false);
163     paste.setEnabled(false);
164
165     prefs.addActionListener(new ActionListener() {
166                            public void actionPerformed(ActionEvent e) {
167                    openPrefsWindow();
168                    }});
169
170     edit.add(undo);
171     edit.addSeparator();
172     edit.add(cut);
173     edit.add(copy);
174     edit.add(paste);
175     edit.addSeparator();
176     edit.add(prefs);
177     return edit;
178     }
179
180     protected JMenu buildViewsMenu() {
181     JMenu views = new JMenu("Views");
182
183     JMenuItem inBox = new JMenuItem("Open In-Box");
184     JMenuItem outBox = new JMenuItem("Open Out-Box");
185     outBox.setEnabled(false);
186
187     inBox.addActionListener(new ActionListener() {
188                            public void actionPerformed(ActionEvent e) {
189                    openInBox();
190                    }});
191
192     views.add(inBox);
193     views.add(outBox);
194     return views;
195     }
196
197      protected JMenu buildSpeedMenu() {
198         JMenu speed = new JMenu("Drag");
199
200         JRadioButtonMenuItem live = new JRadioButtonMenuItem("Live");
201         JRadioButtonMenuItem outline = new JRadioButtonMenuItem("Outline");
202
203         JRadioButtonMenuItem slow = new JRadioButtonMenuItem("Old and Slow");
204
205     ButtonGroup group = new ButtonGroup();
206
207     group.add(live);
208     group.add(outline);
209     group.add(slow);
210
211     live.setSelected(true);
212
213         slow.addActionListener(new ActionListener(){
214                                public void actionPerformed(ActionEvent e){
215                 // for right now I'm saying if you set the mode
216
// to something other than a specified mode
217
// it will revert to the old way
218
// This is mostly for comparison's sake
219
desktop.setDragMode(-1);}});
220
221         live.addActionListener(new ActionListener(){
222                          public void actionPerformed(ActionEvent e){
223                          desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);}});
224       
225         outline.addActionListener(new ActionListener(){
226                          public void actionPerformed(ActionEvent e){
227                          desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);}});
228       
229
230         speed.add(live);
231         speed.add(outline);
232         speed.add(slow);
233         return speed;
234      }
235
236     protected JMenu buildHelpMenu() {
237     JMenu help = new JMenu("Help");
238         JMenuItem about = new JMenuItem("About Metalworks...");
239     JMenuItem openHelp = new JMenuItem("Open Help Window");
240
241     about.addActionListener(new ActionListener() {
242         public void actionPerformed(ActionEvent e) {
243             showAboutBox();
244         }
245     });
246
247     openHelp.addActionListener(new ActionListener() {
248                            public void actionPerformed(ActionEvent e) {
249                    openHelpWindow();
250                    }});
251
252     help.add(about);
253     help.add(openHelp);
254
255     return help;
256     }
257
258     protected void buildContent() {
259         desktop = new JDesktopPane();
260         getContentPane().add(desktop);
261     }
262
263     public void quit() {
264         System.exit(0);
265     }
266
267     public void newDocument() {
268     JInternalFrame doc = new MetalworksDocumentFrame();
269     desktop.add(doc, DOCLAYER);
270     try {
271         doc.setVisible(true);
272         doc.setSelected(true);
273     } catch (java.beans.PropertyVetoException JavaDoc e2) {}
274     }
275
276     public void openDocument() {
277         JFileChooser chooser = new JFileChooser();
278     chooser.showOpenDialog(this);
279     }
280
281     public void openHelpWindow() {
282     JInternalFrame help = new MetalworksHelp();
283     desktop.add(help, HELPLAYER);
284     try {
285         help.setVisible(true);
286         help.setSelected(true);
287     } catch (java.beans.PropertyVetoException JavaDoc e2) {}
288     }
289
290     public void showAboutBox() {
291         JOptionPane.showMessageDialog(this, ABOUTMSG);
292     }
293
294     public void openPrefsWindow() {
295         MetalworksPrefs dialog = new MetalworksPrefs(this);
296     dialog.show();
297
298     }
299
300     public void openInBox() {
301     JInternalFrame doc = new MetalworksInBox();
302     desktop.add(doc, DOCLAYER);
303     try {
304         doc.setVisible(true);
305         doc.setSelected(true);
306     } catch (java.beans.PropertyVetoException JavaDoc e2) {}
307     }
308 }
309
310
311
Popular Tags