1 /* 2 * Copyright (c) 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import com.sun.java.swing.plaf.windows.WindowsComboBoxUI.WindowsComboBoxEditor; 25 import java.awt.Toolkit; 26 import java.awt.event.ItemEvent; 27 import java.awt.event.ItemListener; 28 import javax.swing.ComboBoxEditor; 29 import javax.swing.JComboBox; 30 import javax.swing.JFrame; 31 import javax.swing.JTextField; 32 import javax.swing.UIManager; 33 import sun.awt.SunToolkit; 34 35 import static javax.swing.SwingUtilities.invokeAndWait; 36 import static javax.swing.SwingUtilities.windowForComponent; 37 import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE; 38 39 /* 40 * @test 41 * @bug 8015300 42 * @summary Tests that editable combobox select all text 43 * @author Sergey Malenkov 44 */ 45 46 public class Test8015300 { 47 private static final SunToolkit STK = (SunToolkit) Toolkit.getDefaultToolkit(); 48 private static final String[] ITEMS = { 49 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 50 "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; 51 52 private static JComboBox<String> combo; 53 54 public static void main(String[] args) throws Exception { 55 UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels(); 56 for (UIManager.LookAndFeelInfo info : array) { 57 UIManager.setLookAndFeel(info.getClassName()); 58 System.err.println("L&F: " + info.getName()); 59 invokeAndWait(new Runnable() { 60 @Override 61 public void run() { 62 combo = new JComboBox<>(ITEMS); 63 combo.addItemListener(new ItemListener() { 64 @Override 65 public void itemStateChanged(ItemEvent event) { 66 if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) { 67 ComboBoxEditor editor = combo.getEditor(); 68 Object component = editor.getEditorComponent(); 69 if (component instanceof JTextField) { 70 JTextField text = (JTextField) component; 71 boolean selected = null != text.getSelectedText(); 72 73 StringBuilder sb = new StringBuilder(); 74 sb.append(" - ").append(combo.getSelectedIndex()); 75 sb.append(": ").append(event.getItem()); 76 if (selected) { 77 sb.append("; selected"); 78 } 79 System.err.println(sb); 80 if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) { 81 throw new Error("unexpected state of text selection"); 82 } 83 } 84 } 85 } 86 }); 87 JFrame frame = new JFrame(getClass().getSimpleName()); 88 frame.add(combo); 89 frame.pack(); 90 frame.setLocationRelativeTo(null); 91 frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 92 frame.setVisible(true); 93 } 94 }); 95 for (int i = 0; i < ITEMS.length; ++i) { 96 select(i, true); 97 select(1, false); 98 } 99 invokeAndWait(new Runnable() { 100 @Override 101 public void run() { 102 windowForComponent(combo).dispose(); 103 } 104 }); 105 } 106 } 107 108 private static void select(final int index, final boolean editable) throws Exception { 109 invokeAndWait(new Runnable() { 110 @Override 111 public void run() { 112 combo.setEditable(editable); 113 combo.setSelectedIndex(index); 114 } 115 }); 116 STK.realSync(); 117 Thread.sleep(50L); 118 } 119 }