Path: blob/master/test/jdk/javax/swing/JFileChooser/8016665/JFileChooserOrientation.java
41155 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test25* @key headful26* @bug 801666527* @summary verifies different behaviour of JFileChooser changing orientation28* @run main JFileChooserOrientation29*/30import java.awt.Color;31import java.awt.ComponentOrientation;32import java.awt.GridBagConstraints;33import java.awt.GridBagLayout;34import java.awt.event.ActionEvent;35import java.awt.event.ActionListener;36import java.util.logging.Level;37import java.util.logging.Logger;38import javax.swing.BorderFactory;39import javax.swing.JButton;40import javax.swing.JComboBox;41import javax.swing.JFileChooser;42import javax.swing.JFrame;43import javax.swing.JLabel;44import javax.swing.JPanel;45import javax.swing.JTextArea;46import javax.swing.SwingUtilities;47import javax.swing.UIManager;48import javax.swing.UnsupportedLookAndFeelException;4950public class JFileChooserOrientation {5152private static JFrame frame;53private static GridBagLayout layout;54private static JPanel panel;55private static JPanel lookAndFeelPanel;56private static JPanel orientationPanel;57private static JPanel passFailPanel;58private static JTextArea instructionsTextArea;59private static JLabel lookAndFeelLabel;60private static JLabel orientationLabel;61private static JComboBox lookAndFeelComboBox;62private static JComboBox orientationComboBox;6364private static JButton fileChooserButton;65private static JButton passButton;66private static JButton failButton;67private static JFileChooser openChooser;68private static UIManager.LookAndFeelInfo[] lookAndFeelArray;6970private static final String orientationLTR = " Left to Right";71private static final String orientationRTL = " Right to Left";72private static final String fileChooserString = "Show File Chooser";7374public static void main(String[] args) throws Exception {75createManualTestUI();76}7778private static void createManualTestUI() throws Exception {79SwingUtilities.invokeAndWait(new Runnable() {80@Override81public void run() {82layout = new GridBagLayout();83GridBagConstraints gbc = new GridBagConstraints();84panel = new JPanel(layout);85gbc.fill = GridBagConstraints.HORIZONTAL;86gbc.gridx = 0;87gbc.gridy = 0;88instructionsTextArea = new JTextArea();89String instructions90= "1) Select Look and feel from combobox"91+ "\n2) Select component orientation"92+ "\n3) Click on \"Show File Chooser\""93+ "\n4) Check if orientation is as selected"94+ "\n5) Press \"Cancel\" on the File Chooser Dialog"95+ "\n\n Perform steps 1- 4 for all LAFs & orientations"96+ "\n If all are correct press Pass or else press Fail";97instructionsTextArea.setText(instructions);98instructionsTextArea.setBorder(99BorderFactory.createLineBorder(Color.black));100panel.add(instructionsTextArea, gbc);101102lookAndFeelPanel = new JPanel();103lookAndFeelPanel.setBorder(104BorderFactory.createLineBorder(Color.black));105lookAndFeelLabel = new JLabel("Look And Feel: ");106gbc.gridx = 0;107gbc.gridy = 0;108lookAndFeelPanel.add(lookAndFeelLabel, gbc);109110lookAndFeelComboBox = new JComboBox();111lookAndFeelArray = UIManager.getInstalledLookAndFeels();112for (UIManager.LookAndFeelInfo lookAndFeelItem113: lookAndFeelArray) {114lookAndFeelComboBox.addItem(lookAndFeelItem.getClassName());115}116gbc.gridx = 1;117gbc.gridy = 0;118lookAndFeelPanel.add(lookAndFeelComboBox, gbc);119gbc.gridx = 0;120gbc.gridy = 1;121panel.add(lookAndFeelPanel, gbc);122123orientationPanel = new JPanel();124orientationPanel.setBorder(125BorderFactory.createLineBorder(Color.black));126orientationLabel = new JLabel("Orientation: ");127gbc.gridx = 0;128gbc.gridy = 0;129orientationPanel.add(orientationLabel, gbc);130131orientationComboBox = new JComboBox();132orientationComboBox.addItem(orientationLTR);133orientationComboBox.addItem(orientationRTL);134gbc.gridx = 1;135gbc.gridy = 0;136orientationPanel.add(orientationComboBox, gbc);137gbc.gridx = 0;138gbc.gridy = 2;139panel.add(orientationPanel, gbc);140141fileChooserButton = new JButton(fileChooserString);142fileChooserButton.setActionCommand(fileChooserString);143144fileChooserButton.addActionListener(new ActionListener() {145@Override146public void actionPerformed(ActionEvent e) {147148try {149showFileChooser();150} catch (Exception ex) {151Logger.getLogger(JFileChooserOrientation.class152.getName()).log(Level.SEVERE, null, ex);153}154155}156});157gbc.gridx = 0;158gbc.gridy = 3;159panel.add(fileChooserButton, gbc);160161passFailPanel = new JPanel();162passFailPanel.setBorder(BorderFactory.createLineBorder(Color.black));163passButton = new JButton(" Pass ");164passButton.addActionListener(new ActionListener() {165@Override166public void actionPerformed(ActionEvent e) {167try {168pass();169} catch (Exception ex) {170Logger.getLogger(JFileChooserOrientation.class171.getName()).log(Level.SEVERE, null, ex);172}173}174});175gbc.gridx = 0;176gbc.gridy = 0;177passFailPanel.add(passButton, gbc);178failButton = new JButton(" Fail ");179failButton.addActionListener(new ActionListener() {180@Override181public void actionPerformed(ActionEvent e) {182try {183fail();184} catch (Exception ex) {185Logger.getLogger(JFileChooserOrientation.class186.getName()).log(Level.SEVERE, null, ex);187}188}189});190gbc.gridx = 1;191gbc.gridy = 0;192passFailPanel.add(failButton, gbc);193gbc.gridx = 0;194gbc.gridy = 4;195panel.add(passFailPanel, gbc);196frame = new JFrame();197frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);198frame.setContentPane(panel);199frame.pack();200frame.setVisible(true);201}202});203}204205private static void pass() throws Exception206{207208frame.dispose();209210}211212private static void fail() throws Exception213{214215frame.dispose();216System.err.println(lookAndFeelComboBox.getSelectedItem().toString()217+ " : Incorrect Orientation");218}219220private static void showFileChooser() throws Exception {221if (tryLookAndFeel(lookAndFeelComboBox.getSelectedItem().toString())) {222223openChooser = new JFileChooser();224225ComponentOrientation orientation226= ComponentOrientation.UNKNOWN;227228switch (orientationComboBox.getSelectedItem().toString()) {229case orientationLTR:230orientation = ComponentOrientation.LEFT_TO_RIGHT;231break;232case orientationRTL:233orientation = ComponentOrientation.RIGHT_TO_LEFT;234break;235}236openChooser.setComponentOrientation(orientation);237openChooser.showOpenDialog(frame);238239}240}241private static boolean tryLookAndFeel(String lookAndFeelString)242throws Exception {243try {244UIManager.setLookAndFeel(245lookAndFeelString);246} catch (UnsupportedLookAndFeelException247| ClassNotFoundException248| InstantiationException249| IllegalAccessException e) {250return false;251}252return true;253}254}255256257