8008657: JSpinner setComponentOrientation doesn't affect on text orientation
Reviewed-by: malenkov, serb
--- a/jdk/src/share/classes/javax/swing/JSpinner.java Mon Feb 24 12:51:58 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/JSpinner.java Mon Feb 24 17:23:43 2014 +0400
@@ -1221,6 +1221,7 @@
JFormattedTextField ftf = getTextField();
ftf.setEditable(true);
ftf.setFormatterFactory(factory);
+ // Change the text orientation for the NumberEditor
ftf.setHorizontalAlignment(JTextField.RIGHT);
/* TBD - initializing the column width of the text field
@@ -1264,6 +1265,16 @@
public SpinnerNumberModel getModel() {
return (SpinnerNumberModel)(getSpinner().getModel());
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setComponentOrientation(ComponentOrientation o) {
+ super.setComponentOrientation(o);
+ getTextField().setHorizontalAlignment(
+ o.isLeftToRight() ? JTextField.RIGHT : JTextField.LEFT);
+ }
}
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java Mon Feb 24 12:51:58 2014 +0400
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicSpinnerUI.java Mon Feb 24 17:23:43 2014 +0400
@@ -997,21 +997,32 @@
"model".equals(propertyName)) {
ui.updateEnabledState();
}
- else if ("font".equals(propertyName)) {
- JComponent editor = spinner.getEditor();
- if (editor!=null && editor instanceof JSpinner.DefaultEditor) {
- JTextField tf =
- ((JSpinner.DefaultEditor)editor).getTextField();
- if (tf != null) {
- if (tf.getFont() instanceof UIResource) {
- tf.setFont(spinner.getFont());
+ else if ("font".equals(propertyName)) {
+ JComponent editor = spinner.getEditor();
+ if (editor!=null && editor instanceof JSpinner.DefaultEditor) {
+ JTextField tf =
+ ((JSpinner.DefaultEditor)editor).getTextField();
+ if (tf != null) {
+ if (tf.getFont() instanceof UIResource) {
+ tf.setFont(spinner.getFont());
+ }
}
}
}
- }
- else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
- updateToolTipTextForChildren(spinner);
- }
+ else if (JComponent.TOOL_TIP_TEXT_KEY.equals(propertyName)) {
+ updateToolTipTextForChildren(spinner);
+ } else if ("componentOrientation".equals(propertyName)) {
+ ComponentOrientation o
+ = (ComponentOrientation) e.getNewValue();
+ if (o != (ComponentOrientation) e.getOldValue()) {
+ JComponent editor = spinner.getEditor();
+ if (editor != null) {
+ editor.applyComponentOrientation(o);
+ }
+ spinner.revalidate();
+ spinner.repaint();
+ }
+ }
}
} else if (e.getSource() instanceof JComponent) {
JComponent c = (JComponent)e.getSource();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JSpinner/8008657/bug8008657.java Mon Feb 24 17:23:43 2014 +0400
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.ComponentOrientation;
+import java.awt.Toolkit;
+import java.util.Calendar;
+import java.util.Date;
+import javax.swing.JFrame;
+import javax.swing.JSpinner;
+import javax.swing.JTextField;
+import javax.swing.SpinnerDateModel;
+import javax.swing.SpinnerModel;
+import javax.swing.SpinnerNumberModel;
+import javax.swing.SwingUtilities;
+import sun.awt.SunToolkit;
+
+/**
+ * @test
+ * @bug 8008657
+ * @author Alexander Scherbatiy
+ * @summary JSpinner setComponentOrientation doesn't affect on text orientation
+ * @run main bug8008657
+ */
+public class bug8008657 {
+
+ private static SunToolkit toolkit;
+ private static JSpinner spinner;
+
+ public static void main(String[] args) throws Exception {
+
+ toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
+
+ SwingUtilities.invokeAndWait(() -> {
+ createDateSpinner();
+ createAndShowUI();
+ });
+
+ toolkit.realSync();
+ testSpinner(false);
+
+ SwingUtilities.invokeAndWait(() -> {
+ createNumberSpinner();
+ createAndShowUI();
+ });
+
+ toolkit.realSync();
+ testSpinner(true);
+ }
+
+ static void testSpinner(boolean checkHorizontalAligment)
+ throws Exception {
+
+ SwingUtilities.invokeAndWait(() -> {
+ spinner.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
+ });
+ toolkit.realSync();
+
+ SwingUtilities.invokeAndWait(() -> {
+
+ JTextField textField = getTextField();
+ if (!ComponentOrientation.RIGHT_TO_LEFT.equals(
+ textField.getComponentOrientation())) {
+ throw new RuntimeException("Wrong orientation!");
+ }
+
+ if (checkHorizontalAligment
+ && textField.getHorizontalAlignment() != JTextField.LEFT) {
+ throw new RuntimeException("Wrong horizontal aligment!");
+ }
+
+ spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
+ });
+
+ toolkit.realSync();
+
+ SwingUtilities.invokeAndWait(() -> {
+ JTextField textField = getTextField();
+ if (!ComponentOrientation.LEFT_TO_RIGHT.equals(
+ textField.getComponentOrientation())) {
+ throw new RuntimeException("Wrong orientation!");
+ }
+
+ if (checkHorizontalAligment
+ && textField.getHorizontalAlignment() != JTextField.RIGHT) {
+ throw new RuntimeException("Wrong horizontal aligment!");
+ }
+
+ spinner.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
+ });
+ }
+
+ static JTextField getTextField() {
+ return ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField();
+ }
+
+ static String getOrientation(ComponentOrientation orientation) {
+ return orientation == ComponentOrientation.LEFT_TO_RIGHT ? "LEFT_TO_RIGHT" : "RIGHT_TO_LEFT";
+ }
+
+ static void createDateSpinner() {
+ Calendar calendar = Calendar.getInstance();
+ Date initDate = calendar.getTime();
+ calendar.add(Calendar.YEAR, -1);
+ Date earliestDate = calendar.getTime();
+ calendar.add(Calendar.YEAR, 1);
+ Date latestDate = calendar.getTime();
+ SpinnerModel dateModel = new SpinnerDateModel(initDate,
+ earliestDate,
+ latestDate,
+ Calendar.YEAR);
+ spinner = new JSpinner();
+ spinner.setModel(dateModel);
+ }
+
+ static void createNumberSpinner() {
+ Calendar calendar = Calendar.getInstance();
+ calendar.add(Calendar.YEAR, -1);
+ calendar.add(Calendar.YEAR, 1);
+ int currentYear = calendar.get(Calendar.YEAR);
+ SpinnerModel yearModel = new SpinnerNumberModel(currentYear, //initial value
+ currentYear - 1, //min
+ currentYear + 2, //max
+ 1); //step
+ spinner = new JSpinner();
+ spinner.setModel(yearModel);
+ }
+
+ static void createAndShowUI() {
+ JFrame frame = new JFrame("Test");
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.setSize(300, 100);
+ frame.getContentPane().add(spinner);
+ frame.setVisible(true);
+ }
+}
\ No newline at end of file