001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/randommedia/AbstractMediaDirectory.java,v 1.4 2004/05/10 14:58:54 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.randommedia;
021    
022    import java.util.Vector;
023    import java.io.File;
024    import java.util.jar.*;
025    import java.net.URL;
026    import net.sourceforge.pavlov.zipUtilities.*;
027    import org.apache.log4j.*;
028    
029    /**
030     * A filesystem directory (with subdirectories and JAR files)
031     * full of media items.
032     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
033     * @version 1.0
034     */
035    public abstract class AbstractMediaDirectory
036    {
037        /**
038         * List of all files matching the FileFilter.
039         *
040         */
041        private Vector<URL> urls;
042        /**
043         * True if this directory is enabled for file selection.
044         *
045         */
046        private boolean isEnabled;
047        /**
048         * List of all my subdirectories (and JAR files).
049         *
050         */
051        private Vector<File> subdirs;
052        /**
053         * The directory I refer to.
054         *
055         */
056        private File directory; 
057        /**
058         * Describe variable <code>filter</code> here.
059         *
060         */
061        private ZipCapableFileFilter filter; 
062    
063        /**
064         * Creates and initializes a RandomMediaDirectory object with the given 
065         * directory and fileFilter.
066         * @param dir a <code>File</code> value
067         * @param filt a <code>ZipCapableFileFilter</code> value
068         */
069        public AbstractMediaDirectory(File dir, ZipCapableFileFilter filt)
070        {
071            init(dir,filt);
072            refresh();
073        }
074    
075        private void init(File dir, ZipCapableFileFilter filt)
076        {
077            directory = dir;
078            filter = filt;
079            urls = new Vector<URL>(10,5);
080            subdirs = new Vector<File>(10,5);
081        }
082    
083        /**
084         * Creates and initializes a RandomMediaDirectory object with the given 
085         * file and fileFilter.
086         * @param dir a <code>File</code> value
087         * @param filt a <code>ZipCapableFileFilter</code> value
088         * @param shouldRefresh a <code>boolean</code> value
089         */
090        public AbstractMediaDirectory(File dir, ZipCapableFileFilter filt,boolean shouldRefresh)
091        {
092            init(dir,filt);
093            if(shouldRefresh) refresh();
094        }
095    
096    
097        /**
098         * Get the Directory.
099         * @return the File value.
100         */
101        public File getDirectory() {
102            return directory;
103        }
104    
105        /**
106         * Set the Directory.
107         * @param newDirectory a <code>File</code> value
108         */
109        protected void setDirectory(File newDirectory) {
110            this.directory = newDirectory;
111        }
112    
113        /**
114         * Describe <code>listFiles</code> method here.
115         *
116         * @return a <code>File[]</code> value
117         */
118        protected File[] listFiles()
119        {
120            if(directory==null) return null;
121            return directory.listFiles();
122        }
123    
124        /**
125         * Describe <code>getAbsoluteDirectoryPath</code> method here.
126         *
127         * @return a <code>String</code> value
128         */
129        protected String getAbsoluteDirectoryPath()
130        {
131            return directory.getAbsolutePath();
132        }
133    
134        /**
135         * Describe <code>getDirectoryString</code> method here.
136         *
137         * @return a <code>String</code> value
138         */
139        protected String getDirectoryString()
140        {
141            return directory.toString();
142        }
143    
144    
145    
146        /**
147         * Describe <code>addSubdirectory</code> method here.
148         *
149         * @param f a <code>File</code> value
150         */
151        protected void addSubdirectory(File f)
152        {
153            subdirs.add(f);
154        }
155    
156        
157        /**
158         * Describe <code>addURL</code> method here.
159         *
160         * @param u an <code>URL</code> value
161         */
162        protected void addURL(URL u)
163        {
164            urls.add(u);
165        }
166    
167        /**
168         * Describe <code>clearURLs</code> method here.
169         *
170         */
171        protected void clearURLs()
172        {
173            urls.clear();
174        }
175    
176        /**
177         * Describe <code>getURLIndex</code> method here.
178         *
179         * @param i an <code>int</code> value
180         * @return an <code>URL</code> value
181         */
182        protected URL getURLIndex(int i)
183        {
184            return urls.elementAt(i);
185        }
186    
187        /**
188         * Returns the file name of this directory.
189         * @return a <code>String</code> value
190         */
191        public synchronized String getName()
192        {
193          //if(isAJar(file))
194            //return file.getAbsolutePath();
195         
196          return directory.getAbsolutePath();
197        }
198    
199        abstract void refresh();
200    
201        /**
202         * Describe <code>acceptEntry</code> method here.
203         *
204         * @param j a <code>JarEntry</code> value
205         * @return a <code>boolean</code> value
206         */
207        protected boolean acceptEntry(JarEntry j)
208        {
209            return ( (filter!=null) && (filter.accept(j)));
210        }
211    
212        /**
213         * Describe <code>acceptFile</code> method here.
214         *
215         * @param f a <code>File</code> value
216         * @return a <code>boolean</code> value
217         */
218        protected boolean acceptFile(File f)
219        {
220            return ( (filter!=null) && (filter.accept(f)));
221        }
222    
223        /**
224         *  Set this directory to enabled
225         * @deprecated FIXME: why deprecated?
226         */
227        @Deprecated private void setEnabled(boolean flag)
228        {
229            isEnabled = flag;
230        }
231    
232        /**
233         * Return all media URLs in this directory.
234         * @return a <code>Vector</code> value
235         */
236        public Vector<URL> getURLVector()
237        {
238            return urls;
239        }
240    
241        /**
242         * Return all subdirectories in this directory.
243         * @return a <code>Vector</code> value
244         */
245        public Vector<File> getSubdirectories()
246        {
247            return subdirs;
248        }
249    
250        /**
251         * If this directory is enabled
252         * @return a <code>boolean</code> value
253         */
254        public boolean isEnabled() { return isEnabled; }
255    
256        /**
257         * Number of media URL's in this directory.
258         * @return an <code>int</code> value
259         */
260        public int getSize()
261        {
262            return urls.size();
263        }
264    
265    }
266    
267    
268    
269    
270