8376627: Remove AppContext from javax/swing/plaf/metal classes

Reviewed-by: serb, psadhukhan
This commit is contained in:
Phil Race 2026-02-09 17:26:32 +00:00
parent 36758b0839
commit 3065aa48c9
9 changed files with 24 additions and 436 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -30,7 +30,6 @@ import java.awt.image.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import sun.awt.AppContext;
/**
* Implements the bumps used throughout the Metal Look and Feel.
@ -50,7 +49,7 @@ class MetalBumps implements Icon {
protected Color shadowColor;
protected Color backColor;
private static final Object METAL_BUMPS = new Object();
private static final List<BumpBuffer> bumpsList = new ArrayList<BumpBuffer>();
protected BumpBuffer buffer;
/**
@ -66,20 +65,14 @@ class MetalBumps implements Icon {
private static BumpBuffer createBuffer(GraphicsConfiguration gc,
Color topColor, Color shadowColor, Color backColor) {
AppContext context = AppContext.getAppContext();
@SuppressWarnings("unchecked")
List<BumpBuffer> buffers = (List<BumpBuffer>) context.get(METAL_BUMPS);
if (buffers == null) {
buffers = new ArrayList<BumpBuffer>();
context.put(METAL_BUMPS, buffers);
}
for (BumpBuffer buffer : buffers) {
for (BumpBuffer buffer : bumpsList) {
if (buffer.hasSameConfiguration(gc, topColor, shadowColor, backColor)) {
return buffer;
}
}
BumpBuffer buffer = new BumpBuffer(gc, topColor, shadowColor, backColor);
buffers.add(buffer);
bumpsList.add(buffer);
return buffer;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -26,7 +26,6 @@
package javax.swing.plaf.metal;
import sun.swing.SwingUtilities2;
import sun.awt.AppContext;
import javax.swing.*;
import javax.swing.border.*;
@ -69,7 +68,7 @@ public class MetalButtonUI extends BasicButtonUI {
*/
protected Color disabledTextColor;
private static final Object METAL_BUTTON_UI_KEY = new Object();
private static final ComponentUI UI = new MetalButtonUI();
// ********************************
// Create PLAF
@ -87,14 +86,7 @@ public class MetalButtonUI extends BasicButtonUI {
* @return an instance of {@code MetalButtonUI}
*/
public static ComponentUI createUI(JComponent c) {
AppContext appContext = AppContext.getAppContext();
MetalButtonUI metalButtonUI =
(MetalButtonUI) appContext.get(METAL_BUTTON_UI_KEY);
if (metalButtonUI == null) {
metalButtonUI = new MetalButtonUI();
appContext.put(METAL_BUTTON_UI_KEY, metalButtonUI);
}
return metalButtonUI;
return UI;
}
// ********************************

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -25,8 +25,6 @@
package javax.swing.plaf.metal;
import sun.awt.AppContext;
import javax.swing.*;
import javax.swing.plaf.basic.BasicCheckBoxUI;
@ -57,7 +55,7 @@ public class MetalCheckBoxUI extends MetalRadioButtonUI {
// of BasicCheckBoxUI because we want to pick up all the
// painting changes made in MetalRadioButtonUI.
private static final Object METAL_CHECK_BOX_UI_KEY = new Object();
private static final ComponentUI UI = new MetalCheckBoxUI();
private static final String propertyPrefix = "CheckBox" + ".";
@ -79,14 +77,7 @@ public class MetalCheckBoxUI extends MetalRadioButtonUI {
* @return a new instance of {@code MetalCheckBoxUI}
*/
public static ComponentUI createUI(JComponent b) {
AppContext appContext = AppContext.getAppContext();
MetalCheckBoxUI checkboxUI =
(MetalCheckBoxUI) appContext.get(METAL_CHECK_BOX_UI_KEY);
if (checkboxUI == null) {
checkboxUI = new MetalCheckBoxUI();
appContext.put(METAL_CHECK_BOX_UI_KEY, checkboxUI);
}
return checkboxUI;
return UI;
}
public String getPropertyPrefix() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -61,7 +61,6 @@ import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicLookAndFeel;
import javax.swing.text.DefaultEditorKit;
import sun.awt.AppContext;
import sun.awt.OSInfo;
import sun.awt.SunToolkit;
import sun.swing.DefaultLayoutStyle;
@ -1600,6 +1599,8 @@ public class MetalLookAndFeel extends BasicLookAndFeel
super.provideErrorFeedback(component);
}
private static MetalTheme currentMetalTheme;
/**
* Set the theme used by <code>MetalLookAndFeel</code>.
* <p>
@ -1629,7 +1630,7 @@ public class MetalLookAndFeel extends BasicLookAndFeel
if (theme == null) {
throw new NullPointerException("Can't have null theme");
}
AppContext.getAppContext().put( "currentMetalTheme", theme );
currentMetalTheme = theme;
}
/**
@ -1641,15 +1642,10 @@ public class MetalLookAndFeel extends BasicLookAndFeel
* @since 1.5
*/
public static MetalTheme getCurrentTheme() {
MetalTheme currentTheme;
AppContext context = AppContext.getAppContext();
currentTheme = (MetalTheme) context.get( "currentMetalTheme" );
MetalTheme currentTheme = currentMetalTheme;
if (currentTheme == null) {
// This will happen in two cases:
// . When MetalLookAndFeel is first being initialized.
// . When a new AppContext has been created that hasn't
// triggered UIManager to load a LAF. Rather than invoke
// a method on the UIManager, which would trigger the loading
// This will happen when MetalLookAndFeel is first being initialized.
// Rather than invoke a method on the UIManager, which would trigger the loading
// of a potentially different LAF, we directly set the
// Theme here.
if (useHighContrastTheme()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -26,7 +26,6 @@
package javax.swing.plaf.metal;
import sun.swing.SwingUtilities2;
import sun.awt.AppContext;
import java.awt.*;
import java.awt.event.*;
@ -55,7 +54,7 @@ import javax.swing.text.View;
*/
public class MetalRadioButtonUI extends BasicRadioButtonUI {
private static final Object METAL_RADIO_BUTTON_UI_KEY = new Object();
private static final ComponentUI UI = new MetalRadioButtonUI();
/**
* The color of the focused radio button.
@ -90,14 +89,7 @@ public class MetalRadioButtonUI extends BasicRadioButtonUI {
* @return an instance of {@code MetalRadioButtonUI}
*/
public static ComponentUI createUI(JComponent c) {
AppContext appContext = AppContext.getAppContext();
MetalRadioButtonUI metalRadioButtonUI =
(MetalRadioButtonUI) appContext.get(METAL_RADIO_BUTTON_UI_KEY);
if (metalRadioButtonUI == null) {
metalRadioButtonUI = new MetalRadioButtonUI();
appContext.put(METAL_RADIO_BUTTON_UI_KEY, metalRadioButtonUI);
}
return metalRadioButtonUI;
return UI;
}
// ********************************

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2026, 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
@ -26,7 +26,6 @@
package javax.swing.plaf.metal;
import sun.swing.SwingUtilities2;
import sun.awt.AppContext;
import java.awt.*;
import java.awt.event.*;
@ -57,7 +56,7 @@ import java.io.Serializable;
*/
public class MetalToggleButtonUI extends BasicToggleButtonUI {
private static final Object METAL_TOGGLE_BUTTON_UI_KEY = new Object();
private static final ComponentUI UI = new MetalToggleButtonUI();
/**
* The color of a focused toggle button.
@ -92,14 +91,7 @@ public class MetalToggleButtonUI extends BasicToggleButtonUI {
* @return the {@code MetalToggleButtonUI}.
*/
public static ComponentUI createUI(JComponent b) {
AppContext appContext = AppContext.getAppContext();
MetalToggleButtonUI metalToggleButtonUI =
(MetalToggleButtonUI) appContext.get(METAL_TOGGLE_BUTTON_UI_KEY);
if (metalToggleButtonUI == null) {
metalToggleButtonUI = new MetalToggleButtonUI();
appContext.put(METAL_TOGGLE_BUTTON_UI_KEY, metalToggleButtonUI);
}
return metalToggleButtonUI;
return UI;
}
// ********************************

View File

@ -1,240 +0,0 @@
/*
* Copyright (c) 2009, 2017, 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.
*
* 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.
*/
/*
* @test
* @bug 6657026
* @summary Tests shared MetalBumps in different application contexts
* @author Sergey Malenkov
* @modules java.desktop/sun.awt
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.text.AttributedCharacterIterator;
import javax.swing.JToolBar;
import javax.swing.plaf.metal.MetalBorders.ToolBarBorder;
import sun.awt.SunToolkit;
public class Test6657026 extends ToolBarBorder implements Runnable {
public static void main(String[] args) throws Exception {
new Test6657026().test();
ThreadGroup group = new ThreadGroup("$$$");
Thread thread = new Thread(group, new Test6657026());
thread.start();
thread.join();
}
public void run() {
SunToolkit.createNewAppContext();
test();
}
private void test() {
MyGraphics mg = new MyGraphics();
ToolBarBorder border = new ToolBarBorder();
border.paintBorder(mg.component, mg, 0, 0, 10, 10);
if (mg.image != null) {
boolean failed = true;
int value = mg.image.getRGB(0, 0);
for (int x = 0; x < mg.image.getWidth(); x++) {
for (int y = 0; y < mg.image.getHeight(); y++) {
int current = mg.image.getRGB(x, y);
if (current != value) {
mg.image.setRGB(x, y, value);
failed = false;
}
}
}
if (failed) {
throw new Error("shared metal bumps");
}
}
}
private static class MyGraphics extends Graphics {
private final Component component = new JToolBar() {};
private BufferedImage image;
public Graphics create() {
return null; // TODO: check
}
public void translate(int x, int y) {
// TODO: check
}
public Color getColor() {
return null; // TODO: check
}
public void setColor(Color color) {
// TODO: check
}
public void setPaintMode() {
// TODO: check
}
public void setXORMode(Color c1) {
// TODO: check
}
public Font getFont() {
return null; // TODO: check
}
public void setFont(Font font) {
// TODO: check
}
public FontMetrics getFontMetrics(Font font) {
return null; // TODO: check
}
public Rectangle getClipBounds() {
return null; // TODO: check
}
public void clipRect(int x, int y, int width, int height) {
// TODO: check
}
public void setClip(int x, int y, int width, int height) {
// TODO: check
}
public Shape getClip() {
return null; // TODO: check
}
public void setClip(Shape clip) {
// TODO: check
}
public void copyArea(int x, int y, int width, int height, int dx, int dy) {
// TODO: check
}
public void drawLine(int x1, int y1, int x2, int y2) {
// TODO: check
}
public void fillRect(int x, int y, int width, int height) {
// TODO: check
}
public void clearRect(int x, int y, int width, int height) {
// TODO: check
}
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
// TODO: check
}
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
// TODO: check
}
public void drawOval(int x, int y, int width, int height) {
// TODO: check
}
public void fillOval(int x, int y, int width, int height) {
// TODO: check
}
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
// TODO: check
}
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
// TODO: check
}
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
// TODO: check
}
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
// TODO: check
}
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
// TODO: check
}
public void drawString(String str, int x, int y) {
// TODO: check
}
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
// TODO: check
}
public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
return false; // TODO: check
}
public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
return false; // TODO: check
}
public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
return false; // TODO: check
}
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
return false; // TODO: check
}
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
if (img instanceof BufferedImage) {
this.image = (BufferedImage) img;
}
return false; // TODO: check
}
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
return false; // TODO: check
}
public void dispose() {
// TODO: check
}
}
}

View File

@ -1,61 +0,0 @@
/*
* Copyright (c) 2009, 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 6657026
* @summary Tests shared MetalInternalFrameUI in different application contexts
* @author Sergey Malenkov
* @modules java.desktop/sun.awt
*/
import sun.awt.SunToolkit;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalInternalFrameUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class Test6657026 extends MetalInternalFrameUI implements Runnable {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new MetalLookAndFeel());
ThreadGroup group = new ThreadGroup("$$$");
Thread thread = new Thread(group, new Test6657026());
thread.start();
thread.join();
new JInternalFrame().setContentPane(new JPanel());
}
public Test6657026() {
super(null);
}
public void run() {
SunToolkit.createNewAppContext();
IS_PALETTE = JInternalFrame.CONTENT_PANE_PROPERTY;
}
}

View File

@ -1,67 +0,0 @@
/*
* Copyright (c) 2009, 2015, 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.
*
* 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.
*/
/*
* @test
* @bug 6657026 7077259
* @summary Tests shared MetalSliderUI in different application contexts
* @author Sergey Malenkov
* @modules java.desktop/sun.awt
* @run main/othervm -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel Test6657026
*/
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalSliderUI;
import sun.awt.SunToolkit;
public class Test6657026 extends MetalSliderUI implements Runnable {
public static void main(String[] args) throws Exception {
JSlider slider = new JSlider();
test(slider);
ThreadGroup group = new ThreadGroup("$$$");
Thread thread = new Thread(group, new Test6657026());
thread.start();
thread.join();
test(slider);
}
public void run() {
SunToolkit.createNewAppContext();
JSlider slider = new JSlider();
test(slider);
tickLength = -10000;
}
private static void test(JSlider slider) {
MetalSliderUI ui = (MetalSliderUI) slider.getUI();
int actual = ui.getTickLength();
if (actual != 11) {
throw new Error(actual + ", but expected 11");
}
}
}