sort filenames in path completion (IDEADEV-41220)
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / fileChooser / ex / LocalFsFinder.java
blob452da61b3db4ebf3e50d47e36984a9f1d5edf64e
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.fileChooser.ex;
18 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
19 import com.intellij.openapi.util.io.FileUtil;
20 import com.intellij.openapi.util.text.StringUtil;
21 import com.intellij.openapi.vfs.LocalFileSystem;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.openapi.vfs.VirtualFileManager;
24 import org.jetbrains.annotations.NotNull;
26 import java.io.File;
27 import java.io.FileFilter;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
33 public class LocalFsFinder implements FileLookup.Finder, FileLookup {
35 public LookupFile find(@NotNull final String path) {
36 final VirtualFile byUrl = VirtualFileManager.getInstance().findFileByUrl(path);
37 if (byUrl != null) return getLookupFile(path, byUrl);
39 String toFind = normalize(path);
40 if (toFind.length() == 0) {
41 File[] roots = File.listRoots();
42 if (roots.length > 0) {
43 toFind = roots[0].getAbsolutePath();
46 final File file = new File(toFind);
47 return file.isAbsolute() ? getLookupFile(path, LocalFileSystem.getInstance().findFileByIoFile(file)) : null;
50 private LookupFile getLookupFile(final String path, final VirtualFile vFile) {
51 return vFile != null ? new VfsFile(this, vFile) : new IoFile(new File(path));
54 public String normalize(@NotNull final String path) {
55 final File file = new File(path);
56 if (file.isAbsolute()) return file.getAbsolutePath();
58 return new File(System.getProperty("user.home"), path).getAbsolutePath();
61 public String getSeparator() {
62 return File.separator;
65 public static class FileChooserFilter implements LookupFilter {
67 private final FileChooserDescriptor myDescriptor;
68 private final boolean myShowHidden;
70 public FileChooserFilter(final FileChooserDescriptor descriptor, boolean showHidden) {
71 myDescriptor = descriptor;
72 myShowHidden = showHidden;
75 public boolean isAccepted(final LookupFile file) {
76 VirtualFile vFile = ((VfsFile)file).getFile();
77 if (vFile == null) return false;
78 return myDescriptor.isFileVisible(vFile, myShowHidden);
82 public static class VfsFile implements LookupFile {
83 private final VirtualFile myFile;
84 private final LocalFsFinder myFinder;
86 private String myMacro;
88 public VfsFile(LocalFsFinder finder, final VirtualFile file) {
89 myFinder = finder;
90 myFile = file;
93 public String getName() {
94 if (myFile.getParent() == null && myFile.getName().length() == 0) return "/";
95 return myFile.getName();
98 public boolean isDirectory() {
99 return myFile != null ? myFile.isDirectory() : false;
102 public void setMacro(final String macro) {
103 myMacro = macro;
106 public String getMacro() {
107 return myMacro;
110 public LookupFile getParent() {
111 return myFile != null && myFile.getParent() != null ? new VfsFile(myFinder, myFile.getParent()) : null;
114 public String getAbsolutePath() {
115 if (myFile.getParent() == null && myFile.getName().length() == 0) return "/";
116 return myFile.getPresentableUrl();
119 public List<LookupFile> getChildren(final LookupFilter filter) {
120 List<LookupFile> result = new ArrayList<LookupFile>();
121 if (myFile == null) return result;
123 VirtualFile[] kids = myFile.getChildren();
124 for (VirtualFile each : kids) {
125 LookupFile eachFile = myFinder.getLookupFile(each.getPath(), each);
126 if (eachFile != null && filter.isAccepted(eachFile)) {
127 result.add(eachFile);
130 Collections.sort(result, new Comparator<LookupFile>() {
131 public int compare(LookupFile o1, LookupFile o2) {
132 return FileUtil.comparePaths(o1.getName(), o2.getName());
136 return result;
139 public VirtualFile getFile() {
140 return myFile;
143 public boolean exists() {
144 return myFile.exists();
147 public boolean equals(final Object o) {
148 if (this == o) return true;
149 if (o == null || getClass() != o.getClass()) return false;
151 final VfsFile vfsFile = (VfsFile)o;
153 if (myFile != null ? !myFile.equals(vfsFile.myFile) : vfsFile.myFile != null) return false;
155 return true;
158 public int hashCode() {
159 return (myFile != null ? myFile.hashCode() : 0);
163 public static class IoFile extends VfsFile {
164 private final File myIoFile;
166 public IoFile(final File ioFile) {
167 super(null, null);
168 myIoFile = ioFile;
171 public String getName() {
172 return myIoFile.getName();
175 public boolean isDirectory() {
176 return myIoFile != null ? myIoFile.isDirectory() : false;
179 public LookupFile getParent() {
180 return myIoFile != null && myIoFile.getParentFile() != null ? new IoFile(myIoFile.getParentFile()) : null;
183 public String getAbsolutePath() {
184 return myIoFile.getAbsolutePath();
187 public List<LookupFile> getChildren(final LookupFilter filter) {
188 List<LookupFile> result = new ArrayList<LookupFile>();
189 File[] files = myIoFile.listFiles(new FileFilter() {
190 public boolean accept(final File pathname) {
191 return filter.isAccepted(new IoFile(pathname));
194 if (files == null) return result;
196 for (File each : files) {
197 result.add(new IoFile(each));
199 Collections.sort(result, new Comparator<LookupFile>() {
200 public int compare(LookupFile o1, LookupFile o2) {
201 return FileUtil.comparePaths(o1.getName(), o2.getName());
205 return result;
208 public boolean exists() {
209 return myIoFile.exists();
212 public boolean equals(final Object o) {
213 if (this == o) return true;
214 if (o == null || getClass() != o.getClass()) return false;
216 final IoFile ioFile = (IoFile)o;
218 if (myIoFile != null ? !myIoFile.equals(ioFile.myIoFile) : ioFile.myIoFile != null) return false;
220 return true;
223 public int hashCode() {
224 return (myIoFile != null ? myIoFile.hashCode() : 0);