下面列出了怎么用sun.awt.shell.ShellFolder的API类实例代码及写法,或者点击链接到github查看源代码。
public static boolean canWrite(final File f, final JFileChooser chooser) {
// Return false for non FileSystem files or if file doesn't exist.
if (!f.exists()) {
return false;
}
try {
if (f instanceof ShellFolder) {
return f.canWrite();
} else {
if (usesShellFolder(chooser)) {
try {
return ShellFolder.getShellFolder(f).canWrite();
} catch (FileNotFoundException ex) {
// File doesn't exist
return false;
}
} else {
// Ordinary file
return f.canWrite();
}
}
} catch (SecurityException e) {
return false;
}
}
@Override
public Object createValue(UIDefaults table) {
if (icon == null) {
Image image = (Image)ShellFolder.get(nativeImageName);
if (image != null) {
icon = new ImageIconUIResource(image);
}
}
if (icon == null && fallbackName != null) {
UIDefaults.LazyValue fallback = (UIDefaults.LazyValue)
SwingUtilities2.makeIcon(WindowsLookAndFeel.class,
BasicLookAndFeel.class, fallbackName);
icon = (Icon) fallback.createValue(table);
}
return icon;
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {
File f = (File) list.getModel().getElementAt(index);
try {
// Strip trailing ".."
f = ShellFolder.getNormalizedFile(f);
} catch (IOException ex) {
// That's ok, we'll use f as is
}
if (getFileChooser().isTraversable(f)) {
list.clearSelection();
if (getFileChooser().getCurrentDirectory().equals(f)){
rescanCurrentDirectory(getFileChooser());
} else {
getFileChooser().setCurrentDirectory(f);
}
} else {
getFileChooser().approveSelection();
}
}
}
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {
File f = (File) list.getModel().getElementAt(index);
try {
// Strip trailing ".."
f = ShellFolder.getNormalizedFile(f);
} catch (IOException ex) {
// That's ok, we'll use f as is
}
if (getFileChooser().isTraversable(f)) {
list.clearSelection();
if (getFileChooser().getCurrentDirectory().equals(f)){
rescanCurrentDirectory(getFileChooser());
} else {
getFileChooser().setCurrentDirectory(f);
}
} else {
getFileChooser().approveSelection();
}
}
}
}
public void mouseClicked(MouseEvent evt) {
// Note: we can't depend on evt.getSource() because of backward
// compatibility
if (list != null &&
SwingUtilities.isLeftMouseButton(evt) &&
(evt.getClickCount()%2 == 0)) {
int index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());
if (index >= 0) {
File f = (File)list.getModel().getElementAt(index);
try {
// Strip trailing ".."
f = ShellFolder.getNormalizedFile(f);
} catch (IOException ex) {
// That's ok, we'll use f as is
}
if(getFileChooser().isTraversable(f)) {
list.clearSelection();
changeDirectory(f);
} else {
getFileChooser().approveSelection();
}
}
}
}
public static void main(String[] args) {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test was skipped because it is sensible only for Windows.");
return;
}
for (String key : KEYS) {
Image image = (Image) ShellFolder.get(key);
if (image == null) {
throw new RuntimeException("The image '" + key + "' not found.");
}
if (image != ShellFolder.get(key)) {
throw new RuntimeException("The image '" + key + "' is not cached.");
}
}
System.out.println("The test passed.");
}
/**
* On Windows, a file can appear in multiple folders, other than its
* parent directory in the filesystem. Folder could for example be the
* "Desktop" folder which is not the same as file.getParentFile().
*
* @param folder a <code>File</code> object representing a directory or special folder
* @param file a <code>File</code> object
* @return <code>true</code> if <code>folder</code> is a directory or special folder and contains <code>file</code>.
* @since 1.4
*/
public boolean isParent(File folder, File file) {
if (folder == null || file == null) {
return false;
} else if (folder instanceof ShellFolder) {
File parent = file.getParentFile();
if (parent != null && parent.equals(folder)) {
return true;
}
File[] children = getFiles(folder, false);
for (File child : children) {
if (file.equals(child)) {
return true;
}
}
return false;
} else {
return folder.equals(file.getParentFile());
}
}
public static void main(String[] args) {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test was skipped because it is sensible only for Windows.");
return;
}
for (String key : KEYS) {
Image image = (Image) ShellFolder.get(key);
if (image == null) {
throw new RuntimeException("The image '" + key + "' not found.");
}
if (image != ShellFolder.get(key)) {
throw new RuntimeException("The image '" + key + "' is not cached.");
}
}
System.out.println("The test passed.");
}
public void actionPerformed(ActionEvent e) {
if (isDirectorySelected()) {
File dir = getDirectory();
try {
// Strip trailing ".."
if (dir != null) {
dir = ShellFolder.getNormalizedFile(dir);
}
} catch (IOException ex) {
// Ok, use f as is
}
if (getFileChooser().getCurrentDirectory().equals(dir)) {
directoryList.clearSelection();
fileList.clearSelection();
ListSelectionModel sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
rescanCurrentDirectory(getFileChooser());
return;
}
}
super.actionPerformed(e);
}
/**
* Icon for a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays a CD-ROM icon.
*
* The default implementation gets information from the ShellFolder class.
*
* @param f a <code>File</code> object
* @return an icon as it would be displayed by a native file chooser
* @see JFileChooser#getIcon
* @since 1.4
*/
public Icon getSystemIcon(File f) {
if (f == null) {
return null;
}
ShellFolder sf;
try {
sf = getShellFolder(f);
} catch (FileNotFoundException e) {
return null;
}
Image img = sf.getIcon(false);
if (img != null) {
return new ImageIcon(img, sf.getFolderType());
} else {
return UIManager.getIcon(f.isDirectory() ? "FileView.directoryIcon" : "FileView.fileIcon");
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows, skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
File[] files = (File[]) ShellFolder.get("fileChooserComboBoxFolders");
for (File file : files) {
if (file instanceof ShellFolder && ((ShellFolder) file).isLink()) {
throw new RuntimeException("Link shouldn't be in FileChooser combobox, " + file.getPath());
}
}
}
});
}
public void mouseClicked(MouseEvent evt) {
// Note: we can't depend on evt.getSource() because of backward
// compatibility
if (list != null &&
SwingUtilities.isLeftMouseButton(evt) &&
(evt.getClickCount()%2 == 0)) {
int index = SwingUtilities2.loc2IndexFileList(list, evt.getPoint());
if (index >= 0) {
File f = (File)list.getModel().getElementAt(index);
try {
// Strip trailing ".."
f = ShellFolder.getNormalizedFile(f);
} catch (IOException ex) {
// That's ok, we'll use f as is
}
if(getFileChooser().isTraversable(f)) {
list.clearSelection();
changeDirectory(f);
} else {
getFileChooser().approveSelection();
}
}
}
}
public static void main(String[] args) {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test was skipped because it is sensible only for Windows.");
return;
}
for (String key : KEYS) {
Image image = (Image) ShellFolder.get(key);
if (image == null) {
throw new RuntimeException("The image '" + key + "' not found.");
}
if (image != ShellFolder.get(key)) {
throw new RuntimeException("The image '" + key + "' is not cached.");
}
}
System.out.println("The test passed.");
}
public void actionPerformed(ActionEvent e) {
if (isDirectorySelected()) {
File dir = getDirectory();
try {
// Strip trailing ".."
if (dir != null) {
dir = ShellFolder.getNormalizedFile(dir);
}
} catch (IOException ex) {
// Ok, use f as is
}
if (getFileChooser().getCurrentDirectory().equals(dir)) {
directoryList.clearSelection();
fileList.clearSelection();
ListSelectionModel sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
rescanCurrentDirectory(getFileChooser());
return;
}
}
super.actionPerformed(e);
}
public void actionPerformed(ActionEvent e) {
if (isDirectorySelected()) {
File dir = getDirectory();
try {
// Strip trailing ".."
if (dir != null) {
dir = ShellFolder.getNormalizedFile(dir);
}
} catch (IOException ex) {
// Ok, use f as is
}
if (getFileChooser().getCurrentDirectory().equals(dir)) {
directoryList.clearSelection();
fileList.clearSelection();
ListSelectionModel sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
rescanCurrentDirectory(getFileChooser());
return;
}
}
super.actionPerformed(e);
}
public static void main(String[] args) {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test was skipped because it is sensible only for Windows.");
return;
}
for (String key : KEYS) {
Image image = (Image) ShellFolder.get(key);
if (image == null) {
throw new RuntimeException("The image '" + key + "' not found.");
}
if (image != ShellFolder.get(key)) {
throw new RuntimeException("The image '" + key + "' is not cached.");
}
}
System.out.println("The test passed.");
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows, skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
File[] files = (File[]) ShellFolder.get("fileChooserComboBoxFolders");
for (File file : files) {
if (file instanceof ShellFolder && ((ShellFolder) file).isLink()) {
throw new RuntimeException("Link shouldn't be in FileChooser combobox, " + file.getPath());
}
}
}
});
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
if (index >= 0) {
File f = (File) list.getModel().getElementAt(index);
try {
// Strip trailing ".."
f = ShellFolder.getNormalizedFile(f);
} catch (IOException ex) {
// That's ok, we'll use f as is
}
if (getFileChooser().isTraversable(f)) {
list.clearSelection();
if (getFileChooser().getCurrentDirectory().equals(f)){
rescanCurrentDirectory(getFileChooser());
} else {
getFileChooser().setCurrentDirectory(f);
}
} else {
getFileChooser().approveSelection();
}
}
}
}
public void actionPerformed(ActionEvent e) {
if (isDirectorySelected()) {
File dir = getDirectory();
try {
// Strip trailing ".."
if (dir != null) {
dir = ShellFolder.getNormalizedFile(dir);
}
} catch (IOException ex) {
// Ok, use f as is
}
if (getFileChooser().getCurrentDirectory().equals(dir)) {
directoryList.clearSelection();
fileList.clearSelection();
ListSelectionModel sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
rescanCurrentDirectory(getFileChooser());
return;
}
}
super.actionPerformed(e);
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows, skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
File[] files = (File[]) ShellFolder.get("fileChooserComboBoxFolders");
for (File file : files) {
if (file instanceof ShellFolder && ((ShellFolder) file).isLink()) {
throw new RuntimeException("Link shouldn't be in FileChooser combobox, " + file.getPath());
}
}
}
});
}
public void actionPerformed(ActionEvent e) {
if (isDirectorySelected()) {
File dir = getDirectory();
try {
// Strip trailing ".."
if (dir != null) {
dir = ShellFolder.getNormalizedFile(dir);
}
} catch (IOException ex) {
// Ok, use f as is
}
if (getFileChooser().getCurrentDirectory().equals(dir)) {
directoryList.clearSelection();
fileList.clearSelection();
ListSelectionModel sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
rescanCurrentDirectory(getFileChooser());
return;
}
}
super.actionPerformed(e);
}
public static void main(String[] args) {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test was skipped because it is sensible only for Windows.");
return;
}
for (String key : KEYS) {
Image image = (Image) ShellFolder.get(key);
if (image == null) {
throw new RuntimeException("The image '" + key + "' not found.");
}
if (image != ShellFolder.get(key)) {
throw new RuntimeException("The image '" + key + "' is not cached.");
}
}
System.out.println("The test passed.");
}
protected Object getFileColumnValue(final File f, final int col) {
if (col == COLUMN_SIZE) {
return f.isDirectory() ? null : f.length();
}
return (col == COLUMN_FILENAME)
? f // always return the file itself for the 1st column
: ShellFolder.getFolderColumnValue(f, columnMap[col]);
}
private MyThread(int delay, String link) {
this.delay = delay;
ShellFolder linkFolder;
try {
linkFolder = ShellFolder.getShellFolder(new File(link));
} catch (FileNotFoundException e) {
e.printStackTrace();
linkFolder = null;
}
this.link = linkFolder;
}
public Object createValue(UIDefaults table) {
if (nativeImage != null) {
Image image = (Image)ShellFolder.get(nativeImage);
if (image != null) {
return new ImageIcon(image);
}
}
return SwingUtilities2.makeIcon(getClass(),
WindowsLookAndFeel.class,
resource);
}
private ShellFolder getWin32Folder(String getterName) {
try {
Class win32ShellFolderManager2 = Class.forName("sun.awt.shell.Win32ShellFolderManager2");
Method method = win32ShellFolderManager2.getDeclaredMethod(getterName);
method.setAccessible(true);
return (ShellFolder) method.invoke(null);
} catch (Exception e) {
fail("Cannot call '" + getterName + "' in the Win32ShellFolderManager2 class", e);
return null;
}
}
protected void doDirectoryChanged(PropertyChangeEvent e) {
directoryList.clearSelection();
ListSelectionModel sm = directoryList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
fileList.clearSelection();
sm = fileList.getSelectionModel();
if (sm instanceof DefaultListSelectionModel) {
((DefaultListSelectionModel)sm).moveLeadSelectionIndex(0);
sm.setAnchorSelectionIndex(0);
}
File currentDirectory = getFileChooser().getCurrentDirectory();
if (currentDirectory != null) {
try {
setDirectoryName(ShellFolder.getNormalizedFile((File)e.getNewValue()).getPath());
} catch (IOException ioe) {
setDirectoryName(((File)e.getNewValue()).getAbsolutePath());
}
if ((getFileChooser().getFileSelectionMode() == JFileChooser.DIRECTORIES_ONLY) && !getFileChooser().isMultiSelectionEnabled()) {
setFileName(pathField.getText());
}
directoryComboBoxModel.addItem(currentDirectory);
directoryListModel.directoryChanged();
}
super.doDirectoryChanged(e);
}
public Object createValue(UIDefaults table) {
if (nativeImage != null) {
Image image = (Image)ShellFolder.get(nativeImage);
if (image != null) {
return new ImageIcon(image);
}
}
return SwingUtilities2.makeIcon(getClass(),
WindowsLookAndFeel.class,
resource);
}
/**
* Adds the directory to the model and sets it to be selected,
* additionally clears out the previous selected directory and
* the paths leading up to it, if any.
*/
private void addItem(File directory) {
if (directory == null) {
return;
}
int oldSize = directories.size();
directories.clear();
if (oldSize > 0) {
fireIntervalRemoved(this, 0, oldSize);
}
// Get the canonical (full) path. This has the side
// benefit of removing extraneous chars from the path,
// for example /foo/bar/ becomes /foo/bar
File canonical;
try {
canonical = fsv.createFileObject(ShellFolder.getNormalizedFile(directory).getPath());
} catch (IOException e) {
// Maybe drive is not ready. Can't abort here.
canonical = directory;
}
// create File instances of each directory leading up to the top
File f = canonical;
do {
directories.add(f);
} while ((f = f.getParentFile()) != null);
int newSize = directories.size();
if (newSize > 0) {
fireIntervalAdded(this, 0, newSize);
}
setSelectedItem(canonical);
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows OS. Skipped.");
return;
}
// Init toolkit because it shouldn't be interrupted while initialization
Toolkit.getDefaultToolkit();
// Init the sun.awt.shell.Win32ShellFolderManager2.drives field
ShellFolder.get("fileChooserComboBoxFolders");
// To get NPE the path must obey the following rules:
// path.length() == 3 && path.charAt(1) == ':'
final File tempFile = new File("c:\\");
for (int i = 0; i < 10000; i++) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final Thread thread = new Thread() {
public void run() {
countDownLatch.countDown();
ShellFolder.isFileSystemRoot(tempFile);
}
};
thread.start();
countDownLatch.await();
thread.interrupt();
}
}