1 /* SwingDemo.java -- An example of using the javax.swing UI.
2 Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 package gnu.classpath.examples.swing;
25 import gnu.classpath.examples.java2d.JNIOverhead;
28 import java.awt.event.*;
32 import javax.swing.plaf.basic.BasicLookAndFeel;
33 import javax.swing.plaf.metal.DefaultMetalTheme;
34 import javax.swing.plaf.metal.MetalLookAndFeel;
35 import javax.swing.plaf.metal.MetalTheme;
36 import javax.swing.plaf.metal.OceanTheme;
38 import java.lang.reflect.Method;
46 * The main desktop. This is package private to avoid synthetic accessor
52 * The themes menu. This is implemented as a field so that the L&F switcher
53 * can disable the menu when a non-Metal L&F is selected.
57 static Color blueGray = new Color(0xdc, 0xda, 0xd5);
59 private static Icon stockIcon(String s)
61 return getIcon("/gnu/classpath/examples/icons/stock-" + s + ".png", s);
64 static Icon bigStockIcon(String s)
66 return getIcon("/gnu/classpath/examples/icons/big-" + s + ".png", s);
69 static Icon getIcon(String location, String name)
71 URL url = Demo.class.getResource(location);
72 if (url == null) System.err.println("WARNING " + location + " not found.");
73 return new ImageIcon(url, name);
76 private JMenuBar mkMenuBar()
78 JMenuBar bar = new JMenuBar();
80 JMenu file = new JMenu("File");
81 JMenu edit = new JMenu("Edit");
82 JMenu help = new JMenu("Help");
84 file.setMnemonic(KeyEvent.VK_F);
85 edit.setMnemonic(KeyEvent.VK_E);
86 help.setMnemonic(KeyEvent.VK_H);
88 file.add(new JMenuItem("New", stockIcon("new")));
89 file.add(new JMenuItem("Open", stockIcon("open")));
91 JMenu recent = new JMenu("Recent Files...");
92 recent.add(new JMenuItem("war-and-peace.txt"));
93 recent.add(new JMenuItem("taming-of-shrew.txt"));
94 recent.add(new JMenuItem("sun-also-rises.txt"));
96 file.add(new JMenuItem("Save", stockIcon("save")));
97 file.add(new JMenuItem("Save as...", stockIcon("save-as")));
99 JMenuItem exit = new JMenuItem("Exit", stockIcon("quit"));
100 exit.addActionListener(new ActionListener()
102 public void actionPerformed(ActionEvent e)
110 edit.add(new JMenuItem("Cut", stockIcon("cut")));
111 edit.add(new JMenuItem("Copy", stockIcon("copy")));
112 edit.add(new JMenuItem("Paste", stockIcon("paste")));
114 JMenu preferences = new JMenu("Preferences...");
115 preferences.add(new JCheckBoxMenuItem("Microphone Active",
117 preferences.add(new JCheckBoxMenuItem("Check Spelling",
118 stockIcon("spell-check")));
119 preferences.add(new JCheckBoxMenuItem("World Peace"));
120 preferences.add(new JSeparator());
121 preferences.add(new JRadioButtonMenuItem("Radio Button"));
122 edit.add(preferences);
124 JMenu examples = new JMenu("Examples");
125 examples.add(new JMenuItem(new PopupAction("Buttons",
126 ButtonDemo.createDemoFactory())));
127 examples.add(new JMenuItem(new PopupAction("Slider",
128 SliderDemo.createDemoFactory())));
129 examples.add(new JMenuItem(new PopupAction("ProgressBar",
130 ProgressBarDemo.createDemoFactory())));
131 examples.add(new JMenuItem(new PopupAction("Scrollbar",
132 ScrollBarDemo.createDemoFactory())));
133 examples.add(new JMenuItem(new PopupAction("Spinner",
134 SpinnerDemo.createDemoFactory())));
135 examples.add(new JMenuItem(new PopupAction("TextField",
136 TextFieldDemo.createDemoFactory())));
137 examples.add(new JMenuItem(new PopupAction("TextArea",
138 TextAreaDemo.createDemoFactory())));
139 examples.add(new JMenuItem(new PopupAction("FileChooser",
140 FileChooserDemo.createDemoFactory())));
142 examples.add(new JMenuItem(new PopupAction("ComboBox",
143 ComboBoxDemo.createDemoFactory())));
145 examples.add(new JMenuItem(new PopupAction("Table",
146 TableDemo.createDemoFactory())));
147 examples.add(new JMenuItem(new PopupAction("List",
148 ListDemo.createDemoFactory())));
149 examples.add(new JMenuItem(new PopupAction("TabbedPane",
150 TabbedPaneDemo.createDemoFactory())));
151 examples.add(new JMenuItem(new PopupAction("Tree",
152 TreeDemo.createDemoFactory())));
153 examples.add(new JMenuItem(new PopupAction("Theme Editor",
154 MetalThemeEditor.createDemoFactory())));
156 examples.add(new JMenuItem(new PopupAction("DocumentFilter",
157 DocumentFilterDemo.createDemoFactory())));
159 examples.add(new JMenuItem(new PopupAction("NavigationFilter",
160 NavigationFilterDemo.createDemoFactory())));
161 examples.add(new JMenuItem(new PopupAction("JNI Overhead",
162 JNIOverhead.createDemoFactory())));
163 examples.add(new JMenuItem(new PopupAction("HTML Demo",
164 HtmlDemo.createDemoFactory())));
167 final JMenuItem vmMenu;
169 help.add(new JMenuItem("just play with the widgets"));
170 help.add(new JMenuItem("and enjoy the sensation of"));
171 help.add(new JMenuItem("your neural connections growing"));
172 help.add(new JSeparator());
173 help.add(vmMenu = new JMenuItem("Really, which VM is this running on?"));
174 vmMenu.addActionListener(new ActionListener()
176 public void actionPerformed(ActionEvent ae)
178 String message = "This is "
179 + System.getProperty("java.vm.name")
181 + System.getProperty("java.vm.version")
183 + System.getProperty("java.vm.vendor")
186 String gnuClasspath = System.getProperty("gnu.classpath.version");
187 if(gnuClasspath != null)
188 message += "\nThe runtime's libraries are "
189 + "kindly provided by the "
190 + "members of GNU Classpath and are in version "
191 + gnuClasspath + ".";
193 JOptionPane.showMessageDialog(vmMenu, message);
197 // Installs the BasicLookAndFeel.
198 UIManager.installLookAndFeel("(Basic Look And Feel)",
199 InstantiableBasicLookAndFeel.class.getName());
202 JMenu lafMenu = new JMenu("Look and Feel");
203 ButtonGroup lafGroup = new ButtonGroup();
204 UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
205 String currentLaf = UIManager.getLookAndFeel().getClass().getName();
206 for (int i = 0; i < lafs.length; ++i)
208 UIManager.LookAndFeelInfo laf = lafs[i];
209 ChangeLAFAction action = new ChangeLAFAction(laf);
210 JRadioButtonMenuItem lafItem = new JRadioButtonMenuItem(action);
211 boolean selected = laf.getClassName().equals(currentLaf);
212 lafItem.setSelected(selected);
213 lafMenu.add(lafItem);
215 lafGroup.add(lafItem);
218 // Create themes menu.
219 themesMenu = new JMenu("Themes");
220 ButtonGroup themesGroup = new ButtonGroup();
222 // In order to make the demo runable on a 1.4 type VM we have to avoid calling
223 // MetalLookAndFeel.getCurrentTheme(). We simply check whether this method exists
228 m = MetalLookAndFeel.class.getMethod("getCurrentTheme", null);
230 catch (NoSuchMethodException nsme)
237 JRadioButtonMenuItem ocean =
238 new JRadioButtonMenuItem(new ChangeThemeAction(new OceanTheme()));
239 ocean.setSelected(MetalLookAndFeel.getCurrentTheme() instanceof OceanTheme);
240 themesMenu.add(ocean);
241 themesGroup.add(ocean);
243 JRadioButtonMenuItem steel =
244 new JRadioButtonMenuItem(new ChangeThemeAction(new DefaultMetalTheme()));
245 ocean.setSelected(MetalLookAndFeel.getCurrentTheme()
246 instanceof DefaultMetalTheme);
247 themesMenu.add(steel);
248 themesGroup.add(steel);
252 themesMenu.setEnabled(false);
264 private static void triggerDialog(final JButton but, final String dir)
266 but.addActionListener(new ActionListener()
268 public void actionPerformed(ActionEvent e)
270 JOptionPane.showConfirmDialog(but,
271 "Sure you want to go " + dir + "?",
273 JOptionPane.OK_CANCEL_OPTION,
274 JOptionPane.QUESTION_MESSAGE,
275 bigStockIcon("warning"));
280 static JToolBar mkToolBar()
282 JToolBar bar = new JToolBar();
284 JButton b = mkButton(stockIcon("go-back"));
285 triggerDialog(b, "back");
288 b = mkButton(stockIcon("go-down"));
289 triggerDialog(b, "down");
292 b = mkButton(stockIcon("go-forward"));
293 triggerDialog(b, "forward");
298 static String halign2str(int a)
302 case SwingConstants.CENTER:
304 case SwingConstants.RIGHT:
306 case SwingConstants.LEFT:
313 private static JButton mkButton(String title, Icon icon,
314 int hAlign, int vAlign,
319 b = new JButton(title);
320 else if (title == null)
321 b = new JButton(icon);
323 b = new JButton(title, icon);
325 b.setToolTipText(title);
326 if (hAlign != -1) b.setHorizontalAlignment(hAlign);
327 if (vAlign != -1) b.setVerticalAlignment(vAlign);
328 if (hPos != -1) b.setHorizontalTextPosition(hPos);
329 if (vPos != -1) b.setVerticalTextPosition(vPos);
333 static JButton mkButton(String title)
335 return mkButton(title, null, -1, -1, -1, -1);
338 static JButton mkButton(Icon i)
340 return mkButton(null, i, -1, -1, -1, -1);
345 frame = new JFrame("Swing Activity Board");
346 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
347 frame.setJMenuBar(mkMenuBar());
348 JComponent component = (JComponent) frame.getContentPane();
349 component.setLayout(new BorderLayout());
350 component.add(mkToolBar(), BorderLayout.NORTH);
351 JPanel main = new JPanel();
352 main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
353 desktop = createDesktop();
355 main.add(mkButtonBar());
356 component.add(main, BorderLayout.CENTER);
361 public static class LaterMain
370 public static void main(String args[])
372 SwingUtilities.invokeLater(new LaterMain());
375 private static JButton mkBigButton(String title)
377 JButton b = new JButton(title);
378 b.setMargin(new Insets(5,5,5,5));
382 static JButton mkDisposerButton(final JFrame c)
384 JButton close = mkBigButton("Close");
385 close.addActionListener(new ActionListener()
387 public void actionPerformed(ActionEvent e)
395 public static JColorChooser mkColorChooser()
397 return new JColorChooser();
401 * This action brings up a new Window with the specified content.
403 private class PopupAction
404 extends AbstractAction
407 * The component to be shown.
409 private DemoFactory demoFactory;
412 * Creates a new PopupAction with the specified name and showing the
413 * component created by the specified DemoFactory when activated.
415 * @param n the name of the action
416 * @param factory the demo factory
418 PopupAction(String n, DemoFactory factory)
421 demoFactory = factory;
425 * Brings up the new window showing the component stored in the
428 * @param e the action event that triggered the action
430 public void actionPerformed(ActionEvent e)
432 JInternalFrame frame = new JInternalFrame((String) getValue(NAME));
433 frame.setClosable(true);
434 frame.setIconifiable(true);
435 frame.setMaximizable(true);
436 frame.setResizable(true);
437 frame.setContentPane(demoFactory.createDemo());
440 frame.setVisible(true);
444 private JPanel mkButtonBar()
446 JPanel panel = new JPanel(new GridLayout(3, 1, 5, 5));
447 panel.add(new JButton(new PopupAction("Buttons",
448 ButtonDemo.createDemoFactory())));
449 panel.add(new JButton(new PopupAction("Slider",
450 SliderDemo.createDemoFactory())));
451 panel.add(new JButton(new PopupAction("ProgressBar",
452 ProgressBarDemo.createDemoFactory())));
453 panel.add(new JButton(new PopupAction("ScrollBar",
454 ScrollBarDemo.createDemoFactory())));
455 panel.add(new JButton(new PopupAction("Spinner",
456 SpinnerDemo.createDemoFactory())));
457 panel.add(new JButton(new PopupAction("TextField",
458 TextFieldDemo.createDemoFactory())));
459 panel.add(new JButton(new PopupAction("TextArea",
460 TextAreaDemo.createDemoFactory())));
461 panel.add(new JButton(new PopupAction("FileChooser",
462 FileChooserDemo.createDemoFactory())));
463 panel.add(new JButton(new PopupAction("ComboBox",
464 ComboBoxDemo.createDemoFactory())));
465 panel.add(new JButton(new PopupAction("Table",
466 TableDemo.createDemoFactory())));
467 panel.add(new JButton(new PopupAction("List",
468 ListDemo.createDemoFactory())));
469 panel.add(new JButton(new PopupAction("TabbedPane",
470 TabbedPaneDemo.createDemoFactory())));
471 panel.add(new JButton(new PopupAction("Tree",
472 TreeDemo.createDemoFactory())));
473 panel.add(new JButton(new PopupAction("Theme Editor",
474 MetalThemeEditor.createDemoFactory())));
475 panel.add(new JButton(new PopupAction("JNI Overhead",
476 JNIOverhead.createDemoFactory())));
477 panel.add(new JButton(new PopupAction("HTML",
478 HtmlDemo.createDemoFactory())));
480 JButton exitDisposer = mkDisposerButton(frame);
481 panel.add(exitDisposer);
483 panel.setMaximumSize(new Dimension(Integer.MAX_VALUE,
484 panel.getPreferredSize().height));
485 exitDisposer.addActionListener(new ActionListener()
487 public void actionPerformed(ActionEvent e)
496 * Creates and returns the main desktop.
498 * @return the main desktop
500 private JDesktopPane createDesktop()
502 JDesktopPane d = new DemoDesktop();
503 d.setPreferredSize(new Dimension(900, 500));
508 * This Action is used to switch Metal themes.
510 class ChangeThemeAction extends AbstractAction
513 * The theme to switch to.
518 * Creates a new ChangeThemeAction for the specified theme.
520 * @param t the theme to switch to
522 ChangeThemeAction(MetalTheme t)
525 putValue(NAME, t.getName());
529 * Changes the theme to the one specified in the constructor.
531 * @param event the action event that triggered this action
533 public void actionPerformed(ActionEvent event)
535 MetalLookAndFeel.setCurrentTheme(theme);
538 // Only switch theme if we have a metal L&F. It is still necessary
539 // to install a new MetalLookAndFeel instance.
540 if (UIManager.getLookAndFeel() instanceof MetalLookAndFeel)
541 UIManager.setLookAndFeel(new MetalLookAndFeel());
543 catch (UnsupportedLookAndFeelException ex)
545 ex.printStackTrace();
547 SwingUtilities.updateComponentTreeUI(frame);
553 * This Action is used to switch Metal themes.
555 class ChangeLAFAction extends AbstractAction
558 * The theme to switch to.
560 private UIManager.LookAndFeelInfo laf;
563 * Creates a new ChangeLAFAction for the specified L&F.
565 * @param l the L&F to switch to
567 ChangeLAFAction(UIManager.LookAndFeelInfo l)
570 putValue(NAME, laf.getName());
574 * Changes the theme to the one specified in the constructor.
576 * @param event the action event that triggered this action
578 public void actionPerformed(ActionEvent event)
582 UIManager.setLookAndFeel(laf.getClassName());
586 ex.printStackTrace();
589 SwingUtilities.updateComponentTreeUI(frame);
590 themesMenu.setEnabled(laf.getClassName()
591 .equals("javax.swing.plaf.metal.MetalLookAndFeel"));
596 * An implementation of BasicLookAndFeel which can be instantiated.
598 * @author Robert Schuster (robertschuster@fsfe.org)
601 public static class InstantiableBasicLookAndFeel extends BasicLookAndFeel
603 public String getDescription()
605 return "An instantiable implementation of BasicLookAndFeel";
608 public String getID()
610 return "instantiableBasicLookAndFeel";
613 public String getName()
615 return "Instantiable Basic Look And Feel";
618 public boolean isNativeLookAndFeel()
623 public boolean isSupportedLookAndFeel()