src/macosx/classes/sun/lwawt/LWChoicePeer.java

Print this page


   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     }


 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 }
   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.*;

  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     }


 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 }