001    /*
002     * $Id: RepaintManagerX.java 3290 2009-03-10 17:30:14Z kschaefe $
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     * <p>An implementation of {@link RepaintManager} which adds support for transparency
032     * in {@link JXPanel}s. <code>JXPanel</code> (which supports translucency) will 
033     * replace the current RepaintManager with an instance of RepaintManagerX 
034     * <em>unless</em> the current RepaintManager is tagged by the {@link TranslucentRepaintManager}
035     * annotation.</p>
036     *
037     * @author zixle
038     * @author rbair
039     */
040    @TranslucentRepaintManager
041    public class RepaintManagerX extends ForwardingRepaintManager {
042        /**
043         * @param delegate
044         */
045        public RepaintManagerX(RepaintManager delegate) {
046            super(delegate);
047        }
048    
049        /** 
050         * Add a component in the list of components that should be refreshed.
051         * If <i>c</i> already has a dirty region, the rectangle <i>(x,y,w,h)</i> 
052         * will be unioned with the region that should be redrawn. 
053         * 
054         * @param c Component to repaint, null results in nothing happening.
055         * @param x X coordinate of the region to repaint
056         * @param y Y coordinate of the region to repaint
057         * @param w Width of the region to repaint
058         * @param h Height of the region to repaint
059         * @see JComponent#repaint
060         */
061        public void addDirtyRegion(JComponent c, int x, int y, int w, int h) {
062            Rectangle dirtyRegion = getDirtyRegion(c);
063            if (dirtyRegion.width == 0 && dirtyRegion.height == 0) {
064                int lastDeltaX = c.getX();
065                int lastDeltaY = c.getY();
066                Container parent = c.getParent();
067                while (parent instanceof JComponent) {
068                    if (!parent.isVisible() || !parent.isDisplayable()) {
069                        return;
070                    }
071                    if (parent instanceof JXPanel && (((JXPanel)parent).getAlpha() < 1f ||
072                        !parent.isOpaque())) {
073                        x += lastDeltaX;
074                        y += lastDeltaY;
075                        lastDeltaX = lastDeltaY = 0;
076                        c = (JComponent)parent;
077                    }
078                    lastDeltaX += parent.getX();
079                    lastDeltaY += parent.getY();
080                    parent = parent.getParent();
081                }
082            }
083            super.addDirtyRegion(c, x, y, w, h);
084        }
085    }