Path: blob/master/test/jdk/javax/swing/JSpinner/8008657/bug8008657.java
41155 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.ComponentOrientation;24import java.awt.Robot;25import java.util.Calendar;26import java.util.Date;27import javax.swing.JFrame;28import javax.swing.JSpinner;29import javax.swing.JTextField;30import javax.swing.SpinnerDateModel;31import javax.swing.SpinnerModel;32import javax.swing.SpinnerNumberModel;33import javax.swing.SwingUtilities;34import javax.swing.UIManager;35import javax.swing.UnsupportedLookAndFeelException;3637/**38* @test39* @key headful40* @bug 800865741* @author Alexander Scherbatiy42* @summary JSpinner setComponentOrientation doesn't affect on text orientation43* @run main bug800865744*/45public class bug8008657 {4647private static Robot robot;48private static JSpinner spinner;49private static JFrame frame;5051public static void main(String[] args) throws Exception {5253robot = new Robot();54UIManager.LookAndFeelInfo[] lookAndFeelArray55= UIManager.getInstalledLookAndFeels();56for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {57executeCase(lookAndFeelItem.getClassName());58}5960}61static void executeCase(String lookAndFeelString) throws Exception {62if (tryLookAndFeel(lookAndFeelString)) {63SwingUtilities.invokeAndWait(() -> {64createDateSpinner();65createAndShowUI();66});6768robot.waitForIdle();69testSpinner(false);70cleanUp();7172SwingUtilities.invokeAndWait(() -> {73createNumberSpinner();74createAndShowUI();75});7677robot.waitForIdle();78testSpinner(true);79cleanUp();80}81}82static void testSpinner(boolean checkHorizontalAligment)83throws Exception {8485SwingUtilities.invokeAndWait(() -> {86spinner.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);8788});89robot.waitForIdle();9091SwingUtilities.invokeAndWait(() -> {9293JTextField textField = getTextField();94if (!ComponentOrientation.RIGHT_TO_LEFT.equals(95textField.getComponentOrientation())) {96throw new RuntimeException("Wrong orientation!");97}9899if (checkHorizontalAligment100&& textField.getHorizontalAlignment() != JTextField.LEFT) {101throw new RuntimeException("Wrong horizontal aligment!");102}103104spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);105});106107robot.waitForIdle();108109SwingUtilities.invokeAndWait(() -> {110JTextField textField = getTextField();111if (!ComponentOrientation.LEFT_TO_RIGHT.equals(112textField.getComponentOrientation())) {113throw new RuntimeException("Wrong orientation!");114}115116if (checkHorizontalAligment117&& textField.getHorizontalAlignment() != JTextField.RIGHT) {118throw new RuntimeException("Wrong horizontal aligment!");119}120121spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);122});123}124125static JTextField getTextField() {126return ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();127}128129static String getOrientation(ComponentOrientation orientation) {130return orientation == ComponentOrientation.LEFT_TO_RIGHT ? "LEFT_TO_RIGHT" : "RIGHT_TO_LEFT";131}132133static void createDateSpinner() {134Calendar calendar = Calendar.getInstance();135Date initDate = calendar.getTime();136calendar.add(Calendar.YEAR, -1);137Date earliestDate = calendar.getTime();138calendar.add(Calendar.YEAR, 1);139Date latestDate = calendar.getTime();140SpinnerModel dateModel = new SpinnerDateModel(initDate,141earliestDate,142latestDate,143Calendar.YEAR);144spinner = new JSpinner();145spinner.setModel(dateModel);146}147148static void createNumberSpinner() {149Calendar calendar = Calendar.getInstance();150calendar.add(Calendar.YEAR, -1);151calendar.add(Calendar.YEAR, 1);152int currentYear = calendar.get(Calendar.YEAR);153SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value154currentYear - 1, //min155currentYear + 2, //max1561); //step157spinner = new JSpinner();158spinner.setModel(yearModel);159}160161static void createAndShowUI() {162frame = new JFrame("Test");163frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);164frame.setSize(300, 100);165frame.getContentPane().add(spinner);166frame.setVisible(true);167}168169private static void cleanUp() throws Exception {170SwingUtilities.invokeAndWait(new Runnable() {171@Override172public void run() {173frame.dispose();174}175});176}177178private static boolean tryLookAndFeel(String lookAndFeelString)179throws Exception {180try {181UIManager.setLookAndFeel(182lookAndFeelString);183184} catch (UnsupportedLookAndFeelException185| ClassNotFoundException186| InstantiationException187| IllegalAccessException e) {188return false;189}190return true;191}192}193194195