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 /* 25 * @test 26 * @key headful 27 * @bug 8016665 28 * @summary verifies different behaviour of JFileChooser changing orientation 29 * @run main JFileChooserOrientation 30 */ 31 import java.awt.Color; 32 import java.awt.ComponentOrientation; 33 import java.awt.GridBagConstraints; 34 import java.awt.GridBagLayout; 35 import java.awt.event.ActionEvent; 36 import java.awt.event.ActionListener; 37 import java.util.logging.Level; 38 import java.util.logging.Logger; 39 import javax.swing.BorderFactory; 40 import javax.swing.JButton; 41 import javax.swing.JComboBox; 42 import javax.swing.JFileChooser; 43 import javax.swing.JFrame; 44 import javax.swing.JLabel; 45 import javax.swing.JPanel; 46 import javax.swing.JTextArea; 47 import javax.swing.SwingUtilities; 48 import javax.swing.UIManager; 49 import javax.swing.UnsupportedLookAndFeelException; 50 51 public class JFileChooserOrientation { 52 53 private static JFrame frame; 54 private static GridBagLayout layout; 55 private static JPanel panel; 56 private static JPanel lookAndFeelPanel; 57 private static JPanel orientationPanel; 58 private static JPanel passFailPanel; 59 private static JTextArea instructionsTextArea; 60 private static JLabel lookAndFeelLabel; 61 private static JLabel orientationLabel; 62 private static JComboBox lookAndFeelComboBox; 63 private static JComboBox orientationComboBox; 64 65 private static JButton fileChooserButton; 66 private static JButton passButton; 67 private static JButton failButton; 68 private static JFileChooser openChooser; 69 private static UIManager.LookAndFeelInfo[] lookAndFeelArray; 70 71 private static final String orientationLTR = " Left to Right"; 72 private static final String orientationRTL = " Right to Left"; 73 private static final String fileChooserString = "Show File Chooser"; 74 75 public static void main(String[] args) throws Exception { 76 createManualTestUI(); 77 } 78 79 private static void createManualTestUI() throws Exception { 80 SwingUtilities.invokeAndWait(new Runnable() { 81 @Override 82 public void run() { 83 layout = new GridBagLayout(); 84 GridBagConstraints gbc = new GridBagConstraints(); 85 panel = new JPanel(layout); 86 gbc.fill = GridBagConstraints.HORIZONTAL; 87 gbc.gridx = 0; 88 gbc.gridy = 0; 89 instructionsTextArea = new JTextArea(); 90 String instructions 91 = "1) Select Look and feel from combobox" 92 + "\n2) Select component orientation" 93 + "\n3) Click on \"Show File Chooser\"" 94 + "\n4) Check if orientation is as selected" 95 + "\n5) Press \"Cancel\" on the File Chooser Dialog" 96 + "\n\n Perform steps 1- 4 for all LAFs & orientations" 97 + "\n If all are correct press Pass or else press Fail"; 98 instructionsTextArea.setText(instructions); 99 instructionsTextArea.setBorder( 100 BorderFactory.createLineBorder(Color.black)); 101 panel.add(instructionsTextArea, gbc); 102 103 lookAndFeelPanel = new JPanel(); 104 lookAndFeelPanel.setBorder( 105 BorderFactory.createLineBorder(Color.black)); 106 lookAndFeelLabel = new JLabel("Look And Feel: "); 107 gbc.gridx = 0; 108 gbc.gridy = 0; 109 lookAndFeelPanel.add(lookAndFeelLabel, gbc); 110 111 lookAndFeelComboBox = new JComboBox(); 112 lookAndFeelArray = UIManager.getInstalledLookAndFeels(); 113 for (UIManager.LookAndFeelInfo lookAndFeelItem 114 : lookAndFeelArray) { 115 lookAndFeelComboBox.addItem(lookAndFeelItem.getClassName()); 116 } 117 gbc.gridx = 1; 118 gbc.gridy = 0; 119 lookAndFeelPanel.add(lookAndFeelComboBox, gbc); 120 gbc.gridx = 0; 121 gbc.gridy = 1; 122 panel.add(lookAndFeelPanel, gbc); 123 124 orientationPanel = new JPanel(); 125 orientationPanel.setBorder( 126 BorderFactory.createLineBorder(Color.black)); 127 orientationLabel = new JLabel("Orientation: "); 128 gbc.gridx = 0; 129 gbc.gridy = 0; 130 orientationPanel.add(orientationLabel, gbc); 131 132 orientationComboBox = new JComboBox(); 133 orientationComboBox.addItem(orientationLTR); 134 orientationComboBox.addItem(orientationRTL); 135 gbc.gridx = 1; 136 gbc.gridy = 0; 137 orientationPanel.add(orientationComboBox, gbc); 138 gbc.gridx = 0; 139 gbc.gridy = 2; 140 panel.add(orientationPanel, gbc); 141 142 fileChooserButton = new JButton(fileChooserString); 143 fileChooserButton.setActionCommand(fileChooserString); 144 145 fileChooserButton.addActionListener(new ActionListener() { 146 @Override 147 public void actionPerformed(ActionEvent e) { 148 149 try { 150 showFileChooser(); 151 } catch (Exception ex) { 152 Logger.getLogger(JFileChooserOrientation.class 153 .getName()).log(Level.SEVERE, null, ex); 154 } 155 156 } 157 }); 158 gbc.gridx = 0; 159 gbc.gridy = 3; 160 panel.add(fileChooserButton, gbc); 161 162 passFailPanel = new JPanel(); 163 passFailPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 164 passButton = new JButton(" Pass "); 165 passButton.addActionListener(new ActionListener() { 166 @Override 167 public void actionPerformed(ActionEvent e) { 168 try { 169 pass(); 170 } catch (Exception ex) { 171 Logger.getLogger(JFileChooserOrientation.class 172 .getName()).log(Level.SEVERE, null, ex); 173 } 174 } 175 }); 176 gbc.gridx = 0; 177 gbc.gridy = 0; 178 passFailPanel.add(passButton, gbc); 179 failButton = new JButton(" Fail "); 180 failButton.addActionListener(new ActionListener() { 181 @Override 182 public void actionPerformed(ActionEvent e) { 183 try { 184 fail(); 185 } catch (Exception ex) { 186 Logger.getLogger(JFileChooserOrientation.class 187 .getName()).log(Level.SEVERE, null, ex); 188 } 189 } 190 }); 191 gbc.gridx = 1; 192 gbc.gridy = 0; 193 passFailPanel.add(failButton, gbc); 194 gbc.gridx = 0; 195 gbc.gridy = 4; 196 panel.add(passFailPanel, gbc); 197 frame = new JFrame(); 198 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 199 frame.setContentPane(panel); 200 frame.pack(); 201 frame.setVisible(true); 202 } 203 }); 204 } 205 206 private static void pass() throws Exception 207 { 208 209 frame.dispose(); 210 211 } 212 213 private static void fail() throws Exception 214 { 215 216 frame.dispose(); 217 System.err.println(lookAndFeelComboBox.getSelectedItem().toString() 218 + " : Incorrect Orientation"); 219 } 220 221 private static void showFileChooser() throws Exception { 222 if (tryLookAndFeel(lookAndFeelComboBox.getSelectedItem().toString())) { 223 224 openChooser = new JFileChooser(); 225 226 ComponentOrientation orientation 227 = ComponentOrientation.UNKNOWN; 228 229 switch (orientationComboBox.getSelectedItem().toString()) { 230 case orientationLTR: 231 orientation = ComponentOrientation.LEFT_TO_RIGHT; 232 break; 233 case orientationRTL: 234 orientation = ComponentOrientation.RIGHT_TO_LEFT; 235 break; 236 } 237 openChooser.setComponentOrientation(orientation); 238 openChooser.showOpenDialog(frame); 239 240 } 241 } 242 private static boolean tryLookAndFeel(String lookAndFeelString) 243 throws Exception { 244 try { 245 UIManager.setLookAndFeel( 246 lookAndFeelString); 247 } catch (UnsupportedLookAndFeelException 248 | ClassNotFoundException 249 | InstantiationException 250 | IllegalAccessException e) { 251 return false; 252 } 253 return true; 254 } 255 }