1 /* 2 * Copyright (c) 2016, 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 /* 25 * @test 26 * @bug 8146276 27 * @summary Right aligned toolbar component does not appear 28 * @run main NimbusGlueTest 29 */ 30 import java.awt.BorderLayout; 31 import java.awt.Component; 32 import java.awt.Dimension; 33 import javax.swing.Box; 34 import javax.swing.JButton; 35 import javax.swing.JFrame; 36 import javax.swing.JToolBar; 37 import javax.swing.SwingUtilities; 38 import javax.swing.UIManager; 39 import java.awt.Robot; 40 import javax.swing.UnsupportedLookAndFeelException; 41 42 public class NimbusGlueTest { 43 44 private static JFrame frame; 45 private static Robot robot; 46 private static volatile String errorMessage = ""; 47 private static JToolBar bar; 48 49 public static void main(String[] args) throws Exception { 50 robot = new Robot(); 51 UIManager.LookAndFeelInfo[] lookAndFeelArray 52 = UIManager.getInstalledLookAndFeels(); 53 for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) { 54 String lookAndFeelString = lookAndFeelItem.getClassName(); 55 if (tryLookAndFeel(lookAndFeelString)) { 56 createUI(); 57 performTest(); 58 robot.waitForIdle(); 59 } 60 } 61 if (!"".equals(errorMessage)) { 62 throw new RuntimeException(errorMessage); 63 } 64 } 65 66 private static boolean tryLookAndFeel(String lookAndFeelString) { 67 try { 68 UIManager.setLookAndFeel(lookAndFeelString); 69 return true; 70 } catch (UnsupportedLookAndFeelException | ClassNotFoundException | 71 InstantiationException | IllegalAccessException e) { 72 errorMessage += e.getMessage() + "\n"; 73 System.err.println("Caught Exception: " + e.getMessage()); 74 return false; 75 } 76 } 77 78 private static void performTest() throws Exception { 79 SwingUtilities.invokeAndWait(new Runnable() { 80 public void run() { 81 try { 82 int width = 0; 83 for (Component comp : bar.getComponents()) { 84 width += comp.getWidth(); 85 } 86 if (width > 600) { 87 errorMessage = "Test Failed"; 88 } 89 } finally { 90 frame.dispose(); 91 } 92 93 } 94 }); 95 } 96 97 private static void createUI() throws Exception { 98 SwingUtilities.invokeAndWait(new Runnable() { 99 public void run() { 100 frame = new JFrame(); 101 bar = new JToolBar(); 102 bar.add(createButton(1)); 103 bar.add(createButton(2)); 104 bar.add(Box.createHorizontalGlue()); 105 bar.add(createButton(3)); 106 frame.add(bar, BorderLayout.NORTH); 107 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 108 frame.setSize(600, 400); 109 frame.setVisible(true); 110 } 111 }); 112 } 113 114 private static JButton createButton(int id) { 115 JButton b = new JButton("B: " + id); 116 b.setPreferredSize(new Dimension(60, b.getPreferredSize().height)); 117 return b; 118 } 119 }