/* * @(#)TheLayout.java 1.13 98/06/29 * * Copyright 1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */ import java.awt.*; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import com.sun.java.swing.JPanel; import com.sun.java.swing.JTabbedPane; import com.sun.java.swing.border.*; import com.sun.java.swing.event.ChangeEvent; import com.sun.java.swing.event.ChangeListener; import java.net.URL; import java.io.File; /** * TheLayout handles multiple canvases inside of a panel. Demos are loaded * dynamically (see CanvasPanel) this is achieved by a directory listing of * all the class files in a demo group directory. */ public class TheLayout extends JPanel implements MouseListener, ChangeListener { public String className; public JPanel clonePanels[]; public JTabbedPane tabbedPane; private EmptyBorder emptyborder = new EmptyBorder(5,5,5,5); private BevelBorder bevelborder = new BevelBorder(BevelBorder.LOWERED); public TheLayout(String name) { setLayout(new BorderLayout()); JPanel p = new JPanel(new GridBagLayout()); p.setBorder(new CompoundBorder(emptyborder, bevelborder)); // Retrieve class names in a specified demos/* directory URL url = TheLayout.class.getResource("demos/" + name); File dir = new File(url.getFile()); if (dir != null && dir.isDirectory()) { String list[] = dir.list(); CanvasPanel cp = null; int x = 0, y = 0; for (int i = 0; i < list.length; i++) { if (list[i].indexOf('$') == -1 && list[i].endsWith(".class")) { String s1 = list[i].substring(0,list[i].indexOf('.')); className = "demos." + name + "." + s1; cp = new CanvasPanel(this, p); if (cp.canvas != null) { cp.canvas.addMouseListener(this); } else { // Component size? - go to GridLayout. p.setLayout(new GridLayout(0,2)); } if (p.getLayout() instanceof GridBagLayout) { x = p.getComponentCount() % 2; y = p.getComponentCount() / 2; Java2Demo.addToGridBag(p,cp,x,y,1,1,1,1); } else { p.add(cp); } } } if (p.getLayout() instanceof GridBagLayout && p.getComponentCount() % 2 == 1) { p.remove(cp); Java2Demo.addToGridBag(p,cp,x,y,2,1,1,1); } } else { System.out.println("Couldn't find " + name); } add(p); } public void mouseClicked(MouseEvent e) { if (tabbedPane == null) { JPanel p = new JPanel(new BorderLayout()); p.setBorder(new CompoundBorder(emptyborder, bevelborder)); tabbedPane = new JTabbedPane(); tabbedPane.setFont(new Font("Times New Roman", Font.PLAIN, 10)); int index = Java2Demo.tabbedPane.getSelectedIndex(); String s = Java2Demo.tabbedPane.getTitleAt(index); JPanel tmpP = (JPanel) getComponent(0); tabbedPane.addTab(s, tmpP); clonePanels = new JPanel[tmpP.getComponentCount()]; for (int i = 0; i < clonePanels.length; i++) { clonePanels[i] = new JPanel(new BorderLayout()); className = ((CanvasPanel) tmpP.getComponent(i)).className; CanvasPanel cp = new CanvasPanel(this, clonePanels[i]); if (cp.toolbar != null) cp.toolbar.addMoreTools(); clonePanels[i].add(cp); String s1 = className.substring(className.indexOf('.')+1); tabbedPane.addTab(s1, clonePanels[i]); } p.add(tabbedPane); remove(tmpP); add(p); // even if Controls.toolBarCB is off, lets make visible. if (!Java2Demo.controls.toolBarCB.isSelected()) { Java2Demo.controls.toolBarCB.setSelected(true); } validate(); tabbedPane.addChangeListener(this); } className = e.getComponent().toString(); className = className.substring(0, className.indexOf('[')); for (int i =0; i < tabbedPane.getTabCount(); i++) { String s1 = className.substring(className.indexOf('.')+1); if (tabbedPane.getTitleAt(i).equals(s1)) { tabbedPane.setSelectedIndex(i); break; } } validate(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void stateChanged(ChangeEvent e) { setup(); } public JPanel getPanel() { if (tabbedPane != null) return (JPanel) tabbedPane.getSelectedComponent(); else return (JPanel) getComponent(0); } public void setup() { JPanel p = getPanel(); // Let Controls & PerformanceMonitor know which canvases are running Java2Demo.controls.setup(p); Java2Demo.performancemonitor.pmc.setup(p); // .. toolbar check against global controls settings .. // .. & start Canvases if need be (started in ToolBar.java setup()) .. for (int i = 0; i < p.getComponentCount(); i++) { Component cmp = p.getComponent(i); if (!(cmp instanceof CanvasPanel)) continue; CanvasPanel cp = (CanvasPanel) cmp; if (cp.toolbar == null || cp.canvas == null) continue; cp.canvas.setup(); cp.toolbar.setup(false); if (Java2Demo.controls.verboseCB.isSelected()) cp.canvas.verbose(); } } public void CloneCanvas() { int i = tabbedPane.getSelectedIndex() - 1; if (clonePanels[i].getComponentCount() == 1) { clonePanels[i].setLayout(new GridLayout(0,2,2,2)); clonePanels[i].validate(); } className = "demos." + tabbedPane.getTitleAt(i+1); CanvasPanel cp = new CanvasPanel(this, clonePanels[i]); cp.toolbar.removeCloneB = cp.toolbar.addTool("remove.gif", "Remove the Canvas"); clonePanels[i].add(cp); clonePanels[i].validate(); if (cp.canvas.runnable) { cp.canvas.start(); // special case if user wants to see animation with on screen g. if (cp.canvas.Screen == 1 && cp.canvas.runnable) cp.canvas.newBufferedImage = true; } } public void RemoveClone(CanvasPanel theClone) { int i = tabbedPane.getSelectedIndex() - 1; if (clonePanels[i].getComponentCount() == 2) { Component cmp = clonePanels[i].getComponent(0); clonePanels[i].removeAll(); clonePanels[i].setLayout(new BorderLayout()); clonePanels[i].validate(); clonePanels[i].add(cmp); Java2Demo.repaintFix(); } else { clonePanels[i].remove(theClone); int cmpCount = clonePanels[i].getComponentCount(); for (int j = 1;j < cmpCount; j++) { int top = (j+1 >= 3) ? 0 : 5; int left = ((j+1) % 2) == 0 ? 0 : 5; EmptyBorder eb = new EmptyBorder(top,left,5,5); SoftBevelBorder sbb = new SoftBevelBorder(BevelBorder.RAISED); JPanel p = (JPanel) clonePanels[i].getComponent(j); p.setBorder(new CompoundBorder(eb, sbb)); } } clonePanels[i].repaint(); clonePanels[i].validate(); } }