Source for javax.swing.filechooser.FileSystemView

   1: /* FileSystemView.java --
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing.filechooser;
  39: 
  40: import java.io.File;
  41: import java.io.IOException;
  42: import java.util.ArrayList;
  43: import javax.swing.Icon;
  44: 
  45: 
  46: /**
  47:  * DOCUMENT ME!
  48:  */
  49: public abstract class FileSystemView
  50: {
  51:   /**
  52:    * DOCUMENT ME!
  53:    *
  54:    * @param dir DOCUMENT ME!
  55:    * @param filename DOCUMENT ME!
  56:    *
  57:    * @return DOCUMENT ME!
  58:    */
  59:   public File createFileObject(File dir, String filename)
  60:   {
  61:     return new File(dir, filename);
  62:   }
  63: 
  64:   /**
  65:    * DOCUMENT ME!
  66:    *
  67:    * @param path DOCUMENT ME!
  68:    *
  69:    * @return DOCUMENT ME!
  70:    */
  71:   public File createFileObject(String path)
  72:   {
  73:     return new File(path);
  74:   }
  75: 
  76:   /**
  77:    * DOCUMENT ME!
  78:    *
  79:    * @param f DOCUMENT ME!
  80:    *
  81:    * @return DOCUMENT ME!
  82:    */
  83:   protected File createFileSystemRoot(File f)
  84:   {
  85:     File[] roots = File.listRoots();
  86:     if (roots == null)
  87:       return null;
  88:     return roots[0];
  89:   }
  90: 
  91:   /**
  92:    * DOCUMENT ME!
  93:    *
  94:    * @param containingDir DOCUMENT ME!
  95:    *
  96:    * @return DOCUMENT ME!
  97:    *
  98:    * @throws IOException DOCUMENT ME!
  99:    */
 100:   public abstract File createNewFolder(File containingDir)
 101:                                 throws IOException;
 102: 
 103:   /**
 104:    * DOCUMENT ME!
 105:    *
 106:    * @param parent DOCUMENT ME!
 107:    * @param fileName DOCUMENT ME!
 108:    *
 109:    * @return DOCUMENT ME!
 110:    */
 111:   public File getChild(File parent, String fileName)
 112:   {
 113:     // FIXME: Handle the case when parent and child are special folders.
 114:     return new File(parent, fileName);
 115:   }
 116: 
 117:   /**
 118:    * DOCUMENT ME!
 119:    *
 120:    * @return DOCUMENT ME!
 121:    */
 122:   public File getDefaultDirectory()
 123:   {
 124:     return getHomeDirectory();
 125:   }
 126: 
 127:   /**
 128:    * DOCUMENT ME!
 129:    *
 130:    * @param dir DOCUMENT ME!
 131:    * @param useFileHiding DOCUMENT ME!
 132:    *
 133:    * @return DOCUMENT ME!
 134:    */
 135:   public File[] getFiles(File dir, boolean useFileHiding)
 136:   {
 137:     if (dir == null || dir.listFiles() == null)
 138:       return null;
 139:     File[] files = dir.listFiles();
 140:     if (! useFileHiding)
 141:       return files;
 142:     ArrayList trim = new ArrayList();
 143:     for (int i = 0; i < files.length; i++)
 144:       if (! files[i].isHidden())
 145:     trim.add(files[i]);
 146:     File[] value = (File[]) trim.toArray(new File[0]);
 147:     return value;
 148:   }
 149: 
 150:   /**
 151:    * DOCUMENT ME!
 152:    *
 153:    * @return DOCUMENT ME!
 154:    */
 155:   public static FileSystemView getFileSystemView()
 156:   {
 157:     if (File.separator.equals("/"))
 158:       return new UnixFileSystemView();
 159: 
 160:     // else if (File.Separator.equals("\"))
 161:     //    return new Win32FileSystemView();
 162:     // else 
 163:     //    return new GenericFileSystemView();
 164:     return null;
 165:   }
 166: 
 167:   /**
 168:    * DOCUMENT ME!
 169:    *
 170:    * @return DOCUMENT ME!
 171:    */
 172:   public File getHomeDirectory()
 173:   {
 174:     return createFileObject(System.getProperty("user.home"));
 175:   }
 176: 
 177:   /**
 178:    * DOCUMENT ME!
 179:    *
 180:    * @param f DOCUMENT ME!
 181:    *
 182:    * @return DOCUMENT ME!
 183:    */
 184:   public File getParentDirectory(File f)
 185:   {
 186:     if (f == null)
 187:       return null;
 188:     return f.getParentFile();
 189:   }
 190: 
 191:   /**
 192:    * DOCUMENT ME!
 193:    *
 194:    * @return DOCUMENT ME!
 195:    */
 196:   public File[] getRoots()
 197:   {
 198:     // subclass
 199:     return null;
 200:   }
 201: 
 202:   /**
 203:    * DOCUMENT ME!
 204:    *
 205:    * @param f DOCUMENT ME!
 206:    *
 207:    * @return DOCUMENT ME!
 208:    */
 209:   public String getSystemDisplayName(File f)
 210:   {
 211:     return null;
 212:   }
 213: 
 214:   /**
 215:    * DOCUMENT ME!
 216:    *
 217:    * @param f DOCUMENT ME!
 218:    *
 219:    * @return DOCUMENT ME!
 220:    */
 221:   public Icon getSystemIcon(File f)
 222:   {
 223:     return null;
 224:   }
 225: 
 226:   /**
 227:    * DOCUMENT ME!
 228:    *
 229:    * @param f DOCUMENT ME!
 230:    *
 231:    * @return DOCUMENT ME!
 232:    */
 233:   public String getSystemTypeDescription(File f)
 234:   {
 235:     return null;
 236:   }
 237: 
 238:   /**
 239:    * DOCUMENT ME!
 240:    *
 241:    * @param dir DOCUMENT ME!
 242:    *
 243:    * @return DOCUMENT ME!
 244:    */
 245:   public boolean isComputerNode(File dir)
 246:   {
 247:     return false;
 248:   }
 249: 
 250:   /**
 251:    * DOCUMENT ME!
 252:    *
 253:    * @param dir DOCUMENT ME!
 254:    *
 255:    * @return DOCUMENT ME!
 256:    */
 257:   public boolean isDrive(File dir)
 258:   {
 259:     return false;
 260:   }
 261: 
 262:   /**
 263:    * DOCUMENT ME!
 264:    *
 265:    * @param f DOCUMENT ME!
 266:    *
 267:    * @return DOCUMENT ME!
 268:    */
 269:   public boolean isFileSystem(File f)
 270:   {
 271:     return (f.isFile() || f.isDirectory());
 272:   }
 273: 
 274:   /**
 275:    * DOCUMENT ME!
 276:    *
 277:    * @param dir DOCUMENT ME!
 278:    *
 279:    * @return DOCUMENT ME!
 280:    */
 281:   public boolean isFileSystemRoot(File dir)
 282:   {
 283:     File[] roots = File.listRoots();
 284:     if (roots == null || dir == null)
 285:       return false;
 286:     String filename = dir.getAbsolutePath();
 287:     for (int i = 0; i < roots.length; i++)
 288:       if (roots[i].getAbsolutePath().equals(filename))
 289:     return true;
 290:     return false;
 291:   }
 292: 
 293:   /**
 294:    * DOCUMENT ME!
 295:    *
 296:    * @param dir DOCUMENT ME!
 297:    *
 298:    * @return DOCUMENT ME!
 299:    */
 300:   public boolean isFloppyDrive(File dir)
 301:   {
 302:     return false;
 303:   }
 304: 
 305:   /**
 306:    * DOCUMENT ME!
 307:    *
 308:    * @param f DOCUMENT ME!
 309:    *
 310:    * @return DOCUMENT ME!
 311:    */
 312:   public boolean isHiddenFile(File f)
 313:   {
 314:     return f.isHidden();
 315:   }
 316: 
 317:   /**
 318:    * DOCUMENT ME!
 319:    *
 320:    * @param folder DOCUMENT ME!
 321:    * @param file DOCUMENT ME!
 322:    *
 323:    * @return DOCUMENT ME!
 324:    */
 325:   public boolean isParent(File folder, File file)
 326:   {
 327:     File parent = file.getParentFile();
 328:     if (parent == null)
 329:       return false;
 330:     return folder.equals(parent);
 331:   }
 332: 
 333:   /**
 334:    * DOCUMENT ME!
 335:    *
 336:    * @param f DOCUMENT ME!
 337:    *
 338:    * @return DOCUMENT ME!
 339:    */
 340:   public boolean isRoot(File f)
 341:   {
 342:     // These are not file system roots.
 343:     return false;
 344:   }
 345: 
 346:   /**
 347:    * DOCUMENT ME!
 348:    *
 349:    * @param f DOCUMENT ME!
 350:    *
 351:    * @return DOCUMENT ME!
 352:    */
 353:   public Boolean isTraversable(File f)
 354:   {
 355:     // Tested. A directory where the user has no permission to rwx is still
 356:     // traversable. (No files are listed when you traverse the directory)
 357:     // My best guess is that as long as it's a directory, the file is
 358:     // traversable.
 359:     return new Boolean(f.isDirectory());
 360:   }
 361: }