com.alee.laf.panel.WebPanel Maven / Gradle / Ivy
Show all versions of weblaf-ui Show documentation
/*
* This file is part of WebLookAndFeel library.
*
* WebLookAndFeel library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WebLookAndFeel 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WebLookAndFeel library. If not, see .
*/
package com.alee.laf.panel;
import com.alee.laf.WebLookAndFeel;
import com.alee.managers.hotkey.HotkeyData;
import com.alee.managers.language.LanguageContainerMethods;
import com.alee.managers.language.LanguageManager;
import com.alee.managers.language.data.TooltipWay;
import com.alee.managers.log.Log;
import com.alee.managers.style.*;
import com.alee.managers.style.Skin;
import com.alee.managers.style.Skinnable;
import com.alee.managers.style.StyleListener;
import com.alee.managers.tooltip.ToolTipMethods;
import com.alee.managers.tooltip.TooltipManager;
import com.alee.managers.tooltip.WebCustomTooltip;
import com.alee.painter.Paintable;
import com.alee.painter.Painter;
import com.alee.utils.EventUtils;
import com.alee.utils.ReflectUtils;
import com.alee.utils.SizeUtils;
import com.alee.utils.SwingUtils;
import com.alee.utils.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.util.List;
import java.util.Map;
/**
* This JPanel extension class provides a direct access to WebPanelUI methods.
* By default WebPanel uses BorderLayout instead of FlowLayout (unlike JPanel).
*
* This component should never be used with a non-Web UIs as it might cause an unexpected behavior.
* You could still use that component even if WebLaF is not your application L&F as this component will use Web-UI in any case.
*
* @author Mikle Garin
*/
public class WebPanel extends JPanel
implements Styleable, Skinnable, Paintable, ShapeProvider, MarginSupport, PaddingSupport, EventMethods, ToolTipMethods,
SizeMethods, LanguageContainerMethods
{
/**
* Constructs new panel.
*/
public WebPanel ()
{
super ( new BorderLayout () );
}
/**
* Constructs new panel with the specified component automatically added into panel's center area.
*
* @param component component to add into the panel
*/
public WebPanel ( final Component component )
{
super ( new BorderLayout () );
add ( component, BorderLayout.CENTER );
}
/**
* Constructs new panel with the specified layout.
* Also the specified components are automatically added into panel's center area.
*
* @param layout panel layout
* @param components components to add into panel
*/
public WebPanel ( final LayoutManager layout, final Component... components )
{
super ( layout );
add ( components );
}
/**
* Constructs new panel with the specified style ID.
*
* @param id style ID
*/
public WebPanel ( final StyleId id )
{
super ( new BorderLayout () );
setStyleId ( id );
}
/**
* Constructs new panel with the specified style ID.
*
* @param id style ID
* @param component component to add into panel
*/
public WebPanel ( final StyleId id, final Component component )
{
super ( new BorderLayout () );
setStyleId ( id );
add ( component, BorderLayout.CENTER );
}
/**
* Constructs new panel with the specified style ID.
*
* @param id style ID
* @param layout panel layout
* @param components components to add into panel
*/
public WebPanel ( final StyleId id, final LayoutManager layout, final Component... components )
{
super ( layout );
setStyleId ( id );
add ( components );
}
/**
* Returns whether the specified component belongs to this container or not.
*
* @param component component to process
* @return true if the specified component belongs to this container, false otherwise
*/
public boolean contains ( final Component component )
{
return component != null && component.getParent () == this;
}
/**
* Adds all components from the list into the panel under the specified index.
*
* @param components components to add into panel
* @param index index where components should be placed
* @return this panel
*/
public WebPanel add ( final List extends Component> components, final int index )
{
if ( components != null )
{
int skipped = 0;
for ( int i = 0; i < components.size (); i++ )
{
final Component component = components.get ( i );
if ( component != null )
{
add ( component, index + i - skipped );
}
else
{
skipped++;
}
}
}
return this;
}
/**
* Adds all components from the list into the panel under the specified constraints.
*
* @param components components to add into panel
* @param constraints constraints for all components
* @return this panel
*/
public WebPanel add ( final List extends Component> components, final String constraints )
{
if ( components != null )
{
for ( final Component component : components )
{
if ( component != null )
{
add ( component, constraints );
}
}
}
return this;
}
/**
* Adds all components from the list into the panel.
*
* @param components components to add into panel
* @return this panel
*/
public WebPanel add ( final List extends Component> components )
{
if ( components != null )
{
for ( final Component component : components )
{
if ( component != null )
{
add ( component );
}
}
}
return this;
}
/**
* Adds all components into the panel under the specified index.
*
* @param index index where components should be placed
* @param components components to add into panel
* @return this panel
*/
public WebPanel add ( final int index, final Component... components )
{
if ( components != null && components.length > 0 )
{
int skipped = 0;
for ( int i = 0; i < components.length; i++ )
{
final Component component = components[ i ];
if ( component != null )
{
add ( component, index + i - skipped );
}
else
{
skipped++;
}
}
}
return this;
}
/**
* Adds all components into the panel under the specified constraints.
* It might be a rare case when you would require to put more than one component under the same constraint, but it is possible.
*
* @param constraints constraints for all components
* @param components components to add into panel
* @return this panel
*/
public WebPanel add ( final String constraints, final Component... components )
{
if ( components != null && components.length > 0 )
{
for ( final Component component : components )
{
if ( component != null )
{
add ( component, constraints );
}
}
}
return this;
}
/**
* Adds all specified components into the panel.
* Useful for layouts like FlowLayout and some others.
*
* @param components components to add into panel
* @return this panel
*/
public WebPanel add ( final Component... components )
{
if ( components != null && components.length > 0 )
{
for ( final Component component : components )
{
if ( component != null )
{
add ( component );
}
}
}
return this;
}
/**
* Removes all components from the list from the panel.
*
* @param components components to remove from panel
* @return this panel
*/
public WebPanel remove ( final List extends Component> components )
{
if ( components != null )
{
for ( final Component component : components )
{
if ( component != null )
{
remove ( component );
}
}
}
return this;
}
/**
* Removes all specified components from the panel.
*
* @param components components to remove from panel
* @return this panel
*/
public WebPanel remove ( final Component... components )
{
if ( components != null && components.length > 0 )
{
for ( final Component component : components )
{
if ( component != null )
{
remove ( component );
}
}
}
return this;
}
/**
* Removes all children with the specified component class type.
*
* @param componentClass class type of child components to be removed
* @return this panel
*/
public WebPanel removeAll ( final Class extends Component> componentClass )
{
if ( componentClass != null )
{
for ( int i = 0; i < getComponentCount (); i++ )
{
final Component component = getComponent ( i );
if ( componentClass.isAssignableFrom ( component.getClass () ) )
{
remove ( component );
}
}
}
return this;
}
/**
* Returns first component contained in this panel.
*
* @return first component contained in this panel
*/
public Component getFirstComponent ()
{
if ( getComponentCount () > 0 )
{
return getComponent ( 0 );
}
else
{
return null;
}
}
/**
* Returns last component contained in this panel.
*
* @return last component contained in this panel
*/
public Component getLastComponent ()
{
if ( getComponentCount () > 0 )
{
return getComponent ( getComponentCount () - 1 );
}
else
{
return null;
}
}
@Override
public StyleId getStyleId ()
{
return getWebUI ().getStyleId ();
}
@Override
public StyleId setStyleId ( final StyleId id )
{
return getWebUI ().setStyleId ( id );
}
@Override
public Skin getSkin ()
{
return StyleManager.getSkin ( this );
}
@Override
public Skin setSkin ( final Skin skin )
{
return StyleManager.setSkin ( this, skin );
}
@Override
public Skin setSkin ( final Skin skin, final boolean recursively )
{
return StyleManager.setSkin ( this, skin, recursively );
}
@Override
public Skin restoreSkin ()
{
return StyleManager.restoreSkin ( this );
}
@Override
public void addStyleListener ( final StyleListener listener )
{
StyleManager.addStyleListener ( this, listener );
}
@Override
public void removeStyleListener ( final StyleListener listener )
{
StyleManager.removeStyleListener ( this, listener );
}
@Override
public Map getCustomPainters ()
{
return StyleManager.getCustomPainters ( this );
}
@Override
public Painter getCustomPainter ()
{
return StyleManager.getCustomPainter ( this );
}
@Override
public Painter getCustomPainter ( final String id )
{
return StyleManager.getCustomPainter ( this, id );
}
@Override
public Painter setCustomPainter ( final Painter painter )
{
return StyleManager.setCustomPainter ( this, painter );
}
@Override
public Painter setCustomPainter ( final String id, final Painter painter )
{
return StyleManager.setCustomPainter ( this, id, painter );
}
@Override
public boolean restoreDefaultPainters ()
{
return StyleManager.restoreDefaultPainters ( this );
}
@Override
public Shape provideShape ()
{
return getWebUI ().provideShape ();
}
@Override
public Insets getMargin ()
{
return getWebUI ().getMargin ();
}
/**
* Sets new margin.
*
* @param margin new margin
*/
public void setMargin ( final int margin )
{
setMargin ( margin, margin, margin, margin );
}
/**
* Sets new margin.
*
* @param top new top margin
* @param left new left margin
* @param bottom new bottom margin
* @param right new right margin
*/
public void setMargin ( final int top, final int left, final int bottom, final int right )
{
setMargin ( new Insets ( top, left, bottom, right ) );
}
@Override
public void setMargin ( final Insets margin )
{
getWebUI ().setMargin ( margin );
}
@Override
public Insets getPadding ()
{
return getWebUI ().getPadding ();
}
/**
* Sets new padding.
*
* @param padding new padding
*/
public void setPadding ( final int padding )
{
setPadding ( padding, padding, padding, padding );
}
/**
* Sets new padding.
*
* @param top new top padding
* @param left new left padding
* @param bottom new bottom padding
* @param right new right padding
*/
public void setPadding ( final int top, final int left, final int bottom, final int right )
{
setPadding ( new Insets ( top, left, bottom, right ) );
}
@Override
public void setPadding ( final Insets padding )
{
getWebUI ().setPadding ( padding );
}
/**
* Returns Web-UI applied to this class.
*
* @return Web-UI applied to this class
*/
private WebPanelUI getWebUI ()
{
return ( WebPanelUI ) getUI ();
}
/**
* Installs a Web-UI into this component.
*/
@Override
public void updateUI ()
{
if ( getUI () == null || !( getUI () instanceof WebPanelUI ) )
{
try
{
setUI ( ( WebPanelUI ) ReflectUtils.createInstance ( WebLookAndFeel.panelUI ) );
}
catch ( final Throwable e )
{
Log.error ( this, e );
setUI ( new WebPanelUI () );
}
}
else
{
setUI ( getUI () );
}
}
@Override
public MouseAdapter onMousePress ( final MouseEventRunnable runnable )
{
return EventUtils.onMousePress ( this, runnable );
}
@Override
public MouseAdapter onMousePress ( final MouseButton mouseButton, final MouseEventRunnable runnable )
{
return EventUtils.onMousePress ( this, mouseButton, runnable );
}
@Override
public MouseAdapter onMouseEnter ( final MouseEventRunnable runnable )
{
return EventUtils.onMouseEnter ( this, runnable );
}
@Override
public MouseAdapter onMouseExit ( final MouseEventRunnable runnable )
{
return EventUtils.onMouseExit ( this, runnable );
}
@Override
public MouseAdapter onMouseDrag ( final MouseEventRunnable runnable )
{
return EventUtils.onMouseDrag ( this, runnable );
}
@Override
public MouseAdapter onMouseDrag ( final MouseButton mouseButton, final MouseEventRunnable runnable )
{
return EventUtils.onMouseDrag ( this, mouseButton, runnable );
}
@Override
public MouseAdapter onMouseClick ( final MouseEventRunnable runnable )
{
return EventUtils.onMouseClick ( this, runnable );
}
@Override
public MouseAdapter onMouseClick ( final MouseButton mouseButton, final MouseEventRunnable runnable )
{
return EventUtils.onMouseClick ( this, mouseButton, runnable );
}
@Override
public MouseAdapter onDoubleClick ( final MouseEventRunnable runnable )
{
return EventUtils.onDoubleClick ( this, runnable );
}
@Override
public MouseAdapter onMenuTrigger ( final MouseEventRunnable runnable )
{
return EventUtils.onMenuTrigger ( this, runnable );
}
@Override
public KeyAdapter onKeyType ( final KeyEventRunnable runnable )
{
return EventUtils.onKeyType ( this, runnable );
}
@Override
public KeyAdapter onKeyType ( final HotkeyData hotkey, final KeyEventRunnable runnable )
{
return EventUtils.onKeyType ( this, hotkey, runnable );
}
@Override
public KeyAdapter onKeyPress ( final KeyEventRunnable runnable )
{
return EventUtils.onKeyPress ( this, runnable );
}
@Override
public KeyAdapter onKeyPress ( final HotkeyData hotkey, final KeyEventRunnable runnable )
{
return EventUtils.onKeyPress ( this, hotkey, runnable );
}
@Override
public KeyAdapter onKeyRelease ( final KeyEventRunnable runnable )
{
return EventUtils.onKeyRelease ( this, runnable );
}
@Override
public KeyAdapter onKeyRelease ( final HotkeyData hotkey, final KeyEventRunnable runnable )
{
return EventUtils.onKeyRelease ( this, hotkey, runnable );
}
@Override
public FocusAdapter onFocusGain ( final FocusEventRunnable runnable )
{
return EventUtils.onFocusGain ( this, runnable );
}
@Override
public FocusAdapter onFocusLoss ( final FocusEventRunnable runnable )
{
return EventUtils.onFocusLoss ( this, runnable );
}
@Override
public WebCustomTooltip setToolTip ( final String tooltip )
{
return TooltipManager.setTooltip ( this, tooltip );
}
@Override
public WebCustomTooltip setToolTip ( final Icon icon, final String tooltip )
{
return TooltipManager.setTooltip ( this, icon, tooltip );
}
@Override
public WebCustomTooltip setToolTip ( final String tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.setTooltip ( this, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip setToolTip ( final Icon icon, final String tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.setTooltip ( this, icon, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip setToolTip ( final String tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.setTooltip ( this, tooltip, tooltipWay, delay );
}
@Override
public WebCustomTooltip setToolTip ( final Icon icon, final String tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.setTooltip ( this, icon, tooltip, tooltipWay, delay );
}
@Override
public WebCustomTooltip setToolTip ( final JComponent tooltip )
{
return TooltipManager.setTooltip ( this, tooltip );
}
@Override
public WebCustomTooltip setToolTip ( final JComponent tooltip, final int delay )
{
return TooltipManager.setTooltip ( this, tooltip, delay );
}
@Override
public WebCustomTooltip setToolTip ( final JComponent tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.setTooltip ( this, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip setToolTip ( final JComponent tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.setTooltip ( this, tooltip, tooltipWay, delay );
}
@Override
public WebCustomTooltip addToolTip ( final String tooltip )
{
return TooltipManager.addTooltip ( this, tooltip );
}
@Override
public WebCustomTooltip addToolTip ( final Icon icon, final String tooltip )
{
return TooltipManager.addTooltip ( this, icon, tooltip );
}
@Override
public WebCustomTooltip addToolTip ( final String tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.addTooltip ( this, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip addToolTip ( final Icon icon, final String tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.addTooltip ( this, icon, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip addToolTip ( final String tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.addTooltip ( this, tooltip, tooltipWay, delay );
}
@Override
public WebCustomTooltip addToolTip ( final Icon icon, final String tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.addTooltip ( this, icon, tooltip, tooltipWay, delay );
}
@Override
public WebCustomTooltip addToolTip ( final JComponent tooltip )
{
return TooltipManager.addTooltip ( this, tooltip );
}
@Override
public WebCustomTooltip addToolTip ( final JComponent tooltip, final int delay )
{
return TooltipManager.addTooltip ( this, tooltip, delay );
}
@Override
public WebCustomTooltip addToolTip ( final JComponent tooltip, final TooltipWay tooltipWay )
{
return TooltipManager.addTooltip ( this, tooltip, tooltipWay );
}
@Override
public WebCustomTooltip addToolTip ( final JComponent tooltip, final TooltipWay tooltipWay, final int delay )
{
return TooltipManager.addTooltip ( this, tooltip, tooltipWay, delay );
}
@Override
public void removeToolTip ( final WebCustomTooltip tooltip )
{
TooltipManager.removeTooltip ( this, tooltip );
}
@Override
public void removeToolTips ()
{
TooltipManager.removeTooltips ( this );
}
@Override
public void removeToolTips ( final WebCustomTooltip... tooltips )
{
TooltipManager.removeTooltips ( this, tooltips );
}
@Override
public void removeToolTips ( final List tooltips )
{
TooltipManager.removeTooltips ( this, tooltips );
}
@Override
public int getPreferredWidth ()
{
return SizeUtils.getPreferredWidth ( this );
}
@Override
public WebPanel setPreferredWidth ( final int preferredWidth )
{
return SizeUtils.setPreferredWidth ( this, preferredWidth );
}
@Override
public int getPreferredHeight ()
{
return SizeUtils.getPreferredHeight ( this );
}
@Override
public WebPanel setPreferredHeight ( final int preferredHeight )
{
return SizeUtils.setPreferredHeight ( this, preferredHeight );
}
@Override
public int getMinimumWidth ()
{
return SizeUtils.getMinimumWidth ( this );
}
@Override
public WebPanel setMinimumWidth ( final int minimumWidth )
{
return SizeUtils.setMinimumWidth ( this, minimumWidth );
}
@Override
public int getMinimumHeight ()
{
return SizeUtils.getMinimumHeight ( this );
}
@Override
public WebPanel setMinimumHeight ( final int minimumHeight )
{
return SizeUtils.setMinimumHeight ( this, minimumHeight );
}
@Override
public int getMaximumWidth ()
{
return SizeUtils.getMaximumWidth ( this );
}
@Override
public WebPanel setMaximumWidth ( final int maximumWidth )
{
return SizeUtils.setMaximumWidth ( this, maximumWidth );
}
@Override
public int getMaximumHeight ()
{
return SizeUtils.getMaximumHeight ( this );
}
@Override
public WebPanel setMaximumHeight ( final int maximumHeight )
{
return SizeUtils.setMaximumHeight ( this, maximumHeight );
}
@Override
public Dimension getPreferredSize ()
{
return SizeUtils.getPreferredSize ( this, super.getPreferredSize () );
}
@Override
public WebPanel setPreferredSize ( final int width, final int height )
{
return SizeUtils.setPreferredSize ( this, width, height );
}
@Override
public void setLanguageContainerKey ( final String key )
{
LanguageManager.registerLanguageContainer ( this, key );
}
@Override
public void removeLanguageContainerKey ()
{
LanguageManager.unregisterLanguageContainer ( this );
}
@Override
public String getLanguageContainerKey ()
{
return LanguageManager.getLanguageContainerKey ( this );
}
/**
* Makes all container child component widths equal.
*/
public void equalizeComponentsWidth ()
{
SwingUtils.equalizeComponentsWidth ( getComponents () );
}
/**
* Makes all container child component heights equal.
*/
public void equalizeComponentsHeight ()
{
SwingUtils.equalizeComponentsHeight ( getComponents () );
}
/**
* Makes all container child component sizes equal.
*/
public void equalizeComponentsSize ()
{
SwingUtils.equalizeComponentsSize ( getComponents () );
}
}