1 /*
   2  * Copyright (c) 2015, 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.FlowLayout;
  25 import java.awt.Font;
  26 
  27 import javax.swing.JFrame;
  28 import javax.swing.JSpinner;
  29 import javax.swing.SwingUtilities;
  30 import javax.swing.UIManager;
  31 import javax.swing.UnsupportedLookAndFeelException;
  32 import javax.swing.plaf.UIResource;
  33 
  34 import static javax.swing.JSpinner.*;
  35 import static javax.swing.UIManager.getInstalledLookAndFeels;
  36 
  37 /**
  38  * @test
  39  * @key headful
  40  * @bug 5036022
  41  * @author Sergey Bylokhov
  42  */
  43 public class WrongEditorTextFieldFont implements Runnable {
  44 
  45     private static final Font USERS_FONT = new Font("dialog", Font.BOLD, 41);
  46 
  47     public static void main(final String[] args) throws Exception {
  48         for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  49             SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  50             SwingUtilities.invokeAndWait(new WrongEditorTextFieldFont());
  51         }
  52     }
  53 
  54     @Override
  55     public void run() {
  56         final JFrame frame1 = new JFrame();
  57         try {
  58             testDefaultFont(frame1);
  59         } finally {
  60             frame1.dispose();
  61         }
  62     }
  63 
  64     private static void testDefaultFont(final JFrame frame) {
  65         final JSpinner spinner = new JSpinner();
  66         final JSpinner spinner_u = new JSpinner();
  67         frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
  68         frame.getContentPane().add(spinner);
  69         frame.getContentPane().add(spinner_u);
  70         frame.pack();
  71         frame.setLocationRelativeTo(null);
  72         frame.setVisible(true);
  73         final DefaultEditor ed = (DefaultEditor) spinner.getEditor();
  74         final DefaultEditor ed_u = (DefaultEditor) spinner_u.getEditor();
  75         ed_u.getTextField().setFont(USERS_FONT);
  76 
  77         for (int i = 5; i < 40; i += 5) {
  78             /*
  79              * Validate that the font of the text field is changed to the
  80              * font of JSpinner if the font of text field was not set by the
  81              * user.
  82              */
  83             final Font tff = ed.getTextField().getFont();
  84             if (!(tff instanceof UIResource)) {
  85                 throw new RuntimeException("Font must be UIResource");
  86             }
  87             if (spinner.getFont().getSize() != tff.getSize()) {
  88                 throw new RuntimeException("Rrong size");
  89             }
  90             spinner.setFont(new Font("dialog", Font.BOLD, i));
  91             /*
  92              * Validate that the font of the text field is NOT changed to the
  93              * font of JSpinner if the font of text field was set by the user.
  94              */
  95             final Font tff_u = ed_u.getTextField().getFont();
  96             if (tff_u instanceof UIResource || !tff_u.equals(USERS_FONT)) {
  97                 throw new RuntimeException("Font must NOT be UIResource");
  98             }
  99             if (spinner_u.getFont().getSize() == tff_u.getSize()) {
 100                 throw new RuntimeException("Wrong size");
 101             }
 102             spinner_u.setFont(new Font("dialog", Font.BOLD, i));
 103         }
 104     }
 105 
 106     private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
 107         try {
 108             UIManager.setLookAndFeel(laf.getClassName());
 109             System.out.println("LookAndFeel: " + laf.getClassName());
 110         } catch (ClassNotFoundException | InstantiationException |
 111                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 112             throw new RuntimeException(e);
 113         }
 114     }
 115 }