KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > swingset > SwingSet2


1 /*
2  * Copyright (c) 2003 Sun Microsystems, Inc. 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  * -Redistribution in binary form must reproduct the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the distribution.
14  *
15  * Neither the name of Sun Microsystems, Inc. or the names of contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30  *
31  * You acknowledge that Software is not designed, licensed or intended for
32  * use in the design, construction, operation or maintenance of any nuclear
33  * facility.
34  */

35
36 /*
37  * @(#)SwingSet2.java 1.35 03/01/23
38  */

39 package demo.swingset;
40 import swingwtx.swing.*;
41 import swingwtx.swing.event.*;
42 import swingwtx.swing.text.*;
43 import swingwtx.swing.border.*;
44 import swingwtx.swing.colorchooser.*;
45 import swingwtx.swing.filechooser.*;
46 import javax.accessibility.*;
47
48 import swingwtx.swing.plaf.metal.DefaultMetalTheme;
49 import swingwtx.swing.plaf.metal.MetalLookAndFeel;
50
51 import java.lang.reflect.*;
52 import swingwt.awt.*;
53 import swingwt.awt.event.*;
54 import java.beans.*;
55 import java.util.*;
56 import java.io.*;
57 import java.applet.*;
58 import java.net.*;
59
60 /**
61  * A demo that shows all of the Swing components.
62  *
63  * @version 1.35 01/23/03
64  * @author Jeff Dinkins
65  */

66 public class SwingSet2 extends JPanel {
67
68     String JavaDoc[] demos = {
69       "InternalFrameDemo",
70       "ButtonDemo",
71       "ComboBoxDemo",
72       "FileChooserDemo",
73       "HtmlDemo",
74       "ListDemo",
75       "OptionPaneDemo",
76       "ProgressBarDemo",
77       "ScrollPaneDemo",
78       "SliderDemo",
79       "SplitPaneDemo",
80       "TabbedPaneDemo",
81       "TableDemo",
82       "ToolTipDemo",
83       "TreeDemo"
84     };
85
86     void loadDemos() {
87     for(int i = 0; i < demos.length;) {
88             if(isApplet() && demos[i].equals("FileChooserDemo")) {
89            // don't load the file chooser demo if we are
90
// an applet
91
} else {
92            loadDemo(demos[i]);
93             }
94         i++;
95             if (i == 1)
96                 try {
97                     setDemo((DemoModule) demosVector.get(0));
98                 }
99                 catch (Exception JavaDoc e) {}
100     }
101     }
102
103     // Possible Look & Feels
104
private static final String JavaDoc mac =
105             "com.sun.java.swing.plaf.mac.MacLookAndFeel";
106     private static final String JavaDoc metal =
107             "swingwtx.swing.plaf.metal.MetalLookAndFeel";
108     private static final String JavaDoc motif =
109             "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
110     private static final String JavaDoc windows =
111             "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
112     private static final String JavaDoc gtk =
113             "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
114
115     // The current Look & Feel
116
private static String JavaDoc currentLookAndFeel = metal;
117
118     // List of demos
119
private Vector demosVector = new Vector();
120
121     // The preferred size of the demo
122
private static final int PREFERRED_WIDTH = 720;
123     private static final int PREFERRED_HEIGHT = 640;
124     
125     // Box spacers
126
private Dimension HGAP = new Dimension(1,5);
127     private Dimension VGAP = new Dimension(5,1);
128
129     // Resource bundle for internationalized and accessible text
130
private ResourceBundle bundle = null;
131
132     // A place to hold on to the visible demo
133
private DemoModule currentDemo = null;
134     private JPanel demoPanel = null;
135
136     // About Box
137
private JDialog aboutBox = null;
138
139     // Status Bar
140
private JTextField statusField = null;
141
142     // Tool Bar
143
private ToggleButtonToolBar toolbar = null;
144     private ButtonGroup toolbarGroup = new ButtonGroup();
145
146     // Menus
147
private JMenuBar menuBar = null;
148     private JMenu lafMenu = null;
149     private JMenu themesMenu = null;
150     private JMenu audioMenu = null;
151     private JMenu toolTipMenu = null;
152     private ButtonGroup lafMenuGroup = new ButtonGroup();
153     private ButtonGroup themesMenuGroup = new ButtonGroup();
154     private ButtonGroup audioMenuGroup = new ButtonGroup();
155     private ButtonGroup toolTipMenuGroup = new ButtonGroup();
156
157     // Popup menu
158
private JPopupMenu popupMenu = null;
159     private ButtonGroup popupMenuGroup = new ButtonGroup();
160
161     // Used only if swingset is an application
162
private JFrame frame = null;
163     private JWindow splashScreen = null;
164
165     // Used only if swingset is an applet
166
private Object JavaDoc applet = null;
167
168     // To debug or not to debug, that is the question
169
private boolean DEBUG = true;
170     private int debugCounter = 0;
171
172     // The tab pane that holds the demo
173
private JTabbedPane tabbedPane = null;
174
175     private JEditorPane demoSrcPane = null;
176
177     private JLabel splashLabel = null;
178
179     // contentPane cache, saved from the applet or application frame
180
Container contentPane = null;
181
182
183     // number of swingsets - for multiscreen
184
// keep track of the number of SwingSets created - we only want to exit
185
// the program when the last one has been closed.
186
private static int numSSs = 0;
187     private static Vector swingSets = new Vector();
188
189     public SwingSet2(Object JavaDoc applet) {
190         this(applet, null);
191     }
192
193     /**
194      * SwingSet2 Constructor
195      */

196     public SwingSet2(Object JavaDoc applet, GraphicsConfiguration gc) {
197
198     // Note that the applet may null if this is started as an application
199
this.applet = applet;
200
201     // Create Frame here for app-mode so the splash screen can get the
202
// GraphicsConfiguration from it in createSplashScreen()
203
if (!isApplet()) {
204         frame = createFrame(gc);
205     }
206
207     // setLayout(new BorderLayout());
208
setLayout(new BorderLayout());
209
210     // set the preferred size of the demo
211
setSize(PREFERRED_WIDTH,PREFERRED_HEIGHT);
212
213     // Create and throw the splash screen up. Since this will
214
// physically throw bits on the screen, we need to do this
215
// on the GUI thread using invokeLater.
216
createSplashScreen();
217
218     // do the following on the gui thread
219
SwingUtilities.invokeLater(new Runnable JavaDoc() {
220         public void run() {
221         showSplashScreen();
222         }
223     });
224         
225     initializeDemo();
226     preloadFirstDemo();
227
228     // Show the demo and take down the splash screen. Note that
229
// we again must do this on the GUI thread using invokeLater.
230
SwingUtilities.invokeLater(new Runnable JavaDoc() {
231         public void run() {
232         showSwingSet2();
233         hideSplash();
234         }
235     });
236
237     // Start loading the rest of the demo in the background
238
DemoLoadThread demoLoader = new DemoLoadThread(this);
239     demoLoader.start();
240     }
241
242
243     /**
244      * SwingSet2 Main. Called only if we're an application, not an applet.
245      */

246     public static void main(String JavaDoc[] args) {
247     // Create SwingSet on the default monitor
248
SwingSet2 swingset = new SwingSet2(null, null);
249     }
250
251     // *******************************************************
252
// *************** Demo Loading Methods ******************
253
// *******************************************************
254

255     
256     
257     public void initializeDemo() {
258     JPanel top = new JPanel();
259     top.setLayout(new BorderLayout());
260     add(top, BorderLayout.NORTH);
261
262     menuBar = createMenus();
263         getFrame().setJMenuBar(menuBar);
264
265     // creates popup menu accessible via keyboard
266
popupMenu = createPopupMenu();
267
268     ToolBarPanel toolbarPanel = new ToolBarPanel();
269     toolbarPanel.setLayout(new BorderLayout());
270     toolbar = new ToggleButtonToolBar();
271     toolbarPanel.add(toolbar, BorderLayout.CENTER);
272     top.add(toolbarPanel, BorderLayout.SOUTH);
273     //toolbarPanel.addContainerListener(toolbarPanel);
274

275     tabbedPane = new JTabbedPane();
276     add(tabbedPane, BorderLayout.CENTER);
277     tabbedPane.addChangeListener(new TabListener());
278
279     statusField = new JTextField("");
280     statusField.setEditable(false);
281     add(statusField, BorderLayout.SOUTH);
282     
283     demoPanel = new JPanel();
284     demoPanel.setLayout(new BorderLayout());
285     demoPanel.setBorder(new EtchedBorder());
286     tabbedPane.addTab("Hi There!", demoPanel);
287     
288     // Add html src code viewer
289
demoSrcPane = new JEditorPane("text/html", getString("SourceCode.loading"));
290     demoSrcPane.setEditable(false);
291     
292     JScrollPane scroller = new JScrollPane();
293     scroller.getViewport().add(demoSrcPane);
294     
295     tabbedPane.addTab(
296         getString("TabbedPane.src_label"),
297         null,
298         demoSrcPane,
299         getString("TabbedPane.src_tooltip")
300     );
301     }
302
303     DemoModule currentTabDemo = null;
304     class TabListener implements ChangeListener {
305     public void stateChanged(ChangeEvent e) {
306         boolean srcSelected = tabbedPane.getSelectedIndex() == 1;
307         if(currentTabDemo != currentDemo && demoSrcPane != null && srcSelected) {
308         demoSrcPane.setText(getString("SourceCode.loading"));
309         // Only necessary for GTK2
310
if (!SwingWTUtils.isWindows())
311             SwingUtilities.invokeIn(new Runnable JavaDoc() {
312             public void run() {
313                 Dimension d = getFrame().getSize();
314                 d.width++;
315                 d.height++;
316                 getFrame().setSize(d);
317             }
318             }, 100);
319         invalidate();
320                 repaint();
321         }
322         if(currentTabDemo != currentDemo && srcSelected) {
323         currentTabDemo = currentDemo;
324         setSourceCode(currentDemo);
325         // Only necessary for GTK2
326
if (!SwingWTUtils.isWindows())
327             SwingUtilities.invokeIn(new Runnable JavaDoc() {
328             public void run() {
329                 Dimension d = getFrame().getSize();
330                 d.width++;
331                 d.height++;
332                 getFrame().setSize(d);
333             }
334             }, 100);
335                 invalidate();
336                 repaint();
337         }
338     }
339     }
340
341
342     /**
343      * Create menus
344      */

345     public JMenuBar createMenus() {
346     JMenuItem mi;
347     // ***** create the menubar ****
348
JMenuBar menuBar = new JMenuBar();
349     //menuBar.getAccessibleContext().setAccessibleName(
350
// getString("MenuBar.accessible_description"));
351

352     // ***** create File menu
353
JMenu fileMenu = (JMenu) menuBar.add(new JMenu(getString("FileMenu.file_label")));
354         fileMenu.setMnemonic(getMnemonic("FileMenu.file_mnemonic"));
355     //fileMenu.getAccessibleContext().setAccessibleDescription(getString("FileMenu.accessible_description"));
356

357     createMenuItem(fileMenu, "FileMenu.about_label", "FileMenu.about_mnemonic",
358                "FileMenu.about_accessible_description", new AboutAction(this));
359
360         fileMenu.addSeparator();
361
362     createMenuItem(fileMenu, "FileMenu.open_label", "FileMenu.open_mnemonic",
363                "FileMenu.open_accessible_description", null);
364
365     createMenuItem(fileMenu, "FileMenu.save_label", "FileMenu.save_mnemonic",
366                "FileMenu.save_accessible_description", null);
367
368     createMenuItem(fileMenu, "FileMenu.save_as_label", "FileMenu.save_as_mnemonic",
369                "FileMenu.save_as_accessible_description", null);
370
371
372     if(!isApplet()) {
373         fileMenu.addSeparator();
374         
375         createMenuItem(fileMenu, "FileMenu.exit_label", "FileMenu.exit_mnemonic",
376                "FileMenu.exit_accessible_description", new ExitAction(this)
377         );
378     }
379
380         // Create these menu items for the first SwingSet only.
381
if (numSSs == 0) {
382     // ***** create laf switcher menu
383
lafMenu = (JMenu) menuBar.add(new JMenu(getString("LafMenu.laf_label")));
384         lafMenu.setMnemonic(getMnemonic("LafMenu.laf_mnemonic"));
385     //lafMenu.getAccessibleContext().setAccessibleDescription(
386
// getString("LafMenu.laf_accessible_description"));
387

388     mi = createLafMenuItem(lafMenu, "LafMenu.java_label", "LafMenu.java_mnemonic",
389                "LafMenu.java_accessible_description", metal);
390     mi.setSelected(true); // this is the default l&f
391

392     createLafMenuItem(lafMenu, "LafMenu.mac_label", "LafMenu.mac_mnemonic",
393                "LafMenu.mac_accessible_description", mac);
394
395     createLafMenuItem(lafMenu, "LafMenu.motif_label", "LafMenu.motif_mnemonic",
396                "LafMenu.motif_accessible_description", motif);
397
398     createLafMenuItem(lafMenu, "LafMenu.windows_label", "LafMenu.windows_mnemonic",
399                "LafMenu.windows_accessible_description", windows);
400
401     createLafMenuItem(lafMenu, "LafMenu.gtk_label", "LafMenu.gtk_mnemonic",
402                "LafMenu.gtk_accessible_description", gtk);
403
404     // ***** create themes menu
405
themesMenu = (JMenu) menuBar.add(new JMenu(getString("ThemesMenu.themes_label")));
406         themesMenu.setMnemonic(getMnemonic("ThemesMenu.themes_mnemonic"));
407     //themesMenu.getAccessibleContext().setAccessibleDescription(
408
// getString("ThemesMenu.themes_accessible_description"));
409

410     // ***** create the audio submenu under the theme menu
411
audioMenu = (JMenu) themesMenu.add(new JMenu(getString("AudioMenu.audio_label")));
412         audioMenu.setMnemonic(getMnemonic("AudioMenu.audio_mnemonic"));
413     //audioMenu.getAccessibleContext().setAccessibleDescription(
414
// getString("AudioMenu.audio_accessible_description"));
415

416     createAudioMenuItem(audioMenu, "AudioMenu.on_label",
417                 "AudioMenu.on_mnemonic",
418                 "AudioMenu.on_accessible_description",
419                 new OnAudioAction(this));
420
421     mi = createAudioMenuItem(audioMenu, "AudioMenu.default_label",
422                  "AudioMenu.default_mnemonic",
423                  "AudioMenu.default_accessible_description",
424                  new DefaultAudioAction(this));
425     mi.setSelected(true); // This is the default feedback setting
426

427     createAudioMenuItem(audioMenu, "AudioMenu.off_label",
428                 "AudioMenu.off_mnemonic",
429                 "AudioMenu.off_accessible_description",
430                 new OffAudioAction(this));
431
432     // *** now back to adding color/font themes to the theme menu
433
mi = createThemesMenuItem(themesMenu, "ThemesMenu.default_label", "ThemesMenu.default_mnemonic",
434                "ThemesMenu.default_accessible_description", new DefaultMetalTheme());
435     mi.setSelected(true); // This is the default theme
436

437     createThemesMenuItem(themesMenu, "ThemesMenu.aqua_label", "ThemesMenu.aqua_mnemonic",
438                "ThemesMenu.aqua_accessible_description", new AquaTheme());
439
440     createThemesMenuItem(themesMenu, "ThemesMenu.charcoal_label", "ThemesMenu.charcoal_mnemonic",
441                "ThemesMenu.charcoal_accessible_description", new CharcoalTheme());
442
443     createThemesMenuItem(themesMenu, "ThemesMenu.contrast_label", "ThemesMenu.contrast_mnemonic",
444                "ThemesMenu.contrast_accessible_description", new ContrastTheme());
445
446     createThemesMenuItem(themesMenu, "ThemesMenu.emerald_label", "ThemesMenu.emerald_mnemonic",
447                "ThemesMenu.emerald_accessible_description", new EmeraldTheme());
448
449     createThemesMenuItem(themesMenu, "ThemesMenu.ruby_label", "ThemesMenu.ruby_mnemonic",
450                "ThemesMenu.ruby_accessible_description", new RubyTheme());
451
452     // ***** create the tooltip menu.
453
toolTipMenu = (JMenu) menuBar.add(new JMenu(
454                 getString("ToolTipMenu.tooltip_label")));
455         toolTipMenu.setMnemonic(getMnemonic("ToolTipMenu.tooltip_mnemonic"));
456     //toolTipMenu.getAccessibleContext().setAccessibleDescription(
457
// getString("ToolTipMenu.tooltip_accessible_description"));
458

459         // ***** create tool tip submenu items.
460
mi = createToolTipMenuItem(toolTipMenu, "ToolTipMenu.on_label",
461                 "ToolTipMenu.on_mnemonic",
462                 "ToolTipMenu.on_accessible_description",
463                 new ToolTipAction(this, true));
464         mi.setSelected(true);
465
466         createToolTipMenuItem(toolTipMenu, "ToolTipMenu.off_label",
467                 "ToolTipMenu.off_mnemonic",
468                 "ToolTipMenu.off_accessible_description",
469                 new ToolTipAction(this, false));
470         }
471
472
473     return menuBar;
474     }
475
476     /**
477      * Create the tool tip submenu
478      */

479     public JMenuItem createToolTipMenuItem(JMenu menu, String JavaDoc label,
480                                            String JavaDoc mnemonic,
481                                            String JavaDoc accessibleDescription,
482                                            Action action) {
483         JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add(
484                 new JRadioButtonMenuItem(getString(label)));
485         toolTipMenuGroup.add(mi);
486         mi.setMnemonic(getMnemonic(mnemonic));
487         //mi.getAccessibleContext().setAccessibleDescription(getString(
488
// accessibleDescription));
489
mi.addActionListener(action);
490
491         return mi;
492     }
493
494     /**
495      * Create the theme's audio submenu
496      */

497     public JMenuItem createAudioMenuItem(JMenu menu, String JavaDoc label,
498                      String JavaDoc mnemonic,
499                      String JavaDoc accessibleDescription,
500                      Action action) {
501         JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
502     audioMenuGroup.add(mi);
503     mi.setMnemonic(getMnemonic(mnemonic));
504     //mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
505
mi.addActionListener(action);
506
507     return mi;
508     }
509
510     /**
511      * Creates a generic menu item
512      */

513     public JMenuItem createMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
514                    String JavaDoc accessibleDescription, Action action) {
515         JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label)));
516     mi.setMnemonic(getMnemonic(mnemonic));
517     //mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
518
mi.addActionListener(action);
519     if(action == null) {
520         mi.setEnabled(false);
521     }
522     return mi;
523     }
524
525     /**
526      * Creates a JRadioButtonMenuItem for the Themes menu
527      */

528     public JMenuItem createThemesMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
529                    String JavaDoc accessibleDescription, DefaultMetalTheme theme) {
530         JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
531     themesMenuGroup.add(mi);
532     mi.setMnemonic(getMnemonic(mnemonic));
533     //mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
534
mi.addActionListener(new ChangeThemeAction(this, theme));
535
536     return mi;
537     }
538
539     /**
540      * Creates a JRadioButtonMenuItem for the Look and Feel menu
541      */

542     public JMenuItem createLafMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
543                    String JavaDoc accessibleDescription, String JavaDoc laf) {
544         JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
545     lafMenuGroup.add(mi);
546     mi.setMnemonic(getMnemonic(mnemonic));
547     //mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
548
mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
549
550     mi.setEnabled(isAvailableLookAndFeel(laf));
551
552     return mi;
553     }
554
555     /**
556      * Creates a multi-screen menu item
557      */

558     public JMenuItem createMultiscreenMenuItem(JMenu menu, int screen) {
559         JMenuItem mi = null;
560         if (screen == MultiScreenAction.ALL_SCREENS) {
561             mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.all_label")));
562             mi.setMnemonic(getMnemonic("MultiMenu.all_mnemonic"));
563             //mi.getAccessibleContext().setAccessibleDescription(getString(
564
// "MultiMenu.all_accessible_description"));
565
}
566         else {
567             mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.single_label") + " " +
568                                                                                                  screen));
569             mi.setMnemonic(KeyEvent.VK_0 + screen);
570             //mi.getAccessibleContext().setAccessibleDescription(getString(
571
// "MultiMenu.single_accessible_description") + " " + screen);
572

573         }
574         mi.addActionListener(new MultiScreenAction(this, screen));
575         return mi;
576     }
577
578     public JPopupMenu createPopupMenu() {
579     JPopupMenu popup = new JPopupMenu("JPopupMenu demo");
580     
581     createPopupMenuItem(popup, "LafMenu.java_label", "LafMenu.java_mnemonic",
582                 "LafMenu.java_accessible_description", metal);
583     
584     createPopupMenuItem(popup, "LafMenu.mac_label", "LafMenu.mac_mnemonic",
585                 "LafMenu.mac_accessible_description", mac);
586     
587     createPopupMenuItem(popup, "LafMenu.motif_label", "LafMenu.motif_mnemonic",
588                 "LafMenu.motif_accessible_description", motif);
589     
590     createPopupMenuItem(popup, "LafMenu.windows_label", "LafMenu.windows_mnemonic",
591                 "LafMenu.windows_accessible_description", windows);
592     
593     createPopupMenuItem(popup, "LafMenu.gtk_label", "LafMenu.gtk_mnemonic",
594                 "LafMenu.gtk_accessible_description", gtk);
595
596     // register key binding to activate popup menu
597
//InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
598
//map.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, InputEvent.CTRL_MASK),
599
// "postMenuAction");
600
//getActionMap().put("postMenuAction", new ActivatePopupMenuAction(this, popup));
601

602     return popup;
603     }
604     
605     /**
606      * Creates a JMenuItem for the Look and Feel popup menu
607      */

608     public JMenuItem createPopupMenuItem(JPopupMenu menu, String JavaDoc label, String JavaDoc mnemonic,
609                      String JavaDoc accessibleDescription, String JavaDoc laf) {
610     JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label)));
611     popupMenuGroup.add(mi);
612     mi.setMnemonic(getMnemonic(mnemonic));
613     //mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
614
mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
615     mi.setEnabled(isAvailableLookAndFeel(laf));
616     
617     return mi;
618     }
619     
620
621     /**
622      * Load the first demo. This is done separately from the remaining demos
623      * so that we can get SwingSet2 up and available to the user quickly.
624      */

625     public void preloadFirstDemo() {
626     //DemoModule demo = addDemo(new InternalFrameDemo(this));
627
//setDemo(demo);
628
}
629
630
631     /**
632      * Add a demo to the toolbar
633      */

634     public DemoModule addDemo(DemoModule demo) {
635     demosVector.addElement(demo);
636     // do the following on the gui thread
637
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
638         public void run() {
639         SwitchToDemoAction action = new SwitchToDemoAction(swingset, (DemoModule) obj);
640         JButton tb = swingset.getToolBar().addButton(action);
641                 if (tb == null) return;
642         tb.setText("");
643         tb.setToolTipText(((DemoModule)obj).getToolTip());
644         if(demos[demos.length-1].equals(obj.getClass().getName())) {
645             setStatus(getString("Status.popupMenuAccessible"));
646         }
647         }
648     });
649     return demo;
650     }
651
652
653     /**
654      * Sets the current demo
655      */

656     public void setDemo(DemoModule demo) {
657     currentDemo = demo;
658
659     // Ensure panel's UI is current before making visible
660
JComponent currentDemoPanel = demo.getDemoPanel();
661     SwingUtilities.updateComponentTreeUI(currentDemoPanel);
662
663     demoPanel.removeAll();
664     demoPanel.add(currentDemoPanel, BorderLayout.CENTER);
665
666     tabbedPane.setSelectedIndex(0);
667     tabbedPane.setTitleAt(0, demo.getName());
668     tabbedPane.setToolTipTextAt(0, demo.getToolTip());
669     }
670
671
672     /**
673      * Bring up the SwingSet2 demo by showing the frame (only
674      * applicable if coming up as an application, not an applet);
675      */

676     public void showSwingSet2() {
677     if(!isApplet() && getFrame() != null) {
678         // put swingset in a frame and show it
679
JFrame f = getFrame();
680         f.setTitle(getString("Frame.title"));
681         f.getContentPane().add(this, BorderLayout.CENTER);
682             f.setLocation(0, 0);
683             f.setSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
684         f.show();
685             numSSs++;
686             swingSets.add(this);
687     }
688     }
689
690     /**
691      * Show the spash screen while the rest of the demo loads
692      */

693     public void createSplashScreen() {
694     splashLabel = new JLabel(createImageIcon("Splash.jpg", "Splash.accessible_description"));
695     
696     if(!isApplet()) {
697         splashScreen = new JWindow(getFrame());
698         splashScreen.getContentPane().add(splashLabel);
699         splashScreen.pack();
700         Rectangle screenRect = getFrame().getGraphicsConfiguration().getBounds();
701             splashScreen.setSize(550, 300);
702         splashScreen.setLocation(
703          screenRect.x + screenRect.width/2 - splashScreen.getSize().width/2,
704          screenRect.y + screenRect.height/2 - splashScreen.getSize().height/2);
705             
706     }
707     }
708
709     public void showSplashScreen() {
710     if(!isApplet()) {
711         splashScreen.show();
712     } else {
713         add(splashLabel, BorderLayout.CENTER);
714         validate();
715         repaint();
716     }
717     }
718
719     /**
720      * pop down the spash screen
721      */

722     public void hideSplash() {
723     if(!isApplet()) {
724         splashScreen.setVisible(false);
725         splashScreen = null;
726         splashLabel = null;
727     }
728     }
729
730     // *******************************************************
731
// ****************** Utility Methods ********************
732
// *******************************************************
733

734     /**
735      * Loads a demo from a classname
736      */

737     void loadDemo(String JavaDoc classname) {
738         classname = "demo.swingset." + classname;
739     setStatus(getString("Status.loading") + getString(classname + ".name"));
740     DemoModule demo = null;
741     try {
742         Class JavaDoc demoClass = Class.forName(classname);
743         Constructor demoConstructor = demoClass.getConstructor(new Class JavaDoc[]{SwingSet2.class});
744         demo = (DemoModule) demoConstructor.newInstance(new Object JavaDoc[]{this});
745         addDemo(demo);
746             toolbar.invalidate();
747     } catch (Exception JavaDoc e) {
748         System.out.println("Error occurred loading demo: " + classname);
749             e.printStackTrace();
750     }
751         setStatus("");
752     }
753     
754     /**
755      * A utility function that layers on top of the LookAndFeel's
756      * isSupportedLookAndFeel() method. Returns true if the LookAndFeel
757      * is supported. Returns false if the LookAndFeel is not supported
758      * and/or if there is any kind of error checking if the LookAndFeel
759      * is supported.
760      *
761      * The L&F menu will use this method to detemine whether the various
762      * L&F options should be active or inactive.
763      *
764      */

765      protected boolean isAvailableLookAndFeel(String JavaDoc laf) {
766          try {
767              Class JavaDoc lnfClass = Class.forName(laf);
768              LookAndFeel newLAF = (LookAndFeel)(lnfClass.newInstance());
769              return newLAF.isSupportedLookAndFeel();
770          } catch(Exception JavaDoc e) { // If ANYTHING weird happens, return false
771
return false;
772          }
773      }
774
775
776     /**
777      * Determines if this is an applet or application
778      */

779     public boolean isApplet() {
780     return (applet != null);
781     }
782
783     /**
784      * Returns the applet instance
785      */

786     public Object JavaDoc getApplet() {
787     return applet;
788     }
789
790
791     /**
792      * Returns the frame instance
793      */

794     public JFrame getFrame() {
795     return frame;
796     }
797
798     /**
799      * Returns the menubar
800      */

801     public JMenuBar getMenuBar() {
802     return menuBar;
803     }
804
805     /**
806      * Returns the toolbar
807      */

808     public ToggleButtonToolBar getToolBar() {
809     return toolbar;
810     }
811
812     /**
813      * Returns the toolbar button group
814      */

815     public ButtonGroup getToolBarGroup() {
816     return toolbarGroup;
817     }
818
819     /**
820      * Returns the content pane wether we're in an applet
821      * or application
822      */

823     public Container getContentPane() {
824     if(contentPane == null) {
825         if(getFrame() != null) {
826         contentPane = getFrame().getContentPane();
827         } else if (getApplet() != null) {
828         contentPane = null;
829         }
830     }
831     return contentPane;
832     }
833
834     /**
835      * Create a frame for SwingSet2 to reside in if brought up
836      * as an application.
837      */

838     public static JFrame createFrame(GraphicsConfiguration gc) {
839     JFrame frame = new JFrame(gc);
840         if (numSSs == 0) {
841             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
842         } else {
843         WindowListener l = new WindowAdapter() {
844             public void windowClosing(WindowEvent e) {
845                     numSSs--;
846                     swingSets.remove(this);
847             }
848         };
849         frame.addWindowListener(l);
850         }
851     return frame;
852     }
853
854
855     /**
856      * Set the status
857      */

858     public void setStatus(String JavaDoc s) {
859     // do the following on the gui thread
860
SwingUtilities.invokeLater(new SwingSetRunnable(this, s) {
861         public void run() {
862         swingset.statusField.setText((String JavaDoc) obj);
863         }
864     });
865     }
866
867
868     /**
869      * This method returns a string from the demo's resource bundle.
870      */

871     public String JavaDoc getString(String JavaDoc key) {
872         if (key.startsWith("demo.swingset."))
873             key = key.substring(14, key.length());
874     String JavaDoc value = null;
875     try {
876         value = getResourceBundle().getString(key);
877     } catch (MissingResourceException e) {
878         System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
879     }
880     if(value == null) {
881         value = "Could not find resource: " + key + " ";
882     }
883     return value;
884     }
885
886     /**
887      * Returns the resource bundle associated with this demo. Used
888      * to get accessable and internationalized strings.
889      */

890     public ResourceBundle getResourceBundle() {
891     if(bundle == null) {
892         bundle = ResourceBundle.getBundle("resources.swingset");
893     }
894     return bundle;
895     }
896
897     /**
898      * Returns a mnemonic from the resource bundle. Typically used as
899      * keyboard shortcuts in menu items.
900      */

901     public char getMnemonic(String JavaDoc key) {
902     return (getString(key)).charAt(0);
903     }
904
905     /**
906      * Creates an icon from an image contained in the "images" directory.
907      */

908     public ImageIcon createImageIcon(String JavaDoc filename, String JavaDoc description) {
909     String JavaDoc path = "/resources/images/" + filename;
910     return new ImageIcon(getClass().getResource(path));
911     }
912
913     /**
914      * If DEBUG is defined, prints debug information out to std ouput.
915      */

916     public void debug(String JavaDoc s) {
917     if(DEBUG) {
918         System.out.println((debugCounter++) + ": " + s);
919     }
920     }
921
922     /**
923      * Stores the current L&F, and calls updateLookAndFeel, below
924      */

925     public void setLookAndFeel(String JavaDoc laf) {
926     if(currentLookAndFeel != laf) {
927         currentLookAndFeel = laf;
928         themesMenu.setEnabled(laf == metal);
929         updateLookAndFeel();
930     }
931     }
932
933     /**
934      * Sets the current L&F on each demo module
935      */

936     public void updateLookAndFeel() {
937     try {
938         UIManager.setLookAndFeel(currentLookAndFeel);
939             for (Iterator itr = swingSets.iterator(); itr.hasNext(); ) {
940                 SwingSet2 ss = (SwingSet2)itr.next();
941             SwingUtilities.updateComponentTreeUI(ss);
942             }
943             // update LAF for the toplevel frame, too
944
if (!isApplet()) {
945                 SwingUtilities.updateComponentTreeUI(getFrame());
946             } else {
947                 //SwingUtilities.updateComponentTreeUI(getApplet());
948
}
949         //SwingUtilities.updateComponentTreeUI(popupMenu);
950

951     } catch (Exception JavaDoc ex) {
952         System.out.println("Failed loading L&F: " + currentLookAndFeel);
953         System.out.println(ex);
954     }
955
956     // lazily update update the UI's for the remaining demos
957
for (int i = 0; i < demosVector.size(); i++) {
958         DemoModule demo = (DemoModule) demosVector.elementAt(i);
959         if(currentDemo != demo) {
960         // do the following on the gui thread
961
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
962             public void run() {
963             SwingUtilities.updateComponentTreeUI(((DemoModule)obj).getDemoPanel());
964             }
965         });
966         }
967     }
968
969     }
970
971     /**
972      * Loads and puts the source code text into JEditorPane in the "Source Code" tab
973      */

974     public void setSourceCode(DemoModule demo) {
975     // do the following on the gui thread
976
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
977         public void run() {
978         swingset.demoSrcPane.setText(((DemoModule)obj).getSourceCode());
979         //swingset.demoSrcPane.setCaretPosition(0);
980
invalidate();
981         }
982     });
983     }
984
985     // *******************************************************
986
// ************** ToggleButtonToolbar *****************
987
// *******************************************************
988
static Insets zeroInsets = new Insets(1,1,1,1);
989     protected class ToggleButtonToolBar extends JToolBar {
990     public ToggleButtonToolBar() {
991         super();
992             setPreferredSize(new swingwt.awt.Dimension(700, 48));
993     }
994
995     JButton addButton(Action a) {
996         JButton b = add(a);
997             parent.invalidate();
998             invalidate();
999             parent.invalidate();
1000            return b;
1001    }
1002    }
1003
1004    // *******************************************************
1005
// ********* ToolBar Panel / Docking Listener ***********
1006
// *******************************************************
1007
class ToolBarPanel extends JPanel implements ContainerListener {
1008
1009    public boolean contains(int x, int y) {
1010        Component c = getParent();
1011        if (c != null) {
1012        Rectangle r = c.getBounds();
1013        return (x >= 0) && (x < r.width) && (y >= 0) && (y < r.height);
1014        }
1015        else {
1016        return super.contains(x,y);
1017        }
1018    }
1019
1020    public void componentAdded(ContainerEvent e) {
1021        Container c = e.getContainer().getParent();
1022        if (c != null) {
1023        c.getParent().validate();
1024        c.getParent().repaint();
1025        }
1026    }
1027
1028    public void componentRemoved(ContainerEvent e) {
1029        Container c = e.getContainer().getParent();
1030        if (c != null) {
1031        c.getParent().validate();
1032        c.getParent().repaint();
1033        }
1034    }
1035    }
1036
1037    // *******************************************************
1038
// ****************** Runnables ***********************
1039
// *******************************************************
1040

1041    /**
1042     * Generic SwingSet2 runnable. This is intended to run on the
1043     * AWT gui event thread so as not to muck things up by doing
1044     * gui work off the gui thread. Accepts a SwingSet2 and an Object
1045     * as arguments, which gives subtypes of this class the two
1046     * "must haves" needed in most runnables for this demo.
1047     */

1048    class SwingSetRunnable implements Runnable JavaDoc {
1049    protected SwingSet2 swingset;
1050    protected Object JavaDoc obj;
1051    
1052    public SwingSetRunnable(SwingSet2 swingset, Object JavaDoc obj) {
1053        this.swingset = swingset;
1054        this.obj = obj;
1055    }
1056
1057    public void run() {
1058    }
1059    }
1060    
1061    
1062    // *******************************************************
1063
// ******************** Actions ***********************
1064
// *******************************************************
1065

1066    public class SwitchToDemoAction extends AbstractAction {
1067    SwingSet2 swingset;
1068    DemoModule demo;
1069    
1070    public SwitchToDemoAction(SwingSet2 swingset, DemoModule demo) {
1071        super(demo.getName(), demo.getIcon());
1072        this.swingset = swingset;
1073        this.demo = demo;
1074    }
1075
1076    public void actionPerformed(ActionEvent e) {
1077        swingset.setDemo(demo);
1078    }
1079    }
1080
1081    class OkAction extends AbstractAction {
1082    JDialog aboutBox;
1083
1084        protected OkAction(JDialog aboutBox) {
1085            super("OkAction");
1086        this.aboutBox = aboutBox;
1087        }
1088
1089        public void actionPerformed(ActionEvent e) {
1090        aboutBox.setVisible(false);
1091    }
1092    }
1093
1094    class ChangeLookAndFeelAction extends AbstractAction {
1095    SwingSet2 swingset;
1096    String JavaDoc laf;
1097        protected ChangeLookAndFeelAction(SwingSet2 swingset, String JavaDoc laf) {
1098            super("ChangeTheme");
1099        this.swingset = swingset;
1100        this.laf = laf;
1101        }
1102
1103        public void actionPerformed(ActionEvent e) {
1104        swingset.setLookAndFeel(laf);
1105    }
1106    }
1107
1108    class ActivatePopupMenuAction extends AbstractAction {
1109    SwingSet2 swingset;
1110    JPopupMenu popup;
1111    protected ActivatePopupMenuAction(SwingSet2 swingset, JPopupMenu popup) {
1112        super("ActivatePopupMenu");
1113        this.swingset = swingset;
1114        this.popup = popup;
1115    }
1116    
1117    public void actionPerformed(ActionEvent e) {
1118        Dimension invokerSize = getSize();
1119        Dimension popupSize = popup.getPreferredSize();
1120        popup.show(swingset, (invokerSize.width - popupSize.width) / 2,
1121               (invokerSize.height - popupSize.height) / 2);
1122    }
1123    }
1124
1125    // Turns on all possible auditory feedback
1126
class OnAudioAction extends AbstractAction {
1127    SwingSet2 swingset;
1128        protected OnAudioAction(SwingSet2 swingset) {
1129            super("Audio On");
1130        this.swingset = swingset;
1131        }
1132        public void actionPerformed(ActionEvent e) {
1133        UIManager.put("AuditoryCues.playList",
1134              UIManager.get("AuditoryCues.allAuditoryCues"));
1135        swingset.updateLookAndFeel();
1136    }
1137    }
1138
1139    // Turns on the default amount of auditory feedback
1140
class DefaultAudioAction extends AbstractAction {
1141    SwingSet2 swingset;
1142        protected DefaultAudioAction(SwingSet2 swingset) {
1143            super("Audio Default");
1144        this.swingset = swingset;
1145        }
1146        public void actionPerformed(ActionEvent e) {
1147        UIManager.put("AuditoryCues.playList",
1148              UIManager.get("AuditoryCues.defaultCueList"));
1149        swingset.updateLookAndFeel();
1150    }
1151    }
1152
1153    // Turns off all possible auditory feedback
1154
class OffAudioAction extends AbstractAction {
1155    SwingSet2 swingset;
1156        protected OffAudioAction(SwingSet2 swingset) {
1157            super("Audio Off");
1158        this.swingset = swingset;
1159        }
1160        public void actionPerformed(ActionEvent e) {
1161        UIManager.put("AuditoryCues.playList",
1162              UIManager.get("AuditoryCues.noAuditoryCues"));
1163        swingset.updateLookAndFeel();
1164    }
1165    }
1166
1167    // Turns on or off the tool tips for the demo.
1168
class ToolTipAction extends AbstractAction {
1169        SwingSet2 swingset;
1170        boolean status;
1171        protected ToolTipAction(SwingSet2 swingset, boolean status) {
1172            super("ToolTip Control");
1173            this.swingset = swingset;
1174            this.status = status;
1175        }
1176
1177        public void actionPerformed(ActionEvent e) {
1178            //ToolTipManager.sharedInstance().setEnabled(status);
1179
}
1180    }
1181
1182    class ChangeThemeAction extends AbstractAction {
1183    SwingSet2 swingset;
1184    DefaultMetalTheme theme;
1185        protected ChangeThemeAction(SwingSet2 swingset, DefaultMetalTheme theme) {
1186            super("ChangeTheme");
1187        this.swingset = swingset;
1188        this.theme = theme;
1189        }
1190
1191        public void actionPerformed(ActionEvent e) {
1192        MetalLookAndFeel.setCurrentTheme(theme);
1193        swingset.updateLookAndFeel();
1194    }
1195    }
1196
1197    class ExitAction extends AbstractAction {
1198    SwingSet2 swingset;
1199        protected ExitAction(SwingSet2 swingset) {
1200            super("ExitAction");
1201        this.swingset = swingset;
1202        }
1203
1204        public void actionPerformed(ActionEvent e) {
1205        System.exit(0);
1206        }
1207    }
1208
1209    class AboutAction extends AbstractAction {
1210    SwingSet2 swingset;
1211        protected AboutAction(SwingSet2 swingset) {
1212            super("AboutAction");
1213        this.swingset = swingset;
1214        }
1215    
1216        public void actionPerformed(ActionEvent e) {
1217            // JPanel panel = new JPanel(new BorderLayout());
1218
JLabel panel = new JLabel(swingset.createImageIcon("About.jpg", "AboutBox.accessible_description"));
1219
1220            aboutBox = new JDialog(swingset.getFrame(), getString("AboutBox.title"), false);
1221            aboutBox.getContentPane().add(panel, BorderLayout.CENTER);
1222
1223            // JButton button = new JButton(getString("AboutBox.ok_button_text"));
1224
JPanel buttonpanel = new JPanel();
1225            buttonpanel.setOpaque(false);
1226            JButton button = (JButton) buttonpanel.add(
1227                new JButton(getString("AboutBox.ok_button_text"))
1228            );
1229            aboutBox.add(buttonpanel, BorderLayout.SOUTH);
1230
1231            button.addActionListener(new OkAction(aboutBox));
1232        aboutBox.pack();
1233        Point p = swingset.getLocationOnScreen();
1234        aboutBox.setLocation(p.x + 10, p.y +10);
1235            aboutBox.setSize(480, 480);
1236        aboutBox.show();
1237    }
1238    }
1239
1240    class MultiScreenAction extends AbstractAction {
1241        static final int ALL_SCREENS = -1;
1242        int screen;
1243        protected MultiScreenAction(SwingSet2 swingset, int screen) {
1244            super("MultiScreenAction");
1245            this.screen = screen;
1246        }
1247
1248        public void actionPerformed(ActionEvent e) {
1249            SwingSet2 swingset = new SwingSet2(null,
1250                                  null);
1251        }
1252    }
1253
1254    // *******************************************************
1255
// ********************** Misc *************************
1256
// *******************************************************
1257

1258    class DemoLoadThread extends Thread JavaDoc {
1259    SwingSet2 swingset;
1260    
1261    public DemoLoadThread(SwingSet2 swingset) {
1262        this.swingset = swingset;
1263    }
1264
1265    public void run() {
1266        swingset.loadDemos();
1267    }
1268    }
1269
1270    class AboutPanel extends JComponent {
1271    ImageIcon aboutimage = null;
1272    SwingSet2 swingset = null;
1273
1274    public AboutPanel(SwingSet2 swingset) {
1275        this.swingset = swingset;
1276        aboutimage = swingset.createImageIcon("About.jpg", "AboutBox.accessible_description");
1277        setOpaque(false);
1278    }
1279
1280    public void paint(Graphics g) {
1281        aboutimage.paintIcon(this, g, 0, 0);
1282        super.paint(g);
1283    }
1284
1285    public Dimension getPreferredSize() {
1286        return new Dimension(aboutimage.getIconWidth(),
1287                 aboutimage.getIconHeight());
1288    }
1289    }
1290
1291}
1292
1293
Popular Tags