Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

Folder.java

Go to the documentation of this file.
00001 // -*- Java -*-
00002 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00011 
00014 package pkgMcj;
00015 
00016 import java.io.*;
00017 import java.util.*;
00018 import java.lang.reflect.*;
00019 
00021 public class Folder{
00022 
00023    private Mcj mcj;
00024    private File [] fileNames   = null;
00025    private String parentName   = File.pathSeparator;
00026    private String folderName   = File.pathSeparator;
00027    private int numEntries      = 0; 
00028    private int anchor          = 0; 
00029    private boolean filesLoaded = false;
00030    private int savedAnchor     = 0;
00031    private DirFilter dirFilter = new DirFilter();
00032    private IsFileFilter isFileFilter = new IsFileFilter();
00033 
00034    private ArrayList strList = new ArrayList();
00035 
00036 
00037    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00041    public Folder( Mcj parent,String pathName){
00042       mcj = parent;
00043       checkPathName( pathName );
00044       folderName = pathName;
00045       anchor = 0;
00046       numEntries = 0;
00047       filesLoaded = false;
00048       if( folderName == null ){
00049          folderName = File.pathSeparator;  // root?
00050       }
00051       try {
00052          File f1 = new File( folderName );
00053          folderName = f1.getAbsolutePath();
00054          parentName = f1.getParent();
00055          strList.clear();
00056          if( parentName == null ) { 
00057             parentName = folderName; 
00058          }
00059       }
00060       catch( Exception e ) {
00061          folderName = File.pathSeparator;  // root?
00062          filesLoaded = false;
00063       }
00064    }
00065 
00066 
00067    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00071    public boolean loadNames(){
00072       ArrayList dirSort = new ArrayList();
00073       ArrayList fileSort = new ArrayList();
00074 
00075       dirSort.clear();
00076       fileSort.clear();
00077       anchor = 0;
00078       numEntries = 0;
00079       filesLoaded = false;
00080       if( folderName == null ){
00081          mcj.logFile.putString( "Folder.loadNames has null name.. ");
00082          mcj.errorMsg( "Folder.loadNames has null name.. ");
00083          return filesLoaded;
00084       }
00085       try {
00086          File f1 = new File( folderName );
00087          fileNames = f1.listFiles( dirFilter); // see dirFilter bug
00088          numEntries = fileNames.length;
00089          filesLoaded = true;
00090          strList.clear();
00091          dirSort.clear();
00092          fileSort.clear();
00093          for (int i=0; i < numEntries; i++) {
00094             File f2 = fileNames[i];
00095             if( f2.isDirectory() ){
00096                dirSort.add( f2.getName() ); // list subdirectories
00097             }
00098             else{
00099                fileSort.add( f2.getName() );  // list files
00100             }
00101          }
00102          Collections.sort(dirSort);  // sort subdirectories
00103          Collections.sort(fileSort); // sort files
00104           
00105          // add sorted directories to the list
00106          ListIterator i = dirSort.listIterator(0);
00107          while ( i.hasNext() ) {
00108             strList.add( (String)i.next());
00109          }
00110          // followed by the sorted files
00111          ListIterator j = fileSort.listIterator(0);
00112          while ( j.hasNext() ) {
00113             strList.add( (String)j.next());
00114          }
00115 
00116       }
00117       catch( Exception e ) {
00118          filesLoaded = false;
00119          mcj.goodbye( e.getMessage() );
00120       }
00121       
00122       return filesLoaded;
00123    }
00124 
00125    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00130    public int getNumEntries(){
00131      return this.numEntries; 
00132    } // getNumEntries()
00133 
00134 
00135    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00140    public String getFolderName(){
00141       trapNullName(folderName);
00142       return folderName;
00143    }
00144 
00145    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00150    public String getParentName(){
00151       trapNullName(parentName);
00152       return parentName;
00153    }
00154 
00155    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00159    public String getCurrentName(int numSamples){
00160       if( !filesLoaded ){
00161          return " ";
00162       }
00163       int sa = strList.size();
00164       if( anchor >= sa ){
00165          return " ";            // out of bounds
00166       }
00167       if( anchor < 0 ){
00168          return " ";            // out of bounds
00169       }
00170 
00171       return (String)strList.get(anchor);
00172    }
00173    
00174 
00175    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00181    public String getNextName(int numSamples){
00182       int sa = strList.size();
00183       if( !filesLoaded ){
00184          return null;
00185       }
00186       if( anchor >= sa ){
00187          return null;           // out of bounds
00188       }
00189       if( anchor < 0 ){
00190          return null;           // out of bounds
00191       }
00192 
00193       return (String)strList.get(anchor++);
00194    }
00195 
00196    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00202    public long getLongDate( String fname ){
00203       checkPathName( fname );
00204       File f1 = stringToFile( fname );
00205       if( f1 == null ){
00206          mcj.errorMsg("Folder: fname is not a file");
00207          return 0;
00208       }
00209       long tv = f1.lastModified();
00210 
00211       return tv ;
00212    }
00213 
00214    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00220    public String getStringDate( String fname ){
00221       checkPathName( fname );
00222       File f1 = stringToFile( fname );
00223       if( f1 == null ){
00224          mcj.errorMsg("Folder: fname is not a file");
00225          return null;
00226       }
00227       long tv = f1.lastModified();
00228       return Long.toString( tv );
00229    }
00230 
00231    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00234    public boolean isDirectoryName( String fname ){
00235      
00236       checkPathName( fname );
00237       File f1 = new File(folderName, fname );
00238       if( f1.isDirectory() ){
00239          return true;
00240       }
00241       return false;
00242    }
00243 
00244    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00247    public String stringToPath( String fname ){
00248      
00249       checkPathName( fname );
00250       File f1 = new File(folderName, fname );
00251       return f1.getAbsolutePath();
00252    }
00253 
00254    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00261    public String getLength( String fname, boolean skip){
00262       checkPathName( fname );
00263       File f1 = stringToFile( fname );
00264       
00265       if( f1 == null ){
00266          return "0";
00267       }
00268 
00269       long len = f1.length();
00270       if( len < 1000000L || skip ){
00271          return Long.toString( len );
00272       }
00273       if( len < 1000000000 ){
00274          return Long.toString( len/1000L ) + "K";
00275       }
00276 
00277       return Long.toString( len/1000000L ) + "M";
00278 
00279    }
00280 
00281 
00282 
00283    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00288    public int getAnchor(){
00289      return this.anchor; 
00290    } // getAnchor()
00291 
00292    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00297    public void setAnchor( int startIndex ){
00298       anchor = startIndex;
00299       if( anchor >= numEntries ){
00300          anchor = numEntries-1;
00301          return;
00302       }
00303       if( anchor < 0 ){
00304          anchor = 0;
00305       }
00306    }
00307 
00308    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00312    public void saveAnchor( ){
00313       savedAnchor = anchor;
00314    }
00315 
00316    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00320    public void restoreAnchor( ){
00321       anchor = savedAnchor;
00322    }
00323 
00324    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00327    private File stringToFile( String fname ){
00328      
00329       checkPathName( fname );
00330       File f1 = new File(folderName, fname );
00331       return f1;
00332    }
00333 
00334 
00335    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00336    private void checkPathName( String pathName ){
00337       try{
00338          if( pathName == null ){
00339             throw new 
00340                IllegalArgumentException("\r\nFolder String is nil\r\n");
00341          }
00342       }catch(Exception e){
00343          String s = e.getMessage();
00344          mcj.goodbye(s);
00345       }
00346    }
00347    
00348    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00349    private void trapNullName( String name ){
00350       try{
00351          if( name == null ){
00352             throw new 
00353                IllegalArgumentException("\r\nFolder string is nil\r\n");
00354          }
00355       }catch(Exception e){
00356          String s = e.getMessage();
00357          mcj.goodbye(s);
00358       }
00359    }
00360    
00361 } // end Folder.java
00362 
00363 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00366 
00367 class DirFilter implements FileFilter{
00368     public boolean accept( File pathName ){
00369       if( pathName.isDirectory() ){
00370          return true;
00371       }
00372       return true;
00373    }
00374 }
00375 
00376 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00380 class IsFileFilter implements FileFilter{
00381     public boolean accept( File pathName ){
00382       if( pathName.isFile() ){
00383          return true;
00384       }
00385       return false;
00386 
00387    }
00388 }
00389 
00390 // end Folder.java

SourceForge.net Logo