001 /* 002 * $Id: PainterGlasspane.java 3235 2009-02-01 15:01:07Z rah003 $ 003 * 004 * Copyright 2006 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 023 package org.jdesktop.swingx.painter; 024 025 import javax.swing.*; 026 import java.awt.*; 027 import java.util.ArrayList; 028 import java.util.List; 029 030 /** 031 * This is a glasspane which will draw the specified painter on 032 * top of the specified target components. The PainterGlasspane would 033 * commonly be used for drawing a translucent overlay or icon badge on top 034 * of components which are invalid, indicating to the user what the problem 035 * is. 036 * 037 * The PainterGlasspane can also be used to apply a Painter on top of a component 038 * which does not already support painters on it's own. 039 * 040 * @author joshy 041 */ 042 public class PainterGlasspane extends JComponent { 043 private Painter painter; 044 private List<JComponent>targets; 045 046 /** Creates a new instance of ValidationOverlay */ 047 public PainterGlasspane() { 048 targets = new ArrayList<JComponent>(); 049 } 050 051 public void addTarget(JComponent comp) { 052 targets.add(comp); 053 repaint(); 054 } 055 public void removeTarget(JComponent comp) { 056 targets.remove(comp); 057 repaint(); 058 } 059 060 protected void paintComponent(Graphics gfx) { 061 Graphics2D g = (Graphics2D)gfx; 062 if(getPainter() != null) { 063 for(JComponent target : targets) { 064 Point offset = calcOffset(target); 065 g.translate(offset.x,offset.y); 066 getPainter().paint(g, target, target.getWidth(), target.getHeight()); 067 g.translate(-offset.x,-offset.y); 068 } 069 } 070 } 071 072 private Point calcOffset(JComponent target) { 073 if(target == null) { 074 return new Point(0,0); 075 } 076 // if the parent is the top then we must be the rootpane? 077 if(target.getParent() == SwingUtilities.getWindowAncestor(target)) { 078 return new Point(0,0); 079 } 080 081 Point parent = calcOffset((JComponent)target.getParent()); 082 Point self = target.getLocation(); 083 return new Point(parent.x + self.x, parent.y + self.y); 084 } 085 086 public Painter getPainter() { 087 return painter; 088 } 089 090 public void setPainter(Painter painter) { 091 Painter old = getPainter(); 092 this.painter = painter; 093 firePropertyChange("painter", old, getPainter()); 094 repaint(); 095 } 096 }