1 /*
   2  * Copyright (c) 2013, 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.
   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  * @library ../../../../lib/testlibrary/
  45  * @modules java.desktop/com.sun.java.swing.plaf.windows
  46  * @build ExtendedRobot
  47  * @run main Test8015300
  48  */
  49 
  50 public class Test8015300 {
  51     private static final ExtendedRobot robot = createRobot();
  52     private static final String[] ITEMS = {
  53             "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  54             "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
  55 
  56     private static JComboBox<String> combo;
  57 
  58     public static void main(String[] args) throws Exception {
  59         UIManager.LookAndFeelInfo[] array = UIManager.getInstalledLookAndFeels();
  60         for (UIManager.LookAndFeelInfo info : array) {
  61             UIManager.setLookAndFeel(info.getClassName());
  62             System.err.println("L&F: " + info.getName());
  63             invokeAndWait(new Runnable() {
  64                 @Override
  65                 public void run() {
  66                     combo = new JComboBox<>(ITEMS);
  67                     combo.addItemListener(new ItemListener() {
  68                         @Override
  69                         public void itemStateChanged(ItemEvent event) {
  70                             if (ItemEvent.SELECTED == event.getStateChange() && combo.isEditable()) {
  71                                 ComboBoxEditor editor = combo.getEditor();
  72                                 Object component = editor.getEditorComponent();
  73                                 if (component instanceof JTextField) {
  74                                     JTextField text = (JTextField) component;
  75                                     boolean selected = null != text.getSelectedText();
  76 
  77                                     StringBuilder sb = new StringBuilder();
  78                                     sb.append(" - ").append(combo.getSelectedIndex());
  79                                     sb.append(": ").append(event.getItem());
  80                                     if (selected) {
  81                                         sb.append("; selected");
  82                                     }
  83                                     System.err.println(sb);
  84                                     if ((editor instanceof WindowsComboBoxEditor) == (null == text.getSelectedText())) {
  85                                         throw new Error("unexpected state of text selection");
  86                                     }
  87                                 }
  88                             }
  89                         }
  90                     });
  91                     JFrame frame = new JFrame(getClass().getSimpleName());
  92                     frame.add(combo);
  93                     frame.pack();
  94                     frame.setLocationRelativeTo(null);
  95                     frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  96                     frame.setVisible(true);
  97                 }
  98             });
  99             for (int i = 0; i < ITEMS.length; ++i) {
 100                 select(i, true);
 101                 select(1, false);
 102             }
 103             invokeAndWait(new Runnable() {
 104                 @Override
 105                 public void run() {
 106                     windowForComponent(combo).dispose();
 107                 }
 108             });
 109         }
 110     }
 111 
 112     private static void select(final int index, final boolean editable) throws Exception {
 113         invokeAndWait(new Runnable() {
 114             @Override
 115             public void run() {
 116                 combo.setEditable(editable);
 117                 combo.setSelectedIndex(index);
 118             }
 119         });
 120         robot.waitForIdle(50);
 121     }
 122     private static ExtendedRobot createRobot() {
 123          try {
 124              ExtendedRobot robot = new ExtendedRobot();
 125              return robot;
 126          }catch(Exception ex) {
 127              ex.printStackTrace();
 128              throw new Error("Unexpected Failure");
 129          }
 130     }
 131 }