|
Java example source code file (NimbusLookAndFeel.java)
The NimbusLookAndFeel.java Java example source code
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.swing.plaf.nimbus;
import java.awt.BorderLayout;
import static java.awt.BorderLayout.*;
import javax.swing.JComponent;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.plaf.synth.Region;
import javax.swing.plaf.synth.SynthLookAndFeel;
import javax.swing.plaf.synth.SynthStyle;
import javax.swing.plaf.synth.SynthStyleFactory;
import javax.swing.plaf.UIResource;
import java.security.AccessController;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.LayoutManager;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.*;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.JToolBar;
import javax.swing.border.TitledBorder;
import javax.swing.plaf.BorderUIResource;
import javax.swing.plaf.ColorUIResource;
import sun.swing.ImageIconUIResource;
import sun.swing.plaf.synth.SynthIcon;
import sun.swing.plaf.GTKKeybindings;
import sun.swing.plaf.WindowsKeybindings;
import sun.security.action.GetPropertyAction;
/**
* <p>The NimbusLookAndFeel class.
*
* @author Jasper Potts
* @author Richard Bair
*/
public class NimbusLookAndFeel extends SynthLookAndFeel {
/** Set of standard region names for UIDefaults Keys */
private static final String[] COMPONENT_KEYS = new String[]{"ArrowButton", "Button",
"CheckBox", "CheckBoxMenuItem", "ColorChooser", "ComboBox",
"DesktopPane", "DesktopIcon", "EditorPane", "FileChooser",
"FormattedTextField", "InternalFrame",
"InternalFrameTitlePane", "Label", "List", "Menu",
"MenuBar", "MenuItem", "OptionPane", "Panel",
"PasswordField", "PopupMenu", "PopupMenuSeparator",
"ProgressBar", "RadioButton", "RadioButtonMenuItem",
"RootPane", "ScrollBar", "ScrollBarTrack", "ScrollBarThumb",
"ScrollPane", "Separator", "Slider", "SliderTrack",
"SliderThumb", "Spinner", "SplitPane", "TabbedPane",
"Table", "TableHeader", "TextArea", "TextField", "TextPane",
"ToggleButton", "ToolBar", "ToolTip", "Tree", "Viewport"};
/**
* A reference to the auto-generated file NimbusDefaults. This file contains
* the default mappings and values for the look and feel as specified in the
* visual designer.
*/
private NimbusDefaults defaults;
/**
* Reference to populated LAD uidefaults
*/
private UIDefaults uiDefaults;
private DefaultsListener defaultsListener = new DefaultsListener();
/**
* Create a new NimbusLookAndFeel.
*/
public NimbusLookAndFeel() {
super();
defaults = new NimbusDefaults();
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/** Called by UIManager when this look and feel is uninstalled. */
@Override public void uninitialize() {
super.uninitialize();
defaults.uninitialize();
// clear all cached images to free memory
ImageCache.getInstance().flush();
UIManager.getDefaults().removePropertyChangeListener(defaultsListener);
}
/**
* {@inheritDoc}
*/
@Override public UIDefaults getDefaults() {
if (uiDefaults == null){
// Detect platform
String osName = getSystemProperty("os.name");
boolean isWindows = osName != null && osName.contains("Windows");
// We need to call super for basic's properties file.
uiDefaults = super.getDefaults();
defaults.initializeDefaults(uiDefaults);
// Install Keybindings
if (isWindows) {
WindowsKeybindings.installKeybindings(uiDefaults);
} else {
GTKKeybindings.installKeybindings(uiDefaults);
}
// Add Titled Border
uiDefaults.put("TitledBorder.titlePosition",
TitledBorder.ABOVE_TOP);
uiDefaults.put("TitledBorder.border", new BorderUIResource(
new LoweredBorder()));
uiDefaults.put("TitledBorder.titleColor",
getDerivedColor("text",0.0f,0.0f,0.23f,0,true));
uiDefaults.put("TitledBorder.font",
new NimbusDefaults.DerivedFont("defaultFont",
1f, true, null));
// Choose Dialog button positions
uiDefaults.put("OptionPane.isYesLast", !isWindows);
// Store Table ScrollPane Corner Component
uiDefaults.put("Table.scrollPaneCornerComponent",
new UIDefaults.ActiveValue() {
@Override
public Object createValue(UIDefaults table) {
return new TableScrollPaneCorner();
}
});
// Setup the settings for ToolBarSeparator which is custom
// installed for Nimbus
uiDefaults.put("ToolBarSeparator[Enabled].backgroundPainter",
new ToolBarSeparatorPainter());
// Populate UIDefaults with a standard set of properties
for (String componentKey : COMPONENT_KEYS) {
String key = componentKey+".foreground";
if (!uiDefaults.containsKey(key)){
uiDefaults.put(key,
new NimbusProperty(componentKey,"textForeground"));
}
key = componentKey+".background";
if (!uiDefaults.containsKey(key)){
uiDefaults.put(key,
new NimbusProperty(componentKey,"background"));
}
key = componentKey+".font";
if (!uiDefaults.containsKey(key)){
uiDefaults.put(key,
new NimbusProperty(componentKey,"font"));
}
key = componentKey+".disabledText";
if (!uiDefaults.containsKey(key)){
uiDefaults.put(key,
new NimbusProperty(componentKey,"Disabled",
"textForeground"));
}
key = componentKey+".disabled";
if (!uiDefaults.containsKey(key)){
uiDefaults.put(key,
new NimbusProperty(componentKey,"Disabled",
"background"));
}
}
// FileView icon keys are used by some applications, we don't have
// a computer icon at the moment so using home icon for now
uiDefaults.put("FileView.computerIcon",
new LinkProperty("FileChooser.homeFolderIcon"));
uiDefaults.put("FileView.directoryIcon",
new LinkProperty("FileChooser.directoryIcon"));
uiDefaults.put("FileView.fileIcon",
new LinkProperty("FileChooser.fileIcon"));
uiDefaults.put("FileView.floppyDriveIcon",
new LinkProperty("FileChooser.floppyDriveIcon"));
uiDefaults.put("FileView.hardDriveIcon",
new LinkProperty("FileChooser.hardDriveIcon"));
}
return uiDefaults;
}
/**
* Gets the style associated with the given component and region. This
* will never return null. If an appropriate component and region cannot
* be determined, then a default style is returned.
*
* @param c a non-null reference to a JComponent
* @param r a non-null reference to the region of the component c
* @return a non-null reference to a NimbusStyle.
*/
public static NimbusStyle getStyle(JComponent c, Region r) {
return (NimbusStyle)SynthLookAndFeel.getStyle(c, r);
}
/**
* Return a short string that identifies this look and feel. This
* String will be the unquoted String "Nimbus".
*
* @return a short string identifying this look and feel.
*/
@Override public String getName() {
return "Nimbus";
}
/**
* Return a string that identifies this look and feel. This String will
* be the unquoted String "Nimbus".
*
* @return a short string identifying this look and feel.
*/
@Override public String getID() {
return "Nimbus";
}
/**
* Returns a textual description of this look and feel.
*
* @return textual description of this look and feel.
*/
@Override public String getDescription() {
return "Nimbus Look and Feel";
}
/**
* {@inheritDoc}
* @return {@code true}
*/
@Override public boolean shouldUpdateStyleOnAncestorChanged() {
return true;
}
/**
* {@inheritDoc}
*
* <p>Overridden to return {@code true} when one of the following
* properties change:
* <ul>
* <li>{@code "Nimbus.Overrides"}
* <li>{@code "Nimbus.Overrides.InheritDefaults"}
* <li>{@code "JComponent.sizeVariant"}
* </ul>
*
* @since 1.7
*/
@Override
protected boolean shouldUpdateStyleOnEvent(PropertyChangeEvent ev) {
String eName = ev.getPropertyName();
// These properties affect style cached inside NimbusDefaults (6860433)
if ("name" == eName ||
"ancestor" == eName ||
"Nimbus.Overrides" == eName ||
"Nimbus.Overrides.InheritDefaults" == eName ||
"JComponent.sizeVariant" == eName) {
JComponent c = (JComponent) ev.getSource();
defaults.clearOverridesCache(c);
return true;
}
return super.shouldUpdateStyleOnEvent(ev);
}
/**
* <p>Registers a third party component with the NimbusLookAndFeel.
*
* <p>Regions represent Components and areas within Components that act as
* independent painting areas. Once registered with the NimbusLookAndFeel,
* NimbusStyles for these Regions can be retrieved via the
* <code>getStyle method.
*
* <p>The NimbusLookAndFeel uses a standard naming scheme for entries in the
* UIDefaults table. The key for each property, state, painter, and other
* default registered in UIDefaults for a specific Region will begin with
* the specified <code>prefix
*
* <p>For example, suppose I had a component named JFoo. Suppose I then registered
* this component with the NimbusLookAndFeel in this manner:</p>
*
* <pre>
*
* <p>In this case, I could then register properties for this component with
* UIDefaults in the following manner:</p>
*
* <pre>
* UIManager.put("Foo.background", new ColorUIResource(Color.BLACK));
* UIManager.put("Foo.Enabled.backgroundPainter", new FooBackgroundPainter());
* </code>
*
* <p>It is also possible to register a named component with Nimbus.
* For example, suppose you wanted to style the background of a JPanel
* named "MyPanel" differently from other JPanels. You could accomplish this
* by doing the following:</p>
*
* <pre> table
* @return the created <code>Object
*/
@Override
public Object createValue(UIDefaults table) {
Object obj = null;
// check specified state
if (state!=null){
obj = uiDefaults.get(prefix+"["+state+"]."+suffix);
}
// check enabled state
if (obj==null){
obj = uiDefaults.get(prefix+"[Enabled]."+suffix);
}
// check for defaults
if (obj==null){
if (isFont) {
obj = uiDefaults.get("defaultFont");
} else {
obj = uiDefaults.get(suffix);
}
}
return obj;
}
}
private Map<String, MapOther Java examples (source code examples)Here is a short list of links related to this Java NimbusLookAndFeel.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.