Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JSpinner/8008657/bug8008657.java
38918 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;3435/**36* @test37* @bug 800865738* @author Alexander Scherbatiy39* @summary JSpinner setComponentOrientation doesn't affect on text orientation40* @run main bug800865741*/42public class bug8008657 {4344private static Robot robot;45private static JSpinner spinner;4647public static void main(String[] args) throws Exception {4849robot = new Robot();5051SwingUtilities.invokeAndWait(() -> {52createDateSpinner();53createAndShowUI();54});5556robot.waitForIdle();57testSpinner(false);5859SwingUtilities.invokeAndWait(() -> {60createNumberSpinner();61createAndShowUI();62});6364robot.waitForIdle();65testSpinner(true);66}6768static void testSpinner(boolean checkHorizontalAligment)69throws Exception {7071SwingUtilities.invokeAndWait(() -> {72spinner.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);73});74robot.waitForIdle();7576SwingUtilities.invokeAndWait(() -> {7778JTextField textField = getTextField();79if (!ComponentOrientation.RIGHT_TO_LEFT.equals(80textField.getComponentOrientation())) {81throw new RuntimeException("Wrong orientation!");82}8384if (checkHorizontalAligment85&& textField.getHorizontalAlignment() != JTextField.LEFT) {86throw new RuntimeException("Wrong horizontal aligment!");87}8889spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);90});9192robot.waitForIdle();9394SwingUtilities.invokeAndWait(() -> {95JTextField textField = getTextField();96if (!ComponentOrientation.LEFT_TO_RIGHT.equals(97textField.getComponentOrientation())) {98throw new RuntimeException("Wrong orientation!");99}100101if (checkHorizontalAligment102&& textField.getHorizontalAlignment() != JTextField.RIGHT) {103throw new RuntimeException("Wrong horizontal aligment!");104}105106spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);107});108}109110static JTextField getTextField() {111return ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();112}113114static String getOrientation(ComponentOrientation orientation) {115return orientation == ComponentOrientation.LEFT_TO_RIGHT ? "LEFT_TO_RIGHT" : "RIGHT_TO_LEFT";116}117118static void createDateSpinner() {119Calendar calendar = Calendar.getInstance();120Date initDate = calendar.getTime();121calendar.add(Calendar.YEAR, -1);122Date earliestDate = calendar.getTime();123calendar.add(Calendar.YEAR, 1);124Date latestDate = calendar.getTime();125SpinnerModel dateModel = new SpinnerDateModel(initDate,126earliestDate,127latestDate,128Calendar.YEAR);129spinner = new JSpinner();130spinner.setModel(dateModel);131}132133static void createNumberSpinner() {134Calendar calendar = Calendar.getInstance();135calendar.add(Calendar.YEAR, -1);136calendar.add(Calendar.YEAR, 1);137int currentYear = calendar.get(Calendar.YEAR);138SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value139currentYear - 1, //min140currentYear + 2, //max1411); //step142spinner = new JSpinner();143spinner.setModel(yearModel);144}145146static void createAndShowUI() {147JFrame frame = new JFrame("Test");148frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);149frame.setSize(300, 100);150frame.getContentPane().add(spinner);151frame.setVisible(true);152}153}154155156