Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/DemoGroup.java
41155 views
/*1*2* Copyright (c) 2007, 2018, 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 java.awt.BorderLayout;35import java.awt.Component;36import java.awt.Dimension;37import java.awt.Font;38import java.awt.GridBagLayout;39import java.awt.GridLayout;40import java.awt.Image;41import java.awt.event.ActionEvent;42import java.awt.event.ActionListener;43import java.awt.event.MouseAdapter;44import java.awt.event.MouseEvent;45import java.awt.event.WindowAdapter;46import java.awt.event.WindowEvent;47import javax.swing.JButton;48import javax.swing.JCheckBox;49import javax.swing.JCheckBoxMenuItem;50import javax.swing.JFrame;51import javax.swing.JPanel;52import javax.swing.JTabbedPane;53import javax.swing.JToggleButton;54import javax.swing.border.BevelBorder;55import javax.swing.border.CompoundBorder;56import javax.swing.border.EmptyBorder;57import javax.swing.border.SoftBevelBorder;58import javax.swing.event.ChangeEvent;59import javax.swing.event.ChangeListener;606162/**63* DemoGroup handles multiple demos inside of a panel. Demos are loaded64* from the demos[][] string as listed in J2Ddemo.java.65* Demo groups can be loaded individually, for example :66* java DemoGroup Fonts67* Loads all the demos found in the demos/Fonts directory.68*/69@SuppressWarnings("serial")70public class DemoGroup extends JPanel71implements ChangeListener, ActionListener {72private final DemoInstVarsAccessor demoInstVars;73public int columns = 2;74private static final Font font = new Font(Font.SERIF, Font.PLAIN, 10);75private final EmptyBorder emptyB = new EmptyBorder(5, 5, 5, 5);76private final BevelBorder bevelB = new BevelBorder(BevelBorder.LOWERED);77private String groupName;78public JPanel[] clonePanels;79public JTabbedPane tabbedPane;8081public DemoGroup(String name, DemoInstVarsAccessor demoInstVars) {8283groupName = name;84this.demoInstVars = demoInstVars;8586setLayout(new BorderLayout());8788JPanel p = new JPanel(new GridLayout(0, 2));89p.setBorder(new CompoundBorder(emptyB, bevelB));9091// Find the named demo group in J2Ddemo.demos[].92int ind = -1;93while (!name.equals(J2Ddemo.demos[++ind][0])) {94}95String[] demos = J2Ddemo.demos[ind];9697// If there are an odd number of demos, use GridBagLayout.98// Note that we don't use the first entry.99int numDemos = demos.length - 1;100if (numDemos % 2 == 1) {101p.setLayout(new GridBagLayout());102}103104MouseAdapter mouseListener = new MouseAdapter() {105106@Override107public void mouseClicked(MouseEvent e) {108DemoGroup.this.mouseClicked(e.getComponent());109}110};111112// For each demo in the group, prepare a DemoPanel.113for (int i = 1; i <= numDemos; i++) {114DemoPanel dp =115new DemoPanel("java2d.demos." + name + "." + demos[i], demoInstVars);116dp.setDemoBorder(p);117if (dp.surface != null) {118dp.surface.addMouseListener(mouseListener);119dp.surface.setMonitor(demoInstVars.getPerformanceMonitor() != null);120}121if (p.getLayout() instanceof GridBagLayout) {122int x = p.getComponentCount() % 2;123int y = p.getComponentCount() / 2;124int w = (i == numDemos) ? 2 : 1;125J2Ddemo.addToGridBag(p, dp, x, y, w, 1, 1, 1);126} else {127p.add(dp);128}129}130131add(p);132}133134public void mouseClicked(Component component) {135String className = component.toString();136137if (tabbedPane == null) {138shutDown(getPanel());139JPanel p = new JPanel(new BorderLayout());140p.setBorder(new CompoundBorder(emptyB, bevelB));141142tabbedPane = new JTabbedPane();143tabbedPane.setFont(font);144145JPanel tmpP = (JPanel) getComponent(0);146tabbedPane.addTab(groupName, tmpP);147148clonePanels = new JPanel[tmpP.getComponentCount()];149for (int i = 0; i < clonePanels.length; i++) {150clonePanels[i] = new JPanel(new BorderLayout());151DemoPanel dp = (DemoPanel) tmpP.getComponent(i);152DemoPanel c = new DemoPanel(dp.className, demoInstVars);153c.setDemoBorder(clonePanels[i]);154if (c.surface != null) {155c.surface.setMonitor(demoInstVars.getPerformanceMonitor() != null);156Image cloneImg = DemoImages.getImage("clone.gif", this);157c.tools.cloneB = c.tools.addTool(cloneImg,158"Clone the Surface", this);159Dimension d = c.tools.toolbar.getPreferredSize();160c.tools.toolbar.setPreferredSize(161new Dimension(d.width + 27, d.height));162if (demoInstVars.getBackgroundColor() != null) {163c.surface.setBackground(demoInstVars.getBackgroundColor());164}165}166clonePanels[i].add(c);167String s = dp.className.substring(dp.className.indexOf('.') + 1);168tabbedPane.addTab(s, clonePanels[i]);169}170p.add(tabbedPane);171remove(tmpP);172add(p);173174tabbedPane.addChangeListener(this);175revalidate();176}177178className = className.substring(0, className.indexOf('['));179180for (int i = 0; i < tabbedPane.getTabCount(); i++) {181String s1 = className.substring(className.indexOf('.') + 1);182if (tabbedPane.getTitleAt(i).equals(s1)) {183tabbedPane.setSelectedIndex(i);184break;185}186}187188revalidate();189}190191@Override192public void actionPerformed(ActionEvent e) {193JButton b = (JButton) e.getSource();194if (b.getToolTipText().startsWith("Clone")) {195cloneDemo();196} else {197removeClone(b.getParent().getParent().getParent().getParent());198}199}200private int index;201202@Override203public void stateChanged(ChangeEvent e) {204shutDown((JPanel) tabbedPane.getComponentAt(index));205index = tabbedPane.getSelectedIndex();206setup(false);207}208209public JPanel getPanel() {210if (tabbedPane != null) {211return (JPanel) tabbedPane.getSelectedComponent();212} else {213return (JPanel) getComponent(0);214}215}216217public void setup(boolean issueRepaint) {218219JPanel p = getPanel();220221// Let PerformanceMonitor know which demos are running222if (demoInstVars.getPerformanceMonitor() != null) {223demoInstVars.getPerformanceMonitor().surf.setPanel(p);224demoInstVars.getPerformanceMonitor().surf.setSurfaceState();225}226227GlobalControls c = demoInstVars.getControls();228// .. tools check against global controls settings ..229// .. & start demo & custom control thread if need be ..230for (int i = 0; i < p.getComponentCount(); i++) {231DemoPanel dp = (DemoPanel) p.getComponent(i);232if (dp.surface != null && c != null) {233Tools t = dp.tools;234t.setVisible(isValid());235t.issueRepaint = issueRepaint;236JToggleButton[] b = { t.toggleB, t.aliasB, t.renderB,237t.textureB, t.compositeB };238JCheckBox[] cb = { c.toolBarCB, c.aliasCB, c.renderCB,239c.textureCB, c.compositeCB };240for (int j = 0; j < b.length; j++) {241if (c.obj != null && c.obj.equals(cb[j])) {242if (b[j].isSelected() != cb[j].isSelected()) {243b[j].doClick();244}245} else if (c.obj == null) {246if (b[j].isSelected() != cb[j].isSelected()) {247b[j].doClick();248}249}250}251t.setVisible(true);252if (c.screenCombo.getSelectedIndex()253!= t.screenCombo.getSelectedIndex()) {254t.screenCombo.setSelectedIndex(c.screenCombo.255getSelectedIndex());256}257if (demoInstVars.getVerboseCB().isSelected()) {258dp.surface.verbose(c);259}260dp.surface.setSleepAmount(c.slider.getValue());261if (demoInstVars.getBackgroundColor() != null) {262dp.surface.setBackground(demoInstVars.getBackgroundColor());263}264t.issueRepaint = true;265}266dp.start();267}268revalidate();269}270271public void shutDown(JPanel p) {272for (int i = 0; i < p.getComponentCount(); i++) {273((DemoPanel) p.getComponent(i)).stop();274}275System.gc();276}277278public void cloneDemo() {279JPanel panel = clonePanels[tabbedPane.getSelectedIndex() - 1];280if (panel.getComponentCount() == 1) {281panel.invalidate();282panel.setLayout(new GridLayout(0, columns, 5, 5));283panel.revalidate();284}285DemoPanel original = (DemoPanel) getPanel().getComponent(0);286DemoPanel clone = new DemoPanel(original.className, demoInstVars);287if (columns == 2) {288clone.setDemoBorder(panel);289}290Image removeImg = DemoImages.getImage("remove.gif", this);291clone.tools.cloneB =292clone.tools.addTool(removeImg, "Remove the Surface", this);293Dimension d = clone.tools.toolbar.getPreferredSize();294clone.tools.toolbar.setPreferredSize(295new Dimension(d.width + 27, d.height));296if (demoInstVars.getBackgroundColor() != null) {297clone.surface.setBackground(demoInstVars.getBackgroundColor());298}299if (demoInstVars.getControls() != null) {300if (clone.tools.isExpanded301!= demoInstVars.getControls().toolBarCB.isSelected()) {302clone.tools.toggleB.doClick();303}304}305clone.start();306clone.surface.setMonitor(demoInstVars.getPerformanceMonitor() != null);307panel.add(clone);308panel.repaint();309panel.revalidate();310}311312public void removeClone(Component theClone) {313JPanel panel = clonePanels[tabbedPane.getSelectedIndex() - 1];314if (panel.getComponentCount() == 2) {315Component cmp = panel.getComponent(0);316panel.removeAll();317panel.setLayout(new BorderLayout());318panel.revalidate();319panel.add(cmp);320} else {321panel.remove(theClone);322int cmpCount = panel.getComponentCount();323for (int j = 1; j < cmpCount; j++) {324int top = (j + 1 >= 3) ? 0 : 5;325int left = ((j + 1) % 2) == 0 ? 0 : 5;326EmptyBorder eb = new EmptyBorder(top, left, 5, 5);327SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED);328JPanel p = (JPanel) panel.getComponent(j);329p.setBorder(new CompoundBorder(eb, sbb));330}331}332panel.repaint();333panel.revalidate();334}335336public static void main(String[] args) {337class DemoInstVarsAccessorImpl extends DemoInstVarsAccessorImplBase {338private volatile JCheckBoxMenuItem ccthreadCB;339340public void setCcthreadCB(JCheckBoxMenuItem ccthreadCB) {341this.ccthreadCB = ccthreadCB;342}343344@Override345public JCheckBoxMenuItem getCcthreadCB() {346return ccthreadCB;347}348}349DemoInstVarsAccessorImpl demoInstVars = new DemoInstVarsAccessorImpl();350final DemoGroup group = new DemoGroup(args[0], demoInstVars);351JFrame f = new JFrame("Java2D(TM) Demo - DemoGroup");352f.addWindowListener(new WindowAdapter() {353354@Override355public void windowClosing(WindowEvent e) {356System.exit(0);357}358359@Override360public void windowDeiconified(WindowEvent e) {361group.setup(false);362}363364@Override365public void windowIconified(WindowEvent e) {366group.shutDown(group.getPanel());367}368});369f.getContentPane().add("Center", group);370f.pack();371int FRAME_WIDTH = 620;372int FRAME_HEIGHT = 530;373f.setSize(FRAME_WIDTH, FRAME_HEIGHT);374f.setLocationRelativeTo(null); // centers f on screen375f.setVisible(true);376for (String arg : args) {377if (arg.startsWith("-ccthread")) {378demoInstVars.setCcthreadCB(new JCheckBoxMenuItem("CCThread", true));379}380}381group.setup(false);382}383}384385386