8177919: java.awt.Desktop.setDefaultMenuBar​() should be specified to throw IllegalStateException

Reviewed-by: serb, prr
This commit is contained in:
Alexander Zvegintsev 2017-04-17 18:44:53 +03:00
parent 90161fc0b3
commit c43268c3cb
4 changed files with 20 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -25,9 +25,13 @@
package com.apple.eawt;
import java.awt.Container;
import java.awt.Frame;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenuBar;
import javax.swing.plaf.MenuBarUI;
import com.apple.laf.ScreenMenuBar;
@ -102,10 +106,15 @@ class _AppMenuBarHandler {
return;
}
final MenuBarUI ui = menuBar.getUI();
Container parent = menuBar.getParent();
if (parent instanceof JLayeredPane) {
((JLayeredPane) parent).remove(menuBar);
}
MenuBarUI ui = menuBar.getUI();
if (!(ui instanceof AquaMenuBarUI)) {
// Aqua was not installed
throw new IllegalStateException("Application.setDefaultMenuBar() only works with the Aqua Look and Feel");
ui = new AquaMenuBarUI();
menuBar.setUI(ui);
}
final AquaMenuBarUI aquaUI = (AquaMenuBarUI)ui;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 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
@ -50,7 +50,7 @@ public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvid
public void uninstallUI(final JComponent c) {
if (fScreenMenuBar != null) {
final JFrame frame = (JFrame)(c.getTopLevelAncestor());
if (frame.getMenuBar() == fScreenMenuBar) {
if (frame != null && frame.getMenuBar() == fScreenMenuBar) {
frame.setMenuBar((MenuBar)null);
}
fScreenMenuBar = null;

View File

@ -992,8 +992,6 @@ public class Desktop {
/**
* Sets the default menu bar to use when there are no active frames.
*
* @implNote Aqua Look and Feel should be active to support this on Mac OS.
*
* @param menuBar to use when no other frames are active
* @throws SecurityException if a security manager exists and it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
@ -541,8 +541,10 @@ public class JRootPane extends JComponent implements Accessible {
layeredPane.remove(menuBar);
menuBar = menu;
if(menuBar != null)
if(menuBar != null) {
menuBar.updateUI();
layeredPane.add(menuBar, JLayeredPane.FRAME_CONTENT_LAYER);
}
}
/**