4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A
4632N/Apackage sun.lwawt;
4632N/A
4882N/Aimport java.awt.*;
4632N/Aimport java.awt.event.ItemEvent;
4632N/Aimport java.awt.event.ItemListener;
4632N/Aimport java.awt.peer.ChoicePeer;
4632N/A
4882N/Aimport javax.accessibility.Accessible;
4882N/Aimport javax.swing.*;
4632N/A
4632N/Afinal class LWChoicePeer extends LWComponentPeer<Choice, JComboBox<String>>
4632N/A implements ChoicePeer, ItemListener {
4632N/A
4639N/A /**
4639N/A * According to Choice specification item events are sent in response to
4639N/A * user input, but not in response to calls to select(). But JComboBox are
4639N/A * sent item events in both cases. Should be used under delegateLock.
4639N/A */
4639N/A private boolean skipPostMessage;
4639N/A
4639N/A LWChoicePeer(final Choice target,
4639N/A final PlatformComponent platformComponent) {
4632N/A super(target, platformComponent);
4632N/A }
4632N/A
4632N/A @Override
4632N/A protected JComboBox<String> createDelegate() {
4632N/A return new JComboBoxDelegate();
4632N/A }
4632N/A
4632N/A @Override
5166N/A void initializeImpl() {
5166N/A super.initializeImpl();
4632N/A final Choice choice = getTarget();
4632N/A final JComboBox<String> combo = getDelegate();
4632N/A synchronized (getDelegateLock()) {
4632N/A final int count = choice.getItemCount();
4632N/A for (int i = 0; i < count; ++i) {
4632N/A combo.addItem(choice.getItem(i));
4632N/A }
4632N/A select(choice.getSelectedIndex());
4632N/A
4632N/A // NOTE: the listener must be added at the very end, otherwise it
4632N/A // fires events upon initialization of the combo box.
4632N/A combo.addItemListener(this);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4639N/A public void itemStateChanged(final ItemEvent e) {
4632N/A // AWT Choice sends SELECTED event only whereas JComboBox
4632N/A // sends both SELECTED and DESELECTED.
4639N/A if (e.getStateChange() == ItemEvent.SELECTED) {
4632N/A synchronized (getDelegateLock()) {
4639N/A if (skipPostMessage) {
4639N/A return;
4639N/A }
4632N/A getTarget().select(getDelegate().getSelectedIndex());
4632N/A }
4632N/A postEvent(new ItemEvent(getTarget(), ItemEvent.ITEM_STATE_CHANGED,
4639N/A e.getItem(), ItemEvent.SELECTED));
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void add(final String item, final int index) {
4632N/A synchronized (getDelegateLock()) {
4632N/A getDelegate().insertItemAt(item, index);
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void remove(final int index) {
4632N/A synchronized (getDelegateLock()) {
4639N/A // We shouldn't post event, if selected item was removed.
4639N/A skipPostMessage = true;
4632N/A getDelegate().removeItemAt(index);
4639N/A skipPostMessage = false;
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void removeAll() {
4632N/A synchronized (getDelegateLock()) {
4632N/A getDelegate().removeAllItems();
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public void select(final int index) {
4632N/A synchronized (getDelegateLock()) {
4639N/A if (index != getDelegate().getSelectedIndex()) {
4639N/A skipPostMessage = true;
4639N/A getDelegate().setSelectedIndex(index);
4639N/A skipPostMessage = false;
4639N/A }
4632N/A }
4632N/A }
4632N/A
4632N/A @Override
4632N/A public boolean isFocusable() {
4632N/A return true;
4632N/A }
4632N/A
4632N/A private final class JComboBoxDelegate extends JComboBox<String> {
4632N/A
4632N/A // Empty non private constructor was added because access to this
4632N/A // class shouldn't be emulated by a synthetic accessor method.
4632N/A JComboBoxDelegate() {
4632N/A super();
4632N/A }
4632N/A
4632N/A @Override
4632N/A public boolean hasFocus() {
4632N/A return getTarget().hasFocus();
4632N/A }
4632N/A
4632N/A //Needed for proper popup menu location
4632N/A @Override
4632N/A public Point getLocationOnScreen() {
4632N/A return LWChoicePeer.this.getLocationOnScreen();
4632N/A }
4639N/A
4639N/A /**
4639N/A * We should post ITEM_STATE_CHANGED event when the same element is
4639N/A * reselected.
4639N/A */
4639N/A @Override
4639N/A public void setSelectedItem(final Object anObject) {
4639N/A final Object oldSelection = selectedItemReminder;
4639N/A if (oldSelection != null && oldSelection.equals(anObject)) {
4639N/A selectedItemChanged();
4639N/A }
4639N/A super.setSelectedItem(anObject);
4639N/A }
4882N/A
4882N/A @Override
4882N/A public void firePopupMenuWillBecomeVisible() {
4882N/A super.firePopupMenuWillBecomeVisible();
4882N/A SwingUtilities.invokeLater(new Runnable() {
4882N/A @Override
4882N/A public void run() {
4882N/A JPopupMenu popupMenu = getPopupMenu();
4882N/A if (popupMenu != null) {
4882N/A if (popupMenu.getInvoker() != LWChoicePeer.this.getTarget()) {
4882N/A popupMenu.setVisible(false);
4882N/A popupMenu.show(LWChoicePeer.this.getTarget(), 0, 0);
4882N/A }
4882N/A }
4882N/A }
4882N/A });
4882N/A }
4882N/A
4882N/A private JPopupMenu getPopupMenu() {
4882N/A for (int i = 0; i < getAccessibleContext().getAccessibleChildrenCount(); i++) {
4882N/A Accessible child = getAccessibleContext().getAccessibleChild(i);
4882N/A if (child instanceof JPopupMenu) {
4882N/A return (JPopupMenu) child;
4882N/A }
4882N/A }
4882N/A return null;
4882N/A }
4632N/A }
4632N/A}