| Jean-Baptiste Queru | b56ea2a | 2013-01-08 11:11:20 -0800 | [diff] [blame^] | 1 | |
| 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 | */ |
| 17 | import javax.swing.*; |
| 18 | import java.awt.*; |
| 19 | import java.awt.event.ActionEvent; |
| 20 | import java.text.MessageFormat; |
| 21 | import java.util.ResourceBundle; |
| 22 | |
| 23 | public 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(); |
| 56 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
| 57 | frame.setLocation((screenSize.width - frame.getWidth())/2, (screenSize.height - frame.getHeight())/2); |
| 58 | frame.setVisible(true); |
| 59 | } |
| 60 | |
| 61 | private static final class MyExitAction extends AbstractAction{ |
| 62 | public MyExitAction() { |
| 63 | super(ourBundle.getString("form.menu.file.exit")); |
| 64 | } |
| 65 | |
| 66 | public void actionPerformed(final ActionEvent e) { |
| 67 | System.exit(0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private static final class MyPackAction extends AbstractAction{ |
| 72 | private final JFrame myFrame; |
| 73 | |
| 74 | public MyPackAction(final JFrame frame) { |
| 75 | super(ourBundle.getString("form.menu.view.pack")); |
| 76 | myFrame = frame; |
| 77 | } |
| 78 | |
| 79 | public void actionPerformed(final ActionEvent e) { |
| 80 | myFrame.pack(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private static final class MySetLafAction extends AbstractAction{ |
| 85 | private final JFrame myFrame; |
| 86 | private final UIManager.LookAndFeelInfo myInfo; |
| 87 | |
| 88 | public MySetLafAction(final JFrame frame, final UIManager.LookAndFeelInfo info) { |
| 89 | super(info.getName()); |
| 90 | myFrame = frame; |
| 91 | myInfo = info; |
| 92 | } |
| 93 | |
| 94 | public void actionPerformed(ActionEvent e) { |
| 95 | try{ |
| 96 | UIManager.setLookAndFeel(myInfo.getClassName()); |
| 97 | SwingUtilities.updateComponentTreeUI(myFrame); |
| 98 | Dimension prefSize = myFrame.getPreferredSize(); |
| 99 | if(prefSize.width > myFrame.getWidth() || prefSize.height > myFrame.getHeight()){ |
| 100 | myFrame.pack(); |
| 101 | } |
| 102 | } |
| 103 | catch(Exception exc){ |
| 104 | JOptionPane.showMessageDialog( |
| 105 | myFrame, |
| 106 | MessageFormat.format(ourBundle.getString("error.cannot.change.look.feel"), new Object[] {exc.getMessage()}), |
| 107 | ourBundle.getString("error.title"), |
| 108 | JOptionPane.ERROR_MESSAGE |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |