提交 efbded95 编写于 作者: M mcherkas

8164143: Improve components for menu items

Reviewed-by: ssadetsky, prr, ddehaven
上级 6729a639
...@@ -121,7 +121,7 @@ class _AppMenuBarHandler { ...@@ -121,7 +121,7 @@ class _AppMenuBarHandler {
} }
// grab the pointer to the CMenuBar, and retain it in native // grab the pointer to the CMenuBar, and retain it in native
nativeSetDefaultMenuBar(((CMenuBar)peer).getModel()); ((CMenuBar) peer).execute(_AppMenuBarHandler::nativeSetDefaultMenuBar);
} }
void setAboutMenuItemVisible(final boolean present) { void setAboutMenuItemVisible(final boolean present) {
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -26,29 +26,28 @@ ...@@ -26,29 +26,28 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import java.awt.CheckboxMenuItem; import java.awt.CheckboxMenuItem;
import java.awt.EventQueue;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.peer.CheckboxMenuItemPeer; import java.awt.peer.CheckboxMenuItemPeer;
import sun.awt.SunToolkit; import sun.awt.SunToolkit;
public class CCheckboxMenuItem extends CMenuItem implements CheckboxMenuItemPeer { public class CCheckboxMenuItem extends CMenuItem implements CheckboxMenuItemPeer {
boolean fAutoToggle = true; volatile boolean fAutoToggle = true;
boolean fIsIndeterminate = false; volatile boolean fIsIndeterminate = false;
private native void nativeSetState(long modelPtr, boolean state); private native void nativeSetState(long modelPtr, boolean state);
private native void nativeSetIsCheckbox(long modelPtr); private native void nativeSetIsCheckbox(long modelPtr);
CCheckboxMenuItem(CheckboxMenuItem target) { CCheckboxMenuItem(final CheckboxMenuItem target) {
super(target); super(target);
nativeSetIsCheckbox(getModel()); execute(this::nativeSetIsCheckbox);
setState(target.getState()); setState(target.getState());
} }
// MenuItemPeer implementation // MenuItemPeer implementation
@Override @Override
public void setState(boolean state) { public void setState(final boolean state) {
nativeSetState(getModel(), state); execute(ptr -> nativeSetState(ptr, state));
} }
public void handleAction(final boolean state) { public void handleAction(final boolean state) {
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
* questions. * questions.
*/ */
package sun.lwawt.macosx; package sun.lwawt.macosx;
/** /**
...@@ -34,6 +33,7 @@ public class CFRetainedResource { ...@@ -34,6 +33,7 @@ public class CFRetainedResource {
private static native void nativeCFRelease(final long ptr, final boolean disposeOnAppKitThread); private static native void nativeCFRelease(final long ptr, final boolean disposeOnAppKitThread);
private final boolean disposeOnAppKitThread; private final boolean disposeOnAppKitThread;
// TODO this pointer should be private and accessed via CFNativeAction class
protected volatile long ptr; protected volatile long ptr;
/** /**
...@@ -70,8 +70,72 @@ public class CFRetainedResource { ...@@ -70,8 +70,72 @@ public class CFRetainedResource {
nativeCFRelease(oldPtr, disposeOnAppKitThread); // perform outside of the synchronized block nativeCFRelease(oldPtr, disposeOnAppKitThread); // perform outside of the synchronized block
} }
/**
* The interface which allows to execute some native operations with
* assumption that the native pointer will be valid till the end.
*/
public interface CFNativeAction {
/**
* The native operation should be called from this method.
*
* @param ptr the pointer to the native data
*/
void run(long ptr);
}
/**
* The interface which allows to execute some native operations and get a
* result with assumption that the native pointer will be valid till the
* end.
*/
interface CFNativeActionGet {
/**
* The native operation should be called from this method.
*
* @param ptr the pointer to the native data
* @return result of the native operation
*/
long run(long ptr);
}
/**
* This is utility method which should be used instead of the direct access
* to the {@link #ptr}, because this method guaranteed that the pointer will
* not be zero and will be valid till the end of the operation.It is highly
* recomended to not use any external lock in action. If the current
* {@link #ptr} is {@code 0} then action will be ignored.
*
* @param action The native operation
*/
public final synchronized void execute(final CFNativeAction action) {
if (ptr != 0) {
action.run(ptr);
}
}
/**
* This is utility method which should be used instead of the direct access
* to the {@link #ptr}, because this method guaranteed that the pointer will
* not be zero and will be valid till the end of the operation. It is highly
* recomended to not use any external lock in action. If the current
* {@link #ptr} is {@code 0} then action will be ignored and {@code} is
* returned.
*
* @param action the native operation
* @return result of the native operation, usually the native pointer to
* some other data
*/
final synchronized long executeGet(final CFNativeActionGet action) {
if (ptr != 0) {
return action.run(ptr);
}
return 0;
}
@Override @Override
protected void finalize() throws Throwable { protected final void finalize() throws Throwable {
dispose(); dispose();
} }
} }
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,7 +25,9 @@ ...@@ -25,7 +25,9 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import java.awt.*; import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.peer.MenuItemPeer; import java.awt.peer.MenuItemPeer;
import java.awt.peer.MenuPeer; import java.awt.peer.MenuPeer;
...@@ -37,7 +39,7 @@ public class CMenu extends CMenuItem implements MenuPeer { ...@@ -37,7 +39,7 @@ public class CMenu extends CMenuItem implements MenuPeer {
// This way we avoiding invocation of the setters twice // This way we avoiding invocation of the setters twice
@Override @Override
protected void initialize(MenuItem target) { protected final void initialize(MenuItem target) {
setLabel(target.getLabel()); setLabel(target.getLabel());
setEnabled(target.isEnabled()); setEnabled(target.isEnabled());
} }
...@@ -57,52 +59,50 @@ public class CMenu extends CMenuItem implements MenuPeer { ...@@ -57,52 +59,50 @@ public class CMenu extends CMenuItem implements MenuPeer {
} }
@Override @Override
protected long createModel() { long createModel() {
CMenuComponent parent = (CMenuComponent) CMenuComponent parent = (CMenuComponent)
LWCToolkit.targetToPeer(getTarget().getParent()); LWCToolkit.targetToPeer(getTarget().getParent());
if (parent instanceof CMenu || if (parent instanceof CMenu) {
parent instanceof CPopupMenu) return parent.executeGet(this::nativeCreateSubMenu);
{ }
return nativeCreateSubMenu(parent.getModel()); if (parent instanceof CMenuBar) {
} else if (parent instanceof CMenuBar) {
MenuBar parentContainer = (MenuBar)getTarget().getParent(); MenuBar parentContainer = (MenuBar)getTarget().getParent();
boolean isHelpMenu = parentContainer.getHelpMenu() == getTarget(); boolean isHelpMenu = parentContainer.getHelpMenu() == getTarget();
int insertionLocation = ((CMenuBar)parent).getNextInsertionIndex(); int insertionLocation = ((CMenuBar)parent).getNextInsertionIndex();
return nativeCreateMenu(parent.getModel(), return parent.executeGet(ptr -> nativeCreateMenu(ptr, isHelpMenu,
isHelpMenu, insertionLocation); insertionLocation));
} else {
throw new InternalError("Parent must be CMenu or CMenuBar");
} }
throw new InternalError("Parent must be CMenu or CMenuBar");
} }
@Override @Override
public void addItem(MenuItem item) { public final void addItem(MenuItem item) {
// Nothing to do here -- we added it when we created the // Nothing to do here -- we added it when we created the
// menu item's peer. // menu item's peer.
} }
@Override @Override
public void delItem(int index) { public final void delItem(final int index) {
nativeDeleteItem(getModel(), index); execute(ptr -> nativeDeleteItem(ptr, index));
} }
@Override @Override
public void setLabel(String label) { public final void setLabel(final String label) {
nativeSetMenuTitle(getModel(), label); execute(ptr->nativeSetMenuTitle(ptr, label));
super.setLabel(label); super.setLabel(label);
} }
// Note that addSeparator is never called directly from java.awt.Menu, // Note that addSeparator is never called directly from java.awt.Menu,
// though it is required in the MenuPeer interface. // though it is required in the MenuPeer interface.
@Override @Override
public void addSeparator() { public final void addSeparator() {
nativeAddSeparator(getModel()); execute(this::nativeAddSeparator);
} }
// Used by ScreenMenuBar to get to the native menu for event handling. // Used by ScreenMenuBar to get to the native menu for event handling.
public long getNativeMenu() { public final long getNativeMenu() {
return nativeGetNSMenu(getModel()); return executeGet(this::nativeGetNSMenu);
} }
private native long nativeCreateMenu(long parentMenuPtr, private native long nativeCreateMenu(long parentMenuPtr,
......
/* /*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,7 +29,9 @@ import java.awt.Menu; ...@@ -29,7 +29,9 @@ import java.awt.Menu;
import java.awt.MenuBar; import java.awt.MenuBar;
import java.awt.peer.MenuBarPeer; import java.awt.peer.MenuBarPeer;
public class CMenuBar extends CMenuComponent implements MenuBarPeer { import sun.awt.AWTAccessor;
public final class CMenuBar extends CMenuComponent implements MenuBarPeer {
private int nextInsertionIndex = -1; private int nextInsertionIndex = -1;
...@@ -38,15 +40,16 @@ public class CMenuBar extends CMenuComponent implements MenuBarPeer { ...@@ -38,15 +40,16 @@ public class CMenuBar extends CMenuComponent implements MenuBarPeer {
} }
@Override @Override
protected long createModel() { long createModel() {
return nativeCreateMenuBar(); return nativeCreateMenuBar();
} }
@Override @Override
public void addHelpMenu(Menu m) { public void addHelpMenu(final Menu m) {
CMenu cMenu = (CMenu)m.getPeer(); final CMenu cMenu = AWTAccessor.getMenuComponentAccessor().getPeer(m);
nativeSetHelpMenu(getModel(), cMenu.getModel()); execute(parentPtr -> cMenu.execute(
} menuPtr -> nativeSetHelpMenu(parentPtr, menuPtr)));
}
public int getNextInsertionIndex() { public int getNextInsertionIndex() {
return nextInsertionIndex; return nextInsertionIndex;
...@@ -63,8 +66,8 @@ public class CMenuBar extends CMenuComponent implements MenuBarPeer { ...@@ -63,8 +66,8 @@ public class CMenuBar extends CMenuComponent implements MenuBarPeer {
} }
@Override @Override
public void delMenu(int index) { public void delMenu(final int index) {
nativeDelMenu(getModel(), index); execute(ptr -> nativeDelMenu(ptr, index));
} }
private native long nativeCreateMenuBar(); private native long nativeCreateMenuBar();
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -29,36 +29,32 @@ import java.awt.Font; ...@@ -29,36 +29,32 @@ import java.awt.Font;
import java.awt.MenuComponent; import java.awt.MenuComponent;
import java.awt.peer.MenuComponentPeer; import java.awt.peer.MenuComponentPeer;
public abstract class CMenuComponent implements MenuComponentPeer { abstract class CMenuComponent extends CFRetainedResource
implements MenuComponentPeer {
private MenuComponent target; private final MenuComponent target;
private long modelPtr;
CMenuComponent(MenuComponent target) { CMenuComponent(final MenuComponent target) {
super(0, true);
this.target = target; this.target = target;
this.modelPtr = createModel(); setPtr(createModel());
} }
MenuComponent getTarget() { final MenuComponent getTarget() {
return target; return target;
} }
public long getModel() { abstract long createModel();
return modelPtr;
}
protected abstract long createModel();
public void dispose() { @Override
public final void dispose() {
super.dispose();
LWCToolkit.targetDisposedPeer(target, this); LWCToolkit.targetDisposedPeer(target, this);
nativeDispose(modelPtr);
target = null;
} }
private native void nativeDispose(long modelPtr);
// 1.5 peer method // 1.5 peer method
public void setFont(Font f) { @Override
public final void setFont(final Font f) {
// no-op, as we don't currently support menu fonts // no-op, as we don't currently support menu fonts
// c.f. radar 4032912 // c.f. radar 4032912
} }
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,16 +25,17 @@ ...@@ -25,16 +25,17 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import sun.awt.SunToolkit;
import sun.lwawt.LWToolkit;
import java.awt.MenuContainer;
import java.awt.MenuItem; import java.awt.MenuItem;
import java.awt.MenuShortcut; import java.awt.MenuShortcut;
import java.awt.event.*; import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.peer.MenuItemPeer; import java.awt.peer.MenuItemPeer;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import sun.awt.SunToolkit;
import sun.lwawt.LWToolkit;
public class CMenuItem extends CMenuComponent implements MenuItemPeer { public class CMenuItem extends CMenuComponent implements MenuItemPeer {
private final AtomicBoolean enabled = new AtomicBoolean(true); private final AtomicBoolean enabled = new AtomicBoolean(true);
...@@ -58,9 +59,9 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer { ...@@ -58,9 +59,9 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer {
} }
@Override @Override
protected long createModel() { long createModel() {
CMenuComponent parent = (CMenuComponent)LWToolkit.targetToPeer(getTarget().getParent()); CMenuComponent parent = (CMenuComponent)LWToolkit.targetToPeer(getTarget().getParent());
return nativeCreate(parent.getModel(), isSeparator()); return parent.executeGet(ptr->nativeCreate(ptr, isSeparator()));
} }
public void setLabel(String label, char keyChar, int keyCode, int modifiers) { public void setLabel(String label, char keyChar, int keyCode, int modifiers) {
...@@ -90,7 +91,12 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer { ...@@ -90,7 +91,12 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer {
keyChar = 0; keyChar = 0;
} }
nativeSetLabel(getModel(), label, keyChar, keyCode, keyMask); final String finalLabel = label;
final char finalKeyChar = keyChar;
final int finalKeyCode = keyCode;
final int finalKeyMask = keyMask;
execute(ptr -> nativeSetLabel(ptr, finalLabel, finalKeyChar,
finalKeyCode, finalKeyMask));
} }
@Override @Override
...@@ -105,16 +111,16 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer { ...@@ -105,16 +111,16 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer {
* There isn't a need to expose this except in a instanceof because * There isn't a need to expose this except in a instanceof because
* it isn't defined in the peer api. * it isn't defined in the peer api.
*/ */
public void setImage(java.awt.Image img) { public final void setImage(final java.awt.Image img) {
CImage cimg = CImage.getCreator().createFromImage(img); CImage cimg = CImage.getCreator().createFromImage(img);
nativeSetImage(getModel(), cimg == null ? 0L : cimg.ptr); execute(ptr -> nativeSetImage(ptr, cimg == null ? 0L : cimg.ptr));
} }
/** /**
* New API for tooltips * New API for tooltips
*/ */
public void setToolTipText(String text) { public final void setToolTipText(final String text) {
nativeSetTooltip(getModel(), text); execute(ptr -> nativeSetTooltip(ptr, text));
} }
// @Override // @Override
...@@ -138,7 +144,8 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer { ...@@ -138,7 +144,8 @@ public class CMenuItem extends CMenuComponent implements MenuItemPeer {
b &= ((CMenuItem) parent).isEnabled(); b &= ((CMenuItem) parent).isEnabled();
} }
if (enabled.compareAndSet(!b, b)) { if (enabled.compareAndSet(!b, b)) {
nativeSetEnabled(getModel(), b); final boolean finalB = b;
execute(ptr->nativeSetEnabled(ptr, finalB));
} }
} }
......
...@@ -419,7 +419,7 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo ...@@ -419,7 +419,7 @@ public class CPlatformWindow extends CFRetainedResource implements PlatformWindo
final long nsWindowPtr = getNSWindowPtr(); final long nsWindowPtr = getNSWindowPtr();
CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb); CMenuBar mbPeer = (CMenuBar)LWToolkit.targetToPeer(mb);
if (mbPeer != null) { if (mbPeer != null) {
nativeSetNSWindowMenuBar(nsWindowPtr, mbPeer.getModel()); mbPeer.execute(ptr -> nativeSetNSWindowMenuBar(nsWindowPtr, ptr));
} else { } else {
nativeSetNSWindowMenuBar(nsWindowPtr, 0); nativeSetNSWindowMenuBar(nsWindowPtr, 0);
} }
......
/* /*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -25,18 +25,20 @@ ...@@ -25,18 +25,20 @@
package sun.lwawt.macosx; package sun.lwawt.macosx;
import java.awt.*; import java.awt.Component;
import java.awt.Event;
import java.awt.Point;
import java.awt.PopupMenu;
import java.awt.peer.PopupMenuPeer; import java.awt.peer.PopupMenuPeer;
import sun.lwawt.LWWindowPeer; final class CPopupMenu extends CMenu implements PopupMenuPeer {
public class CPopupMenu extends CMenu implements PopupMenuPeer {
CPopupMenu(PopupMenu target) { CPopupMenu(PopupMenu target) {
super(target); super(target);
} }
@Override @Override
protected long createModel() { long createModel() {
return nativeCreatePopupMenu(); return nativeCreatePopupMenu();
} }
...@@ -50,7 +52,7 @@ public class CPopupMenu extends CMenu implements PopupMenuPeer { ...@@ -50,7 +52,7 @@ public class CPopupMenu extends CMenu implements PopupMenuPeer {
Point loc = origin.getLocationOnScreen(); Point loc = origin.getLocationOnScreen();
e.x += loc.x; e.x += loc.x;
e.y += loc.y; e.y += loc.y;
nativeShowPopupMenu(getModel(), e.x, e.y); execute(ptr -> nativeShowPopupMenu(ptr, e.x, e.y));
} }
} }
} }
/* /*
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -104,7 +104,10 @@ public class CTrayIcon extends CFRetainedResource implements TrayIconPeer { ...@@ -104,7 +104,10 @@ public class CTrayIcon extends CFRetainedResource implements TrayIconPeer {
return 0L; return 0L;
} }
} }
return checkAndCreatePopupPeer().getModel(); // This method is executed on Appkit, so if ptr is not zero means that,
// it is still not deallocated(even if we call NSApp postRunnableEvent)
// and sent CFRelease to the native queue
return checkAndCreatePopupPeer().ptr;
} }
/** /**
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
- (id)initWithPeer:(jobject)peer { - (id)initWithPeer:(jobject)peer {
AWT_ASSERT_APPKIT_THREAD; AWT_ASSERT_APPKIT_THREAD;
// Create the new NSMenu // Create the new NSMenu
self = [super initWithPeer:peer asSeparator:[NSNumber numberWithBool:NO]]; self = [super initWithPeer:peer asSeparator:NO];
if (self) { if (self) {
fMenu = [NSMenu javaMenuWithTitle:@""]; fMenu = [NSMenu javaMenuWithTitle:@""];
[fMenu retain]; [fMenu retain];
...@@ -134,14 +134,13 @@ AWT_ASSERT_APPKIT_THREAD; ...@@ -134,14 +134,13 @@ AWT_ASSERT_APPKIT_THREAD;
CMenu * createCMenu (jobject cPeerObjGlobal) { CMenu * createCMenu (jobject cPeerObjGlobal) {
CMenu *aCMenu = nil; __block CMenu *aCMenu = nil;
// We use an array here only to be able to get a return value
NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithBytes:&cPeerObjGlobal objCType:@encode(jobject)], nil];
[ThreadUtilities performOnMainThread:@selector(_create_OnAppKitThread:) on:[CMenu alloc] withObject:args waitUntilDone:YES]; [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
aCMenu = (CMenu *)[args objectAtIndex: 0]; aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];
// the aCMenu is released in CMenuComponent.dispose()
}];
if (aCMenu == nil) { if (aCMenu == nil) {
return 0L; return 0L;
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -383,27 +383,20 @@ JNIEXPORT jlong JNICALL ...@@ -383,27 +383,20 @@ JNIEXPORT jlong JNICALL
Java_sun_lwawt_macosx_CMenuBar_nativeCreateMenuBar Java_sun_lwawt_macosx_CMenuBar_nativeCreateMenuBar
(JNIEnv *env, jobject peer) (JNIEnv *env, jobject peer)
{ {
CMenuBar *aCMenuBar = nil; __block CMenuBar *aCMenuBar = nil;
JNF_COCOA_ENTER(env); JNF_COCOA_ENTER(env);
jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer); jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
// We use an array here only to be able to get a return value [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
NSMutableArray *args = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithBytes:&cPeerObjGlobal objCType:@encode(jobject)], nil];
[ThreadUtilities performOnMainThread:@selector(_create_OnAppKitThread:) on:[CMenuBar alloc] withObject:args waitUntilDone:YES];
aCMenuBar = (CMenuBar *)[args objectAtIndex: 0];
aCMenuBar = [[CMenuBar alloc] initWithPeer:cPeerObjGlobal];
// the aCMenuBar is released in CMenuComponent.dispose()
}];
if (aCMenuBar == nil) { if (aCMenuBar == nil) {
return 0L; return 0L;
} }
// [args release];
// A strange memory managment after that.
JNF_COCOA_EXIT(env); JNF_COCOA_EXIT(env);
if (aCMenuBar) { if (aCMenuBar) {
CFRetain(aCMenuBar); // GC CFRetain(aCMenuBar); // GC
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
......
/* /*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
} }
// Setup // Setup
- (id) initWithPeer:(jobject)peer asSeparator: (NSNumber *) asSeparator; - (id) initWithPeer:(jobject)peer asSeparator: (BOOL) asSeparator;
- (void) setIsCheckbox; - (void) setIsCheckbox;
// Events // Events
......
/* /*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -39,11 +39,11 @@ ...@@ -39,11 +39,11 @@
@implementation CMenuItem @implementation CMenuItem
- (id) initWithPeer:(jobject)peer asSeparator: (NSNumber *) asSeparator{ - (id) initWithPeer:(jobject)peer asSeparator: (BOOL) asSeparator{
AWT_ASSERT_APPKIT_THREAD; AWT_ASSERT_APPKIT_THREAD;
self = [super initWithPeer:peer]; self = [super initWithPeer:peer];
if (self) { if (self) {
if ([asSeparator boolValue]) { if (asSeparator) {
fMenuItem = (NSMenuItem*)[NSMenuItem separatorItem]; fMenuItem = (NSMenuItem*)[NSMenuItem separatorItem];
[fMenuItem retain]; [fMenuItem retain];
} else { } else {
...@@ -199,12 +199,9 @@ JNF_COCOA_EXIT(env); ...@@ -199,12 +199,9 @@ JNF_COCOA_EXIT(env);
}]; }];
} }
- (void)cleanup { - (void)dealloc {
[fMenuItem setAction:NULL]; [fMenuItem setAction:NULL];
[fMenuItem setTarget:nil]; [fMenuItem setTarget:nil];
}
- (void)dealloc {
[fMenuItem release]; [fMenuItem release];
fMenuItem = nil; fMenuItem = nil;
...@@ -223,14 +220,6 @@ JNF_COCOA_EXIT(env); ...@@ -223,14 +220,6 @@ JNF_COCOA_EXIT(env);
fIsCheckbox = YES; fIsCheckbox = YES;
} }
- (void) _createMenuItem_OnAppKitThread: (NSMutableArray *)argValue {
jobject cPeerObjGlobal = (jobject)[[argValue objectAtIndex: 0] pointerValue];
NSNumber * asSeparator = (NSNumber *)[argValue objectAtIndex: 1];
CMenuItem *aCMenuItem = [self initWithPeer: cPeerObjGlobal asSeparator: asSeparator];
[argValue removeAllObjects];
[argValue addObject: aCMenuItem];
}
- (NSString *)description { - (NSString *)description {
return [NSString stringWithFormat:@"CMenuItem[ %@ ]", fMenuItem]; return [NSString stringWithFormat:@"CMenuItem[ %@ ]", fMenuItem];
} }
...@@ -392,24 +381,18 @@ Java_sun_lwawt_macosx_CMenuItem_nativeCreate ...@@ -392,24 +381,18 @@ Java_sun_lwawt_macosx_CMenuItem_nativeCreate
(JNIEnv *env, jobject peer, jlong parentCMenuObj, jboolean isSeparator) (JNIEnv *env, jobject peer, jlong parentCMenuObj, jboolean isSeparator)
{ {
CMenuItem *aCMenuItem = nil; __block CMenuItem *aCMenuItem = nil;
BOOL asSeparator = (isSeparator == JNI_TRUE) ? YES: NO;
CMenu *parentCMenu = (CMenu *)jlong_to_ptr(parentCMenuObj); CMenu *parentCMenu = (CMenu *)jlong_to_ptr(parentCMenuObj);
JNF_COCOA_ENTER(env); JNF_COCOA_ENTER(env);
jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer); jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
NSMutableArray *args = nil; [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
aCMenuItem = [[CMenuItem alloc] initWithPeer: cPeerObjGlobal
// Create a new item.... asSeparator: asSeparator];
if (isSeparator == JNI_TRUE) { // the CMenuItem is released in CMenuComponent.dispose()
args = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithBytes:&cPeerObjGlobal objCType:@encode(jobject)], [NSNumber numberWithBool:YES], nil]; }];
} else {
args = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithBytes:&cPeerObjGlobal objCType:@encode(jobject)], [NSNumber numberWithBool:NO], nil];
}
[ThreadUtilities performOnMainThread:@selector(_createMenuItem_OnAppKitThread:) on:[CMenuItem alloc] withObject:args waitUntilDone:YES];
aCMenuItem = (CMenuItem *)[args objectAtIndex: 0];
if (aCMenuItem == nil) { if (aCMenuItem == nil) {
return 0L; return 0L;
......
...@@ -145,6 +145,10 @@ public abstract class MenuComponent implements java.io.Serializable { ...@@ -145,6 +145,10 @@ public abstract class MenuComponent implements java.io.Serializable {
public Font getFont_NoClientCode(MenuComponent menuComp) { public Font getFont_NoClientCode(MenuComponent menuComp) {
return menuComp.getFont_NoClientCode(); return menuComp.getFont_NoClientCode();
} }
@SuppressWarnings("unchecked")
public <T extends MenuComponentPeer> T getPeer(MenuComponent menuComp) {
return (T) menuComp.peer;
}
}); });
} }
......
...@@ -36,6 +36,7 @@ import java.awt.event.InvocationEvent; ...@@ -36,6 +36,7 @@ import java.awt.event.InvocationEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.geom.Point2D; import java.awt.geom.Point2D;
import java.awt.peer.ComponentPeer; import java.awt.peer.ComponentPeer;
import java.awt.peer.MenuComponentPeer;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.security.AccessControlContext; import java.security.AccessControlContext;
...@@ -463,6 +464,11 @@ public final class AWTAccessor { ...@@ -463,6 +464,11 @@ public final class AWTAccessor {
* Gets the font used for this menu component. * Gets the font used for this menu component.
*/ */
Font getFont_NoClientCode(MenuComponent menuComp); Font getFont_NoClientCode(MenuComponent menuComp);
/**
* Returns the peer of the menu component.
*/
<T extends MenuComponentPeer> T getPeer(MenuComponent menuComp);
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册