Source for javax.swing.plaf.basic.BasicDirectoryModel

   1: /* BasicDirectoryModel.java --
   2:    Copyright (C) 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.plaf.basic;
  39: 
  40: import java.beans.PropertyChangeEvent;
  41: import java.beans.PropertyChangeListener;
  42: import java.io.File;
  43: import java.util.Collections;
  44: import java.util.Comparator;
  45: import java.util.Enumeration;
  46: import java.util.Vector;
  47: import javax.swing.AbstractListModel;
  48: import javax.swing.JFileChooser;
  49: import javax.swing.event.ListDataEvent;
  50: import javax.swing.filechooser.FileSystemView;
  51: 
  52: 
  53: /**
  54:  * DOCUMENT ME!
  55:  */
  56: public class BasicDirectoryModel extends AbstractListModel
  57:   implements PropertyChangeListener
  58: {
  59:   /** DOCUMENT ME! */
  60:   private Vector contents;
  61: 
  62:   /** DOCUMENT ME! */
  63:   private int directories;
  64: 
  65:   /** DOCUMENT ME! */
  66:   private int listingMode;
  67: 
  68:   /** DOCUMENT ME! */
  69:   private JFileChooser filechooser;
  70: 
  71:   /** DOCUMENT ME! */
  72:   private Comparator comparator = new Comparator()
  73:     {
  74:       public int compare(Object o1, Object o2)
  75:       {
  76:     if (lt((File) o1, (File) o2))
  77:       return -1;
  78:     else
  79:       return 1;
  80:       }
  81:     };
  82: 
  83:   /**
  84:    * Creates a new BasicDirectoryModel object.
  85:    *
  86:    * @param filechooser DOCUMENT ME!
  87:    */
  88:   public BasicDirectoryModel(JFileChooser filechooser)
  89:   {
  90:     this.filechooser = filechooser;
  91:     filechooser.addPropertyChangeListener(this);
  92:     listingMode = filechooser.getFileSelectionMode();
  93:     contents = new Vector();
  94:   }
  95: 
  96:   /**
  97:    * DOCUMENT ME!
  98:    *
  99:    * @param o DOCUMENT ME!
 100:    *
 101:    * @return DOCUMENT ME!
 102:    */
 103:   public boolean contains(Object o)
 104:   {
 105:     return contents.contains(o);
 106:   }
 107: 
 108:   /**
 109:    * DOCUMENT ME!
 110:    */
 111:   public void fireContentsChanged()
 112:   {
 113:     fireContentsChanged(this, 0, getSize() - 1);
 114:   }
 115: 
 116:   /**
 117:    * DOCUMENT ME!
 118:    *
 119:    * @return DOCUMENT ME!
 120:    */
 121:   public Vector getDirectories()
 122:   {
 123:     Vector tmp = new Vector();
 124:     for (int i = 0; i < directories; i++)
 125:       tmp.add(contents.get(i));
 126:     return tmp;
 127:   }
 128: 
 129:   /**
 130:    * DOCUMENT ME!
 131:    *
 132:    * @param index DOCUMENT ME!
 133:    *
 134:    * @return DOCUMENT ME!
 135:    */
 136:   public Object getElementAt(int index)
 137:   {
 138:     if (index > getSize() - 1)
 139:       return null;
 140:     if (listingMode == JFileChooser.FILES_ONLY)
 141:       return contents.get(directories + index);
 142:     else
 143:       return contents.elementAt(index);
 144:   }
 145: 
 146:   /**
 147:    * DOCUMENT ME!
 148:    *
 149:    * @return DOCUMENT ME!
 150:    */
 151:   public Vector getFiles()
 152:   {
 153:     Vector tmp = new Vector();
 154:     for (int i = directories; i < getSize(); i++)
 155:       tmp.add(contents.get(i));
 156:     return tmp;
 157:   }
 158: 
 159:   /**
 160:    * DOCUMENT ME!
 161:    *
 162:    * @return DOCUMENT ME!
 163:    */
 164:   public int getSize()
 165:   {
 166:     if (listingMode == JFileChooser.DIRECTORIES_ONLY)
 167:       return directories;
 168:     else if (listingMode == JFileChooser.FILES_ONLY)
 169:       return contents.size() - directories;
 170:     return contents.size();
 171:   }
 172: 
 173:   /**
 174:    * DOCUMENT ME!
 175:    *
 176:    * @param o DOCUMENT ME!
 177:    *
 178:    * @return DOCUMENT ME!
 179:    */
 180:   public int indexOf(Object o)
 181:   {
 182:     if (listingMode == JFileChooser.FILES_ONLY)
 183:       return contents.indexOf(o) - directories;
 184:     return contents.indexOf(o);
 185:   }
 186: 
 187:   /**
 188:    * DOCUMENT ME!
 189:    *
 190:    * @param e DOCUMENT ME!
 191:    */
 192:   public void intervalAdded(ListDataEvent e)
 193:   {
 194:     // obsoleted
 195:   }
 196: 
 197:   /**
 198:    * DOCUMENT ME!
 199:    *
 200:    * @param e DOCUMENT ME!
 201:    */
 202:   public void intervalRemoved(ListDataEvent e)
 203:   {
 204:     // obsoleted
 205:   }
 206: 
 207:   /**
 208:    * DOCUMENT ME!
 209:    */
 210:   public void invalidateFileCache()
 211:   {
 212:     // obsoleted
 213:   }
 214: 
 215:   /**
 216:    * DOCUMENT ME!
 217:    *
 218:    * @param a DOCUMENT ME!
 219:    * @param b DOCUMENT ME!
 220:    *
 221:    * @return DOCUMENT ME!
 222:    */
 223:   protected boolean lt(File a, File b)
 224:   {
 225:     boolean aTrav = filechooser.isTraversable(a);
 226:     boolean bTrav = filechooser.isTraversable(b);
 227: 
 228:     if (aTrav == bTrav)
 229:       {
 230:     String aname = a.getName().toLowerCase();
 231:     String bname = b.getName().toLowerCase();
 232:     return ((aname.compareTo(bname) < 0) ? true : false);
 233:       }
 234:     else
 235:       {
 236:     if (aTrav)
 237:       return true;
 238:     else
 239:       return false;
 240:       }
 241:   }
 242: 
 243:   /**
 244:    * DOCUMENT ME!
 245:    *
 246:    * @param e DOCUMENT ME!
 247:    */
 248:   public void propertyChange(PropertyChangeEvent e)
 249:   {
 250:     if (e.getPropertyName().equals(JFileChooser.FILE_SELECTION_MODE_CHANGED_PROPERTY))
 251:       listingMode = filechooser.getFileSelectionMode();
 252:   }
 253: 
 254:   /**
 255:    * DOCUMENT ME!
 256:    *
 257:    * @param oldFile DOCUMENT ME!
 258:    * @param newFile DOCUMENT ME!
 259:    *
 260:    * @return DOCUMENT ME!
 261:    */
 262:   public boolean renameFile(File oldFile, File newFile)
 263:   {
 264:     // FIXME: implement
 265:     return false;
 266:   }
 267: 
 268:   /**
 269:    * DOCUMENT ME!
 270:    *
 271:    * @param v DOCUMENT ME!
 272:    */
 273:   protected void sort(Vector v)
 274:   {
 275:     Collections.sort(v, comparator);
 276:     Enumeration e = Collections.enumeration(v);
 277:     Vector tmp = new Vector();
 278:     for (; e.hasMoreElements();)
 279:       tmp.add(e.nextElement());
 280: 
 281:     contents = tmp;
 282:   }
 283: 
 284:   /**
 285:    * DOCUMENT ME!
 286:    */
 287:   public void validateFileCache()
 288:   {
 289:     contents.clear();
 290:     directories = 0;
 291:     FileSystemView fsv = filechooser.getFileSystemView();
 292:     File[] list = fsv.getFiles(filechooser.getCurrentDirectory(),
 293:                                filechooser.isFileHidingEnabled());
 294: 
 295:     if (list == null)
 296:       return;
 297: 
 298:     for (int i = 0; i < list.length; i++)
 299:       {
 300:     if (list[i] == null)
 301:       continue;
 302:     if (filechooser.accept(list[i]))
 303:       {
 304:         contents.add(list[i]);
 305:         if (filechooser.isTraversable(list[i]))
 306:           directories++;
 307:       }
 308:       }
 309:     sort(contents);
 310:     filechooser.revalidate();
 311:     filechooser.repaint();
 312:   }
 313: }