1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* Copyright (c) 2022, 2023, 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.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Arrays;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/*
* @test
* @bug 8281966
* @key headful
* @requires (os.family == "windows")
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @summary Test to check if the absolute path of Symbolic Link folder
* is valid on ValueChanged property listener.
* @run main/manual FileChooserSymLinkTest
*/
public class FileChooserSymLinkTest {
private static final String INSTRUCTIONS = """
<html><body>
Instructions to Test:
<ol>
<li>Open an elevated <i>Command Prompt</i>.
<li>Paste the following commands:
<pre><code>cd /d C:\\
mkdir FileChooserTest
cd FileChooserTest
mkdir target
mklink /d link target</code></pre>
<li>Navigate to <code>C:\\FileChooserTest</code> in
the <code>JFileChooser</code>.
<li>Perform testing in single- and multi-selection modes:
<ul style="margin-bottom: 0px">
<li><strong>Single-selection:</strong>
<ol>
<li>Ensure <b>Enable multi-selection</b> is cleared
(the default state).
<li>Click <code>link</code> directory,
the absolute path of the symbolic
link should be displayed.<br>
If it's <code>null</code>, click <b>Fail</b>.
<li>Click <code>target</code> directory,
its absolute path should be displayed.
</ol>
<li><strong>Multi-selection:</strong>
<ol>
<li>Select <b>Enable multi-selection</b>.
<li>Click <code>link</code>,
<li>Press <kbd>Ctrl</kbd> and
then click <code>target</code>.
<li>Both should be selected and
their absolute paths should be displayed.
<li>If <code>link</code> can't be selected or
if its absolute path is <code>null</code>,
click <b>Fail</b>.
</ol>
</ul>
<p>If <code>link</code> can be selected in both
single- and multi-selection modes, click <b>Pass</b>.</p>
<li>When done with testing, paste the following commands to
remove the <code>FileChooserTest</code> directory:
<pre><code>cd \\
rmdir /s /q C:\\FileChooserTest</code></pre>
or use File Explorer to clean it up.
</ol>
""";
static JFrame frame;
static JFileChooser jfc;
static JPanel panel;
static JTextArea pathList;
static JCheckBox multiSelection;
public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.rows(35)
.columns(50)
.testUI(FileChooserSymLinkTest::createTestUI)
.build()
.awaitAndCheck();
}
private static JFrame createTestUI() {
frame = new JFrame("JFileChooser Symbolic Link test");
panel = new JPanel(new BorderLayout());
multiSelection = new JCheckBox("Enable Multi-Selection");
pathList = new JTextArea(10, 50);
jfc = new JFileChooser(new File("C:\\"));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel.add(multiSelection, BorderLayout.EAST);
panel.add(new JScrollPane(pathList), BorderLayout.WEST);
jfc.setDialogType(JFileChooser.CUSTOM_DIALOG);
jfc.setControlButtonsAreShown(false);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setMultiSelectionEnabled(multiSelection.isSelected());
pathList.append("Path List\n");
multiSelection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
jfc.setMultiSelectionEnabled(((JCheckBox)source).isSelected());
}
});
jfc.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
String msg = null;
if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
msg = "Absolute Path : " + evt.getNewValue();
} else if (JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(evt.getPropertyName())) {
msg = "Selected Files : " + Arrays.toString((File[]) evt.getNewValue());
}
if (msg != null) {
System.out.println(msg);
pathList.append(msg + "\n");
}
}
});
frame.add(panel, BorderLayout.NORTH);
frame.add(jfc, BorderLayout.CENTER);
frame.pack();
return frame;
}
}
|