1 /*
   2  *
   3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package java2d;
  33 
  34 
  35 import static java2d.CustomControlsContext.State.START;
  36 import static java2d.CustomControlsContext.State.STOP;
  37 import java.awt.BorderLayout;
  38 import java.awt.Component;
  39 import java.util.logging.Level;
  40 import java.util.logging.Logger;
  41 import javax.swing.JPanel;
  42 import javax.swing.border.BevelBorder;
  43 import javax.swing.border.CompoundBorder;
  44 import javax.swing.border.EmptyBorder;
  45 import javax.swing.border.SoftBevelBorder;
  46 
  47 
  48 /**
  49  * The panel for the Surface, Custom Controls & Tools.
  50  * Other component types welcome.
  51  */
  52 @SuppressWarnings("serial")
  53 public class DemoPanel extends JPanel {
  54     private final DemoInstVarsAccessor demoInstVars;
  55     public Surface surface;
  56     public CustomControlsContext ccc;
  57     public Tools tools;
  58     public String className;
  59 
  60     public DemoPanel(Object obj, DemoInstVarsAccessor demoInstVars) {
  61         this.demoInstVars = demoInstVars;
  62 
  63         setLayout(new BorderLayout());
  64         try {
  65             if (obj instanceof String) {
  66                 className = (String) obj;
  67                 obj = Class.forName(className).newInstance();
  68             }
  69             if (obj instanceof Component) {
  70                 add((Component) obj);
  71             }
  72             if (obj instanceof Surface) {
  73                 add("South", tools = new Tools(surface = (Surface) obj, demoInstVars));
  74             }
  75             if (obj instanceof CustomControlsContext) {
  76                 ccc = (CustomControlsContext) obj;
  77                 Component cmps[] = ccc.getControls();
  78                 String cons[] = ccc.getConstraints();
  79                 for (int i = 0; i < cmps.length; i++) {
  80                     add(cmps[i], cons[i]);
  81                 }
  82             }
  83         } catch (Exception e) {
  84             Logger.getLogger(DemoPanel.class.getName()).log(Level.SEVERE, null,
  85                     e);
  86         }
  87     }
  88 
  89     public void start() {
  90         if (surface != null) {
  91             surface.startClock();
  92         }
  93         if (tools != null && surface != null) {
  94             if (tools.startStopB != null && tools.startStopB.isSelected()) {
  95                 surface.animating.start();
  96             }
  97         }
  98         if (ccc != null
  99                 && demoInstVars.getCcthreadCB() != null
 100                 && demoInstVars.getCcthreadCB().isSelected()) {
 101             ccc.handleThread(START);
 102         }
 103     }
 104 
 105     public void stop() {
 106         if (surface != null) {
 107             if (surface.animating != null) {
 108                 surface.animating.stop();
 109             }
 110             surface.bimg = null;
 111         }
 112         if (ccc != null) {
 113             ccc.handleThread(STOP);
 114         }
 115     }
 116 
 117     public void setDemoBorder(JPanel p) {
 118         int top = (p.getComponentCount() + 1 >= 3) ? 0 : 5;
 119         int left = ((p.getComponentCount() + 1) % 2) == 0 ? 0 : 5;
 120         EmptyBorder eb = new EmptyBorder(top, left, 5, 5);
 121         SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);
 122         setBorder(new CompoundBorder(eb, sbb));
 123     }
 124 }