0N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.metal;
0N/A
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.*;
0N/Aimport java.awt.*;
0N/A
0N/Aimport sun.awt.AppContext;
0N/Aimport sun.security.action.GetPropertyAction;
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/A/**
0N/A * A concrete implementation of {@code MetalTheme} providing
0N/A * the original look of the Java Look and Feel, code-named "Steel". Refer
0N/A * to {@link MetalLookAndFeel#setCurrentTheme} for details on changing
0N/A * the default theme.
0N/A * <p>
0N/A * All colors returned by {@code DefaultMetalTheme} are completely
0N/A * opaque.
0N/A *
0N/A * <h3><a name="fontStyle"></a>Font Style</h3>
0N/A *
0N/A * {@code DefaultMetalTheme} uses bold fonts for many controls. To make all
0N/A * controls (with the exception of the internal frame title bars and
0N/A * client decorated frame title bars) use plain fonts you can do either of
0N/A * the following:
0N/A * <ul>
0N/A * <li>Set the system property <code>swing.boldMetal</code> to
0N/A * <code>false</code>. For example,
0N/A * <code>java&nbsp;-Dswing.boldMetal=false&nbsp;MyApp</code>.
0N/A * <li>Set the defaults property <code>swing.boldMetal</code> to
0N/A * <code>Boolean.FALSE</code>. For example:
0N/A * <code>UIManager.put("swing.boldMetal",&nbsp;Boolean.FALSE);</code>
0N/A * </ul>
0N/A * The defaults property <code>swing.boldMetal</code>, if set,
0N/A * takes precendence over the system property of the same name. After
0N/A * setting this defaults property you need to re-install
0N/A * <code>MetalLookAndFeel</code>, as well as update the UI
0N/A * of any previously created widgets. Otherwise the results are undefined.
0N/A * The following illustrates how to do this:
0N/A * <pre>
0N/A * // turn off bold fonts
0N/A * UIManager.put("swing.boldMetal", Boolean.FALSE);
0N/A *
0N/A * // re-install the Metal Look and Feel
0N/A * UIManager.setLookAndFeel(new MetalLookAndFeel());
0N/A *
0N/A * // Update the ComponentUIs for all Components. This
0N/A * // needs to be invoked for all windows.
0N/A * SwingUtilities.updateComponentTreeUI(rootComponent);
0N/A * </pre>
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * @see MetalLookAndFeel
0N/A * @see MetalLookAndFeel#setCurrentTheme
0N/A *
0N/A * @author Steve Wilson
0N/A */
0N/Apublic class DefaultMetalTheme extends MetalTheme {
0N/A /**
0N/A * Whether or not fonts should be plain. This is only used if
0N/A * the defaults property 'swing.boldMetal' == "false".
0N/A */
0N/A private static final boolean PLAIN_FONTS;
0N/A
0N/A /**
0N/A * Names of the fonts to use.
0N/A */
0N/A private static final String[] fontNames = {
0N/A Font.DIALOG,Font.DIALOG,Font.DIALOG,Font.DIALOG,Font.DIALOG,Font.DIALOG
0N/A };
0N/A /**
0N/A * Styles for the fonts. This is ignored if the defaults property
0N/A * <code>swing.boldMetal</code> is false, or PLAIN_FONTS is true.
0N/A */
0N/A private static final int[] fontStyles = {
0N/A Font.BOLD, Font.PLAIN, Font.PLAIN, Font.BOLD, Font.BOLD, Font.PLAIN
0N/A };
0N/A /**
0N/A * Sizes for the fonts.
0N/A */
0N/A private static final int[] fontSizes = {
0N/A 12, 12, 12, 12, 12, 10
0N/A };
0N/A
0N/A // note the properties listed here can currently be used by people
0N/A // providing runtimes to hint what fonts are good. For example the bold
0N/A // dialog font looks bad on a Mac, so Apple could use this property to
0N/A // hint at a good font.
0N/A //
0N/A // However, we don't promise to support these forever. We may move
0N/A // to getting these from the swing.properties file, or elsewhere.
0N/A /**
0N/A * System property names used to look up fonts.
0N/A */
0N/A private static final String[] defaultNames = {
0N/A "swing.plaf.metal.controlFont",
0N/A "swing.plaf.metal.systemFont",
0N/A "swing.plaf.metal.userFont",
0N/A "swing.plaf.metal.controlFont",
0N/A "swing.plaf.metal.controlFont",
0N/A "swing.plaf.metal.smallFont"
0N/A };
0N/A
0N/A /**
0N/A * Returns the ideal font name for the font identified by key.
0N/A */
0N/A static String getDefaultFontName(int key) {
0N/A return fontNames[key];
0N/A }
0N/A
0N/A /**
0N/A * Returns the ideal font size for the font identified by key.
0N/A */
0N/A static int getDefaultFontSize(int key) {
0N/A return fontSizes[key];
0N/A }
0N/A
0N/A /**
0N/A * Returns the ideal font style for the font identified by key.
0N/A */
0N/A static int getDefaultFontStyle(int key) {
0N/A if (key != WINDOW_TITLE_FONT) {
0N/A Object boldMetal = null;
0N/A if (AppContext.getAppContext().get(
0N/A SwingUtilities2.LAF_STATE_KEY) != null) {
0N/A // Only access the boldMetal key if a look and feel has
0N/A // been loaded, otherwise we'll trigger loading the look
0N/A // and feel.
0N/A boldMetal = UIManager.get("swing.boldMetal");
0N/A }
0N/A if (boldMetal != null) {
0N/A if (Boolean.FALSE.equals(boldMetal)) {
0N/A return Font.PLAIN;
0N/A }
0N/A }
0N/A else if (PLAIN_FONTS) {
0N/A return Font.PLAIN;
0N/A }
0N/A }
0N/A return fontStyles[key];
0N/A }
0N/A
0N/A /**
0N/A * Returns the default used to look up the specified font.
0N/A */
0N/A static String getDefaultPropertyName(int key) {
0N/A return defaultNames[key];
0N/A }
0N/A
0N/A static {
0N/A Object boldProperty = java.security.AccessController.doPrivileged(
0N/A new GetPropertyAction("swing.boldMetal"));
0N/A if (boldProperty == null || !"false".equals(boldProperty)) {
0N/A PLAIN_FONTS = false;
0N/A }
0N/A else {
0N/A PLAIN_FONTS = true;
0N/A }
0N/A }
0N/A
0N/A private static final ColorUIResource primary1 = new ColorUIResource(
0N/A 102, 102, 153);
0N/A private static final ColorUIResource primary2 = new ColorUIResource(153,
0N/A 153, 204);
0N/A private static final ColorUIResource primary3 = new ColorUIResource(
0N/A 204, 204, 255);
0N/A private static final ColorUIResource secondary1 = new ColorUIResource(
0N/A 102, 102, 102);
0N/A private static final ColorUIResource secondary2 = new ColorUIResource(
0N/A 153, 153, 153);
0N/A private static final ColorUIResource secondary3 = new ColorUIResource(
0N/A 204, 204, 204);
0N/A
0N/A private FontDelegate fontDelegate;
0N/A
0N/A /**
0N/A * Returns the name of this theme. This returns {@code "Steel"}.
0N/A *
0N/A * @return the name of this theme.
0N/A */
0N/A public String getName() { return "Steel"; }
0N/A
0N/A /**
0N/A * Creates and returns an instance of {@code DefaultMetalTheme}.
0N/A */
0N/A public DefaultMetalTheme() {
0N/A install();
0N/A }
0N/A
0N/A /**
0N/A * Returns the primary 1 color. This returns a color with rgb values
0N/A * of 102, 102, and 153, respectively.
0N/A *
0N/A * @return the primary 1 color
0N/A */
0N/A protected ColorUIResource getPrimary1() { return primary1; }
0N/A
0N/A /**
0N/A * Returns the primary 2 color. This returns a color with rgb values
0N/A * of 153, 153, 204, respectively.
0N/A *
0N/A * @return the primary 2 color
0N/A */
0N/A protected ColorUIResource getPrimary2() { return primary2; }
0N/A
0N/A /**
0N/A * Returns the primary 3 color. This returns a color with rgb values
0N/A * 204, 204, 255, respectively.
0N/A *
0N/A * @return the primary 3 color
0N/A */
0N/A protected ColorUIResource getPrimary3() { return primary3; }
0N/A
0N/A /**
0N/A * Returns the secondary 1 color. This returns a color with rgb values
0N/A * 102, 102, and 102, respectively.
0N/A *
0N/A * @return the secondary 1 color
0N/A */
0N/A protected ColorUIResource getSecondary1() { return secondary1; }
0N/A
0N/A /**
0N/A * Returns the secondary 2 color. This returns a color with rgb values
0N/A * 153, 153, and 153, respectively.
0N/A *
0N/A * @return the secondary 2 color
0N/A */
0N/A protected ColorUIResource getSecondary2() { return secondary2; }
0N/A
0N/A /**
0N/A * Returns the secondary 3 color. This returns a color with rgb values
0N/A * 204, 204, and 204, respectively.
0N/A *
0N/A * @return the secondary 3 color
0N/A */
0N/A protected ColorUIResource getSecondary3() { return secondary3; }
0N/A
0N/A
0N/A /**
0N/A * Returns the control text font. This returns Dialog, 12pt. If
0N/A * plain fonts have been enabled as described in <a href="#fontStyle">
0N/A * font style</a>, the font style is plain. Otherwise the font style is
0N/A * bold.
0N/A *
0N/A * @return the control text font
0N/A */
0N/A public FontUIResource getControlTextFont() {
0N/A return getFont(CONTROL_TEXT_FONT);
0N/A }
0N/A
0N/A /**
0N/A * Returns the system text font. This returns Dialog, 12pt, plain.
0N/A *
0N/A * @return the sytem text font
0N/A */
0N/A public FontUIResource getSystemTextFont() {
0N/A return getFont(SYSTEM_TEXT_FONT);
0N/A }
0N/A
0N/A /**
0N/A * Returns the user text font. This returns Dialog, 12pt, plain.
0N/A *
0N/A * @return the user text font
0N/A */
0N/A public FontUIResource getUserTextFont() {
0N/A return getFont(USER_TEXT_FONT);
0N/A }
0N/A
0N/A /**
0N/A * Returns the menu text font. This returns Dialog, 12pt. If
0N/A * plain fonts have been enabled as described in <a href="#fontStyle">
0N/A * font style</a>, the font style is plain. Otherwise the font style is
0N/A * bold.
0N/A *
0N/A * @return the menu text font
0N/A */
0N/A public FontUIResource getMenuTextFont() {
0N/A return getFont(MENU_TEXT_FONT);
0N/A }
0N/A
0N/A /**
0N/A * Returns the window title font. This returns Dialog, 12pt, bold.
0N/A *
0N/A * @return the window title font
0N/A */
0N/A public FontUIResource getWindowTitleFont() {
0N/A return getFont(WINDOW_TITLE_FONT);
0N/A }
0N/A
0N/A /**
0N/A * Returns the sub-text font. This returns Dialog, 10pt, plain.
0N/A *
0N/A * @return the sub-text font
0N/A */
0N/A public FontUIResource getSubTextFont() {
0N/A return getFont(SUB_TEXT_FONT);
0N/A }
0N/A
0N/A private FontUIResource getFont(int key) {
0N/A return fontDelegate.getFont(key);
0N/A }
0N/A
0N/A void install() {
0N/A if (MetalLookAndFeel.isWindows() &&
0N/A MetalLookAndFeel.useSystemFonts()) {
0N/A fontDelegate = new WindowsFontDelegate();
0N/A }
0N/A else {
0N/A fontDelegate = new FontDelegate();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this is a theme provided by the core platform.
0N/A */
0N/A boolean isSystemTheme() {
0N/A return (getClass() == DefaultMetalTheme.class);
0N/A }
0N/A
0N/A /**
0N/A * FontDelegates add an extra level of indirection to obtaining fonts.
0N/A */
0N/A private static class FontDelegate {
0N/A private static int[] defaultMapping = {
0N/A CONTROL_TEXT_FONT, SYSTEM_TEXT_FONT,
0N/A USER_TEXT_FONT, CONTROL_TEXT_FONT,
0N/A CONTROL_TEXT_FONT, SUB_TEXT_FONT
0N/A };
0N/A FontUIResource fonts[];
0N/A
0N/A // menu and window are mapped to controlFont
0N/A public FontDelegate() {
0N/A fonts = new FontUIResource[6];
0N/A }
0N/A
0N/A public FontUIResource getFont(int type) {
0N/A int mappedType = defaultMapping[type];
0N/A if (fonts[type] == null) {
0N/A Font f = getPrivilegedFont(mappedType);
0N/A
0N/A if (f == null) {
0N/A f = new Font(getDefaultFontName(type),
0N/A getDefaultFontStyle(type),
0N/A getDefaultFontSize(type));
0N/A }
0N/A fonts[type] = new FontUIResource(f);
0N/A }
0N/A return fonts[type];
0N/A }
0N/A
0N/A /**
0N/A * This is the same as invoking
0N/A * <code>Font.getFont(key)</code>, with the exception
0N/A * that it is wrapped inside a <code>doPrivileged</code> call.
0N/A */
0N/A protected Font getPrivilegedFont(final int key) {
614N/A return java.security.AccessController.doPrivileged(
614N/A new java.security.PrivilegedAction<Font>() {
614N/A public Font run() {
0N/A return Font.getFont(getDefaultPropertyName(key));
0N/A }
0N/A }
0N/A );
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The WindowsFontDelegate uses DesktopProperties to obtain fonts.
0N/A */
0N/A private static class WindowsFontDelegate extends FontDelegate {
0N/A private MetalFontDesktopProperty[] props;
0N/A private boolean[] checkedPriviledged;
0N/A
0N/A public WindowsFontDelegate() {
0N/A props = new MetalFontDesktopProperty[6];
0N/A checkedPriviledged = new boolean[6];
0N/A }
0N/A
0N/A public FontUIResource getFont(int type) {
0N/A if (fonts[type] != null) {
0N/A return fonts[type];
0N/A }
0N/A if (!checkedPriviledged[type]) {
0N/A Font f = getPrivilegedFont(type);
0N/A
0N/A checkedPriviledged[type] = true;
0N/A if (f != null) {
0N/A fonts[type] = new FontUIResource(f);
0N/A return fonts[type];
0N/A }
0N/A }
0N/A if (props[type] == null) {
0N/A props[type] = new MetalFontDesktopProperty(type);
0N/A }
0N/A // While passing null may seem bad, we don't actually use
0N/A // the table and looking it up is rather expensive.
0N/A return (FontUIResource)props[type].createValue(null);
0N/A }
0N/A }
0N/A}