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