1 /*
   2  * Copyright (c) 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.
   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 java.awt.ComponentOrientation;
  25 import java.awt.Robot;
  26 import java.util.Calendar;
  27 import java.util.Date;
  28 import javax.swing.JFrame;
  29 import javax.swing.JSpinner;
  30 import javax.swing.JTextField;
  31 import javax.swing.SpinnerDateModel;
  32 import javax.swing.SpinnerModel;
  33 import javax.swing.SpinnerNumberModel;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.UIManager;
  36 import javax.swing.UnsupportedLookAndFeelException;
  37 
  38 /**
  39  * @test
  40  * @key headful
  41  * @bug 8008657
  42  * @author Alexander Scherbatiy
  43  * @summary JSpinner setComponentOrientation doesn't affect on text orientation
  44  * @run main bug8008657
  45  */
  46 public class bug8008657 {
  47 
  48     private static Robot robot;
  49     private static JSpinner spinner;
  50     private static JFrame frame;
  51 
  52     public static void main(String[] args) throws Exception {
  53 
  54         robot = new Robot();
  55         UIManager.LookAndFeelInfo[] lookAndFeelArray
  56                 = UIManager.getInstalledLookAndFeels();
  57         for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
  58             executeCase(lookAndFeelItem.getClassName());
  59         }
  60 
  61     }
  62     static void executeCase(String lookAndFeelString) throws Exception {
  63          if (tryLookAndFeel(lookAndFeelString)) {
  64          SwingUtilities.invokeAndWait(() -> {
  65             createDateSpinner();
  66             createAndShowUI();
  67         });
  68 
  69         robot.waitForIdle();
  70         testSpinner(false);
  71         cleanUp();
  72 
  73         SwingUtilities.invokeAndWait(() -> {
  74             createNumberSpinner();
  75             createAndShowUI();
  76         });
  77 
  78         robot.waitForIdle();
  79         testSpinner(true);
  80         cleanUp();
  81          }
  82     }
  83     static void testSpinner(boolean checkHorizontalAligment)
  84             throws Exception {
  85 
  86         SwingUtilities.invokeAndWait(() -> {
  87             spinner.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  88 
  89                                      });
  90         robot.waitForIdle();
  91 
  92         SwingUtilities.invokeAndWait(() -> {
  93 
  94             JTextField textField = getTextField();
  95             if (!ComponentOrientation.RIGHT_TO_LEFT.equals(
  96                     textField.getComponentOrientation())) {
  97                 throw new RuntimeException("Wrong orientation!");
  98             }
  99 
 100             if (checkHorizontalAligment
 101                     && textField.getHorizontalAlignment() != JTextField.LEFT) {
 102                 throw new RuntimeException("Wrong horizontal aligment!");
 103             }
 104 
 105             spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 106         });
 107 
 108         robot.waitForIdle();
 109 
 110         SwingUtilities.invokeAndWait(() -> {
 111             JTextField textField = getTextField();
 112             if (!ComponentOrientation.LEFT_TO_RIGHT.equals(
 113                     textField.getComponentOrientation())) {
 114                 throw new RuntimeException("Wrong orientation!");
 115             }
 116 
 117             if (checkHorizontalAligment
 118                     && textField.getHorizontalAlignment() != JTextField.RIGHT) {
 119                 throw new RuntimeException("Wrong horizontal aligment!");
 120             }
 121 
 122             spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 123         });
 124     }
 125 
 126     static JTextField getTextField() {
 127         return ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
 128     }
 129 
 130     static String getOrientation(ComponentOrientation orientation) {
 131         return orientation == ComponentOrientation.LEFT_TO_RIGHT ? "LEFT_TO_RIGHT" : "RIGHT_TO_LEFT";
 132     }
 133 
 134     static void createDateSpinner() {
 135         Calendar calendar = Calendar.getInstance();
 136         Date initDate = calendar.getTime();
 137         calendar.add(Calendar.YEAR, -1);
 138         Date earliestDate = calendar.getTime();
 139         calendar.add(Calendar.YEAR, 1);
 140         Date latestDate = calendar.getTime();
 141         SpinnerModel dateModel = new SpinnerDateModel(initDate,
 142                 earliestDate,
 143                 latestDate,
 144                 Calendar.YEAR);
 145         spinner = new JSpinner();
 146         spinner.setModel(dateModel);
 147     }
 148 
 149     static void createNumberSpinner() {
 150         Calendar calendar = Calendar.getInstance();
 151         calendar.add(Calendar.YEAR, -1);
 152         calendar.add(Calendar.YEAR, 1);
 153         int currentYear = calendar.get(Calendar.YEAR);
 154         SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
 155                 currentYear - 1, //min
 156                 currentYear + 2, //max
 157                 1);                //step
 158         spinner = new JSpinner();
 159         spinner.setModel(yearModel);
 160     }
 161 
 162     static void createAndShowUI() {
 163         frame = new JFrame("Test");
 164         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 165         frame.setSize(300, 100);
 166         frame.getContentPane().add(spinner);
 167         frame.setVisible(true);
 168     }
 169 
 170     private static void cleanUp() throws Exception {
 171         SwingUtilities.invokeAndWait(new Runnable() {
 172             @Override
 173             public void run() {
 174                 frame.dispose();
 175             }
 176         });
 177     }
 178 
 179     private static boolean tryLookAndFeel(String lookAndFeelString)
 180             throws Exception {
 181         try {
 182             UIManager.setLookAndFeel(
 183                     lookAndFeelString);
 184 
 185         } catch (UnsupportedLookAndFeelException
 186                 | ClassNotFoundException
 187                 | InstantiationException
 188                 | IllegalAccessException e) {
 189             return false;
 190         }
 191         return true;
 192     }
 193 }