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 java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.Component;
38 import java.awt.Dimension;
39 import java.awt.Font;
40 import javax.swing.JPanel;
41 import javax.swing.JScrollPane;
42 import javax.swing.JTextArea;
43 import javax.swing.border.BevelBorder;
44 import javax.swing.border.CompoundBorder;
45 import javax.swing.border.EmptyBorder;
46 import javax.swing.border.SoftBevelBorder;
47 
48 
49 /**
50  * Illustration of how to use the clone feature of the demo.
51  */
52 @SuppressWarnings("serial")
53 public final class CloningFeature extends JPanel implements Runnable {
54 
55     private final DemoInstVarsAccessor demoInstVars;
56     private Thread thread;
57     private JTextArea ta;
58 
CloningFeature(DemoInstVarsAccessor demoInstVars)59     public CloningFeature(DemoInstVarsAccessor demoInstVars) {
60         this.demoInstVars = demoInstVars;
61 
62         setLayout(new BorderLayout());
63         EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);
64         SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);
65         setBorder(new CompoundBorder(eb, sbb));
66 
67         ta = new JTextArea("Cloning Demonstrated\n\nClicking once on a demo\n");
68         ta.setMinimumSize(new Dimension(300, 500));
69         JScrollPane scroller = new JScrollPane();
70         scroller.getViewport().add(ta);
71         ta.setFont(new Font("Dialog", Font.PLAIN, 14));
72         ta.setForeground(Color.black);
73         ta.setBackground(Color.lightGray);
74         ta.setEditable(false);
75 
76         add("Center", scroller);
77 
78         start();
79     }
80 
start()81     public void start() {
82         thread = new Thread(this);
83         thread.setPriority(Thread.MAX_PRIORITY);
84         thread.setName("CloningFeature");
85         thread.start();
86     }
87 
stop()88     public void stop() {
89         if (thread != null) {
90             thread.interrupt();
91         }
92         thread = null;
93     }
94 
95     @Override
96     @SuppressWarnings("SleepWhileHoldingLock")
run()97     public void run() {
98 
99 
100         int index = demoInstVars.getTabbedPane().getSelectedIndex();
101         if (index == 0) {
102             demoInstVars.getTabbedPane().setSelectedIndex(1);
103             try {
104                 Thread.sleep(3333);
105             } catch (Exception e) {
106                 return;
107             }
108         }
109 
110         if (!demoInstVars.getControls().toolBarCB.isSelected()) {
111             demoInstVars.getControls().toolBarCB.setSelected(true);
112             try {
113                 Thread.sleep(2222);
114             } catch (Exception e) {
115                 return;
116             }
117         }
118 
119         index = demoInstVars.getTabbedPane().getSelectedIndex() - 1;
120         DemoGroup dg = demoInstVars.getGroup()[index];
121         DemoPanel dp = (DemoPanel) dg.getPanel().getComponent(0);
122         if (dp.surface == null) {
123             ta.append("Sorry your zeroth component is not a Surface.");
124             return;
125         }
126 
127         dg.mouseClicked(dp.surface);
128 
129         try {
130             Thread.sleep(3333);
131         } catch (Exception e) {
132             return;
133         }
134 
135         ta.append("Clicking the ToolBar double document button\n");
136         try {
137             Thread.sleep(3333);
138         } catch (Exception e) {
139             return;
140         }
141 
142         dp = (DemoPanel) dg.clonePanels[0].getComponent(0);
143 
144         if (dp.tools != null) {
145             for (int i = 0; i < 3 && thread != null; i++) {
146                 ta.append("   Cloning\n");
147                 dp.tools.cloneB.doClick();
148                 try {
149                     Thread.sleep(3333);
150                 } catch (Exception e) {
151                     return;
152                 }
153             }
154         }
155 
156         ta.append("Changing attributes \n");
157 
158         try {
159             Thread.sleep(3333);
160         } catch (Exception e) {
161             return;
162         }
163 
164         Component cmps[] = dg.clonePanels[0].getComponents();
165         for (int i = 0; i < cmps.length && thread != null; i++) {
166             if ((dp = (DemoPanel) cmps[i]).tools == null) {
167                 continue;
168             }
169             switch (i) {
170                 case 0:
171                     ta.append("   Changing AntiAliasing\n");
172                     dp.tools.aliasB.doClick();
173                     break;
174                 case 1:
175                     ta.append("   Changing Composite & Texture\n");
176                     dp.tools.compositeB.doClick();
177                     dp.tools.textureB.doClick();
178                     break;
179                 case 2:
180                     ta.append("   Changing Screen\n");
181                     dp.tools.screenCombo.setSelectedIndex(4);
182                     break;
183                 case 3:
184                     ta.append("   Removing a clone\n");
185                     dp.tools.cloneB.doClick();
186             }
187             try {
188                 Thread.sleep(3333);
189             } catch (Exception e) {
190                 return;
191             }
192         }
193 
194         ta.append("\nAll Done!");
195     }
196 }
197