Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/RunWindow.java
41154 views
/*1*2* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7*8* - Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10*11* - Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* - Neither the name of Oracle nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS20* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,21* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR22* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR23* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,24* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,25* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR26* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF27* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING28* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31package java2d;323334import static java.awt.Color.BLACK;35import static java.awt.Color.GREEN;36import static java.awt.Color.RED;37import java.awt.Component;38import java.awt.Dimension;39import java.awt.Font;40import java.awt.GridBagLayout;41import java.awt.GridLayout;42import java.awt.event.ActionEvent;43import java.awt.event.ActionListener;44import java.util.Date;45import java.util.logging.Level;46import java.util.logging.Logger;47import javax.swing.JButton;48import javax.swing.JCheckBox;49import javax.swing.JLabel;50import javax.swing.JPanel;51import javax.swing.JProgressBar;52import javax.swing.JTextField;53import javax.swing.SwingConstants;54import javax.swing.SwingUtilities;55import javax.swing.border.BevelBorder;56import javax.swing.border.CompoundBorder;57import javax.swing.border.EmptyBorder;585960/**61* A separate window for running the J2Ddemo. Go from tab to tab or demo to62* demo.63*/64@SuppressWarnings("serial")65public class RunWindow extends JPanel implements Runnable, ActionListener {66private final DemoInstVarsAccessor demoInstVars;67private final JButton runB;68private int delay = 10;69private int numRuns = 20;70private boolean exit;71private final JCheckBox zoomCB = new JCheckBox("Zoom");72private final JCheckBox printCB = new JCheckBox("Print");73private boolean buffersFlag;74private int bufBeg, bufEnd;75private JTextField delayTextField, runsTextField;76private Thread thread;77private JProgressBar pb;7879@SuppressWarnings("LeakingThisInConstructor")80public RunWindow(DemoInstVarsAccessor demoInstVars, RunWindowSettings runWndSetts) {81this.demoInstVars = demoInstVars;8283delay = runWndSetts.getDelay();84numRuns = runWndSetts.getNumRuns();85exit = runWndSetts.getExit();86zoomCB.setSelected(runWndSetts.isZoomCBSelected());87printCB.setSelected(runWndSetts.isPrintCBSelected());88buffersFlag = runWndSetts.getBuffersFlag();89bufBeg = runWndSetts.getBufBeg();90bufEnd = runWndSetts.getBufEnd();9192setLayout(new GridBagLayout());93EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);94setBorder(new CompoundBorder(eb, new BevelBorder(BevelBorder.LOWERED)));9596Font font = new Font(Font.SERIF, Font.PLAIN, 10);9798runB = new JButton("Run");99runB.setBackground(GREEN);100runB.addActionListener(this);101runB.setMinimumSize(new Dimension(70, 30));102J2Ddemo.addToGridBag(this, runB, 0, 0, 1, 1, 0.0, 0.0);103104pb = new JProgressBar();105pb.setPreferredSize(new Dimension(100, 30));106pb.setMinimum(0);107J2Ddemo.addToGridBag(this, pb, 1, 0, 2, 1, 1.0, 0.0);108109JPanel p1 = new JPanel(new GridLayout(2, 2));110JPanel p2 = new JPanel();111JLabel l = new JLabel("Runs:");112l.setFont(font);113l.setForeground(BLACK);114p2.add(l);115p2.add(runsTextField = new JTextField(String.valueOf(numRuns)));116runsTextField.setPreferredSize(new Dimension(30, 20));117runsTextField.addActionListener(this);118p1.add(p2);119p2 = new JPanel();120l = new JLabel("Delay:");121l.setFont(font);122l.setForeground(BLACK);123p2.add(l);124p2.add(delayTextField = new JTextField(String.valueOf(delay)));125delayTextField.setPreferredSize(new Dimension(30, 20));126delayTextField.addActionListener(this);127p1.add(p2);128129zoomCB.setHorizontalAlignment(SwingConstants.CENTER);130zoomCB.setFont(font);131printCB.setFont(font);132p1.add(zoomCB);133p1.add(printCB);134printCB.addActionListener(this);135J2Ddemo.addToGridBag(this, p1, 0, 1, 3, 1, 1.0, 1.0);136}137138@Override139public void actionPerformed(ActionEvent e) {140if (e.getSource().equals(printCB)) {141demoInstVars.getPrintCB().setSelected(printCB.isSelected());142} else if (e.getSource().equals(delayTextField)) {143delay = Integer.parseInt(delayTextField.getText().trim());144} else if (e.getSource().equals(runsTextField)) {145numRuns = Integer.parseInt(runsTextField.getText().trim());146} else if ("Run".equals(e.getActionCommand())) {147doRunAction();148} else if ("Stop".equals(e.getActionCommand())) {149stop();150}151}152153public void doRunAction() {154runB.setText("Stop");155runB.setBackground(RED);156start();157}158159public void start() {160thread = new Thread(this);161thread.setPriority(Thread.NORM_PRIORITY + 1);162thread.setName("RunWindow");163thread.start();164}165166public synchronized void stop() {167if (thread != null) {168thread.interrupt();169}170thread = null;171notifyAll();172}173174@SuppressWarnings("SleepWhileHoldingLock")175public void sleepPerTab() {176for (int j = 0; j < delay + 1 && thread != null; j++) {177for (int k = 0; k < 10 && thread != null; k++) {178try {179Thread.sleep(100);180} catch (Exception e) {181}182}183Runnable pbUpdateRunnable = new Runnable() {184185@Override186public void run() {187pb.setValue(pb.getValue() + 1);188}189};190SwingUtilities.invokeLater(pbUpdateRunnable);191}192}193194private void printDemo(final DemoGroup dg) {195Runnable printDemoRunnable = new Runnable() {196197@Override198public void run() {199if (!demoInstVars.getControls().toolBarCB.isSelected()) {200demoInstVars.getControls().toolBarCB.setSelected(true);201dg.invalidate();202}203for (Component comp : dg.getPanel().getComponents()) {204DemoPanel dp = (DemoPanel) comp;205if (dp.tools != null) {206if (dp.surface.animating != null) {207if (dp.surface.animating.running()) {208dp.tools.startStopB.doClick();209}210}211dp.tools.printB.doClick();212}213}214}215};216invokeAndWait(printDemoRunnable);217}218private DemoGroup dg = null;219private DemoPanel dp = null;220221@Override222public void run() {223224System.out.println("\nJ2D Demo RunWindow : " + numRuns + " Runs, "225+ delay + " second delay between tabs\n" + "java version: " + System.226getProperty("java.version") + "\n" + System.getProperty(227"os.name") + " " + System.getProperty("os.version") + "\n");228Runtime r = Runtime.getRuntime();229230for (int runNum = 0; runNum < numRuns && thread != null; runNum++) {231232Date d = new Date();233System.out.print("#" + runNum + " " + d.toString() + ", ");234r.gc();235float freeMemory = r.freeMemory();236float totalMemory = r.totalMemory();237System.out.println(((totalMemory - freeMemory) / 1024) + "K used");238239for (int i = 0; i < demoInstVars.getTabbedPane().getTabCount() && thread240!= null; i++) {241242final int mainTabIndex = i;243Runnable initDemoRunnable = new Runnable() {244245@Override246public void run() {247pb.setValue(0);248pb.setMaximum(delay);249if (mainTabIndex != 0) {250dg = demoInstVars.getGroup()[mainTabIndex - 1];251dg.invalidate();252}253demoInstVars.getTabbedPane().setSelectedIndex(mainTabIndex);254}255};256invokeAndWait(initDemoRunnable);257258if (i != 0 && (zoomCB.isSelected() || buffersFlag)) {259dp = (DemoPanel) dg.getPanel().getComponent(0);260if (dg.tabbedPane == null && dp.surface != null) {261Runnable mouseClickedRunnable = new Runnable() {262263@Override264public void run() {265dg.mouseClicked(dp.surface);266}267};268invokeAndWait(mouseClickedRunnable);269}270for (int j = 1; j < dg.tabbedPane.getTabCount() && thread271!= null; j++) {272273final int subTabIndex = j;274275Runnable initPanelRunnable = new Runnable() {276277@Override278public void run() {279pb.setValue(0);280pb.setMaximum(delay);281dg.tabbedPane.setSelectedIndex(subTabIndex);282}283};284invokeAndWait(initPanelRunnable);285286final JPanel p = dg.getPanel();287if (buffersFlag && p.getComponentCount() == 1) {288dp = (DemoPanel) p.getComponent(0);289if (dp.surface.animating != null) {290dp.surface.animating.stop();291}292for (int k = bufBeg; k <= bufEnd && thread != null;293k++) {294295final int cloneIndex = k;296Runnable cloneRunnable = new Runnable() {297298@Override299public void run() {300dp.tools.cloneB.doClick();301int n = p.getComponentCount();302DemoPanel clone = (DemoPanel) p.303getComponent(n - 1);304if (clone.surface.animating != null) {305clone.surface.animating.stop();306}307clone.tools.issueRepaint = true;308clone.tools.screenCombo.setSelectedIndex(309cloneIndex);310clone.tools.issueRepaint = false;311}312};313invokeAndWait(cloneRunnable);314}315}316if (printCB.isSelected()) {317printDemo(dg);318}319sleepPerTab();320}321} else if (i != 0 && printCB.isSelected()) {322printDemo(dg);323sleepPerTab();324} else {325sleepPerTab();326}327}328if (runNum + 1 == numRuns) {329System.out.println("Finished.");330if (exit && thread != null) {331System.out.println("System.exit(0).");332System.exit(0);333}334}335}336Runnable resetRunnable = new Runnable() {337338@Override339public void run() {340runB.setText("Run");341runB.setBackground(GREEN);342pb.setValue(0);343}344};345invokeAndWait(resetRunnable);346347thread = null;348dg = null;349dp = null;350}351352private static void invokeAndWait(Runnable run) {353try {354SwingUtilities.invokeAndWait(run);355} catch (Exception e) {356Logger.getLogger(RunWindow.class.getName()).log(Level.SEVERE,357"ERROR in invokeAndWait", e);358}359}360361/**362* This class contains initial values for instance variables of 'RunWindow' class,363* and its instance is used in creation of 'RunWindow' object. Values parsed from364* certain command line options of the demo or from the demo applet parameters are365* set to the fields of this class instance. It is a part of the fix which changed366* static variables for instance variables in certain demo classes.367*368* This class is not thread safe, its instances must be accessed only from EDT.369*/370public static class RunWindowSettings {371private int delay = 10;372private int numRuns = 20;373private boolean exit;374private boolean zoomCBIsSelected;375private boolean printCBIsSelected;376private boolean buffersFlag;377private int bufBeg, bufEnd;378379public int getDelay() {380return delay;381}382383public void setDelay(int delay) {384this.delay = delay;385}386387public int getNumRuns() {388return numRuns;389}390391public void setNumRuns(int numRuns) {392this.numRuns = numRuns;393}394395public boolean getExit() {396return exit;397}398399public void setExit(boolean exit) {400this.exit = exit;401}402403public boolean isZoomCBSelected() {404return zoomCBIsSelected;405}406407public void setZoomCBSelected(boolean b) {408zoomCBIsSelected = b;409}410411public boolean isPrintCBSelected() {412return printCBIsSelected;413}414415public void setPrintCBIsSelected(boolean b) {416printCBIsSelected = b;417}418419public boolean getBuffersFlag() {420return buffersFlag;421}422423public void setBuffersFlag(boolean buffersFlag) {424this.buffersFlag = buffersFlag;425}426427public int getBufBeg() {428return bufBeg;429}430431public void setBufBeg(int bufBeg) {432this.bufBeg = bufBeg;433}434435public int getBufEnd() {436return bufEnd;437}438439public void setBufEnd(int bufEnd) {440this.bufEnd = bufEnd;441}442}443}444445446