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.*;
  30 import java.awt.event.ItemEvent;
  31 import java.awt.event.ItemListener;
  32 import java.awt.peer.ChoicePeer;
  33 
  34 import javax.accessibility.Accessible;
  35 import javax.swing.*;
  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     public void initialize() {
  59         super.initialize();
  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     }
  74 
  75     @Override
  76     public void itemStateChanged(final ItemEvent e) {
  77         // AWT Choice sends SELECTED event only whereas JComboBox
  78         // sends both SELECTED and DESELECTED.
  79         if (e.getStateChange() == ItemEvent.SELECTED) {
  80             synchronized (getDelegateLock()) {
  81                 if (skipPostMessage) {
  82                     return;
  83                 }
  84                 getTarget().select(getDelegate().getSelectedIndex());
  85             }
  86             postEvent(new ItemEvent(getTarget(), ItemEvent.ITEM_STATE_CHANGED,
  87                                     e.getItem(), ItemEvent.SELECTED));
  88         }
  89     }
  90 
  91     @Override
  92     public void add(final String item, final int index) {
  93         synchronized (getDelegateLock()) {
  94             getDelegate().insertItemAt(item, index);
  95         }
  96     }
  97 
  98     @Override
  99     public void remove(final int index) {
 100         synchronized (getDelegateLock()) {
 101             // We shouldn't post event, if selected item was removed.
 102             skipPostMessage = true;
 103             getDelegate().removeItemAt(index);
 104             skipPostMessage = false;
 105         }
 106     }
 107 
 108     @Override
 109     public void removeAll() {
 110         synchronized (getDelegateLock()) {
 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         /**
 151          * We should post ITEM_STATE_CHANGED event when the same element is
 152          * reselected.
 153          */
 154         @Override
 155         public void setSelectedItem(final Object anObject) {
 156             final Object oldSelection = selectedItemReminder;
 157             if (oldSelection != null && oldSelection.equals(anObject)) {
 158                 selectedItemChanged();
 159             }
 160             super.setSelectedItem(anObject);
 161         }
 162 
 163         @Override
 164         public void firePopupMenuWillBecomeVisible() {
 165             super.firePopupMenuWillBecomeVisible();
 166             SwingUtilities.invokeLater(new Runnable() {
 167                 @Override
 168                 public void run() {
 169                     JPopupMenu popupMenu = getPopupMenu();
 170                     if (popupMenu != null) {
 171                         if (popupMenu.getInvoker() != LWChoicePeer.this.getTarget()) {
 172                             popupMenu.setVisible(false);
 173                             popupMenu.show(LWChoicePeer.this.getTarget(), 0, 0);
 174                         }
 175                     }
 176                 }
 177             });
 178         }
 179 
 180         private JPopupMenu getPopupMenu() {
 181             for (int i = 0; i < getAccessibleContext().getAccessibleChildrenCount(); i++) {
 182                 Accessible child = getAccessibleContext().getAccessibleChild(i);
 183                 if (child instanceof JPopupMenu) {
 184                     return  (JPopupMenu) child;
 185                 }
 186             }
 187             return null;
 188         }
 189     }
 190 }