1 /*
   2  * Copyright (c) 1996, 2015, 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 package sun.awt.windows;
  26 
  27 import java.awt.*;
  28 import java.awt.peer.*;
  29 import java.awt.event.ItemEvent;
  30 import java.awt.event.WindowEvent;
  31 import java.awt.event.WindowListener;
  32 import java.awt.event.WindowAdapter;
  33 
  34 import sun.awt.AWTAccessor;
  35 import sun.awt.SunToolkit;
  36 
  37 final class WChoicePeer extends WComponentPeer implements ChoicePeer {
  38 
  39     // WComponentPeer overrides
  40 
  41     @Override
  42     public Dimension getMinimumSize() {
  43         FontMetrics fm = getFontMetrics(((Choice)target).getFont());
  44         Choice c = (Choice)target;
  45         int w = 0;
  46         for (int i = c.getItemCount() ; i-- > 0 ;) {
  47             w = Math.max(fm.stringWidth(c.getItem(i)), w);
  48         }
  49         return new Dimension(28 + w, Math.max(fm.getHeight() + 6, 15));
  50     }
  51     @Override
  52     public boolean isFocusable() {
  53         return true;
  54     }
  55 
  56     // ChoicePeer implementation
  57 
  58     @Override
  59     public native void select(int index);
  60 
  61     @Override
  62     public void add(String item, int index) {
  63         addItem(item, index);
  64     }
  65 
  66     @Override
  67     public boolean shouldClearRectBeforePaint() {
  68         return false;
  69     }
  70 
  71     @Override
  72     public native void removeAll();
  73     @Override
  74     public native void remove(int index);
  75 
  76     /**
  77      * DEPRECATED, but for now, called by add(String, int).
  78      */
  79     public void addItem(String item, int index) {
  80         addItems(new String[] {item}, index);
  81     }
  82     public native void addItems(String[] items, int index);
  83 
  84     @Override
  85     public synchronized native void reshape(int x, int y, int width, int height);
  86 
  87     private WindowListener windowListener;
  88 
  89     // Toolkit & peer internals
  90 
  91     WChoicePeer(Choice target) {
  92         super(target);
  93     }
  94 
  95     @Override
  96     native void create(WComponentPeer parent);
  97 
  98     @Override
  99     void initialize() {
 100         Choice opt = (Choice)target;
 101         int itemCount = opt.getItemCount();
 102         if (itemCount > 0) {
 103             String[] items = new String[itemCount];
 104             for (int i=0; i < itemCount; i++) {
 105                 items[i] = opt.getItem(i);
 106             }
 107             addItems(items, 0);
 108             if (opt.getSelectedIndex() >= 0) {
 109                 select(opt.getSelectedIndex());
 110             }
 111         }
 112 
 113         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 114         if (parentWindow != null) {
 115             final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()
 116                                                  .getPeer(parentWindow);
 117             if (wpeer != null) {
 118                 windowListener = new WindowAdapter() {
 119                         @Override
 120                         public void windowIconified(WindowEvent e) {
 121                             closeList();
 122                         }
 123                         @Override
 124                         public void windowClosing(WindowEvent e) {
 125                             closeList();
 126                         }
 127                     };
 128                 wpeer.addWindowListener(windowListener);
 129             }
 130         }
 131         super.initialize();
 132     }
 133 
 134     @Override
 135     protected void disposeImpl() {
 136         // TODO: we should somehow reset the listener when the choice
 137         // is moved to another toplevel without destroying its peer.
 138         Window parentWindow = SunToolkit.getContainingWindow((Component)target);
 139         if (parentWindow != null) {
 140             final WWindowPeer wpeer = AWTAccessor.getComponentAccessor()
 141                                                 .getPeer(parentWindow);
 142             if (wpeer != null) {
 143                 wpeer.removeWindowListener(windowListener);
 144             }
 145         }
 146         super.disposeImpl();
 147     }
 148 
 149     // native callbacks
 150 
 151     void handleAction(final int index) {
 152         final Choice c = (Choice)target;
 153         WToolkit.executeOnEventHandlerThread(c, new Runnable() {
 154             @Override
 155             public void run() {
 156                 c.select(index);
 157                 postEvent(new ItemEvent(c, ItemEvent.ITEM_STATE_CHANGED,
 158                                 c.getItem(index), ItemEvent.SELECTED));
 159             }
 160         });
 161     }
 162 
 163     int getDropDownHeight() {
 164         Choice c = (Choice)target;
 165         FontMetrics fm = getFontMetrics(c.getFont());
 166         int maxItems = Math.min(c.getItemCount(), 8);
 167         return fm.getHeight() * maxItems;
 168     }
 169 
 170     native void closeList();
 171 }