com.alee.laf.rootpane.WebWindow 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.rootpane;
import com.alee.managers.focus.DefaultFocusTracker;
import com.alee.managers.focus.FocusManager;
import com.alee.managers.language.LanguageContainerMethods;
import com.alee.managers.language.LanguageManager;
import com.alee.managers.settings.DefaultValue;
import com.alee.managers.settings.SettingsManager;
import com.alee.managers.settings.SettingsMethods;
import com.alee.managers.settings.SettingsProcessor;
import com.alee.managers.style.*;
import com.alee.painter.Paintable;
import com.alee.painter.Painter;
import com.alee.utils.EventUtils;
import com.alee.utils.SwingUtils;
import com.alee.utils.WindowUtils;
import com.alee.utils.swing.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.util.List;
import java.util.Map;
/**
* This JWindow extenstion class provides some additional methods and options to manipulate window behavior.
*
* @author Mikle Garin
*/
public class WebWindow extends JWindow
implements Styleable, Skinnable, Paintable, PaddingSupport, WindowEventMethods, LanguageContainerMethods, SettingsMethods,
WindowMethods
{
/**
* Whether should close window on focus loss or not.
*/
protected boolean closeOnFocusLoss = false;
/**
* Window focus tracker.
*/
protected DefaultFocusTracker focusTracker;
/**
* Creates a window with no specified owner. This window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*/
public WebWindow ()
{
this ( StyleId.window );
}
/**
* Creates a window with the specified {@code GraphicsConfiguration} of a screen device. This window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param gc the {@code GraphicsConfiguration} that is used to construct the new window with; if gc is {@code null},
* the system default {@code GraphicsConfiguration} is assumed
*/
public WebWindow ( final GraphicsConfiguration gc )
{
this ( StyleId.window, gc );
}
/**
* Creates a window with the owner window from the specified component.
* This window will not be focusable unless its owner is showing on the screen.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param owner the componnt from which parent window this window is displayed
*/
public WebWindow ( final Component owner )
{
this ( StyleId.window, owner );
}
/**
* Creates a window with the specified owner window. This window will not be focusable unless its owner is showing on the screen.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param owner the window from which the window is displayed
*/
public WebWindow ( final Window owner )
{
this ( StyleId.window, owner );
}
/**
* Creates a window with the specified owner window and {@code GraphicsConfiguration} of a screen device.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param owner the window from which the window is displayed
* @param gc the {@code GraphicsConfiguration} that is used to construct the new window with; if gc is {@code null},
* the system default {@code GraphicsConfiguration} is assumed, unless {@code owner} is also null, in which
* case the {@code GraphicsConfiguration} from the shared owner frame will be used
*/
public WebWindow ( final Window owner, final GraphicsConfiguration gc )
{
this ( StyleId.window, owner, gc );
}
/**
* Creates a window with the specified owner frame.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
* Also, this window will not be focusable unless its owner is showing on the screen.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param owner the frame from which the window is displayed
*/
public WebWindow ( final Frame owner )
{
this ( StyleId.window, owner );
}
/**
* Creates a window with no specified owner. This window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
*/
public WebWindow ( final StyleId id )
{
super ();
initialize ( id );
}
/**
* Creates a window with the specified {@code GraphicsConfiguration} of a screen device. This window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
* @param gc the {@code GraphicsConfiguration} that is used to construct the new window with; if gc is {@code null},
* the system default {@code GraphicsConfiguration} is assumed
*/
public WebWindow ( final StyleId id, final GraphicsConfiguration gc )
{
super ( gc );
initialize ( id );
}
/**
* Creates a window with the owner window from the specified component.
* This window will not be focusable unless its owner is showing on the screen.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
* @param owner the componnt from which parent window this window is displayed
*/
public WebWindow ( final StyleId id, final Component owner )
{
super ( SwingUtils.getWindowAncestor ( owner ) );
initialize ( id );
}
/**
* Creates a window with the specified owner window. This window will not be focusable unless its owner is showing on the screen.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
* @param owner the window from which the window is displayed
*/
public WebWindow ( final StyleId id, final Window owner )
{
super ( owner );
initialize ( id );
}
/**
* Creates a window with the specified owner window and {@code GraphicsConfiguration} of a screen device.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
* @param owner the window from which the window is displayed
* @param gc the {@code GraphicsConfiguration} that is used to construct the new window with; if gc is {@code null},
* the system default {@code GraphicsConfiguration} is assumed, unless {@code owner} is also null, in which
* case the {@code GraphicsConfiguration} from the shared owner frame will be used
*/
public WebWindow ( final StyleId id, final Window owner, final GraphicsConfiguration gc )
{
super ( owner, gc );
initialize ( id );
}
/**
* Creates a window with the specified owner frame.
* If {@code owner} is {@code null}, the shared owner will be used and this window will not be focusable.
* Also, this window will not be focusable unless its owner is showing on the screen.
*
* This constructor sets the component's locale property to the value returned by {@code JComponent.getDefaultLocale}.
*
* @param id style ID
* @param owner the frame from which the window is displayed
*/
public WebWindow ( final StyleId id, final Frame owner )
{
super ( owner );
initialize ( id );
}
/**
* Additional initializtion of WebWindow settings.
*
* @param id initial style ID
*/
protected void initialize ( final StyleId id )
{
// Updating base settings
setFocusable ( true );
setFocusableWindowState ( true );
SwingUtils.setOrientation ( this );
// Installing root pane style
if ( id != null )
{
setStyleId ( id );
}
// Adding focus tracker for this window
// It is stored into a separate field to avoid its disposal from memory
focusTracker = new DefaultFocusTracker ( true )
{
@Override
public boolean isTrackingEnabled ()
{
return isShowing () && closeOnFocusLoss;
}
@Override
public void focusChanged ( final boolean focused )
{
if ( closeOnFocusLoss && isShowing () && !focused )
{
dispose ();
}
}
};
FocusManager.addFocusTracker ( this, focusTracker );
}
/**
* Called by the constructor methods to create the default {@code rootPane}.
*/
@Override
protected JRootPane createRootPane ()
{
return new WebRootPane ();
}
/**
* Returns whether should close window on focus loss or not.
*
* @return true if should close window on focus loss, false otherwise
*/
public boolean isCloseOnFocusLoss ()
{
return closeOnFocusLoss;
}
/**
* Sets whether should close window on focus loss or not.
*
* @param closeOnFocusLoss whether should close window on focus loss or not
*/
public void setCloseOnFocusLoss ( final boolean closeOnFocusLoss )
{
this.closeOnFocusLoss = closeOnFocusLoss;
}
/**
* Returns focusable children that don't force window to close even if it set to close on focus loss.
*
* @return focusable children that don't force window to close even if it set to close on focus loss
*/
public List getFocusableChildren ()
{
return focusTracker.getCustomChildren ();
}
/**
* Adds focusable child that won't force window to close even if it set to close on focus loss.
*
* @param child focusable child that won't force window to close even if it set to close on focus loss
*/
public void addFocusableChild ( final Component child )
{
focusTracker.addCustomChild ( child );
}
/**
* Removes focusable child that doesn't force window to close even if it set to close on focus loss.
*
* @param child focusable child that doesn't force window to close even if it set to close on focus loss
*/
public void removeFocusableChild ( final Component child )
{
focusTracker.removeCustomChild ( child );
}
@Override
public StyleId getStyleId ()
{
return getRootPaneWebUI ().getStyleId ();
}
@Override
public StyleId setStyleId ( final StyleId id )
{
return getRootPaneWebUI ().setStyleId ( id );
}
@Override
public Skin getSkin ()
{
return StyleManager.getSkin ( getRootPane () );
}
@Override
public Skin setSkin ( final Skin skin )
{
return StyleManager.setSkin ( getRootPane (), skin );
}
@Override
public Skin setSkin ( final Skin skin, final boolean recursively )
{
return StyleManager.setSkin ( getRootPane (), skin, recursively );
}
@Override
public Skin restoreSkin ()
{
return StyleManager.restoreSkin ( getRootPane () );
}
@Override
public void addStyleListener ( final StyleListener listener )
{
StyleManager.addStyleListener ( getRootPane (), listener );
}
@Override
public void removeStyleListener ( final StyleListener listener )
{
StyleManager.removeStyleListener ( getRootPane (), listener );
}
@Override
public Map getCustomPainters ()
{
return StyleManager.getCustomPainters ( getRootPane () );
}
@Override
public Painter getCustomPainter ()
{
return StyleManager.getCustomPainter ( getRootPane () );
}
@Override
public Painter getCustomPainter ( final String id )
{
return StyleManager.getCustomPainter ( getRootPane (), id );
}
@Override
public Painter setCustomPainter ( final Painter painter )
{
return StyleManager.setCustomPainter ( getRootPane (), painter );
}
@Override
public Painter setCustomPainter ( final String id, final Painter painter )
{
return StyleManager.setCustomPainter ( getRootPane (), id, painter );
}
@Override
public boolean restoreDefaultPainters ()
{
return StyleManager.restoreDefaultPainters ( getRootPane () );
}
@Override
public Insets getPadding ()
{
return getRootPaneWebUI ().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 )
{
getRootPaneWebUI ().setPadding ( padding );
}
/**
* Returns Web-UI applied to this class.
*
* @return Web-UI applied to this class
*/
protected WebRootPaneUI getWebUI ()
{
return getRootPaneWebUI ();
}
/**
* Returns Web-UI applied to root pane used by this window.
*
* @return Web-UI applied to root pane used by this window
*/
protected WebRootPaneUI getRootPaneWebUI ()
{
return ( WebRootPaneUI ) getRootPane ().getUI ();
}
@Override
public WindowAdapter onClosing ( final WindowEventRunnable runnable )
{
return EventUtils.onClosing ( this, runnable );
}
@Override
public WindowCloseAdapter onClose ( final ComponentEventRunnable runnable )
{
return EventUtils.onClose ( this, runnable );
}
@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 );
}
@Override
public void registerSettings ( final String key )
{
SettingsManager.registerComponent ( getRootPane (), key );
}
@Override
public void registerSettings ( final String key, final Class defaultValueClass )
{
SettingsManager.registerComponent ( getRootPane (), key, defaultValueClass );
}
@Override
public void registerSettings ( final String key, final Object defaultValue )
{
SettingsManager.registerComponent ( getRootPane (), key, defaultValue );
}
@Override
public void registerSettings ( final String group, final String key )
{
SettingsManager.registerComponent ( getRootPane (), group, key );
}
@Override
public void registerSettings ( final String group, final String key, final Class defaultValueClass )
{
SettingsManager.registerComponent ( getRootPane (), group, key, defaultValueClass );
}
@Override
public void registerSettings ( final String group, final String key, final Object defaultValue )
{
SettingsManager.registerComponent ( getRootPane (), group, key, defaultValue );
}
@Override
public void registerSettings ( final String key, final boolean loadInitialSettings, final boolean applySettingsChanges )
{
SettingsManager.registerComponent ( getRootPane (), key, loadInitialSettings, applySettingsChanges );
}
@Override
public void registerSettings ( final String key, final Class defaultValueClass,
final boolean loadInitialSettings, final boolean applySettingsChanges )
{
SettingsManager.registerComponent ( getRootPane (), key, defaultValueClass, loadInitialSettings, applySettingsChanges );
}
@Override
public void registerSettings ( final String key, final Object defaultValue, final boolean loadInitialSettings,
final boolean applySettingsChanges )
{
SettingsManager.registerComponent ( getRootPane (), key, defaultValue, loadInitialSettings, applySettingsChanges );
}
@Override
public void registerSettings ( final String group, final String key, final Class defaultValueClass,
final boolean loadInitialSettings, final boolean applySettingsChanges )
{
SettingsManager.registerComponent ( getRootPane (), group, key, defaultValueClass, loadInitialSettings, applySettingsChanges );
}
@Override
public void registerSettings ( final String group, final String key, final Object defaultValue, final boolean loadInitialSettings,
final boolean applySettingsChanges )
{
SettingsManager.registerComponent ( getRootPane (), group, key, defaultValue, loadInitialSettings, applySettingsChanges );
}
@Override
public void registerSettings ( final SettingsProcessor settingsProcessor )
{
SettingsManager.registerComponent ( getRootPane (), settingsProcessor );
}
@Override
public void unregisterSettings ()
{
SettingsManager.unregisterComponent ( getRootPane () );
}
@Override
public void loadSettings ()
{
SettingsManager.loadComponentSettings ( getRootPane () );
}
@Override
public void saveSettings ()
{
SettingsManager.saveComponentSettings ( getRootPane () );
}
@Override
public WebWindow setWindowOpaque ( final boolean opaque )
{
return WindowUtils.setWindowOpaque ( this, opaque );
}
@Override
public boolean isWindowOpaque ()
{
return WindowUtils.isWindowOpaque ( this );
}
@Override
public WebWindow setWindowOpacity ( final float opacity )
{
return WindowUtils.setWindowOpacity ( this, opacity );
}
@Override
public float getWindowOpacity ()
{
return WindowUtils.getWindowOpacity ( this );
}
@Override
public WebWindow center ()
{
return WindowUtils.center ( this );
}
@Override
public WebWindow center ( final Component relativeTo )
{
return WindowUtils.center ( this, relativeTo );
}
@Override
public WebWindow center ( final int width, final int height )
{
return WindowUtils.center ( this, width, height );
}
@Override
public WebWindow center ( final Component relativeTo, final int width, final int height )
{
return WindowUtils.center ( this, relativeTo, width, height );
}
@Override
public WebWindow packToWidth ( final int width )
{
return WindowUtils.packToWidth ( this, width );
}
@Override
public WebWindow packToHeight ( final int height )
{
return WindowUtils.packToHeight ( this, height );
}
@Override
public WebWindow packAndCenter ()
{
return WindowUtils.packAndCenter ( this );
}
@Override
public WebWindow packAndCenter ( final boolean animate )
{
return WindowUtils.packAndCenter ( this, animate );
}
}