blob: 5e69856eb827fbb12b0c27f31ec7106b064497af [file] [log] [blame]
Jean-Baptiste Querub56ea2a2013-01-08 11:11:20 -08001
2/*
3 * Copyright 2000-2009 JetBrains s.r.o.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17import javax.swing.*;
18import java.awt.*;
19import java.awt.event.ActionEvent;
20import java.text.MessageFormat;
21import java.util.ResourceBundle;
22
23public class FormPreviewFrame {
24 private JComponent myComponent;
25 private static final ResourceBundle ourBundle = ResourceBundle.getBundle("RuntimeBundle");
26
27 // Note: this class should not be obfuscated
28
29 public static void main(String[] args) {
30 FormPreviewFrame f = new FormPreviewFrame();
31
32 JFrame frame = new JFrame(ourBundle.getString("form.preview.title"));
33 frame.setContentPane(f.myComponent);
34 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35
36 // Add menu bar
37 final JMenuBar menuBar = new JMenuBar();
38 frame.setJMenuBar(menuBar);
39
40 final JMenu menuFile = new JMenu(ourBundle.getString("form.menu.preview"));
41 menuFile.setMnemonic(ourBundle.getString("form.menu.preview.mnemonic").charAt(0));
42 menuFile.add(new JMenuItem(new MyPackAction(frame)));
43 menuFile.add(new JMenuItem(new MyExitAction()));
44 menuBar.add(menuFile);
45
46 final JMenu viewMenu = new JMenu(ourBundle.getString("form.menu.laf"));
47 viewMenu.setMnemonic(ourBundle.getString("form.menu.laf.mnemonic").charAt(0));
48 menuBar.add(viewMenu);
49
50 final UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
51 for(int i = 0; i < lafs.length; i++){
52 viewMenu.add(new MySetLafAction(frame, lafs[i]));
53 }
54
55 frame.pack();
Tor Norbyeb17587c2013-04-24 10:43:41 -070056 Rectangle screenBounds =
57 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
58 frame.setLocation(screenBounds.x + (screenBounds.width - frame.getWidth()) / 2,
59 screenBounds.y + (screenBounds.height - frame.getHeight()) / 2);
Jean-Baptiste Querub56ea2a2013-01-08 11:11:20 -080060 frame.setVisible(true);
61 }
62
63 private static final class MyExitAction extends AbstractAction{
64 public MyExitAction() {
65 super(ourBundle.getString("form.menu.file.exit"));
66 }
67
68 public void actionPerformed(final ActionEvent e) {
69 System.exit(0);
70 }
71 }
72
73 private static final class MyPackAction extends AbstractAction{
74 private final JFrame myFrame;
75
76 public MyPackAction(final JFrame frame) {
77 super(ourBundle.getString("form.menu.view.pack"));
78 myFrame = frame;
79 }
80
81 public void actionPerformed(final ActionEvent e) {
82 myFrame.pack();
83 }
84 }
85
86 private static final class MySetLafAction extends AbstractAction{
87 private final JFrame myFrame;
88 private final UIManager.LookAndFeelInfo myInfo;
89
90 public MySetLafAction(final JFrame frame, final UIManager.LookAndFeelInfo info) {
91 super(info.getName());
92 myFrame = frame;
93 myInfo = info;
94 }
95
96 public void actionPerformed(ActionEvent e) {
97 try{
98 UIManager.setLookAndFeel(myInfo.getClassName());
99 SwingUtilities.updateComponentTreeUI(myFrame);
100 Dimension prefSize = myFrame.getPreferredSize();
101 if(prefSize.width > myFrame.getWidth() || prefSize.height > myFrame.getHeight()){
102 myFrame.pack();
103 }
104 }
105 catch(Exception exc){
106 JOptionPane.showMessageDialog(
107 myFrame,
108 MessageFormat.format(ourBundle.getString("error.cannot.change.look.feel"), new Object[] {exc.getMessage()}),
109 ourBundle.getString("error.title"),
110 JOptionPane.ERROR_MESSAGE
111 );
112 }
113 }
114 }
115}