bug6798062.java revision 1088
1088N/A/*
1088N/A * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
1088N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1088N/A *
1088N/A * This code is free software; you can redistribute it and/or modify it
1088N/A * under the terms of the GNU General Public License version 2 only, as
1088N/A * published by the Free Software Foundation.
1088N/A *
1088N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1088N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1088N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1088N/A * version 2 for more details (a copy is included in the LICENSE file that
1088N/A * accompanied this code).
1088N/A *
1088N/A * You should have received a copy of the GNU General Public License version
1088N/A * 2 along with this work; if not, write to the Free Software Foundation,
1088N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1088N/A *
1088N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1088N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1088N/A * have any questions.
1088N/A */
1088N/A
1088N/A/* @test %W% %E%
1088N/A @bug 6798062
1088N/A @summary Memory Leak on using getFiles of FileSystemView
1088N/A @author Pavel Porvatov
1088N/A @run applet/manual=done bug6798062.html
1088N/A*/
1088N/A
1088N/Aimport sun.awt.shell.ShellFolder;
1088N/A
1088N/Aimport javax.swing.*;
1088N/Aimport java.awt.event.ActionEvent;
1088N/Aimport java.awt.event.ActionListener;
1088N/Aimport java.awt.*;
1088N/Aimport java.io.File;
1088N/Aimport java.io.FileNotFoundException;
1088N/A
1088N/Apublic class bug6798062 extends JApplet {
1088N/A
1088N/A private final JSlider slider = new JSlider(0, 100);
1088N/A
1088N/A private final JTextField tfLink = new JTextField();
1088N/A
1088N/A private final JButton btnStart = new JButton("Start");
1088N/A
1088N/A private final JButton btnStop = new JButton("Stop");
1088N/A
1088N/A private final JButton btnGC = new JButton("Run System.gc()");
1088N/A
1088N/A private ShellFolder folder;
1088N/A
1088N/A private Thread thread;
1088N/A
1088N/A public static void main(String[] args) {
1088N/A JFrame frame = new JFrame("bug6798062");
1088N/A
1088N/A frame.setSize(400, 300);
1088N/A frame.setLocationRelativeTo(null);
1088N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1088N/A frame.add(new bug6798062().initialize());
1088N/A
1088N/A frame.setVisible(true);
1088N/A }
1088N/A
1088N/A public void init() {
1088N/A add(initialize());
1088N/A }
1088N/A
1088N/A private JPanel initialize() {
1088N/A File file = new File("c:/");
1088N/A
1088N/A try {
1088N/A folder = ShellFolder.getShellFolder(file);
1088N/A } catch (FileNotFoundException e) {
1088N/A fail("Directory " + file.getPath() + " not found");
1088N/A }
1088N/A
1088N/A slider.setMajorTickSpacing(10);
1088N/A slider.setPaintTicks(true);
1088N/A slider.setPaintLabels(true);
1088N/A slider.setSnapToTicks(true);
1088N/A slider.setValue(10);
1088N/A
1088N/A btnStart.addActionListener(new ActionListener() {
1088N/A public void actionPerformed(ActionEvent e) {
1088N/A setEnabledState(false);
1088N/A
1088N/A thread = new MyThread(slider.getValue(), tfLink.getText());
1088N/A thread.start();
1088N/A }
1088N/A });
1088N/A
1088N/A btnStop.setEnabled(false);
1088N/A
1088N/A btnStop.addActionListener(new ActionListener() {
1088N/A public void actionPerformed(ActionEvent e) {
1088N/A thread.interrupt();
1088N/A thread = null;
1088N/A
1088N/A setEnabledState(true);
1088N/A }
1088N/A });
1088N/A
1088N/A btnGC.addActionListener(new ActionListener() {
1088N/A public void actionPerformed(ActionEvent e) {
1088N/A System.gc();
1088N/A }
1088N/A });
1088N/A
1088N/A setEnabledState(true);
1088N/A
1088N/A JPanel pnButtons = new JPanel();
1088N/A
1088N/A pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));
1088N/A
1088N/A pnButtons.add(btnStart);
1088N/A pnButtons.add(btnStop);
1088N/A pnButtons.add(btnGC);
1088N/A
1088N/A tfLink.setMaximumSize(new Dimension(300, 20));
1088N/A
1088N/A JPanel pnContent = new JPanel();
1088N/A
1088N/A pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));
1088N/A pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));
1088N/A pnContent.add(slider);
1088N/A pnContent.add(new JLabel("Provide link here:"));
1088N/A pnContent.add(tfLink);
1088N/A pnContent.add(pnButtons);
1088N/A
1088N/A return pnContent;
1088N/A }
1088N/A
1088N/A private void setEnabledState(boolean enabled) {
1088N/A slider.setEnabled(enabled);
1088N/A btnStart.setEnabled(enabled);
1088N/A btnStop.setEnabled(!enabled);
1088N/A }
1088N/A
1088N/A private static void fail(String msg) {
1088N/A throw new RuntimeException(msg);
1088N/A }
1088N/A
1088N/A private class MyThread extends Thread {
1088N/A private final int delay;
1088N/A
1088N/A private final ShellFolder link;
1088N/A
1088N/A private MyThread(int delay, String link) {
1088N/A this.delay = delay;
1088N/A
1088N/A ShellFolder linkFolder;
1088N/A
1088N/A try {
1088N/A linkFolder = ShellFolder.getShellFolder(new File(link));
1088N/A } catch (FileNotFoundException e) {
1088N/A e.printStackTrace();
1088N/A
1088N/A linkFolder = null;
1088N/A }
1088N/A
1088N/A this.link = linkFolder;
1088N/A }
1088N/A
1088N/A public void run() {
1088N/A while (!isInterrupted()) {
1088N/A folder.listFiles();
1088N/A if (link != null) {
1088N/A try {
1088N/A link.getLinkLocation();
1088N/A } catch (FileNotFoundException e) {
1088N/A e.printStackTrace();
1088N/A }
1088N/A }
1088N/A
1088N/A if (delay > 0) {
1088N/A try {
1088N/A Thread.sleep(delay);
1088N/A } catch (InterruptedException e1) {
1088N/A // The thread was interrupted
1088N/A return;
1088N/A }
1088N/A }
1088N/A }
1088N/A }
1088N/A }
1088N/A}