下面列出了怎么用javax.swing.RootPaneContainer的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Installs a key listener into the application that allows enabling and disabling
* the UI inspector with the given keystroke (e.g. "ctrl shift alt X").
*/
public static void install( String activationKeys ) {
KeyStroke keyStroke = KeyStroke.getKeyStroke( activationKeys );
Toolkit.getDefaultToolkit().addAWTEventListener( e -> {
if( e.getID() == KeyEvent.KEY_RELEASED &&
((KeyEvent)e).getKeyCode() == keyStroke.getKeyCode() &&
(((KeyEvent)e).getModifiersEx() & KEY_MODIFIERS_MASK) == (keyStroke.getModifiers() & KEY_MODIFIERS_MASK) )
{
Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
if( activeWindow instanceof RootPaneContainer ) {
JRootPane rootPane = ((RootPaneContainer)activeWindow).getRootPane();
FlatInspector inspector = (FlatInspector) rootPane.getClientProperty( FlatInspector.class );
if( inspector == null ) {
inspector = new FlatInspector( rootPane );
rootPane.putClientProperty( FlatInspector.class, inspector );
inspector.setEnabled( true );
} else {
inspector.uninstall();
rootPane.putClientProperty( FlatInspector.class, null );
}
}
}
}, AWTEvent.KEY_EVENT_MASK );
}
/**
* Are we painting against a dark background? This checks the JVM version,
* the os, and whether the window's ultimate parent uses Apple's
* brush-metal-look.
*/
protected static boolean isDarkBackground(Window w) {
if (!isMac)
return false;
while (w != null) {
if (w instanceof RootPaneContainer) {
JRootPane rootPane = ((RootPaneContainer) w).getRootPane();
Object obj = rootPane
.getClientProperty("apple.awt.brushMetalLook");
if (obj == null)
obj = Boolean.FALSE;
if (obj.toString().equals("true")) {
return true;
}
}
w = w.getOwner();
}
return false;
}
/**
* {@inheritDoc}
*/
public boolean isInState(JComponent c) {
Component parent = c;
while (parent.getParent() != null) {
if (parent instanceof RootPaneContainer) {
break;
}
parent = parent.getParent();
}
if (parent instanceof JFrame) {
return (((JFrame) parent).getExtendedState() & Frame.MAXIMIZED_BOTH) != 0;
} else if (parent instanceof JInternalFrame) {
return ((JInternalFrame) parent).isMaximum();
}
return false;
}
public final void run() {
try {
exectute();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
afterComplete();
}
});
} catch(Exception err) {
err.printStackTrace();
} finally {
t.stop();
RootPaneContainer r = (RootPaneContainer)ResourceEditorApp.getApplication().getMainFrame();
r.setGlassPane(glassPane);
}
}
/** Stops to track given window (RootPaneContainer).
*/
public boolean detachWindow (RootPaneContainer rpc) {
logger.entering(getClass().getName(), "detachWindow");
if (!(rpc instanceof Window)) {
throw new IllegalArgumentException("Argument must be subclas of java.awt.Window: " + rpc); //NOI18N
}
WeakReference<RootPaneContainer> ww = getWeak(rpc);
if (ww == null) {
return false;
}
((Window)rpc).removeWindowListener(this);
return zOrder.remove(ww);
}
/**
* Get the glass pane of the main panel
* @return
*/
public Component getGlassPane() {
if(mainPanel == null) {
return null;
}
final RootPaneContainer root =
(RootPaneContainer) mainPanel.getTopLevelAncestor();
if(root == null) {
return null;
}
return root.getGlassPane();
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
@Test
@Ignore("Not implemented yet, see https://jira.springframework.org/browse/SPR-5708")
public void testExclusionOfNonPublicInterfaces() {
JFrame frame = new JFrame();
ProxyFactory proxyFactory = new ProxyFactory(frame);
Object proxy = proxyFactory.getProxy();
assertTrue(proxy instanceof RootPaneContainer);
assertTrue(proxy instanceof Accessible);
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
public void start() {
System.setProperty("jbre.popupwindow.settype", "true");
jFrame = new JFrame("Wrong popup z-order");
jFrame.setSize(200, 200);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(200, 200));
Popup popup = PopupFactory.getSharedInstance().getPopup(jFrame, jPanel, 100, 100);
windowAncestor = SwingUtilities.getWindowAncestor(jPanel);
((RootPaneContainer) windowAncestor).getRootPane().putClientProperty("SIMPLE_WINDOW", true);
windowAncestor.setFocusable(true);
windowAncestor.setFocusableWindowState(true);
windowAncestor.setAutoRequestFocus(true);
jFrame.setVisible(true);
popup.show();
modalBlocker = new JDialog(windowAncestor, "Modal Blocker");
modalBlocker.setModal(true);
modalBlocker.setSize(new Dimension(200, 200));
modalBlocker.setLocation(200, 200);
modalBlocker.addWindowListener(new JDialog741Listener());
modalBlocker.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
modalBlocker.setVisible(true);
}
public static Container getRootPaneContainer(JComponent component) {
Container parent = component;
Container lastParent;
do {
if (parent instanceof RootPaneContainer) {
return parent;
}
lastParent = parent;
parent = lastParent.getParent();
} while (parent != null);
return lastParent;
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
@Test
@Ignore("Not implemented yet, see http://jira.springframework.org/browse/SPR-5708")
public void testExclusionOfNonPublicInterfaces() {
JFrame frame = new JFrame();
ProxyFactory proxyFactory = new ProxyFactory(frame);
Object proxy = proxyFactory.getProxy();
assertTrue(proxy instanceof RootPaneContainer);
assertTrue(proxy instanceof Accessible);
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
static void handleFullScreenEventFromNative(final Window window, final int type) {
if (!(window instanceof RootPaneContainer)) return; // handles null
SunToolkit.executeOnEventHandlerThread(window, new Runnable() {
public void run() {
final FullScreenHandler handler = getHandlerFor((RootPaneContainer)window);
if (handler != null) handler.notifyListener(new FullScreenEvent(window), type);
}
});
}
public static void startWaitCursor(JComponent component)
{
RootPaneContainer root = ((RootPaneContainer) component.getTopLevelAncestor());
root.getGlassPane().setCursor(WAIT_CURSOR);
root.getGlassPane().addMouseListener(CLICK_CONSUMER);
root.getGlassPane().setVisible(true);
root.getRootPane().validate();
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
public void clear () {
RootPaneContainer rpc;
for (WeakReference<RootPaneContainer> elem : zOrder) {
rpc = elem.get();
if (rpc != null) {
((Window)rpc).removeWindowListener(this);
}
}
zOrder.clear();
}
@Test
@Ignore("Not implemented yet, see http://jira.springframework.org/browse/SPR-5708")
public void testExclusionOfNonPublicInterfaces() {
JFrame frame = new JFrame();
ProxyFactory proxyFactory = new ProxyFactory(frame);
Object proxy = proxyFactory.getProxy();
assertTrue(proxy instanceof RootPaneContainer);
assertTrue(proxy instanceof Accessible);
}
private WeakReference<RootPaneContainer> getWeak (RootPaneContainer rpc) {
for (WeakReference<RootPaneContainer> elem : zOrder) {
if (elem.get() == rpc) {
return elem;
}
}
return null;
}
private WeakReference<RootPaneContainer> getExcludedWeak (RootPaneContainer rpc) {
for (WeakReference<RootPaneContainer> elem : excludeSet) {
if (elem.get() == rpc) {
return elem;
}
}
return null;
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(() -> {
jFrame = new JFrame("Wrong popup z-order");
jFrame.setSize(200, 200);
JPanel jPanel = new JPanel();
jPanel.setPreferredSize(new Dimension(200, 200));
jPanel.setBackground(Color.BLACK);
Popup popup = PopupFactory.getSharedInstance().getPopup(jFrame, jPanel, 100, 100);
windowAncestor = SwingUtilities.getWindowAncestor(jPanel);
((RootPaneContainer) windowAncestor).getRootPane().putClientProperty("SIMPLE_WINDOW", true);
windowAncestor.setFocusable(true);
windowAncestor.setFocusableWindowState(true);
windowAncestor.setAutoRequestFocus(true);
jFrame.setVisible(true);
popup.show();
modalBlocker = new JDialog(windowAncestor, "Modal Blocker");
modalBlocker.setModal(true);
modalBlocker.setSize(new Dimension(200, 200));
modalBlocker.setLocation(200, 200);
modalBlocker.addWindowListener(new DialogListener());
modalBlocker.setVisible(true);
});
}
/**
* Configures the given window to be closed when the Escape button is pressed.
* @param <W> A window (JFrame, JDialog, etc.).
* @param window Window to configure.
*/
public static <W extends Window & RootPaneContainer> void enableCloseWindowWithEscape(final W window) {
Action closeAction = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
}
};
window.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
window.getRootPane().getActionMap().put("close", closeAction);
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
public static void stopWaitCursor(JComponent component)
{
RootPaneContainer root = ((RootPaneContainer) component.getTopLevelAncestor());
root.getGlassPane().setCursor(DEFAULT_CURSOR);
root.getGlassPane().removeMouseListener(CLICK_CONSUMER);
root.getGlassPane().setVisible(false);
root.getRootPane().validate();
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
public static void stopWaitCursor(JComponent component)
{
RootPaneContainer root = ((RootPaneContainer) component.getTopLevelAncestor());
root.getGlassPane().setCursor(DEFAULT_CURSOR);
root.getGlassPane().removeMouseListener(CLICK_CONSUMER);
root.getGlassPane().setVisible(false);
root.getRootPane().validate();
}
static void addFullScreenListenerTo(final RootPaneContainer window, final FullScreenListener listener) {
final Object value = window.getRootPane().getClientProperty(CLIENT_PROPERTY);
if (value instanceof FullScreenHandler) {
((FullScreenHandler)value).addListener(listener);
return;
}
if (value != null) return; // some other garbage is in our client property
final FullScreenHandler newHandler = new FullScreenHandler();
newHandler.addListener(listener);
window.getRootPane().putClientProperty(CLIENT_PROPERTY, newHandler);
}
static void handleFullScreenEventFromNative(final Window window, final int type) {
if (!(window instanceof RootPaneContainer)) return; // handles null
SunToolkit.executeOnEventHandlerThread(window, new Runnable() {
public void run() {
final FullScreenHandler handler = getHandlerFor((RootPaneContainer)window);
if (handler != null) handler.notifyListener(new FullScreenEvent(window), type);
}
});
}
/**
* Creates a new SeaGlassTitlePane object.
*
* @param rootPane the JRootPane containing the title pane.
* @param ui the UI delegate for the root pane.
*/
public SeaGlassTitlePane(JRootPane rootPane, SeaGlassRootPaneUI ui) {
this.rootPane = rootPane;
this.rootPaneUI = ui;
rootParent = (RootPaneContainer) rootPane.getParent();
installTitlePane();
}