001    /*
002     * $Id: RepaintManagerX.java,v 1.8 2006/04/19 23:52:10 gfx Exp $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx;
023    
024    import java.awt.Container;
025    import java.awt.Rectangle;
026    
027    import javax.swing.JComponent;
028    import javax.swing.RepaintManager;
029    
030    /**
031     * This repaint manager is used by Swingx for translucency. The default implementation
032     * of JXPanel (which supports translucency) will replace the current RepaintManager
033     * with a RepaintManagerX *unless* the current RepaintManager is
034     * tagged by the "TranslucentRepaintManager" annotation. 
035     * <p>TODO: Add this to the main documentation (make it visible) so that people
036     * don't bump into it accidently and spend time debugging</p>
037     *
038     * @author zixle
039     * @author rbair
040     */
041    @TranslucentRepaintManager
042    public class RepaintManagerX extends RepaintManager {
043        public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
044            Rectangle dirtyRegion = getDirtyRegion(c);
045            if (dirtyRegion.width == 0 && dirtyRegion.height == 0) {
046                int lastDeltaX = c.getX();
047                int lastDeltaY = c.getY();
048                Container parent = c.getParent();
049                while (parent instanceof JComponent) {
050                    if (!parent.isVisible() || (parent.getPeer() == null)) {
051                        return;
052                    }
053                    if (parent instanceof JXPanel && (((JXPanel)parent).getAlpha() < 1f ||
054                        !parent.isOpaque())) {
055                        x += lastDeltaX;
056                        y += lastDeltaY;
057                        lastDeltaX = lastDeltaY = 0;
058                        c = (JComponent)parent;
059                    }
060                    lastDeltaX += parent.getX();
061                    lastDeltaY += parent.getY();
062                    parent = parent.getParent();
063                }
064            }
065            super.addDirtyRegion(c, x, y, w, h);
066        }
067    }