File chooser: show logical path in a path field

This commit is contained in:
Roman Shevchenko
2012-02-15 18:14:24 +01:00
parent 2220f79f2b
commit e9dca91401
6 changed files with 106 additions and 90 deletions
@@ -18,6 +18,8 @@ package com.intellij.openapi.fileChooser;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.vfs.JarFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.StringBuilderSpinAllocator;
import org.jetbrains.annotations.Nullable;
import java.io.File;
@@ -25,10 +27,10 @@ public class FileElement {
private final VirtualFile myFile;
private final String myName;
private Boolean myIsHidden;
private String myPath;
private FileElement myParent;
public FileElement(VirtualFile file, String name) {
public FileElement(@Nullable VirtualFile file, String name) {
myFile = file;
myName = name;
}
@@ -41,10 +43,41 @@ public class FileElement {
return myParent;
}
public final VirtualFile getFile() {
return myFile;
}
public final String getName() {
return myName;
}
public final String getPath() {
if (myPath == null) {
final StringBuilder sb = StringBuilderSpinAllocator.alloc();
try {
FileElement element = this;
while (element != null) {
sb.insert(0, element.myName);
element = element.myParent;
if (element != null && element.myParent != null /*&& !element.myName.endsWith(File.separator)*/) {
sb.insert(0, File.separator);
}
}
myPath = sb.toString();
}
finally {
StringBuilderSpinAllocator.dispose(sb);
}
}
return myPath;
}
@Override
public int hashCode() {
return myFile == null ? 0 : myFile.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FileElement) {
if (((FileElement)obj).myFile == myFile) return true;
@@ -52,6 +85,11 @@ public class FileElement {
return false;
}
@Override
public final String toString() {
return myName != null ? myName : "";
}
public final boolean isHidden() {
if (myIsHidden == null) {
myIsHidden = Boolean.valueOf(isFileHidden(getFile()));
@@ -59,30 +97,19 @@ public class FileElement {
return myIsHidden.booleanValue();
}
public final VirtualFile getFile() {
return myFile;
public final boolean isArchive() {
return isArchive(getFile());
}
public final String toString() {
if (myName != null) {
return myName;
}
else if (myFile != null) {
return myFile.getName();
}
return "";
public static boolean isFileHidden(@Nullable final VirtualFile file) {
if (file == null || !file.isValid()) return false;
if (!file.isInLocalFileSystem()) return false;
final File ioFile = new File(file.getPath().replace('/', File.separatorChar));
return ioFile.isHidden() && ioFile.getParent() != null; // Under Windows logical driver files (e.g C:\) are hidden.
}
public final String getName() { return myName; }
public static boolean isFileHidden(VirtualFile virtualFile) {
if (virtualFile == null || !virtualFile.isValid()) return false;
if (!virtualFile.isInLocalFileSystem()) return false;
File file = new File(virtualFile.getPath().replace('/', File.separatorChar));
return file.getParent() != null && file.isHidden(); // Under Windows logical driver files (e.g C:\) are hidden.
}
public static boolean isArchive(VirtualFile file) {
public static boolean isArchive(@Nullable final VirtualFile file) {
if (file == null) return false;
if (isArchiveFileSystem(file) && file.getParent() == null) return true;
return !file.isDirectory() &&
file.getFileType() == FileTypes.ARCHIVE &&
@@ -92,8 +119,4 @@ public class FileElement {
private static boolean isArchiveFileSystem(VirtualFile file) {
return file.getFileSystem() instanceof JarFileSystem;
}
public boolean isArchive() {
return isArchive(getFile());
}
}
@@ -17,6 +17,7 @@ package com.intellij.openapi.fileChooser;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.DataKey;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;
@@ -25,6 +26,7 @@ import java.util.List;
public interface FileSystemTree extends Disposable {
DataKey<FileSystemTree> DATA_KEY = DataKey.create("FileSystemTree");
Key<String> PATH_KEY = Key.create("FileSystemTree.Path");
JTree getTree();
@@ -485,24 +485,12 @@ public class FileChooserDialogImpl extends DialogWrapper implements FileChooserD
String text = "";
if (selection.size() > 0) {
final VirtualFile vFile = selection.get(0);
if (vFile.isInLocalFileSystem()) {
text = vFile.getPresentableUrl();
}
else {
text = vFile.getUrl();
}
text = getFilePath(selection.get(0));
}
else {
List<VirtualFile> roots = myChooserDescriptor.getRoots();
if (!myFileSystemTree.getTree().isRootVisible() && roots.size() == 1) {
VirtualFile vFile = roots.get(0);
if (vFile.isInLocalFileSystem()) {
text = vFile.getPresentableUrl();
}
else {
text = vFile.getUrl();
}
text = getFilePath(roots.get(0));
}
}
@@ -514,6 +502,15 @@ public class FileChooserDialogImpl extends DialogWrapper implements FileChooserD
});
}
private static String getFilePath(final VirtualFile file) {
final String path = file.getUserData(FileSystemTree.PATH_KEY);
if (path != null) {
return path;
}
return file.isInLocalFileSystem() ? file.getPresentableUrl() : file.getUrl();
}
private void updateTreeFromPath(final String text) {
if (!isToShowTextField()) return;
if (myPathTextField.isPathUpdating()) return;
@@ -422,6 +422,7 @@ public class FileSystemTreeImpl implements FileSystemTree {
final FileElement element = ((FileNodeDescriptor)object).getElement();
final VirtualFile file = element.getFile();
if (file != null) {
file.putUserData(PATH_KEY, element.getPath());
selection.add(file);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,57 +16,54 @@
package com.intellij.openapi.fileChooser.ex;
import com.intellij.openapi.fileChooser.FileElement;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class RootFileElement extends FileElement {
private final VirtualFile[] myFiles;
private final boolean myShowFileSystemRoots;
private Object[] myChildren;
public RootFileElement(VirtualFile[] files, String name, boolean showFileSystemRoots) {
super(files.length == 1 ? files[0] : null, name);
myFiles = files;
myShowFileSystemRoots = showFileSystemRoots;
public RootFileElement(@NotNull final VirtualFile[] files, final String name, final boolean showFileSystemRoots) {
super(null, name);
myFiles = files.length == 0 && showFileSystemRoots ? getFileSystemRoots() : files;
}
public Object[] getChildren() {
if (myFiles.length <= 1 && myShowFileSystemRoots) {
return getFileSystemRoots();
}
if (myChildren == null) {
myChildren = createFileElementArray();
final List<FileElement> children = new ArrayList<FileElement>();
for (final VirtualFile file : myFiles) {
if (file != null) {
children.add(new FileElement(file, file.getPresentableUrl()));
}
}
myChildren = ArrayUtil.toObjectArray(children);
}
return myChildren;
}
private Object[] createFileElementArray() {
final List<FileElement> roots = new ArrayList<FileElement>();
for (final VirtualFile file : myFiles) {
if (file != null) {
roots.add(new FileElement(file, file.getPresentableUrl()));
private static VirtualFile[] getFileSystemRoots() {
final LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
final Set<VirtualFile> roots = new HashSet<VirtualFile>();
final File[] ioRoots = File.listRoots();
if (ioRoots != null) {
for (final File root : ioRoots) {
final String path = FileUtil.toSystemIndependentName(root.getAbsolutePath());
final VirtualFile file = localFileSystem.findFileByPath(path);
if (file != null) {
roots.add(file);
}
}
}
return ArrayUtil.toObjectArray(roots);
}
private static Object[] getFileSystemRoots() {
File[] roots = File.listRoots();
LocalFileSystem localFileSystem = LocalFileSystem.getInstance();
HashSet<FileElement> rootChildren = new HashSet<FileElement>();
for (File root : roots) {
String path = root.getAbsolutePath();
path = path.replace(File.separatorChar, '/');
VirtualFile file = localFileSystem.findFileByPath(path);
if (file == null) continue;
rootChildren.add(new FileElement(file, file.getPresentableUrl()));
}
return ArrayUtil.toObjectArray(rootChildren);
return VfsUtil.toVirtualFileArray(roots);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,6 @@
* limitations under the License.
*/
/**
* @author Yura Cangea
*/
package com.intellij.openapi.fileChooser.impl;
import com.intellij.ide.util.PropertiesComponent;
@@ -38,25 +35,25 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.HashSet;
import java.util.List;
/**
* @author Yura Cangea
*/
public class FileTreeStructure extends AbstractTreeStructure {
private static final Logger LOG = Logger.getInstance("#com.intellij.chooser.FileTreeStructure");
private final RootFileElement myRootElement;
private final FileChooserDescriptor myChooserDescriptor;
private boolean myShownHiddens;
private boolean myShowHidden;
private final Project myProject;
public FileTreeStructure(Project project, FileChooserDescriptor chooserDescriptor) {
myProject = project;
List<VirtualFile> roots = chooserDescriptor.getRoots();
final VirtualFile[] rootFiles = VfsUtil.toVirtualFileArray(roots);
VirtualFile rootFile = rootFiles.length == 1 ? rootFiles[0] : null;
myRootElement = new RootFileElement(rootFiles, rootFile != null? rootFile.getPresentableUrl() : chooserDescriptor.getTitle(), chooserDescriptor.isShowFileSystemRoots());
final VirtualFile[] rootFiles = VfsUtil.toVirtualFileArray(chooserDescriptor.getRoots());
final String name = rootFiles.length == 1 && rootFiles[0] != null ? rootFiles[0].getPresentableUrl() : chooserDescriptor.getTitle();
myRootElement = new RootFileElement(rootFiles, name, chooserDescriptor.isShowFileSystemRoots());
myChooserDescriptor = chooserDescriptor;
String value = PropertiesComponent.getInstance().getValue("FileChooser.showHiddens");
myShownHiddens = Boolean.valueOf(value).booleanValue();
myShowHidden = PropertiesComponent.getInstance().getBoolean("FileChooser.showHiddens", false);
}
public boolean isToBuildChildrenInBackground(final Object element) {
@@ -64,11 +61,11 @@ public class FileTreeStructure extends AbstractTreeStructure {
}
public final boolean areHiddensShown() {
return myShownHiddens;
return myShowHidden;
}
public final void showHiddens(final boolean showHiddens) {
myShownHiddens = showHiddens;
myShowHidden = showHiddens;
}
public final Object getRootElement() {
@@ -115,7 +112,7 @@ public class FileTreeStructure extends AbstractTreeStructure {
HashSet<FileElement> childrenSet = new HashSet<FileElement>();
for (VirtualFile child : children) {
if (myChooserDescriptor.isFileVisible(child, myShownHiddens)) {
if (myChooserDescriptor.isFileVisible(child, myShowHidden)) {
final FileElement childElement = new FileElement(child, child.getName());
childElement.setParent(element);
childrenSet.add(childElement);
@@ -166,21 +163,20 @@ public class FileTreeStructure extends AbstractTreeStructure {
}
@Nullable
private VirtualFile getValidFile(FileElement element) {
private static VirtualFile getValidFile(FileElement element) {
if (element == null) return null;
final VirtualFile file = element.getFile();
return file != null && file.isValid() ? file : null;
}
public final void commit() {
}
public final void commit() { }
public final boolean hasSomethingToCommit() {
return false;
}
public final void dispose() {
PropertiesComponent.getInstance().setValue("FileChooser.showHiddens", Boolean.toString(myShownHiddens));
PropertiesComponent.getInstance().setValue("FileChooser.showHiddens", Boolean.toString(myShowHidden));
}
@NotNull