|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2024, 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.Button; |
| 25 | +import java.awt.Checkbox; |
| 26 | +import java.awt.Component; |
| 27 | +import java.awt.Container; |
| 28 | +import java.awt.Event; |
| 29 | +import java.awt.FileDialog; |
| 30 | +import java.awt.Frame; |
| 31 | +import java.awt.GridBagConstraints; |
| 32 | +import java.awt.GridBagLayout; |
| 33 | +import java.awt.Label; |
| 34 | +import java.awt.Panel; |
| 35 | +import java.awt.TextField; |
| 36 | +import java.io.File; |
| 37 | +import java.io.FilenameFilter; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @bug 4293697 4416433 4417139 4409600 |
| 42 | + * @summary Test to verify that user filter always gets called on changing the |
| 43 | + * directory in FileDialog |
| 44 | + * @library /java/awt/regtesthelpers |
| 45 | + * @build PassFailJFrame |
| 46 | + * @run main/manual FileDialogUserFilterTest |
| 47 | + */ |
| 48 | + |
| 49 | +public class FileDialogUserFilterTest { |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + String INSTRUCTIONS = """ |
| 52 | + 1. Enter a mask into the <filter> field, a directory into |
| 53 | + the <directory> field (or leave the default values). |
| 54 | + 2. Then click the <Load> button, file dialog will appear. |
| 55 | + Output of the user filter will be shown in the output |
| 56 | + area. Enter several different directories to the file dialog |
| 57 | + via double-clicking on the directory list. The output |
| 58 | + area should show some filtering output on each directory |
| 59 | + change. If any output was only given on dialog startup, |
| 60 | + the test is FAILED. |
| 61 | + 3. Look at the list of files accepted by the filter. |
| 62 | + If some files do not match the filter, |
| 63 | + the test is FAILED. |
| 64 | + 4. Open dialog with an empty filter. |
| 65 | + Enter some directories with a lot of files (like /usr/bin). |
| 66 | + If dialog crashes the test is FAILED. |
| 67 | + Enter the directory that contain files and other directories. |
| 68 | + If the directories are shown in the files box along with files |
| 69 | + then the test is FAILED. |
| 70 | + 5. Click in checkbox 'do not use filter', make it checked. |
| 71 | + Open dialog, enter the directory with some files. |
| 72 | + If no files is shown in the File list box (while you are sure |
| 73 | + there are some files there) the test is FAILED |
| 74 | + Otherwise it is PASSED." |
| 75 | + """; |
| 76 | + |
| 77 | + PassFailJFrame.builder() |
| 78 | + .title("Test Instructions") |
| 79 | + .instructions(INSTRUCTIONS) |
| 80 | + .rows((int) INSTRUCTIONS.lines().count() + 2) |
| 81 | + .columns(35) |
| 82 | + .testUI(new DialogFilterTest()) |
| 83 | + .build() |
| 84 | + .awaitAndCheck(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class DialogFilterTest extends Frame implements FilenameFilter { |
| 89 | + FileDialog fd; |
| 90 | + static TextField tfDirectory = new TextField(); |
| 91 | + static TextField tfFile = new TextField(); |
| 92 | + static TextField tfFilter = new TextField(); |
| 93 | + static Checkbox useFilterCheck = new Checkbox("do not use filter"); |
| 94 | + |
| 95 | + public DialogFilterTest() { |
| 96 | + setTitle("File Dialog User Filter test"); |
| 97 | + add("North", new Button("Load")); |
| 98 | + Panel p = new Panel(); |
| 99 | + p.setLayout(new GridBagLayout()); |
| 100 | + addRow(p, new Label("directory:", Label.RIGHT), tfDirectory); |
| 101 | + addRow(p, new Label("file:", Label.RIGHT), tfFile); |
| 102 | + addRow(p, new Label("filter:", Label.RIGHT), tfFilter); |
| 103 | + addRow(p, new Label(""), useFilterCheck); |
| 104 | + tfFilter.setText(".java"); |
| 105 | + tfDirectory.setText("."); |
| 106 | + add("Center", p); |
| 107 | + setSize(300, 200); |
| 108 | + } |
| 109 | + |
| 110 | + static void addRow(Container cont, Component c1, Component c2) { |
| 111 | + GridBagLayout gbl = (GridBagLayout) cont.getLayout(); |
| 112 | + GridBagConstraints c = new GridBagConstraints(); |
| 113 | + c.fill = GridBagConstraints.BOTH; |
| 114 | + cont.add(c1); |
| 115 | + gbl.setConstraints(c1, c); |
| 116 | + |
| 117 | + c.gridwidth = GridBagConstraints.REMAINDER; |
| 118 | + c.weightx = 1.0; |
| 119 | + cont.add(c2); |
| 120 | + gbl.setConstraints(c2, c); |
| 121 | + } |
| 122 | + |
| 123 | + public boolean accept(File dir, String name) { |
| 124 | + System.out.println("File " + dir + " String " + name); |
| 125 | + if (fd.getMode() == FileDialog.LOAD) { |
| 126 | + return name.lastIndexOf(tfFilter.getText()) > 0; |
| 127 | + } |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + public boolean action(Event evt, Object what) { |
| 132 | + boolean load = "Load".equals(what); |
| 133 | + |
| 134 | + if (load || "Save".equals(what)) { |
| 135 | + fd = new FileDialog(new Frame(), null, |
| 136 | + load ? FileDialog.LOAD : FileDialog.SAVE); |
| 137 | + fd.setDirectory(tfDirectory.getText()); |
| 138 | + fd.setFile(tfFile.getText()); |
| 139 | + if (!useFilterCheck.getState()) { |
| 140 | + fd.setFilenameFilter(this); |
| 141 | + } |
| 142 | + fd.setVisible(true); |
| 143 | + tfDirectory.setText(fd.getDirectory()); |
| 144 | + tfFile.setText(fd.getFile()); |
| 145 | + |
| 146 | + return true; |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | +} |
0 commit comments