11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.lwawt.macosx;
27
28 import sun.awt.SunToolkit;
29 import sun.lwawt.LWToolkit;
30
31 import java.awt.MenuItem;
32 import java.awt.MenuShortcut;
33 import java.awt.event.*;
34 import java.awt.peer.MenuItemPeer;
35
36 public class CMenuItem extends CMenuComponent implements MenuItemPeer {
37
38 public CMenuItem(MenuItem target) {
39 super(target);
40 initialize(target);
41
42 }
43
44 // This way we avoiding invocation of the setters twice
45 protected void initialize(MenuItem target) {
46 if (!isSeparator()) {
47 setLabel(target.getLabel());
48 setEnabled(target.isEnabled());
49 }
50 }
51
52 private boolean isSeparator() {
53 String label = ((MenuItem)getTarget()).getLabel();
54 return (label != null && label.equals("-"));
55 }
56
57 @Override
58 protected long createModel() {
59 CMenuComponent parent = (CMenuComponent)LWToolkit.targetToPeer(getTarget().getParent());
60 return nativeCreate(parent.getModel(), isSeparator());
61 }
107 nativeSetImage(getModel(), cimg == null ? 0L : cimg.ptr);
108 }
109
110 /**
111 * New API for tooltips
112 */
113 public void setToolTipText(String text) {
114 nativeSetTooltip(getModel(), text);
115 }
116
117 // @Override
118 public void enable() {
119 setEnabled(true);
120 }
121
122 // @Override
123 public void disable() {
124 setEnabled(false);
125 }
126
127 @Override
128 public void setEnabled(boolean b) {
129 nativeSetEnabled(getModel(), b);
130 }
131
132 private native long nativeCreate(long parentMenu, boolean isSeparator);
133 private native void nativeSetLabel(long modelPtr, String label, char keyChar, int keyCode, int modifiers);
134 private native void nativeSetImage(long modelPtr, long image);
135 private native void nativeSetTooltip(long modelPtr, String text);
136 private native void nativeSetEnabled(long modelPtr, boolean b);
137
138 // native callbacks
139 void handleAction(final long when, final int modifiers) {
140 assert CThreading.assertAppKit();
141
142 SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
143 public void run() {
144 final String cmd = ((MenuItem)getTarget()).getActionCommand();
145 final ActionEvent event = new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, cmd, when, modifiers);
146 SunToolkit.postEvent(SunToolkit.targetToAppContext(getTarget()), event);
147 }
148 });
149 }
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.lwawt.macosx;
27
28 import sun.awt.SunToolkit;
29 import sun.lwawt.LWToolkit;
30
31 import java.awt.MenuContainer;
32 import java.awt.MenuItem;
33 import java.awt.MenuShortcut;
34 import java.awt.event.*;
35 import java.awt.peer.MenuItemPeer;
36 import java.util.concurrent.atomic.AtomicBoolean;
37
38 public class CMenuItem extends CMenuComponent implements MenuItemPeer {
39
40 private final AtomicBoolean enabled = new AtomicBoolean(true);
41
42 public CMenuItem(MenuItem target) {
43 super(target);
44 initialize(target);
45 }
46
47 // This way we avoiding invocation of the setters twice
48 protected void initialize(MenuItem target) {
49 if (!isSeparator()) {
50 setLabel(target.getLabel());
51 setEnabled(target.isEnabled());
52 }
53 }
54
55 private boolean isSeparator() {
56 String label = ((MenuItem)getTarget()).getLabel();
57 return (label != null && label.equals("-"));
58 }
59
60 @Override
61 protected long createModel() {
62 CMenuComponent parent = (CMenuComponent)LWToolkit.targetToPeer(getTarget().getParent());
63 return nativeCreate(parent.getModel(), isSeparator());
64 }
110 nativeSetImage(getModel(), cimg == null ? 0L : cimg.ptr);
111 }
112
113 /**
114 * New API for tooltips
115 */
116 public void setToolTipText(String text) {
117 nativeSetTooltip(getModel(), text);
118 }
119
120 // @Override
121 public void enable() {
122 setEnabled(true);
123 }
124
125 // @Override
126 public void disable() {
127 setEnabled(false);
128 }
129
130 public final boolean isEnabled() {
131 return enabled.get();
132 }
133
134 @Override
135 public void setEnabled(boolean b) {
136 final Object parent = LWToolkit.targetToPeer(getTarget().getParent());
137 if (parent instanceof CMenuItem) {
138 b &= ((CMenuItem) parent).isEnabled();
139 }
140 if (enabled.compareAndSet(!b, b)) {
141 nativeSetEnabled(getModel(), b);
142 }
143 }
144
145 private native long nativeCreate(long parentMenu, boolean isSeparator);
146 private native void nativeSetLabel(long modelPtr, String label, char keyChar, int keyCode, int modifiers);
147 private native void nativeSetImage(long modelPtr, long image);
148 private native void nativeSetTooltip(long modelPtr, String text);
149 private native void nativeSetEnabled(long modelPtr, boolean b);
150
151 // native callbacks
152 void handleAction(final long when, final int modifiers) {
153 assert CThreading.assertAppKit();
154
155 SunToolkit.executeOnEventHandlerThread(getTarget(), new Runnable() {
156 public void run() {
157 final String cmd = ((MenuItem)getTarget()).getActionCommand();
158 final ActionEvent event = new ActionEvent(getTarget(), ActionEvent.ACTION_PERFORMED, cmd, when, modifiers);
159 SunToolkit.postEvent(SunToolkit.targetToAppContext(getTarget()), event);
160 }
161 });
162 }
|