1 /*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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
27 package sun.lwawt;
28
29 import java.awt.Choice;
30 import java.awt.Point;
31 import java.awt.event.ItemEvent;
32 import java.awt.event.ItemListener;
33 import java.awt.peer.ChoicePeer;
34
35 import javax.swing.JComboBox;
36
37 final class LWChoicePeer extends LWComponentPeer<Choice, JComboBox<String>>
38 implements ChoicePeer, ItemListener {
39
40 /**
41 * According to Choice specification item events are sent in response to
42 * user input, but not in response to calls to select(). But JComboBox are
43 * sent item events in both cases. Should be used under delegateLock.
44 */
45 private boolean skipPostMessage;
46
47 LWChoicePeer(final Choice target,
48 final PlatformComponent platformComponent) {
49 super(target, platformComponent);
50 }
51
52 @Override
53 protected JComboBox<String> createDelegate() {
54 return new JComboBoxDelegate();
55 }
56
57 @Override
58 void initializeImpl() {
59 super.initializeImpl();
60 final Choice choice = getTarget();
61 final JComboBox<String> combo = getDelegate();
62 synchronized (getDelegateLock()) {
63 final int count = choice.getItemCount();
64 for (int i = 0; i < count; ++i) {
65 combo.addItem(choice.getItem(i));
66 }
67 select(choice.getSelectedIndex());
68
69 // NOTE: the listener must be added at the very end, otherwise it
70 // fires events upon initialization of the combo box.
71 combo.addItemListener(this);
72 }
73 }
111 getDelegate().removeAllItems();
112 }
113 }
114
115 @Override
116 public void select(final int index) {
117 synchronized (getDelegateLock()) {
118 if (index != getDelegate().getSelectedIndex()) {
119 skipPostMessage = true;
120 getDelegate().setSelectedIndex(index);
121 skipPostMessage = false;
122 }
123 }
124 }
125
126 @Override
127 public boolean isFocusable() {
128 return true;
129 }
130
131 private final class JComboBoxDelegate extends JComboBox<String> {
132
133 // Empty non private constructor was added because access to this
134 // class shouldn't be emulated by a synthetic accessor method.
135 JComboBoxDelegate() {
136 super();
137 }
138
139 @Override
140 public boolean hasFocus() {
141 return getTarget().hasFocus();
142 }
143
144 //Needed for proper popup menu location
145 @Override
146 public Point getLocationOnScreen() {
147 return LWChoicePeer.this.getLocationOnScreen();
148 }
149
150 /**
|
1 /*
2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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
27 package sun.lwawt;
28
29 import java.awt.Choice;
30 import java.awt.Point;
31 import java.awt.event.ItemEvent;
32 import java.awt.event.ItemListener;
33 import java.awt.peer.ChoicePeer;
34
35 import javax.swing.JComboBox;
36
37 /**
38 * Lightweight implementation of {@link ChoicePeer}. Delegates most of the work
39 * to the {@link JComboBox}.
40 */
41 final class LWChoicePeer extends LWComponentPeer<Choice, JComboBox<String>>
42 implements ChoicePeer, ItemListener {
43
44 /**
45 * According to Choice specification item events are sent in response to
46 * user input, but not in response to calls to select(). But JComboBox are
47 * sent item events in both cases. Should be used under delegateLock.
48 */
49 private boolean skipPostMessage;
50
51 LWChoicePeer(final Choice target,
52 final PlatformComponent platformComponent) {
53 super(target, platformComponent);
54 }
55
56 @Override
57 JComboBox<String> createDelegate() {
58 return new JComboBoxDelegate();
59 }
60
61 @Override
62 void initializeImpl() {
63 super.initializeImpl();
64 final Choice choice = getTarget();
65 final JComboBox<String> combo = getDelegate();
66 synchronized (getDelegateLock()) {
67 final int count = choice.getItemCount();
68 for (int i = 0; i < count; ++i) {
69 combo.addItem(choice.getItem(i));
70 }
71 select(choice.getSelectedIndex());
72
73 // NOTE: the listener must be added at the very end, otherwise it
74 // fires events upon initialization of the combo box.
75 combo.addItemListener(this);
76 }
77 }
115 getDelegate().removeAllItems();
116 }
117 }
118
119 @Override
120 public void select(final int index) {
121 synchronized (getDelegateLock()) {
122 if (index != getDelegate().getSelectedIndex()) {
123 skipPostMessage = true;
124 getDelegate().setSelectedIndex(index);
125 skipPostMessage = false;
126 }
127 }
128 }
129
130 @Override
131 public boolean isFocusable() {
132 return true;
133 }
134
135 @SuppressWarnings("serial")// Safe: outer class is non-serializable.
136 private final class JComboBoxDelegate extends JComboBox<String> {
137
138 // Empty non private constructor was added because access to this
139 // class shouldn't be emulated by a synthetic accessor method.
140 JComboBoxDelegate() {
141 super();
142 }
143
144 @Override
145 public boolean hasFocus() {
146 return getTarget().hasFocus();
147 }
148
149 //Needed for proper popup menu location
150 @Override
151 public Point getLocationOnScreen() {
152 return LWChoicePeer.this.getLocationOnScreen();
153 }
154
155 /**
|