org.netbeans.swing.plaf.Startup Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.swing.plaf;
import java.awt.Toolkit;
import java.util.logging.Logger;
import org.netbeans.swing.plaf.aqua.AquaLFCustoms;
import org.netbeans.swing.plaf.gtk.GtkLFCustoms;
import org.netbeans.swing.plaf.metal.MetalLFCustoms;
import org.netbeans.swing.plaf.util.NbTheme;
import org.netbeans.swing.plaf.util.RelativeColor;
import org.netbeans.swing.plaf.util.UIBootstrapValue;
import org.netbeans.swing.plaf.util.UIUtils;
import javax.swing.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import org.netbeans.swing.plaf.nimbus.NimbusLFCustoms;
import org.netbeans.swing.plaf.winclassic.WindowsLFCustoms;
import org.netbeans.swing.plaf.windows8.Windows8LFCustoms;
import org.netbeans.swing.plaf.winvista.VistaLFCustoms;
import org.netbeans.swing.plaf.winxp.XPLFCustoms;
/** Singleton, manages customizers for various LFs. Installs, uninstalls them on LF change.
* LF customizers works with Swing UIManager, putting info about various UI elements
* in it. Other modules then can query UIManager to get UI elements to get needed
* visual design and behaviour.
*
* @author Dafe Simonek, Tim Boudreau
*/
public final class Startup {
//originally LFCustomsManager
/** For debugging purposes, enable forcing the customizations for, i.e.,
* Windows look and feel on a platform that doesn't support it */
private static final String FORCED_CUSTOMS = System.getProperty("nb.forceui"); //NOI18N
/** Provides the ability to disable customizations for applications which, for example, provide their own
* subclass of MetalLookAndFeel. See issue XXX
*/
private static final boolean NO_CUSTOMIZATIONS = Boolean.getBoolean("netbeans.plaf.disable.ui.customizations"); //NOI18N
/** Constant for Nimbus L&F name */
private static final String NIMBUS="Nimbus";
/** Singleton instance */
private static Startup instance = null;
/** Currently used LF customizer */
private LFCustoms curCustoms = null;
private LFCustoms globalCustoms = null;
private static URL themeURL = null;
private static Class uiClass = null;
private static ResourceBundle bundle;
private boolean installed = false;
/** Starts handling of LF customizers. Called only from getInstance. */
private Startup() {
initialize();
}
/** Initializes defaulf customs for all LFs and fills UIManager with
* references to LF customizers for supported LFs.
*/
private void initialize() {
LFInstanceOrName lfon = getLookAndFeel();
boolean forceLaf = false;
if (lfon.lf instanceof MetalLookAndFeel) {
//Metal theme must be assigned before using the look and feel
forceLaf = installTheme(lfon.lf);
}
// overall defaults for all LFs
// defaults for supported LFs
try {
if (forceLaf ||
(lfon.lf != null && lfon.lf != UIManager.getLookAndFeel()) ||
(lfon.lfClassName != null && !lfon.lfClassName.equals(UIManager.getLookAndFeel().getClass().getName()))) {
if (lfon.lf != null) {
UIManager.setLookAndFeel(lfon.lf);
uiClass = lfon.lf.getClass();
} else {
boolean success = false;
try {
UIManager.setLookAndFeel(lfon.lfClassName);
success = true;
uiClass = UIManager.getLookAndFeel().getClass();
} catch (ClassNotFoundException ex) {
System.err.println("Custom UI class " + lfon.lfClassName + " not found."); // NOI18N
} catch (IllegalAccessException ex) {
System.err.println("Custom UI class " + lfon.lfClassName + " not possible to access."); // NOI18N
} catch (InstantiationException ex) {
System.err.println("Custom UI class " + lfon.lfClassName + " not possible to instantiate."); // NOI18N
} catch (UnsupportedLookAndFeelException ex) {
System.err.println("Custom UI class " + lfon.lfClassName + " not supported as a look & feel."); // NOI18N
}
if (!success) {
//#144402 - try fallback to Metal L&F
LookAndFeel mlf = new MetalLookAndFeel();
installTheme(mlf);
UIManager.setLookAndFeel(mlf);
uiClass = MetalLookAndFeel.class;
}
}
}
} catch (Exception e) {
System.err.println ("Could not install look and feel " + lfon.getClassName());
}
}
private LFInstanceOrName getLookAndFeel() {
// related to #118534 - log info about Nimbus L&F
if (uiClass != null && uiClass.getName().contains(NIMBUS)) {
Logger.getLogger(getClass().getName()).warning(
"L&F Warning: Nimbus L&F is not supported L&F yet and system " +
"may exhibit various drawing problems. Please use for experimental purposes only.");
}
if (uiClass == null) {
ResourceBundle b = bundle != null ? bundle : ResourceBundle.getBundle("org.netbeans.swing.plaf.Bundle"); // NOI18N
String uiClassName = b.getString("LookAndFeelClassName"); // NOI18N
if ("default".equals(uiClassName)) { // NOI18N
uiClassName = defaultLaF();
}
if (uiClassName.equals(MetalLookAndFeel.class.getName())) {
return new LFInstanceOrName(new MetalLookAndFeel());
} else {
return new LFInstanceOrName(uiClassName);
}
} else {
LookAndFeel lf = UIManager.getLookAndFeel();
if (uiClass != lf.getClass()) {
try {
lf = (LookAndFeel) uiClass.getDeclaredConstructor().newInstance();
} catch (ReflectiveOperationException ex) {
return new LFInstanceOrName(uiClass.getName());
}
}
return new LFInstanceOrName(lf);
}
}
/** Default NetBeans logic for finding out the right L&F.
* @return name of the LaF to instantiate
*/
private static String defaultLaF() {
String uiClassName;
if (isWindows()) {
uiClassName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; //NOI18N
} else if (isMac()) {
uiClassName = "com.apple.laf.AquaLookAndFeel";
} else if (shouldUseMetal()) {
uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel"; //NOI18N
} else {
//Should get us metal where it doesn't get us GTK
uiClassName = UIManager.getSystemLookAndFeelClassName();
// Enable GTK L&F only for JDK version 1.6.0 update 1 and later.
// GTK L&F quality unacceptable for earlier versions.
// Also enable GTK L&F for OpenJDK
String javaVersion = System.getProperty("java.version");
if ("1.6.0_01".compareTo(javaVersion) > 0 && System.getProperty("java.vm.name") != null && System.getProperty("java.vm.name").indexOf("OpenJDK") < 0) {
// IDE runs on 1.5 or 1.6 - useGtk can enabled Gtk
if (uiClassName.indexOf("gtk") >= 0 && !Boolean.getBoolean("useGtk")) {
uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
}
} else {
// IDE runs on 1.6_0_01 or higher - useGtk can disabled Gtk
if (uiClassName.indexOf("gtk") >= 0 && System.getProperty("useGtk") != null && !Boolean.getBoolean("useGtk")) {
uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
}
}
// #118534 - don't allow Nimbus L&F as default system L&F,
// as we're not ready to support it yet
if (uiClassName.contains("Nimbus")) {
uiClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
}
}
return uiClassName;
}
private boolean installTheme(LookAndFeel lf) {
boolean themeInstalled = false;
//Load the theme
if (themeURL != null) {
themeInstalled = true;
NbTheme nbTheme = new NbTheme(themeURL, lf);
MetalLookAndFeel.setCurrentTheme(nbTheme);
}
return themeInstalled;
}
/** Enables, installs LF customization. */
private void install () {
if (installed) {
return;
}
if (globalCustoms == null) {
globalCustoms = new AllLFCustoms();
installLFCustoms (globalCustoms);
}
installPerLFDefaults();
installTheme(UIManager.getLookAndFeel());
runPostInstall();
attachListener();
}
private void installPerLFDefaults() {
boolean isLFChange = curCustoms != null;
curCustoms = findCustoms();
if (curCustoms != null) {
Integer in = (Integer) UIManager.get(LFCustoms.CUSTOM_FONT_SIZE); //NOI18N
if (in == null && UIManager.getLookAndFeel().getClass() == MetalLookAndFeel.class) {
in = new Integer (11);
}
//#161761: Do not want to use font size param for GTK L&F because it causes mixed font size
if ((in != null) && !UIUtils.isGtkLF()) {
AllLFCustoms.initCustomFontSize (in.intValue());
}
installLFCustoms (curCustoms);
if (isLFChange) {
//make sure UIBootstrapValue.Lazy instances really get a chance
//to replace their values
loadAllLazyValues (curCustoms);
}
curCustoms.disposeValues();
}
installSmoothScrollPane();
}
private void installSmoothScrollPane() {
UIDefaults defaults = UIManager.getDefaults();
final String SCROLL_PANE_UI_CLASS_ID = "ScrollPaneUI";
Object existingValue = defaults.get(SCROLL_PANE_UI_CLASS_ID);
if ("javax.swing.plaf.basic.BasicScrollPaneUI".equals(existingValue))
defaults.put(SCROLL_PANE_UI_CLASS_ID, "org.netbeans.swing.plaf.util.SmoothScrollPaneUI");
}
private void loadAllLazyValues (LFCustoms customs) {
if (globalCustoms != null) {
loadLazy (globalCustoms.getApplicationSpecificKeysAndValues());
loadLazy (globalCustoms.getGuaranteedKeysAndValues());
loadLazy (globalCustoms.getLookAndFeelCustomizationKeysAndValues());
}
loadLazy (customs.getApplicationSpecificKeysAndValues());
loadLazy (customs.getGuaranteedKeysAndValues());
loadLazy (customs.getLookAndFeelCustomizationKeysAndValues());
}
private void loadLazy (Object[] o) {
if (o.length > 0) {
UIDefaults uidefaults = UIManager.getDefaults();
for (int i=1; i < o.length; i+=2) {
if (o[i] instanceof UIBootstrapValue.Lazy) {
((UIBootstrapValue.Lazy) o[i]).createValue(uidefaults);
}
if (o[i] instanceof RelativeColor) {
((RelativeColor) o[i]).clear();
}
}
}
}
private void uninstallPerLFDefaults() {
assert globalCustoms != null;
if (curCustoms != null) {
Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy