/* * CardLayoutTest.java * * Created on January 19, 2005, 4:36 PM */ import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** CardLayout test app, attempting to expose bugs in libjava/xlib * The main bug is that addNotify doesn't get called for components under * the following conditions: * - The component is lightweight AWT * - The component is added to a container with a CardLayout * - The container has already been shown on-screen before this component * is added. * That last one's the crux of the thing, I think. It seems like in Container, * invalidateTree should clear the valid flag, but it doesn't. I could have * that wrong, though, because I haven't looked that closely yet. * * GCC versions: * - old "good" one is gcc (GCC) 3.5.0 20040603 (experimental) * - new gui branch is gcc (GCC) 3.5.0 20040619 (experimental) * (timer doesn't run with that one) * - new main branch from cvs is gcc (GCC) 4.0.0 20050108 (experimental) * (fails to call addNotify - see about line 80, below) * @author scott */ public class CardLayoutTest extends Frame { /* At time of writing, Label didn't work on gcj with xlib peers, but this component did */ class TestLabel extends Component { String labelText; TestLabel (String title, Color color) { labelText = title; setBackground (color); } private java.awt.Dimension mySize = new java.awt.Dimension (100,20); public java.awt.Dimension getPreferredSize () { return mySize; } public void paint ( java.awt.Graphics graphics ) { System.out.println ("Painting " + this); graphics.setColor (getBackground ()); graphics.fillRect (0,0,getWidth (),getHeight ()); graphics.setColor (getForeground ()); graphics.drawString (labelText,2,15); } public String toString () { return "[TestLabel \"" + labelText + "\"]"; } } private Panel panel1; private int activeCard = 0; private TestLabel [] cards = new TestLabel [4]; private String [] cardNames = new String [cards.length]; private java.util.Timer timer = new java.util.Timer (); private java.util.TimerTask ttask = new java.util.TimerTask () { public void run () { if (++activeCard >= cards.length) activeCard = 0; // create most cards only when being shown for the first time (That's what // my larger app does, and it's busted with recent gcc from cvs). if (cards[activeCard] == null) createOneCard (activeCard); System.out.println ("Showing card " + activeCard + " - isLightweight()=" + cards[activeCard].isLightweight()); if (!cards[activeCard].isLightweight()) { /* With my old "good" gcc, it doesn't go in here, and everything works. * With the new gcc from cvs, only the first card shows unless this * code is inserted, because addNotify isn't getting called automatically. * The component (cards[activeCard]) therefore doesn't appear to be * lightweight, because its peer is null, so it doesn't display properly. */ System.out.println(" - I can fix that, by calling addNotify()"); cards[activeCard].addNotify (); // THE PROBLEM: without this call, the thing doesn't work System.out.println (" - OK, showing card " + activeCard + " - isLightweight()=" + cards[activeCard].isLightweight()); panel1.doLayout (); // with xlib, this seems also to be required } ((CardLayout)(panel1.getLayout ())).show (panel1,cardNames[activeCard]); panel1.repaint (); } }; /** Creates new form CardLayoutTest */ public CardLayoutTest () { System.out.println ("Starting now"); panel1 = new Panel () { /* paint and update are overridden to avoid an exception * in XGraphics.setColor */ public void paint ( java.awt.Graphics graphics ) { System.out.println("Painting Panel"); graphics.setColor (getBackground ()); super.paint (graphics); } public void update (Graphics g) { paint (g); } }; panel1.setBackground (Color.white); setBackground (Color.white); setForeground (Color.black); addWindowListener (new WindowAdapter () { public void windowClosing (WindowEvent evt) { exitForm (evt); } }); panel1.setLayout (new CardLayout ()); createOneCard (0); /* for (int i=0; i