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