1/*
2 * Copyright (c) 2000, 2013, 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.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.awt.shell;
27
28import java.io.File;
29import java.io.FileNotFoundException;
30import java.util.concurrent.Callable;
31
32/**
33 * @author Michael Martak
34 * @since 1.4
35 */
36
37class ShellFolderManager {
38    /**
39     * Create a shell folder from a file.
40     * Override to return machine-dependent behavior.
41     */
42    public ShellFolder createShellFolder(File file) throws FileNotFoundException {
43        return new DefaultShellFolder(null, file);
44    }
45
46    /**
47     * @param key a {@code String}
48     *  "fileChooserDefaultFolder":
49     *    Returns a {@code File} - the default shellfolder for a new filechooser
50     *  "roots":
51     *    Returns a {@code File[]} - containing the root(s) of the displayable hierarchy
52     *  "fileChooserComboBoxFolders":
53     *    Returns a {@code File[]} - an array of shellfolders representing the list to
54     *    show by default in the file chooser's combobox
55     *   "fileChooserShortcutPanelFolders":
56     *    Returns a {@code File[]} - an array of shellfolders representing well-known
57     *    folders, such as Desktop, Documents, History, Network, Home, etc.
58     *    This is used in the shortcut panel of the filechooser on Windows 2000
59     *    and Windows Me.
60     *  "fileChooserIcon <icon>":
61     *    Returns an {@code Image} - icon can be ListView, DetailsView, UpFolder, NewFolder or
62     *    ViewMenu (Windows only).
63     *
64     * @return An Object matching the key string.
65     */
66    public Object get(String key) {
67        if (key.equals("fileChooserDefaultFolder")) {
68            // Return the default shellfolder for a new filechooser
69            File homeDir = new File(System.getProperty("user.home"));
70            try {
71                return createShellFolder(homeDir);
72            } catch (FileNotFoundException e) {
73                return homeDir;
74            }
75        } else if (key.equals("roots")) {
76            // The root(s) of the displayable hierarchy
77            return File.listRoots();
78        } else if (key.equals("fileChooserComboBoxFolders")) {
79            // Return an array of ShellFolders representing the list to
80            // show by default in the file chooser's combobox
81            return get("roots");
82        } else if (key.equals("fileChooserShortcutPanelFolders")) {
83            // Return an array of ShellFolders representing well-known
84            // folders, such as Desktop, Documents, History, Network, Home, etc.
85            // This is used in the shortcut panel of the filechooser on Windows 2000
86            // and Windows Me
87            return new File[] { (File)get("fileChooserDefaultFolder") };
88        }
89        return null;
90    }
91
92    /**
93     * Does {@code dir} represent a "computer" such as a node on the network, or
94     * "My Computer" on the desktop.
95     */
96    public boolean isComputerNode(File dir) {
97        return false;
98    }
99
100    public boolean isFileSystemRoot(File dir) {
101        if (dir instanceof ShellFolder && !((ShellFolder) dir).isFileSystem()) {
102            return false;
103        }
104        return (dir.getParentFile() == null);
105    }
106
107    protected ShellFolder.Invoker createInvoker() {
108        return new DirectInvoker();
109    }
110
111    private static class DirectInvoker implements ShellFolder.Invoker {
112        public <T> T invoke(Callable<T> task) throws Exception {
113            return task.call();
114        }
115    }
116}
117