KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SwingSet2


1 /*
2  * @(#)SwingSet2.java 1.54 06/05/31
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)SwingSet2.java 1.54 06/05/31
39  */

40
41 import javax.swing.*;
42 import javax.swing.event.*;
43 import javax.swing.text.*;
44 import javax.swing.border.*;
45 import javax.swing.colorchooser.*;
46 import javax.swing.filechooser.*;
47 import javax.accessibility.*;
48
49 import javax.swing.plaf.metal.MetalTheme JavaDoc;
50 import javax.swing.plaf.metal.OceanTheme JavaDoc;
51 import javax.swing.plaf.metal.DefaultMetalTheme JavaDoc;
52 import javax.swing.plaf.metal.MetalLookAndFeel JavaDoc;
53
54 import java.lang.reflect.*;
55 import java.awt.*;
56 import java.awt.event.*;
57 import java.beans.*;
58 import java.util.*;
59 import java.io.*;
60 import java.applet.*;
61 import java.net.*;
62
63 /**
64  * A demo that shows all of the Swing components.
65  *
66  * @version 1.54 05/31/06
67  * @author Jeff Dinkins
68  */

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

193     public SwingSet2(SwingSet2Applet applet, GraphicsConfiguration gc) {
194
195     // Note that applet may be null if this is started as an application
196
this.applet = applet;
197
198         if (!isApplet()) {
199             frame = createFrame(gc);
200         }
201
202     // set the layout
203
setLayout(new BorderLayout());
204
205     // set the preferred size of the demo
206
setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT));
207
208     initializeDemo();
209     preloadFirstDemo();
210
211     // Show the demo. Must do this on the GUI thread using invokeLater.
212
SwingUtilities.invokeLater(new Runnable JavaDoc() {
213         public void run() {
214         showSwingSet2();
215         }
216     });
217
218     // Start loading the rest of the demo in the background
219
DemoLoadThread demoLoader = new DemoLoadThread(this);
220     demoLoader.start();
221     }
222
223
224     /**
225      * SwingSet2 Main. Called only if we're an application, not an applet.
226      */

227     public static void main(String JavaDoc[] args) {
228     // Create SwingSet on the default monitor
229
UIManager.put("swing.boldMetal", Boolean.FALSE);
230     SwingSet2 swingset = new SwingSet2(null, GraphicsEnvironment.
231                                              getLocalGraphicsEnvironment().
232                                              getDefaultScreenDevice().
233                                              getDefaultConfiguration());
234     }
235
236     // *******************************************************
237
// *************** Demo Loading Methods ******************
238
// *******************************************************
239

240     
241     
242     public void initializeDemo() {
243     JPanel top = new JPanel();
244     top.setLayout(new BorderLayout());
245     add(top, BorderLayout.NORTH);
246
247     menuBar = createMenus();
248     
249     if (isApplet()) {
250         applet.setJMenuBar(menuBar);
251     } else {
252         frame.setJMenuBar(menuBar);
253     }
254
255     // creates popup menu accessible via keyboard
256
popupMenu = createPopupMenu();
257
258     ToolBarPanel toolbarPanel = new ToolBarPanel();
259     toolbarPanel.setLayout(new BorderLayout());
260     toolbar = new ToggleButtonToolBar();
261     toolbarPanel.add(toolbar, BorderLayout.CENTER);
262     top.add(toolbarPanel, BorderLayout.SOUTH);
263     toolbarPanel.addContainerListener(toolbarPanel);
264
265     tabbedPane = new JTabbedPane();
266     add(tabbedPane, BorderLayout.CENTER);
267     tabbedPane.getModel().addChangeListener(new TabListener());
268
269     statusField = new JTextField("");
270     statusField.setEditable(false);
271     add(statusField, BorderLayout.SOUTH);
272     
273     demoPanel = new JPanel();
274     demoPanel.setLayout(new BorderLayout());
275     demoPanel.setBorder(new EtchedBorder());
276     tabbedPane.addTab("Hi There!", demoPanel);
277     
278     // Add html src code viewer
279
demoSrcPane = new JEditorPane("text/html", getString("SourceCode.loading"));
280     demoSrcPane.setEditable(false);
281
282     JScrollPane scroller = new JScrollPane();
283     scroller.getViewport().add(demoSrcPane);
284     
285     tabbedPane.addTab(
286         getString("TabbedPane.src_label"),
287         null,
288         scroller,
289         getString("TabbedPane.src_tooltip")
290     );
291     }
292
293     DemoModule currentTabDemo = null;
294     class TabListener implements ChangeListener {
295     public void stateChanged(ChangeEvent e) {
296         SingleSelectionModel model = (SingleSelectionModel) e.getSource();
297         boolean srcSelected = model.getSelectedIndex() == 1;
298         if(currentTabDemo != currentDemo && demoSrcPane != null && srcSelected) {
299         demoSrcPane.setText(getString("SourceCode.loading"));
300         repaint();
301         }
302         if(currentTabDemo != currentDemo && srcSelected) {
303         currentTabDemo = currentDemo;
304         setSourceCode(currentDemo);
305         }
306     }
307     }
308
309
310     /**
311      * Create menus
312      */

313     public JMenuBar createMenus() {
314     JMenuItem mi;
315     // ***** create the menubar ****
316
JMenuBar menuBar = new JMenuBar();
317     menuBar.getAccessibleContext().setAccessibleName(
318         getString("MenuBar.accessible_description"));
319
320     // ***** create File menu
321
JMenu fileMenu = (JMenu) menuBar.add(new JMenu(getString("FileMenu.file_label")));
322         fileMenu.setMnemonic(getMnemonic("FileMenu.file_mnemonic"));
323     fileMenu.getAccessibleContext().setAccessibleDescription(getString("FileMenu.accessible_description"));
324
325     createMenuItem(fileMenu, "FileMenu.about_label", "FileMenu.about_mnemonic",
326                "FileMenu.about_accessible_description", new AboutAction(this));
327
328         fileMenu.addSeparator();
329
330     createMenuItem(fileMenu, "FileMenu.open_label", "FileMenu.open_mnemonic",
331                "FileMenu.open_accessible_description", null);
332
333     createMenuItem(fileMenu, "FileMenu.save_label", "FileMenu.save_mnemonic",
334                "FileMenu.save_accessible_description", null);
335
336     createMenuItem(fileMenu, "FileMenu.save_as_label", "FileMenu.save_as_mnemonic",
337                "FileMenu.save_as_accessible_description", null);
338
339
340     if(!isApplet()) {
341         fileMenu.addSeparator();
342         
343         createMenuItem(fileMenu, "FileMenu.exit_label", "FileMenu.exit_mnemonic",
344                "FileMenu.exit_accessible_description", new ExitAction(this)
345         );
346     }
347
348         // Create these menu items for the first SwingSet only.
349
if (numSSs == 0) {
350     // ***** create laf switcher menu
351
lafMenu = (JMenu) menuBar.add(new JMenu(getString("LafMenu.laf_label")));
352         lafMenu.setMnemonic(getMnemonic("LafMenu.laf_mnemonic"));
353     lafMenu.getAccessibleContext().setAccessibleDescription(
354         getString("LafMenu.laf_accessible_description"));
355
356     mi = createLafMenuItem(lafMenu, "LafMenu.java_label", "LafMenu.java_mnemonic",
357                "LafMenu.java_accessible_description", metal);
358     mi.setSelected(true); // this is the default l&f
359

360         UIManager.LookAndFeelInfo[] lafInfo = UIManager.
361                                        getInstalledLookAndFeels();
362
363         for (int counter = 0; counter < lafInfo.length; counter++) {
364             String JavaDoc className = lafInfo[counter].getClassName();
365             if (className == motif) {
366                 createLafMenuItem(lafMenu, "LafMenu.motif_label", "LafMenu.motif_mnemonic",
367                         "LafMenu.motif_accessible_description", motif);
368             } else if (className == windows) {
369                 createLafMenuItem(lafMenu, "LafMenu.windows_label", "LafMenu.windows_mnemonic",
370                         "LafMenu.windows_accessible_description", windows);
371             } else if (className == gtk) {
372                 createLafMenuItem(lafMenu, "LafMenu.gtk_label", "LafMenu.gtk_mnemonic",
373                         "LafMenu.gtk_accessible_description", gtk);
374             }
375         }
376
377     // ***** create themes menu
378
themesMenu = (JMenu) menuBar.add(new JMenu(getString("ThemesMenu.themes_label")));
379         themesMenu.setMnemonic(getMnemonic("ThemesMenu.themes_mnemonic"));
380     themesMenu.getAccessibleContext().setAccessibleDescription(
381         getString("ThemesMenu.themes_accessible_description"));
382
383     // ***** create the audio submenu under the theme menu
384
audioMenu = (JMenu) themesMenu.add(new JMenu(getString("AudioMenu.audio_label")));
385         audioMenu.setMnemonic(getMnemonic("AudioMenu.audio_mnemonic"));
386     audioMenu.getAccessibleContext().setAccessibleDescription(
387         getString("AudioMenu.audio_accessible_description"));
388
389     createAudioMenuItem(audioMenu, "AudioMenu.on_label",
390                 "AudioMenu.on_mnemonic",
391                 "AudioMenu.on_accessible_description",
392                 new OnAudioAction(this));
393
394     mi = createAudioMenuItem(audioMenu, "AudioMenu.default_label",
395                  "AudioMenu.default_mnemonic",
396                  "AudioMenu.default_accessible_description",
397                  new DefaultAudioAction(this));
398     mi.setSelected(true); // This is the default feedback setting
399

400     createAudioMenuItem(audioMenu, "AudioMenu.off_label",
401                 "AudioMenu.off_mnemonic",
402                 "AudioMenu.off_accessible_description",
403                 new OffAudioAction(this));
404
405
406     // ***** create the font submenu under the theme menu
407
JMenu fontMenu = (JMenu) themesMenu.add(new JMenu(getString("FontMenu.fonts_label")));
408         fontMenu.setMnemonic(getMnemonic("FontMenu.fonts_mnemonic"));
409     fontMenu.getAccessibleContext().setAccessibleDescription(
410         getString("FontMenu.fonts_accessible_description"));
411         ButtonGroup fontButtonGroup = new ButtonGroup();
412         mi = createButtonGroupMenuItem(fontMenu, "FontMenu.plain_label",
413                 "FontMenu.plain_mnemonic",
414                 "FontMenu.plain_accessible_description",
415                 new ChangeFontAction(this, true), fontButtonGroup);
416         mi.setSelected(true);
417         mi = createButtonGroupMenuItem(fontMenu, "FontMenu.bold_label",
418                 "FontMenu.bold_mnemonic",
419                 "FontMenu.bold_accessible_description",
420                 new ChangeFontAction(this, false), fontButtonGroup);
421
422
423
424     // *** now back to adding color/font themes to the theme menu
425
mi = createThemesMenuItem(themesMenu, "ThemesMenu.ocean_label",
426                                               "ThemesMenu.ocean_mnemonic",
427                                               "ThemesMenu.ocean_accessible_description",
428                                               new OceanTheme JavaDoc());
429         mi.setSelected(true); // This is the default theme
430

431         createThemesMenuItem(themesMenu, "ThemesMenu.steel_label",
432                              "ThemesMenu.steel_mnemonic",
433                              "ThemesMenu.steel_accessible_description",
434                              new DefaultMetalTheme JavaDoc());
435     
436     createThemesMenuItem(themesMenu, "ThemesMenu.aqua_label", "ThemesMenu.aqua_mnemonic",
437                "ThemesMenu.aqua_accessible_description", new AquaTheme());
438
439     createThemesMenuItem(themesMenu, "ThemesMenu.charcoal_label", "ThemesMenu.charcoal_mnemonic",
440                "ThemesMenu.charcoal_accessible_description", new CharcoalTheme());
441
442     createThemesMenuItem(themesMenu, "ThemesMenu.contrast_label", "ThemesMenu.contrast_mnemonic",
443                "ThemesMenu.contrast_accessible_description", new ContrastTheme());
444
445     createThemesMenuItem(themesMenu, "ThemesMenu.emerald_label", "ThemesMenu.emerald_mnemonic",
446                "ThemesMenu.emerald_accessible_description", new EmeraldTheme());
447
448     createThemesMenuItem(themesMenu, "ThemesMenu.ruby_label", "ThemesMenu.ruby_mnemonic",
449                "ThemesMenu.ruby_accessible_description", new RubyTheme());
450
451         // ***** create the options menu
452
optionsMenu = (JMenu)menuBar.add(
453             new JMenu(getString("OptionsMenu.options_label")));
454         optionsMenu.setMnemonic(getMnemonic("OptionsMenu.options_mnemonic"));
455         optionsMenu.getAccessibleContext().setAccessibleDescription(
456             getString("OptionsMenu.options_accessible_description"));
457
458         // ***** create tool tip submenu item.
459
mi = createCheckBoxMenuItem(optionsMenu, "OptionsMenu.tooltip_label",
460                 "OptionsMenu.tooltip_mnemonic",
461                 "OptionsMenu.tooltip_accessible_description",
462                 new ToolTipAction());
463         mi.setSelected(true);
464
465         // ***** create drag support submenu item.
466
createCheckBoxMenuItem(optionsMenu, "OptionsMenu.dragEnabled_label",
467                 "OptionsMenu.dragEnabled_mnemonic",
468                 "OptionsMenu.dragEnabled_accessible_description",
469                 new DragSupportAction());
470
471         }
472
473
474     // ***** create the multiscreen menu, if we have multiple screens
475
if (!isApplet()) {
476         GraphicsDevice[] screens = GraphicsEnvironment.
477                                     getLocalGraphicsEnvironment().
478                                     getScreenDevices();
479         if (screens.length > 1) {
480
481             JMenu multiScreenMenu = (JMenu) menuBar.add(new JMenu(
482                                      getString("MultiMenu.multi_label")));
483
484             multiScreenMenu.setMnemonic(getMnemonic("MultiMenu.multi_mnemonic"));
485             multiScreenMenu.getAccessibleContext().setAccessibleDescription(
486              getString("MultiMenu.multi_accessible_description"));
487
488             createMultiscreenMenuItem(multiScreenMenu, MultiScreenAction.ALL_SCREENS);
489             for (int i = 0; i < screens.length; i++) {
490                 createMultiscreenMenuItem(multiScreenMenu, i);
491             }
492         }
493     }
494
495     return menuBar;
496     }
497
498     /**
499      * Create a checkbox menu menu item
500      */

501     private JMenuItem createCheckBoxMenuItem(JMenu menu, String JavaDoc label,
502                                              String JavaDoc mnemonic,
503                                              String JavaDoc accessibleDescription,
504                                              Action action) {
505         JCheckBoxMenuItem mi = (JCheckBoxMenuItem)menu.add(
506                 new JCheckBoxMenuItem(getString(label)));
507         mi.setMnemonic(getMnemonic(mnemonic));
508         mi.getAccessibleContext().setAccessibleDescription(getString(
509                 accessibleDescription));
510         mi.addActionListener(action);
511         return mi;
512     }
513
514     /**
515      * Create a radio button menu menu item for items that are part of a
516      * button group.
517      */

518     private JMenuItem createButtonGroupMenuItem(JMenu menu, String JavaDoc label,
519                                                 String JavaDoc mnemonic,
520                                                 String JavaDoc accessibleDescription,
521                                                 Action action,
522                                                 ButtonGroup buttonGroup) {
523         JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add(
524                 new JRadioButtonMenuItem(getString(label)));
525         buttonGroup.add(mi);
526         mi.setMnemonic(getMnemonic(mnemonic));
527         mi.getAccessibleContext().setAccessibleDescription(getString(
528                 accessibleDescription));
529         mi.addActionListener(action);
530         return mi;
531     }
532
533     /**
534      * Create the theme's audio submenu
535      */

536     public JMenuItem createAudioMenuItem(JMenu menu, String JavaDoc label,
537                      String JavaDoc mnemonic,
538                      String JavaDoc accessibleDescription,
539                      Action action) {
540         JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
541     audioMenuGroup.add(mi);
542     mi.setMnemonic(getMnemonic(mnemonic));
543     mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
544     mi.addActionListener(action);
545
546     return mi;
547     }
548
549     /**
550      * Creates a generic menu item
551      */

552     public JMenuItem createMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
553                    String JavaDoc accessibleDescription, Action action) {
554         JMenuItem mi = (JMenuItem) menu.add(new JMenuItem(getString(label)));
555     mi.setMnemonic(getMnemonic(mnemonic));
556     mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
557     mi.addActionListener(action);
558     if(action == null) {
559         mi.setEnabled(false);
560     }
561     return mi;
562     }
563
564     /**
565      * Creates a JRadioButtonMenuItem for the Themes menu
566      */

567     public JMenuItem createThemesMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
568                    String JavaDoc accessibleDescription, MetalTheme JavaDoc theme) {
569         JRadioButtonMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
570     themesMenuGroup.add(mi);
571     mi.setMnemonic(getMnemonic(mnemonic));
572     mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
573     mi.addActionListener(new ChangeThemeAction(this, theme));
574
575     return mi;
576     }
577
578     /**
579      * Creates a JRadioButtonMenuItem for the Look and Feel menu
580      */

581     public JMenuItem createLafMenuItem(JMenu menu, String JavaDoc label, String JavaDoc mnemonic,
582                    String JavaDoc accessibleDescription, String JavaDoc laf) {
583         JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
584     lafMenuGroup.add(mi);
585     mi.setMnemonic(getMnemonic(mnemonic));
586     mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
587     mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
588
589     mi.setEnabled(isAvailableLookAndFeel(laf));
590
591     return mi;
592     }
593
594     /**
595      * Creates a multi-screen menu item
596      */

597     public JMenuItem createMultiscreenMenuItem(JMenu menu, int screen) {
598         JMenuItem mi = null;
599         if (screen == MultiScreenAction.ALL_SCREENS) {
600             mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.all_label")));
601             mi.setMnemonic(getMnemonic("MultiMenu.all_mnemonic"));
602             mi.getAccessibleContext().setAccessibleDescription(getString(
603                                                                  "MultiMenu.all_accessible_description"));
604         }
605         else {
606             mi = (JMenuItem) menu.add(new JMenuItem(getString("MultiMenu.single_label") + " " +
607                                                                                                  screen));
608             mi.setMnemonic(KeyEvent.VK_0 + screen);
609             mi.getAccessibleContext().setAccessibleDescription(getString(
610                                                "MultiMenu.single_accessible_description") + " " + screen);
611                                                                                             
612         }
613         mi.addActionListener(new MultiScreenAction(this, screen));
614         return mi;
615     }
616
617     public JPopupMenu createPopupMenu() {
618     JPopupMenu popup = new JPopupMenu("JPopupMenu demo");
619     
620     createPopupMenuItem(popup, "LafMenu.java_label", "LafMenu.java_mnemonic",
621                 "LafMenu.java_accessible_description", metal);
622     
623     createPopupMenuItem(popup, "LafMenu.mac_label", "LafMenu.mac_mnemonic",
624                 "LafMenu.mac_accessible_description", mac);
625     
626     createPopupMenuItem(popup, "LafMenu.motif_label", "LafMenu.motif_mnemonic",
627                 "LafMenu.motif_accessible_description", motif);
628     
629     createPopupMenuItem(popup, "LafMenu.windows_label", "LafMenu.windows_mnemonic",
630                 "LafMenu.windows_accessible_description", windows);
631     
632     createPopupMenuItem(popup, "LafMenu.gtk_label", "LafMenu.gtk_mnemonic",
633                 "LafMenu.gtk_accessible_description", gtk);
634
635     // register key binding to activate popup menu
636
InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
637     map.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.SHIFT_MASK),
638         "postMenuAction");
639         map.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTEXT_MENU, 0), "postMenuAction");
640     getActionMap().put("postMenuAction", new ActivatePopupMenuAction(this, popup));
641     
642     return popup;
643     }
644     
645     /**
646      * Creates a JMenuItem for the Look and Feel popup menu
647      */

648     public JMenuItem createPopupMenuItem(JPopupMenu menu, String JavaDoc label, String JavaDoc mnemonic,
649                      String JavaDoc accessibleDescription, String JavaDoc laf) {
650     JMenuItem mi = menu.add(new JMenuItem(getString(label)));
651     popupMenuGroup.add(mi);
652     mi.setMnemonic(getMnemonic(mnemonic));
653     mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
654     mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
655     mi.setEnabled(isAvailableLookAndFeel(laf));
656     
657     return mi;
658     }
659     
660
661     /**
662      * Load the first demo. This is done separately from the remaining demos
663      * so that we can get SwingSet2 up and available to the user quickly.
664      */

665     public void preloadFirstDemo() {
666     DemoModule demo = addDemo(new InternalFrameDemo(this));
667     setDemo(demo);
668     }
669
670
671     /**
672      * Add a demo to the toolbar
673      */

674     public DemoModule addDemo(DemoModule demo) {
675     demosList.add(demo);
676         if (dragEnabled) {
677             demo.updateDragEnabled(true);
678         }
679     // do the following on the gui thread
680
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
681         public void run() {
682         SwitchToDemoAction action = new SwitchToDemoAction(swingset, (DemoModule) obj);
683         JToggleButton tb = swingset.getToolBar().addToggleButton(action);
684         swingset.getToolBarGroup().add(tb);
685         if(swingset.getToolBarGroup().getSelection() == null) {
686             tb.setSelected(true);
687         }
688         tb.setText(null);
689         tb.setToolTipText(((DemoModule)obj).getToolTip());
690
691         if(demos[demos.length-1].equals(obj.getClass().getName())) {
692             setStatus(getString("Status.popupMenuAccessible"));
693         }
694           
695         }
696     });
697     return demo;
698     }
699
700
701     /**
702      * Sets the current demo
703      */

704     public void setDemo(DemoModule demo) {
705     currentDemo = demo;
706
707     // Ensure panel's UI is current before making visible
708
JComponent currentDemoPanel = demo.getDemoPanel();
709     SwingUtilities.updateComponentTreeUI(currentDemoPanel);
710
711     demoPanel.removeAll();
712     demoPanel.add(currentDemoPanel, BorderLayout.CENTER);
713
714     tabbedPane.setSelectedIndex(0);
715     tabbedPane.setTitleAt(0, demo.getName());
716     tabbedPane.setToolTipTextAt(0, demo.getToolTip());
717     }
718
719
720     /**
721      * Bring up the SwingSet2 demo by showing the frame (only
722      * applicable if coming up as an application, not an applet);
723      */

724     public void showSwingSet2() {
725     if(!isApplet() && getFrame() != null) {
726         // put swingset in a frame and show it
727
JFrame f = getFrame();
728         f.setTitle(getString("Frame.title"));
729         f.getContentPane().add(this, BorderLayout.CENTER);
730         f.pack();
731
732         Rectangle screenRect = f.getGraphicsConfiguration().getBounds();
733             Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
734                     f.getGraphicsConfiguration());
735
736             // Make sure we don't place the demo off the screen.
737
int centerWidth = screenRect.width < f.getSize().width ?
738                     screenRect.x :
739                     screenRect.x + screenRect.width/2 - f.getSize().width/2;
740             int centerHeight = screenRect.height < f.getSize().height ?
741                     screenRect.y :
742                     screenRect.y + screenRect.height/2 - f.getSize().height/2;
743
744             centerHeight = centerHeight < screenInsets.top ?
745                     screenInsets.top : centerHeight;
746
747             f.setLocation(centerWidth, centerHeight);
748         f.show();
749             numSSs++;
750             swingSets.add(this);
751     }
752     }
753
754     // *******************************************************
755
// ****************** Utility Methods ********************
756
// *******************************************************
757

758     /**
759      * Loads a demo from a classname
760      */

761     void loadDemo(String JavaDoc classname) {
762     setStatus(getString("Status.loading") + getString(classname + ".name"));
763     DemoModule demo = null;
764     try {
765         Class JavaDoc demoClass = Class.forName(classname);
766         Constructor demoConstructor = demoClass.getConstructor(new Class JavaDoc[]{SwingSet2.class});
767         demo = (DemoModule) demoConstructor.newInstance(new Object JavaDoc[]{this});
768         addDemo(demo);
769     } catch (Exception JavaDoc e) {
770         System.out.println("Error occurred loading demo: " + classname);
771     }
772     }
773     
774     /**
775      * A utility function that layers on top of the LookAndFeel's
776      * isSupportedLookAndFeel() method. Returns true if the LookAndFeel
777      * is supported. Returns false if the LookAndFeel is not supported
778      * and/or if there is any kind of error checking if the LookAndFeel
779      * is supported.
780      *
781      * The L&F menu will use this method to detemine whether the various
782      * L&F options should be active or inactive.
783      *
784      */

785      protected boolean isAvailableLookAndFeel(String JavaDoc laf) {
786          try {
787              Class JavaDoc lnfClass = Class.forName(laf);
788              LookAndFeel JavaDoc newLAF = (LookAndFeel JavaDoc)(lnfClass.newInstance());
789              return newLAF.isSupportedLookAndFeel();
790          } catch(Exception JavaDoc e) { // If ANYTHING weird happens, return false
791
return false;
792          }
793      }
794
795
796     /**
797      * Determines if this is an applet or application
798      */

799     public boolean isApplet() {
800     return (applet != null);
801     }
802
803     /**
804      * Returns the applet instance
805      */

806     public SwingSet2Applet getApplet() {
807     return applet;
808     }
809
810
811     /**
812      * Returns the frame instance
813      */

814     public JFrame getFrame() {
815     return frame;
816     }
817
818     /**
819      * Returns the menubar
820      */

821     public JMenuBar getMenuBar() {
822     return menuBar;
823     }
824
825     /**
826      * Returns the toolbar
827      */

828     public ToggleButtonToolBar getToolBar() {
829     return toolbar;
830     }
831
832     /**
833      * Returns the toolbar button group
834      */

835     public ButtonGroup getToolBarGroup() {
836     return toolbarGroup;
837     }
838
839     /**
840      * Returns the content pane wether we're in an applet
841      * or application
842      */

843     public Container getContentPane() {
844     if(contentPane == null) {
845         if(getFrame() != null) {
846         contentPane = getFrame().getContentPane();
847         } else if (getApplet() != null) {
848         contentPane = getApplet().getContentPane();
849         }
850     }
851     return contentPane;
852     }
853
854     /**
855      * Create a frame for SwingSet2 to reside in if brought up
856      * as an application.
857      */

858     public static JFrame createFrame(GraphicsConfiguration gc) {
859     JFrame frame = new JFrame(gc);
860         if (numSSs == 0) {
861             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
862         } else {
863         WindowListener l = new WindowAdapter() {
864             public void windowClosing(WindowEvent e) {
865                     numSSs--;
866                     swingSets.remove(this);
867             }
868         };
869         frame.addWindowListener(l);
870         }
871     return frame;
872     }
873
874
875     /**
876      * Set the status
877      */

878     public void setStatus(String JavaDoc s) {
879     // do the following on the gui thread
880
SwingUtilities.invokeLater(new SwingSetRunnable(this, s) {
881         public void run() {
882         swingset.statusField.setText((String JavaDoc) obj);
883         }
884     });
885     }
886
887
888     /**
889      * This method returns a string from the demo's resource bundle.
890      */

891     public String JavaDoc getString(String JavaDoc key) {
892     String JavaDoc value = null;
893     try {
894         value = getResourceBundle().getString(key);
895     } catch (MissingResourceException e) {
896         System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
897     }
898     if(value == null) {
899         value = "Could not find resource: " + key + " ";
900     }
901     return value;
902     }
903
904     void setDragEnabled(boolean dragEnabled) {
905         if (dragEnabled == this.dragEnabled) {
906             return;
907         }
908
909         this.dragEnabled = dragEnabled;
910
911         for (DemoModule dm : demosList) {
912             dm.updateDragEnabled(dragEnabled);
913         }
914
915         demoSrcPane.setDragEnabled(dragEnabled);
916     }
917     
918     boolean isDragEnabled() {
919         return dragEnabled;
920     }
921
922     /**
923      * Returns the resource bundle associated with this demo. Used
924      * to get accessable and internationalized strings.
925      */

926     public ResourceBundle getResourceBundle() {
927     if(bundle == null) {
928         bundle = ResourceBundle.getBundle("resources.swingset");
929     }
930     return bundle;
931     }
932
933     /**
934      * Returns a mnemonic from the resource bundle. Typically used as
935      * keyboard shortcuts in menu items.
936      */

937     public char getMnemonic(String JavaDoc key) {
938     return (getString(key)).charAt(0);
939     }
940
941     /**
942      * Creates an icon from an image contained in the "images" directory.
943      */

944     public ImageIcon createImageIcon(String JavaDoc filename, String JavaDoc description) {
945     String JavaDoc path = "/resources/images/" + filename;
946     return new ImageIcon(getClass().getResource(path));
947     }
948
949     /**
950      * If DEBUG is defined, prints debug information out to std ouput.
951      */

952     public void debug(String JavaDoc s) {
953     if(DEBUG) {
954         System.out.println((debugCounter++) + ": " + s);
955     }
956     }
957
958     /**
959      * Stores the current L&F, and calls updateLookAndFeel, below
960      */

961     public void setLookAndFeel(String JavaDoc laf) {
962     if(currentLookAndFeel != laf) {
963         currentLookAndFeel = laf;
964             /* The recommended way of synchronizing state between multiple
965              * controls that represent the same command is to use Actions.
966              * The code below is a workaround and will be replaced in future
967              * version of SwingSet2 demo.
968              */

969             String JavaDoc lafName = null;
970             if(laf == mac) lafName = getString("LafMenu.mac_label");
971             if(laf == metal) lafName = getString("LafMenu.java_label");
972             if(laf == motif) lafName = getString("LafMenu.motif_label");
973             if(laf == windows) lafName = getString("LafMenu.windows_label");
974             if(laf == gtk) lafName = getString("LafMenu.gtk_label");
975         themesMenu.setEnabled(laf == metal);
976         updateLookAndFeel();
977             for(int i=0;i<lafMenu.getItemCount();i++) {
978                 JMenuItem item = lafMenu.getItem(i);
979                 if(item.getText() == lafName) {
980                     item.setSelected(true);
981                 } else {
982                     item.setSelected(false);
983                 }
984             }
985     }
986     }
987
988     private void updateThisSwingSet() {
989         if (isApplet()) {
990             SwingUtilities.updateComponentTreeUI(getApplet());
991         } else {
992             JFrame frame = getFrame();
993             if (frame == null) {
994                 SwingUtilities.updateComponentTreeUI(this);
995             } else {
996                 SwingUtilities.updateComponentTreeUI(frame);
997             }
998         }
999
1000        SwingUtilities.updateComponentTreeUI(popupMenu);
1001        if (aboutBox != null) {
1002            SwingUtilities.updateComponentTreeUI(aboutBox);
1003        }
1004    }
1005
1006    /**
1007     * Sets the current L&F on each demo module
1008     */

1009    public void updateLookAndFeel() {
1010    try {
1011        UIManager.setLookAndFeel(currentLookAndFeel);
1012            if (isApplet()) {
1013                updateThisSwingSet();
1014            } else {
1015                for (SwingSet2 ss : swingSets) {
1016                    ss.updateThisSwingSet();
1017                }
1018            }
1019    } catch (Exception JavaDoc ex) {
1020        System.out.println("Failed loading L&F: " + currentLookAndFeel);
1021        System.out.println(ex);
1022    }
1023    }
1024
1025    /**
1026     * Loads and puts the source code text into JEditorPane in the "Source Code" tab
1027     */

1028    public void setSourceCode(DemoModule demo) {
1029    // do the following on the gui thread
1030
SwingUtilities.invokeLater(new SwingSetRunnable(this, demo) {
1031        public void run() {
1032        swingset.demoSrcPane.setText(((DemoModule)obj).getSourceCode());
1033        swingset.demoSrcPane.setCaretPosition(0);
1034
1035        }
1036    });
1037    }
1038
1039    // *******************************************************
1040
// ************** ToggleButtonToolbar *****************
1041
// *******************************************************
1042
static Insets zeroInsets = new Insets(1,1,1,1);
1043    protected class ToggleButtonToolBar extends JToolBar {
1044    public ToggleButtonToolBar() {
1045        super();
1046    }
1047
1048    JToggleButton addToggleButton(Action a) {
1049        JToggleButton tb = new JToggleButton(
1050        (String JavaDoc)a.getValue(Action.NAME),
1051        (Icon)a.getValue(Action.SMALL_ICON)
1052        );
1053        tb.setMargin(zeroInsets);
1054        tb.setText(null);
1055        tb.setEnabled(a.isEnabled());
1056        tb.setToolTipText((String JavaDoc)a.getValue(Action.SHORT_DESCRIPTION));
1057        tb.setAction(a);
1058        add(tb);
1059        return tb;
1060    }
1061    }
1062
1063    // *******************************************************
1064
// ********* ToolBar Panel / Docking Listener ***********
1065
// *******************************************************
1066
class ToolBarPanel extends JPanel implements ContainerListener {
1067
1068    public boolean contains(int x, int y) {
1069        Component c = getParent();
1070        if (c != null) {
1071        Rectangle r = c.getBounds();
1072        return (x >= 0) && (x < r.width) && (y >= 0) && (y < r.height);
1073        }
1074        else {
1075        return super.contains(x,y);
1076        }
1077    }
1078
1079    public void componentAdded(ContainerEvent e) {
1080        Container c = e.getContainer().getParent();
1081        if (c != null) {
1082        c.getParent().validate();
1083        c.getParent().repaint();
1084        }
1085    }
1086
1087    public void componentRemoved(ContainerEvent e) {
1088        Container c = e.getContainer().getParent();
1089        if (c != null) {
1090        c.getParent().validate();
1091        c.getParent().repaint();
1092        }
1093    }
1094    }
1095
1096    // *******************************************************
1097
// ****************** Runnables ***********************
1098
// *******************************************************
1099

1100    /**
1101     * Generic SwingSet2 runnable. This is intended to run on the
1102     * AWT gui event thread so as not to muck things up by doing
1103     * gui work off the gui thread. Accepts a SwingSet2 and an Object
1104     * as arguments, which gives subtypes of this class the two
1105     * "must haves" needed in most runnables for this demo.
1106     */

1107    class SwingSetRunnable implements Runnable JavaDoc {
1108    protected SwingSet2 swingset;
1109    protected Object JavaDoc obj;
1110    
1111    public SwingSetRunnable(SwingSet2 swingset, Object JavaDoc obj) {
1112        this.swingset = swingset;
1113        this.obj = obj;
1114    }
1115
1116    public void run() {
1117    }
1118    }
1119    
1120    
1121    // *******************************************************
1122
// ******************** Actions ***********************
1123
// *******************************************************
1124

1125    public class SwitchToDemoAction extends AbstractAction {
1126    SwingSet2 swingset;
1127    DemoModule demo;
1128    
1129    public SwitchToDemoAction(SwingSet2 swingset, DemoModule demo) {
1130        super(demo.getName(), demo.getIcon());
1131        this.swingset = swingset;
1132        this.demo = demo;
1133    }
1134
1135    public void actionPerformed(ActionEvent e) {
1136        swingset.setDemo(demo);
1137    }
1138    }
1139
1140    class OkAction extends AbstractAction {
1141    JDialog aboutBox;
1142
1143        protected OkAction(JDialog aboutBox) {
1144            super("OkAction");
1145        this.aboutBox = aboutBox;
1146        }
1147
1148        public void actionPerformed(ActionEvent e) {
1149        aboutBox.setVisible(false);
1150    }
1151    }
1152
1153    class ChangeLookAndFeelAction extends AbstractAction {
1154    SwingSet2 swingset;
1155    String JavaDoc laf;
1156        protected ChangeLookAndFeelAction(SwingSet2 swingset, String JavaDoc laf) {
1157            super("ChangeTheme");
1158        this.swingset = swingset;
1159        this.laf = laf;
1160        }
1161
1162        public void actionPerformed(ActionEvent e) {
1163        swingset.setLookAndFeel(laf);
1164    }
1165    }
1166
1167    class ActivatePopupMenuAction extends AbstractAction {
1168    SwingSet2 swingset;
1169    JPopupMenu popup;
1170    protected ActivatePopupMenuAction(SwingSet2 swingset, JPopupMenu popup) {
1171        super("ActivatePopupMenu");
1172        this.swingset = swingset;
1173        this.popup = popup;
1174    }
1175    
1176    public void actionPerformed(ActionEvent e) {
1177        Dimension invokerSize = getSize();
1178        Dimension popupSize = popup.getPreferredSize();
1179        popup.show(swingset, (invokerSize.width - popupSize.width) / 2,
1180               (invokerSize.height - popupSize.height) / 2);
1181    }
1182    }
1183
1184    // Turns on all possible auditory feedback
1185
class OnAudioAction extends AbstractAction {
1186    SwingSet2 swingset;
1187        protected OnAudioAction(SwingSet2 swingset) {
1188            super("Audio On");
1189        this.swingset = swingset;
1190        }
1191        public void actionPerformed(ActionEvent e) {
1192        UIManager.put("AuditoryCues.playList",
1193              UIManager.get("AuditoryCues.allAuditoryCues"));
1194        swingset.updateLookAndFeel();
1195    }
1196    }
1197
1198    // Turns on the default amount of auditory feedback
1199
class DefaultAudioAction extends AbstractAction {
1200    SwingSet2 swingset;
1201        protected DefaultAudioAction(SwingSet2 swingset) {
1202            super("Audio Default");
1203        this.swingset = swingset;
1204        }
1205        public void actionPerformed(ActionEvent e) {
1206        UIManager.put("AuditoryCues.playList",
1207              UIManager.get("AuditoryCues.defaultCueList"));
1208        swingset.updateLookAndFeel();
1209    }
1210    }
1211
1212    // Turns off all possible auditory feedback
1213
class OffAudioAction extends AbstractAction {
1214    SwingSet2 swingset;
1215        protected OffAudioAction(SwingSet2 swingset) {
1216            super("Audio Off");
1217        this.swingset = swingset;
1218        }
1219        public void actionPerformed(ActionEvent e) {
1220        UIManager.put("AuditoryCues.playList",
1221              UIManager.get("AuditoryCues.noAuditoryCues"));
1222        swingset.updateLookAndFeel();
1223    }
1224    }
1225
1226    // Turns on or off the tool tips for the demo.
1227
class ToolTipAction extends AbstractAction {
1228        protected ToolTipAction() {
1229            super("ToolTip Control");
1230        }
1231
1232        public void actionPerformed(ActionEvent e) {
1233            boolean status = ((JCheckBoxMenuItem)e.getSource()).isSelected();
1234            ToolTipManager.sharedInstance().setEnabled(status);
1235        }
1236    }
1237
1238    class DragSupportAction extends AbstractAction {
1239        protected DragSupportAction() {
1240            super("DragSupport Control");
1241        }
1242        
1243        public void actionPerformed(ActionEvent e) {
1244            boolean dragEnabled = ((JCheckBoxMenuItem)e.getSource()).isSelected();
1245            if (isApplet()) {
1246                setDragEnabled(dragEnabled);
1247            } else {
1248                for (SwingSet2 ss : swingSets) {
1249                    ss.setDragEnabled(dragEnabled);
1250                }
1251            }
1252        }
1253    }
1254
1255    class ChangeThemeAction extends AbstractAction {
1256    SwingSet2 swingset;
1257    MetalTheme JavaDoc theme;
1258        protected ChangeThemeAction(SwingSet2 swingset, MetalTheme JavaDoc theme) {
1259            super("ChangeTheme");
1260        this.swingset = swingset;
1261        this.theme = theme;
1262        }
1263
1264        public void actionPerformed(ActionEvent e) {
1265        MetalLookAndFeel.setCurrentTheme(theme);
1266        swingset.updateLookAndFeel();
1267    }
1268    }
1269
1270    class ExitAction extends AbstractAction {
1271    SwingSet2 swingset;
1272        protected ExitAction(SwingSet2 swingset) {
1273            super("ExitAction");
1274        this.swingset = swingset;
1275        }
1276
1277        public void actionPerformed(ActionEvent e) {
1278        System.exit(0);
1279        }
1280    }
1281
1282    class AboutAction extends AbstractAction {
1283    SwingSet2 swingset;
1284        protected AboutAction(SwingSet2 swingset) {
1285            super("AboutAction");
1286        this.swingset = swingset;
1287        }
1288    
1289        public void actionPerformed(ActionEvent e) {
1290        if(aboutBox == null) {
1291        // JPanel panel = new JPanel(new BorderLayout());
1292
JPanel panel = new AboutPanel(swingset);
1293        panel.setLayout(new BorderLayout());
1294
1295        aboutBox = new JDialog(swingset.getFrame(), getString("AboutBox.title"), false);
1296                aboutBox.setResizable(false);
1297        aboutBox.getContentPane().add(panel, BorderLayout.CENTER);
1298
1299        // JButton button = new JButton(getString("AboutBox.ok_button_text"));
1300
JPanel buttonpanel = new JPanel();
1301                buttonpanel.setBorder(new javax.swing.border.EmptyBorder JavaDoc(0, 0, 3, 0));
1302        buttonpanel.setOpaque(false);
1303        JButton button = (JButton) buttonpanel.add(
1304            new JButton(getString("AboutBox.ok_button_text"))
1305        );
1306        panel.add(buttonpanel, BorderLayout.SOUTH);
1307
1308        button.addActionListener(new OkAction(aboutBox));
1309        }
1310        aboutBox.pack();
1311            if (isApplet()) {
1312                aboutBox.setLocationRelativeTo(getApplet());
1313            } else {
1314                aboutBox.setLocationRelativeTo(getFrame());
1315            }
1316        aboutBox.show();
1317    }
1318    }
1319
1320    class MultiScreenAction extends AbstractAction {
1321        static final int ALL_SCREENS = -1;
1322        int screen;
1323        protected MultiScreenAction(SwingSet2 swingset, int screen) {
1324            super("MultiScreenAction");
1325            this.screen = screen;
1326        }
1327
1328        public void actionPerformed(ActionEvent e) {
1329            GraphicsDevice[] gds = GraphicsEnvironment.
1330                                   getLocalGraphicsEnvironment().
1331                                   getScreenDevices();
1332            if (screen == ALL_SCREENS) {
1333                for (int i = 0; i < gds.length; i++) {
1334                    SwingSet2 swingset = new SwingSet2(null,
1335                                  gds[i].getDefaultConfiguration());
1336                    swingset.setDragEnabled(dragEnabled);
1337                }
1338            }
1339            else {
1340                SwingSet2 swingset = new SwingSet2(null,
1341                             gds[screen].getDefaultConfiguration());
1342                swingset.setDragEnabled(dragEnabled);
1343            }
1344        }
1345    }
1346
1347    // *******************************************************
1348
// ********************** Misc *************************
1349
// *******************************************************
1350

1351    class DemoLoadThread extends Thread JavaDoc {
1352    SwingSet2 swingset;
1353    
1354    public DemoLoadThread(SwingSet2 swingset) {
1355        this.swingset = swingset;
1356    }
1357
1358    public void run() {
1359        swingset.loadDemos();
1360    }
1361    }
1362
1363    class AboutPanel extends JPanel {
1364    ImageIcon aboutimage = null;
1365    SwingSet2 swingset = null;
1366
1367    public AboutPanel(SwingSet2 swingset) {
1368        this.swingset = swingset;
1369        aboutimage = swingset.createImageIcon("About.jpg", "AboutBox.accessible_description");
1370        setOpaque(false);
1371    }
1372
1373    public void paint(Graphics g) {
1374        aboutimage.paintIcon(this, g, 0, 0);
1375        super.paint(g);
1376    }
1377
1378    public Dimension getPreferredSize() {
1379        return new Dimension(aboutimage.getIconWidth(),
1380                 aboutimage.getIconHeight());
1381    }
1382    }
1383
1384
1385    private class ChangeFontAction extends AbstractAction {
1386    private SwingSet2 swingset;
1387        private boolean plain;
1388
1389        ChangeFontAction(SwingSet2 swingset, boolean plain) {
1390            super("FontMenu");
1391        this.swingset = swingset;
1392            this.plain = plain;
1393        }
1394
1395        public void actionPerformed(ActionEvent e) {
1396            if (plain) {
1397                UIManager.put("swing.boldMetal", Boolean.FALSE);
1398            }
1399            else {
1400                UIManager.put("swing.boldMetal", Boolean.TRUE);
1401            }
1402            // Change the look and feel to force the settings to take effect.
1403
updateLookAndFeel();
1404        }
1405    }
1406}
1407
1408
Popular Tags