bibliothek.gui.dock.util.BackgroundPanel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docking-frames-core Show documentation
Show all versions of docking-frames-core Show documentation
${project.name} is base or core library
The newest version!
/*
* Bibliothek - DockingFrames
* Library built on Java/Swing, allows the user to "drag and drop"
* panels containing any Swing-Component the developer likes to add.
*
* Copyright (C) 2010 Benjamin Sigg
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Benjamin Sigg
* [email protected]
* CH - Switzerland
*/
package bibliothek.gui.dock.util;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.LayoutManager;
import javax.swing.JPanel;
/**
* This {@link JPanel} implements {@link PaintableComponent} and
* can use a {@link BackgroundAlgorithm} to paint its background.
* @author Benjamin Sigg
*/
public abstract class BackgroundPanel extends JPanel implements PaintableComponent{
private BackgroundAlgorithm background;
/** whether this panel is transparent, paints some or all pixels */
private Transparency transparency;
/** added to the {@link BackgroundAlgorithm} that paints this component */
private BackgroundAlgorithmListener listener = new BackgroundAlgorithmListener(){
public void transparencyChanged( BackgroundAlgorithm algorithm, Transparency transparency ){
configure( transparency );
}
};
/**
* Creates a new panel.
* @param transparency how many pixels are painted
*/
public BackgroundPanel( Transparency transparency ){
setTransparency( transparency );
}
/**
* Creates a new panel setting a default {@link LayoutManager}.
* @param layout the layout manager, can be null
* @param transparency how many pixels are painted
*/
public BackgroundPanel( LayoutManager layout, Transparency transparency ){
super( layout );
setTransparency( transparency );
}
/**
* Sets the background algorithm that should be used by this panel.
* @param background the background algorithm
*/
public void setBackground( BackgroundAlgorithm background ){
if( this.background != null ){
this.background.removeListener( listener );
}
this.background = background;
if( background != null ){
background.addListener( listener );
configure( background.getTransparency() );
}
}
/**
* Called if the {@link Transparency} of the {@link BackgroundAlgorithm} changed, this panel
* should configure itself to met the requested transparency settings.
* @param transparency the setting to use
*/
protected abstract void configure( Transparency transparency );
/**
* Gets the algorithm that paints the background of this panel.
* @return the algorithm, can be null
*/
public BackgroundAlgorithm getBackgroundAlgorithm(){
return background;
}
@Override
public void paint( Graphics g ){
if( background == null || background.getPaint() == null ){
super.paint( g );
paintOverlay( g );
}
else{
background.paint( this, g );
}
}
protected void paintComponent( Graphics g ){
paintBackground( g );
paintForeground( g );
}
public void setTransparency( Transparency transparency ){
this.transparency = transparency;
setOpaque( transparency == Transparency.SOLID );
}
public Transparency getTransparency(){
return transparency;
}
public Component getComponent(){
return this;
}
public void paintBackground( Graphics g ){
if( transparency != Transparency.TRANSPARENT ){
super.paintComponent( g );
}
}
public void paintForeground( Graphics g ){
// ignore
}
@Override
public void paintBorder( Graphics g ){
super.paintBorder( g );
}
@Override
public void paintChildren( Graphics g ){
super.paintChildren( g );
}
public void paintOverlay( Graphics g ){
// ignore
}
}