1 /*
2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. 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  *   - Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *   - Neither the name of Oracle nor the names of its
16  *     contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
42 import java.awt.Dimension;
43 import java.awt.Toolkit;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.WindowAdapter;
47 import java.awt.event.WindowEvent;
48 import java.io.InputStream;
49 import javax.swing.ButtonGroup;
50 import javax.swing.JCheckBoxMenuItem;
51 import javax.swing.JComponent;
52 import javax.swing.JDesktopPane;
53 import javax.swing.JFileChooser;
54 import javax.swing.JFrame;
55 import javax.swing.JInternalFrame;
56 import javax.swing.JMenu;
57 import javax.swing.JMenuBar;
58 import javax.swing.JMenuItem;
59 import javax.swing.JOptionPane;
60 import javax.swing.JRadioButtonMenuItem;
61 import javax.swing.UIManager;
62 import javax.swing.plaf.metal.DefaultMetalTheme;
63 import javax.swing.plaf.metal.MetalTheme;
64 import javax.swing.plaf.metal.OceanTheme;
65 
66 
67 /**
68  * This is the main container frame for the Metalworks demo app
69  *
70  * @author Steve Wilson
71  * @author Alexander Kouznetsov
72  */
73 @SuppressWarnings("serial")
74 public final class MetalworksFrame extends JFrame {
75 
76     JMenuBar menuBar;
77     JDesktopPane desktop;
78     JInternalFrame toolPalette;
79     JCheckBoxMenuItem showToolPaletteMenuItem;
80     static final Integer DOCLAYER = 5;
81     static final Integer TOOLLAYER = 6;
82     static final Integer HELPLAYER = 7;
83     static final String ABOUTMSG = "Metalworks \n \nAn application written to "
84             + "show off the Java Look & Feel. \n \nWritten by the JavaSoft "
85             + "Look & Feel Team \n  Michael Albers\n  Tom Santos\n  "
86             + "Jeff Shapiro\n  Steve Wilson";
87 
MetalworksFrame()88     public MetalworksFrame() {
89         super("Metalworks");
90         final int inset = 50;
91         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
92         setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset
93                 * 2);
94         buildContent();
95         buildMenus();
96         this.addWindowListener(new WindowAdapter() {
97 
98             @Override
99             public void windowClosing(WindowEvent e) {
100                 quit();
101             }
102         });
103         UIManager.addPropertyChangeListener(new UISwitchListener(
104                 (JComponent) getRootPane()));
105     }
106 
buildMenus()107     protected void buildMenus() {
108         menuBar = new JMenuBar();
109         menuBar.setOpaque(true);
110         JMenu file = buildFileMenu();
111         JMenu edit = buildEditMenu();
112         JMenu views = buildViewsMenu();
113         JMenu speed = buildSpeedMenu();
114         JMenu help = buildHelpMenu();
115 
116         // load a theme from a text file
117         MetalTheme myTheme = null;
118         try {
119             InputStream istream = getClass().getResourceAsStream(
120                     "/resources/MyTheme.theme");
121             myTheme = new PropertiesMetalTheme(istream);
122         } catch (NullPointerException e) {
123             System.out.println(e);
124         }
125 
126         // build an array of themes
127         MetalTheme[] themes = { new OceanTheme(),
128             new DefaultMetalTheme(),
129             new GreenMetalTheme(),
130             new AquaMetalTheme(),
131             new KhakiMetalTheme(),
132             new DemoMetalTheme(),
133             new ContrastMetalTheme(),
134             new BigContrastMetalTheme(),
135             myTheme };
136 
137         // put the themes in a menu
138         JMenu themeMenu = new MetalThemeMenu("Theme", themes);
139 
140         menuBar.add(file);
141         menuBar.add(edit);
142         menuBar.add(views);
143         menuBar.add(themeMenu);
144         menuBar.add(speed);
145         menuBar.add(help);
146         setJMenuBar(menuBar);
147     }
148 
buildFileMenu()149     protected JMenu buildFileMenu() {
150         JMenu file = new JMenu("File");
151         JMenuItem newWin = new JMenuItem("New");
152         JMenuItem open = new JMenuItem("Open");
153         JMenuItem quit = new JMenuItem("Quit");
154 
155         newWin.addActionListener(new ActionListener() {
156 
157             public void actionPerformed(ActionEvent e) {
158                 newDocument();
159             }
160         });
161 
162         open.addActionListener(new ActionListener() {
163 
164             public void actionPerformed(ActionEvent e) {
165                 openDocument();
166             }
167         });
168 
169         quit.addActionListener(new ActionListener() {
170 
171             public void actionPerformed(ActionEvent e) {
172                 quit();
173             }
174         });
175 
176         file.add(newWin);
177         file.add(open);
178         file.addSeparator();
179         file.add(quit);
180         return file;
181     }
182 
buildEditMenu()183     protected JMenu buildEditMenu() {
184         JMenu edit = new JMenu("Edit");
185         JMenuItem undo = new JMenuItem("Undo");
186         JMenuItem copy = new JMenuItem("Copy");
187         JMenuItem cut = new JMenuItem("Cut");
188         JMenuItem paste = new JMenuItem("Paste");
189         JMenuItem prefs = new JMenuItem("Preferences...");
190 
191         undo.setEnabled(false);
192         copy.setEnabled(false);
193         cut.setEnabled(false);
194         paste.setEnabled(false);
195 
196         prefs.addActionListener(new ActionListener() {
197 
198             public void actionPerformed(ActionEvent e) {
199                 openPrefsWindow();
200             }
201         });
202 
203         edit.add(undo);
204         edit.addSeparator();
205         edit.add(cut);
206         edit.add(copy);
207         edit.add(paste);
208         edit.addSeparator();
209         edit.add(prefs);
210         return edit;
211     }
212 
buildViewsMenu()213     protected JMenu buildViewsMenu() {
214         JMenu views = new JMenu("Views");
215 
216         JMenuItem inBox = new JMenuItem("Open In-Box");
217         JMenuItem outBox = new JMenuItem("Open Out-Box");
218         outBox.setEnabled(false);
219 
220         inBox.addActionListener(new ActionListener() {
221 
222             public void actionPerformed(ActionEvent e) {
223                 openInBox();
224             }
225         });
226 
227         views.add(inBox);
228         views.add(outBox);
229         return views;
230     }
231 
buildSpeedMenu()232     protected JMenu buildSpeedMenu() {
233         JMenu speed = new JMenu("Drag");
234 
235         JRadioButtonMenuItem live = new JRadioButtonMenuItem("Live");
236         JRadioButtonMenuItem outline = new JRadioButtonMenuItem("Outline");
237 
238         JRadioButtonMenuItem slow = new JRadioButtonMenuItem("Old and Slow");
239 
240         ButtonGroup group = new ButtonGroup();
241 
242         group.add(live);
243         group.add(outline);
244         group.add(slow);
245 
246         live.setSelected(true);
247 
248         slow.addActionListener(new ActionListener() {
249 
250             public void actionPerformed(ActionEvent e) {
251                 // for right now I'm saying if you set the mode
252                 // to something other than a specified mode
253                 // it will revert to the old way
254                 // This is mostly for comparison's sake
255                 desktop.setDragMode(-1);
256             }
257         });
258 
259         live.addActionListener(new ActionListener() {
260 
261             public void actionPerformed(ActionEvent e) {
262                 desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
263             }
264         });
265 
266         outline.addActionListener(new ActionListener() {
267 
268             public void actionPerformed(ActionEvent e) {
269                 desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
270             }
271         });
272 
273 
274         speed.add(live);
275         speed.add(outline);
276         speed.add(slow);
277         return speed;
278     }
279 
buildHelpMenu()280     protected JMenu buildHelpMenu() {
281         JMenu help = new JMenu("Help");
282         JMenuItem about = new JMenuItem("About Metalworks...");
283         JMenuItem openHelp = new JMenuItem("Open Help Window");
284 
285         about.addActionListener(new ActionListener() {
286 
287             public void actionPerformed(ActionEvent e) {
288                 showAboutBox();
289             }
290         });
291 
292         openHelp.addActionListener(new ActionListener() {
293 
294             public void actionPerformed(ActionEvent e) {
295                 openHelpWindow();
296             }
297         });
298 
299         help.add(about);
300         help.add(openHelp);
301 
302         return help;
303     }
304 
buildContent()305     protected void buildContent() {
306         desktop = new JDesktopPane();
307         getContentPane().add(desktop);
308     }
309 
quit()310     public void quit() {
311         System.exit(0);
312     }
313 
newDocument()314     public void newDocument() {
315         JInternalFrame doc = new MetalworksDocumentFrame();
316         desktop.add(doc, DOCLAYER);
317         try {
318             doc.setVisible(true);
319             doc.setSelected(true);
320         } catch (java.beans.PropertyVetoException e2) {
321         }
322     }
323 
openDocument()324     public void openDocument() {
325         JFileChooser chooser = new JFileChooser();
326         chooser.showOpenDialog(this);
327     }
328 
openHelpWindow()329     public void openHelpWindow() {
330         JInternalFrame help = new MetalworksHelp();
331         desktop.add(help, HELPLAYER);
332         try {
333             help.setVisible(true);
334             help.setSelected(true);
335         } catch (java.beans.PropertyVetoException e2) {
336         }
337     }
338 
showAboutBox()339     public void showAboutBox() {
340         JOptionPane.showMessageDialog(this, ABOUTMSG);
341     }
342 
openPrefsWindow()343     public void openPrefsWindow() {
344         MetalworksPrefs dialog = new MetalworksPrefs(this);
345         dialog.setVisible(true);
346 
347     }
348 
openInBox()349     public void openInBox() {
350         JInternalFrame doc = new MetalworksInBox();
351         desktop.add(doc, DOCLAYER);
352         try {
353             doc.setVisible(true);
354             doc.setSelected(true);
355         } catch (java.beans.PropertyVetoException e2) {
356         }
357     }
358 }
359