1 36 37 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 ; 50 import javax.swing.plaf.metal.OceanTheme ; 51 import javax.swing.plaf.metal.DefaultMetalTheme ; 52 import javax.swing.plaf.metal.MetalLookAndFeel ; 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 69 public class SwingSet2 extends JPanel { 70 71 String [] 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 } else { 95 loadDemo(demos[i]); 96 } 97 i++; 98 } 99 } 100 101 private static final String mac = 103 "com.sun.java.swing.plaf.mac.MacLookAndFeel"; 104 private static final String metal = 105 "javax.swing.plaf.metal.MetalLookAndFeel"; 106 private static final String motif = 107 "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; 108 private static final String windows = 109 "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; 110 private static final String gtk = 111 "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; 112 113 private static String currentLookAndFeel = metal; 115 116 private ArrayList<DemoModule> demosList = new ArrayList<DemoModule>(); 118 119 private static final int PREFERRED_WIDTH = 720; 121 private static final int PREFERRED_HEIGHT = 640; 122 123 private Dimension HGAP = new Dimension(1,5); 125 private Dimension VGAP = new Dimension(5,1); 126 127 private ResourceBundle bundle = null; 129 130 private DemoModule currentDemo = null; 132 private JPanel demoPanel = null; 133 134 private JDialog aboutBox = null; 136 137 private JTextField statusField = null; 139 140 private ToggleButtonToolBar toolbar = null; 142 private ButtonGroup toolbarGroup = new ButtonGroup(); 143 144 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 private JPopupMenu popupMenu = null; 156 private ButtonGroup popupMenuGroup = new ButtonGroup(); 157 158 private JFrame frame = null; 160 161 private SwingSet2Applet applet = null; 163 164 private boolean DEBUG = true; 166 private int debugCounter = 0; 167 168 private JTabbedPane tabbedPane = null; 170 171 private JEditorPane demoSrcPane = null; 172 173 174 Container contentPane = null; 176 177 178 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 193 public SwingSet2(SwingSet2Applet applet, GraphicsConfiguration gc) { 194 195 this.applet = applet; 197 198 if (!isApplet()) { 199 frame = createFrame(gc); 200 } 201 202 setLayout(new BorderLayout()); 204 205 setPreferredSize(new Dimension(PREFERRED_WIDTH,PREFERRED_HEIGHT)); 207 208 initializeDemo(); 209 preloadFirstDemo(); 210 211 SwingUtilities.invokeLater(new Runnable () { 213 public void run() { 214 showSwingSet2(); 215 } 216 }); 217 218 DemoLoadThread demoLoader = new DemoLoadThread(this); 220 demoLoader.start(); 221 } 222 223 224 227 public static void main(String [] args) { 228 UIManager.put("swing.boldMetal", Boolean.FALSE); 230 SwingSet2 swingset = new SwingSet2(null, GraphicsEnvironment. 231 getLocalGraphicsEnvironment(). 232 getDefaultScreenDevice(). 233 getDefaultConfiguration()); 234 } 235 236 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 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 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 313 public JMenuBar createMenus() { 314 JMenuItem mi; 315 JMenuBar menuBar = new JMenuBar(); 317 menuBar.getAccessibleContext().setAccessibleName( 318 getString("MenuBar.accessible_description")); 319 320 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 if (numSSs == 0) { 350 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); 360 UIManager.LookAndFeelInfo[] lafInfo = UIManager. 361 getInstalledLookAndFeels(); 362 363 for (int counter = 0; counter < lafInfo.length; counter++) { 364 String 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 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 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); 400 createAudioMenuItem(audioMenu, "AudioMenu.off_label", 401 "AudioMenu.off_mnemonic", 402 "AudioMenu.off_accessible_description", 403 new OffAudioAction(this)); 404 405 406 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 mi = createThemesMenuItem(themesMenu, "ThemesMenu.ocean_label", 426 "ThemesMenu.ocean_mnemonic", 427 "ThemesMenu.ocean_accessible_description", 428 new OceanTheme ()); 429 mi.setSelected(true); 431 createThemesMenuItem(themesMenu, "ThemesMenu.steel_label", 432 "ThemesMenu.steel_mnemonic", 433 "ThemesMenu.steel_accessible_description", 434 new DefaultMetalTheme ()); 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 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 mi = createCheckBoxMenuItem(optionsMenu, "OptionsMenu.tooltip_label", 460 "OptionsMenu.tooltip_mnemonic", 461 "OptionsMenu.tooltip_accessible_description", 462 new ToolTipAction()); 463 mi.setSelected(true); 464 465 createCheckBoxMenuItem(optionsMenu, "OptionsMenu.dragEnabled_label", 467 "OptionsMenu.dragEnabled_mnemonic", 468 "OptionsMenu.dragEnabled_accessible_description", 469 new DragSupportAction()); 470 471 } 472 473 474 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 501 private JMenuItem createCheckBoxMenuItem(JMenu menu, String label, 502 String mnemonic, 503 String 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 518 private JMenuItem createButtonGroupMenuItem(JMenu menu, String label, 519 String mnemonic, 520 String 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 536 public JMenuItem createAudioMenuItem(JMenu menu, String label, 537 String mnemonic, 538 String 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 552 public JMenuItem createMenuItem(JMenu menu, String label, String mnemonic, 553 String 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 567 public JMenuItem createThemesMenuItem(JMenu menu, String label, String mnemonic, 568 String accessibleDescription, MetalTheme 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 581 public JMenuItem createLafMenuItem(JMenu menu, String label, String mnemonic, 582 String accessibleDescription, String 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 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 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 648 public JMenuItem createPopupMenuItem(JPopupMenu menu, String label, String mnemonic, 649 String accessibleDescription, String 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 665 public void preloadFirstDemo() { 666 DemoModule demo = addDemo(new InternalFrameDemo(this)); 667 setDemo(demo); 668 } 669 670 671 674 public DemoModule addDemo(DemoModule demo) { 675 demosList.add(demo); 676 if (dragEnabled) { 677 demo.updateDragEnabled(true); 678 } 679 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 704 public void setDemo(DemoModule demo) { 705 currentDemo = demo; 706 707 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 724 public void showSwingSet2() { 725 if(!isApplet() && getFrame() != null) { 726 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 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 758 761 void loadDemo(String classname) { 762 setStatus(getString("Status.loading") + getString(classname + ".name")); 763 DemoModule demo = null; 764 try { 765 Class demoClass = Class.forName(classname); 766 Constructor demoConstructor = demoClass.getConstructor(new Class []{SwingSet2.class}); 767 demo = (DemoModule) demoConstructor.newInstance(new Object []{this}); 768 addDemo(demo); 769 } catch (Exception e) { 770 System.out.println("Error occurred loading demo: " + classname); 771 } 772 } 773 774 785 protected boolean isAvailableLookAndFeel(String laf) { 786 try { 787 Class lnfClass = Class.forName(laf); 788 LookAndFeel newLAF = (LookAndFeel )(lnfClass.newInstance()); 789 return newLAF.isSupportedLookAndFeel(); 790 } catch(Exception e) { return false; 792 } 793 } 794 795 796 799 public boolean isApplet() { 800 return (applet != null); 801 } 802 803 806 public SwingSet2Applet getApplet() { 807 return applet; 808 } 809 810 811 814 public JFrame getFrame() { 815 return frame; 816 } 817 818 821 public JMenuBar getMenuBar() { 822 return menuBar; 823 } 824 825 828 public ToggleButtonToolBar getToolBar() { 829 return toolbar; 830 } 831 832 835 public ButtonGroup getToolBarGroup() { 836 return toolbarGroup; 837 } 838 839 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 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 878 public void setStatus(String s) { 879 SwingUtilities.invokeLater(new SwingSetRunnable(this, s) { 881 public void run() { 882 swingset.statusField.setText((String ) obj); 883 } 884 }); 885 } 886 887 888 891 public String getString(String key) { 892 String 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 926 public ResourceBundle getResourceBundle() { 927 if(bundle == null) { 928 bundle = ResourceBundle.getBundle("resources.swingset"); 929 } 930 return bundle; 931 } 932 933 937 public char getMnemonic(String key) { 938 return (getString(key)).charAt(0); 939 } 940 941 944 public ImageIcon createImageIcon(String filename, String description) { 945 String path = "/resources/images/" + filename; 946 return new ImageIcon(getClass().getResource(path)); 947 } 948 949 952 public void debug(String s) { 953 if(DEBUG) { 954 System.out.println((debugCounter++) + ": " + s); 955 } 956 } 957 958 961 public void setLookAndFeel(String laf) { 962 if(currentLookAndFeel != laf) { 963 currentLookAndFeel = laf; 964 969 String 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 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 ex) { 1020 System.out.println("Failed loading L&F: " + currentLookAndFeel); 1021 System.out.println(ex); 1022 } 1023 } 1024 1025 1028 public void setSourceCode(DemoModule demo) { 1029 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 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 )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 )a.getValue(Action.SHORT_DESCRIPTION)); 1057 tb.setAction(a); 1058 add(tb); 1059 return tb; 1060 } 1061 } 1062 1063 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 1100 1107 class SwingSetRunnable implements Runnable { 1108 protected SwingSet2 swingset; 1109 protected Object obj; 1110 1111 public SwingSetRunnable(SwingSet2 swingset, Object obj) { 1112 this.swingset = swingset; 1113 this.obj = obj; 1114 } 1115 1116 public void run() { 1117 } 1118 } 1119 1120 1121 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 laf; 1156 protected ChangeLookAndFeelAction(SwingSet2 swingset, String 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 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 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 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 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 theme; 1258 protected ChangeThemeAction(SwingSet2 swingset, MetalTheme 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 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 JPanel buttonpanel = new JPanel(); 1301 buttonpanel.setBorder(new javax.swing.border.EmptyBorder (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 1351 class DemoLoadThread extends Thread { 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 updateLookAndFeel(); 1404 } 1405 } 1406} 1407 1408 | Popular Tags |