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  * @key headful
  42  * @bug 8015300
  43  * @summary Tests that editable combobox selects all text.
  44  * @author Sergey Malenkov
  45  */
  46 
  47 public class Test8015300 {
  48     private static final SunToolkit STK = (SunToolkit) Toolkit.getDefaultToolkit();
  49     private static final String[] ITEMS = {
  50             "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  51             "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  52 
  53     private static JComboBox<String> combo;
  54 
  55     public static void main(String[] args) throws Exception {
  56         UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
  57         for (UIManager.LookAndFeelInfo info : array) {
  58             UIManager.setLookAndFeel(info.getClassName());
  59             System.err.println("L&F: " + info.getName());
  60             invokeAndWait(new Runnable() {
  61                 @Override
  62                 public void run() {
  63                     combo = new JComboBox<>(ITEMS);
  64                     combo.addItemListener(new ItemListener() {
  65                         @Override
  66                         public void itemStateChanged(ItemEvent event) {
  67                             if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
  68                                 ComboBoxEditor editor = combo.getEditor();
  69                                 Object component = editor.getEditorComponent();
  70                                 if (component instanceof JTextField) {
  71                                     JTextField text = (JTextField) component;
  72                                     boolean selected = null != text.getSelectedText();
  73 
  74                                     StringBuilder sb = new StringBuilder();
  75                                     sb.append(" - ").append(combo.getSelectedIndex());
  76                                     sb.append(": ").append(event.getItem());
  77                                     if (selected) {
  78                                         sb.append("; selected");
  79                                     }
  80                                     System.err.println(sb);
  81                                     if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
  82                                         throw new Error("unexpected state of text selection");
  83                                     }
  84                                 }
  85                             }
  86                         }
  87                     });
  88                     JFrame frame = new JFrame(getClass().getSimpleName());
  89                     frame.add(combo);
  90                     frame.pack();
  91                     frame.setLocationRelativeTo(null);
  92                     frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  93                     frame.setVisible(true);
  94                 }
  95             });
  96             for (int i = 0; i < ITEMS.length; ++i) {
  97                 select(i, true);
  98                 select(1, false);
  99             }
 100             invokeAndWait(new Runnable() {
 101                 @Override
 102                 public void run() {
 103                     windowForComponent(combo).dispose();
 104                 }
 105             });
 106         }
 107     }
 108 
 109     private static void select(final int index, final boolean editable) throws Exception {
 110         invokeAndWait(new Runnable() {
 111             @Override
 112             public void run() {
 113                 combo.setEditable(editable);
 114                 combo.setSelectedIndex(index);
 115             }
 116         });
 117         STK.realSync();
 118         Thread.sleep(50L);
 119     }
 120 }