home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / DemoGroup.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  12.9 KB  |  356 lines

  1. /*
  2.  * Copyright (c) 2002 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.  * @(#)DemoGroup.java    1.32 02/06/13
  38.  */
  39.  
  40.  
  41. package java2d;
  42.  
  43. import java.awt.*;
  44. import java.awt.event.*;
  45. import javax.swing.*;
  46. import javax.swing.border.*;
  47. import javax.swing.event.ChangeEvent;
  48. import javax.swing.event.ChangeListener;
  49. import java.util.Vector;
  50.  
  51.  
  52. /**
  53.  * DemoGroup handles multiple demos inside of a panel.  Demos are loaded
  54.  * from the demos[][] string as listed in Java2Demo.java.
  55.  * Demo groups can be loaded individually, for example : 
  56.  *      java DemoGroup Fonts
  57.  * Loads all the demos found in the demos/Fonts directory.
  58.  */
  59. public class DemoGroup extends JPanel implements MouseListener, ChangeListener, ActionListener {
  60.  
  61.     static int columns = 2;
  62.  
  63.     private static Font font = new Font("serif", Font.PLAIN, 10);
  64.     private static EmptyBorder emptyB = new EmptyBorder(5,5,5,5);
  65.     private static BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED);
  66.     private String groupName;
  67.     public JPanel clonePanels[];
  68.     public JTabbedPane tabbedPane;
  69.  
  70.  
  71.     public DemoGroup(String name) {
  72.  
  73.         groupName = name;
  74.  
  75.         setLayout(new BorderLayout());
  76.  
  77.         JPanel p = new JPanel(new GridLayout(0,2));
  78.         p.setBorder(new CompoundBorder(emptyB, bevelB));
  79.  
  80.         Vector vector = new Vector(40);
  81.  
  82.         int index = 0;
  83.         for (; index < Java2Demo.demos.length; index++) {
  84.             if (name.compareTo(Java2Demo.demos[index][0]) == 0) {
  85.                break;
  86.             }
  87.         }
  88.         String[] demos = Java2Demo.demos[index];
  89.         for (int j = 1; j < demos.length; j++) {
  90.             vector.add("java2d.demos." + name + "." + demos[j]);
  91.         }
  92.         if (vector.size()%2 == 1) {
  93.             p.setLayout(new GridBagLayout());
  94.         } 
  95.         for (int i = 0; i < vector.size(); i++) {
  96.             DemoPanel dp = new DemoPanel((String) vector.elementAt(i));
  97.             dp.setDemoBorder(p);
  98.             if (dp.surface != null) {
  99.                 dp.surface.addMouseListener(this);
  100.                 dp.surface.setMonitor(Java2Demo.performancemonitor != null);
  101.             } 
  102.             if (p.getLayout() instanceof GridBagLayout) {
  103.                 int x = p.getComponentCount() % 2;
  104.                 int y = p.getComponentCount() / 2;
  105.                 int w = i == vector.size()-1 ? 2 : 1;
  106.                 Java2Demo.addToGridBag(p,dp,x,y,w,1,1,1);
  107.             } else {
  108.                 p.add(dp);
  109.             }
  110.         }
  111.  
  112.         add(p);
  113.     }
  114.  
  115.  
  116.     public void mouseClicked(MouseEvent e) {
  117.  
  118.         if (tabbedPane == null) {
  119.             shutDown(getPanel());
  120.             JPanel p = new JPanel(new BorderLayout());
  121.             p.setBorder(new CompoundBorder(emptyB, bevelB));
  122.  
  123.             tabbedPane = new JTabbedPane();
  124.             tabbedPane.setFont(font);
  125.  
  126.             JPanel tmpP = (JPanel) getComponent(0);
  127.             tabbedPane.addTab(groupName, tmpP);
  128.  
  129.             clonePanels = new JPanel[tmpP.getComponentCount()];
  130.             for (int i = 0; i < clonePanels.length; i++) {
  131.                 clonePanels[i] = new JPanel(new BorderLayout());
  132.                 DemoPanel dp = (DemoPanel) tmpP.getComponent(i);
  133.                 DemoPanel c = new DemoPanel(dp.className);
  134.                 c.setDemoBorder(clonePanels[i]);
  135.                 if (c.surface != null) {
  136.                     c.surface.setMonitor(Java2Demo.performancemonitor != null);
  137.                     c.tools.cloneB = 
  138.                         c.tools.addTool("clone.gif","Clone the Surface",this);
  139.                     Dimension d = c.tools.toolbar.getPreferredSize();
  140.                     c.tools.toolbar.setPreferredSize(
  141.                         new Dimension(d.width+27, d.height));
  142.                     if (Java2Demo.backgroundColor != null) {
  143.                         c.surface.setBackground(Java2Demo.backgroundColor);
  144.                     }
  145.                 } 
  146.                 clonePanels[i].add(c);
  147.                 String s = dp.className.substring(dp.className.indexOf('.')+1);
  148.                 tabbedPane.addTab(s, clonePanels[i]);
  149.             }
  150.             p.add(tabbedPane);
  151.             remove(tmpP);
  152.             add(p);
  153.  
  154.             tabbedPane.addChangeListener(this);
  155.             validate();
  156.         }
  157.  
  158.         String className = e.getComponent().toString();
  159.         className = className.substring(0, className.indexOf('['));
  160.  
  161.         for (int i = 0; i < tabbedPane.getTabCount(); i++) {
  162.             String s1 = className.substring(className.indexOf('.')+1);
  163.             if (tabbedPane.getTitleAt(i).equals(s1)) {
  164.                 tabbedPane.setSelectedIndex(i);
  165.                 break;
  166.             }
  167.         }
  168.  
  169.         validate();
  170.     }
  171.  
  172.     public void mousePressed(MouseEvent e) { }
  173.     public void mouseReleased(MouseEvent e) { }
  174.     public void mouseEntered(MouseEvent e) { }
  175.     public void mouseExited(MouseEvent e) { }
  176.  
  177.  
  178.     public void actionPerformed(ActionEvent e) {
  179.         JButton b = (JButton) e.getSource();
  180.         if (b.getToolTipText().startsWith("Clone")) {
  181.             cloneDemo();
  182.         } else {
  183.             removeClone(b.getParent().getParent().getParent().getParent());
  184.         }
  185.     }
  186.  
  187.  
  188.     private int index;
  189.  
  190.     public void stateChanged(ChangeEvent e) {
  191.         shutDown((JPanel) tabbedPane.getComponentAt(index));
  192.         index = tabbedPane.getSelectedIndex();
  193.         setup(false);
  194.     }
  195.  
  196.  
  197.     public JPanel getPanel() {
  198.         if (tabbedPane != null) {
  199.             return (JPanel) tabbedPane.getSelectedComponent();
  200.         } else {
  201.             return (JPanel) getComponent(0);
  202.         }
  203.     }
  204.  
  205.  
  206.     public void setup(boolean issueRepaint) {
  207.  
  208.         JPanel p = getPanel();
  209.  
  210.         // Let PerformanceMonitor know which demos are running
  211.         if (Java2Demo.performancemonitor != null) {
  212.             Java2Demo.performancemonitor.surf.setPanel(p);
  213.             Java2Demo.performancemonitor.surf.setSurfaceState();
  214.         }
  215.  
  216.         GlobalControls c = Java2Demo.controls;
  217.         // .. tools check against global controls settings ..
  218.         // .. & start demo & custom control thread if need be ..
  219.         for (int i = 0; i < p.getComponentCount(); i++) {
  220.             DemoPanel dp = (DemoPanel) p.getComponent(i);
  221.             if (dp.surface != null && c != null) {
  222.                 Tools t = dp.tools;
  223.                 t.setVisible(isValid());
  224.                 t.issueRepaint = issueRepaint;
  225.                 JButton b[] = {t.toggleB, t.aliasB, t.renderB,
  226.                                t.textureB, t.compositeB};
  227.                 JCheckBox cb[] = {c.toolBarCB, c.aliasCB, c.renderCB,
  228.                                   c.textureCB, c.compositeCB};
  229.                 for (int j = 0; j < b.length; j++) {
  230.                     if (c.obj != null && c.obj.equals(cb[j])) {
  231.                         if (b[j].isSelected() != cb[j].isSelected()) {
  232.                             b[j].doClick();
  233.                         }
  234.                     } else if (c.obj == null) {
  235.                         if (b[j].isSelected() != cb[j].isSelected()) {
  236.                             b[j].doClick();
  237.                         }
  238.                     }
  239.                 }
  240.                 t.setVisible(true);
  241.                 if (c.screenCombo.getSelectedIndex() != t.screenCombo.getSelectedIndex()) 
  242.                 {
  243.                     t.screenCombo.setSelectedIndex(c.screenCombo.getSelectedIndex());
  244.                 }
  245.                 if (Java2Demo.verboseCB.isSelected()) {
  246.                     dp.surface.verbose();
  247.                 }
  248.                 dp.surface.setSleepAmount(c.slider.getValue());
  249.                 if (Java2Demo.backgroundColor != null) {
  250.                     dp.surface.setBackground(Java2Demo.backgroundColor);
  251.                 }
  252.                 t.issueRepaint = true;
  253.             }
  254.             dp.start();
  255.         } 
  256.         validate();
  257.     }
  258.  
  259.  
  260.     public void shutDown(JPanel p) {
  261.         invalidate();
  262.         for (int i = 0; i < p.getComponentCount(); i++) {
  263.             ((DemoPanel) p.getComponent(i)).stop();
  264.         } 
  265.         System.gc();
  266.     }
  267.  
  268.  
  269.     public void cloneDemo() {
  270.         int i = tabbedPane.getSelectedIndex() - 1;
  271.         if (clonePanels[i].getComponentCount() == 1) {
  272.             clonePanels[i].invalidate();
  273.             clonePanels[i].setLayout(new GridLayout(0,columns,5,5));
  274.             clonePanels[i].validate();
  275.         }
  276.         DemoPanel original = (DemoPanel) getPanel().getComponent(0);
  277.         DemoPanel clone = new DemoPanel(original.className);
  278.         if (columns == 2) {
  279.             clone.setDemoBorder(clonePanels[i]);
  280.         }
  281.         clone.tools.cloneB = 
  282.               clone.tools.addTool("remove.gif","Remove the Surface",this);
  283.         Dimension d = clone.tools.toolbar.getPreferredSize();
  284.         clone.tools.toolbar.setPreferredSize(
  285.                         new Dimension(d.width+27, d.height));
  286.         if (Java2Demo.backgroundColor != null) {
  287.             clone.surface.setBackground(Java2Demo.backgroundColor);
  288.         }
  289.         if (Java2Demo.controls != null) {
  290.             if (clone.tools.isExpanded 
  291.                 != Java2Demo.controls.toolBarCB.isSelected())
  292.             {
  293.                 clone.tools.toggleB.doClick();
  294.             } 
  295.         }
  296.         clone.start();
  297.         clone.surface.setMonitor(Java2Demo.performancemonitor != null);
  298.         clonePanels[i].add(clone);
  299.         clonePanels[i].repaint();
  300.         clonePanels[i].validate();
  301.     }
  302.  
  303.  
  304.     public void removeClone(Component theClone) {
  305.         int i = tabbedPane.getSelectedIndex() - 1;
  306.         if (clonePanels[i].getComponentCount() == 2) {
  307.             Component cmp = clonePanels[i].getComponent(0);
  308.             clonePanels[i].removeAll();
  309.             clonePanels[i].setLayout(new BorderLayout());
  310.             clonePanels[i].validate();
  311.             clonePanels[i].add(cmp);
  312.         } else {
  313.             clonePanels[i].remove(theClone);
  314.             int cmpCount = clonePanels[i].getComponentCount();
  315.             for (int j = 1;j < cmpCount; j++) {
  316.                 int top = (j+1 >= 3) ? 0 : 5;
  317.                 int left = ((j+1) % 2) == 0 ? 0 : 5;
  318.                 EmptyBorder eb = new EmptyBorder(top,left,5,5);
  319.                 SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);
  320.                 JPanel p = (JPanel) clonePanels[i].getComponent(j);
  321.                 p.setBorder(new CompoundBorder(eb, sbb));
  322.             }
  323.         }
  324.         clonePanels[i].repaint();
  325.         clonePanels[i].validate();
  326.     }
  327.  
  328.  
  329.     public static void main(String args[]) {
  330.         final DemoGroup group = new DemoGroup(args[0]);
  331.         JFrame f = new JFrame("Java2D Demo - DemoGroup");
  332.         f.addWindowListener(new WindowAdapter() {
  333.             public void windowClosing(WindowEvent e) {System.exit(0);}
  334.             public void windowDeiconified(WindowEvent e) { group.setup(false); }
  335.             public void windowIconified(WindowEvent e) { 
  336.                 group.shutDown(group.getPanel()); 
  337.             }
  338.         });
  339.         f.getContentPane().add("Center", group);
  340.         f.pack();
  341.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  342.         int WIDTH = 620;
  343.         int HEIGHT = 530;
  344.         f.setLocation(screenSize.width/2 - WIDTH/2,
  345.                           screenSize.height/2 - HEIGHT/2);
  346.         f.setSize(WIDTH, HEIGHT);
  347.         f.setVisible(true);
  348.         for (int i = 0; i < args.length; i++) {
  349.             if (args[i].startsWith("-ccthread")) {
  350.                 Java2Demo.ccthreadCB = new JCheckBoxMenuItem("CCThread", true);
  351.             }
  352.         }
  353.         group.setup(false);
  354.     }
  355. }
  356.