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/RandomImageFactory.java,v 1.4 2004/05/10 14:58:54 tj_willis Exp $
019 */
020 package net.sourceforge.pavlov.randommedia;
021
022 import javax.swing.ImageIcon;
023 import net.sourceforge.pavlov.zipUtilities.*;
024 import org.apache.log4j.*;
025
026 // This class no longer provides a singleton.
027 /**
028 * RandomImageFactory provides an interface for retrieving a
029 * random image from a filesystem directory tree. Images can
030 * be individual files or JAR files of images. JAR files are
031 * treated as directories. Directories can be interactively
032 * enabled and disabled.
033 *
034 * <P>This implementation is thread safe and is an example of the
035 * Singleton OOD design pattern.
036 *
037 * <P><B>Note: If the directory tree has closed loops, it will
038 * make the current implementation go into an infinite loop.
039 * For example if b is a subdirectory of a (a/b) and c is a link
040 * in b to a (a/b/c->a), this implementation will add an infinite
041 * number of copies of the a and b directories.</B>
042 *
043 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
044 * @version $Revision: 1.4 $
045 */
046 public class RandomImageFactory
047 extends AbstractRandomMediaFactory
048 {
049 //private volatile static RandomImageFactory instance;
050 //private volatile static ImageLoader loader;
051 private volatile ImageCache loader;
052 private ZipCapableFileFilter filter;
053
054 //FIXED: desingleton this class
055 // /**
056 // *
057 // * @return a <code>RandomImageFactory</code> value
058 // */
059 // public synchronized static RandomImageFactory getInstance() {
060 // if (instance == null)
061 // instance = new RandomImageFactory();
062 // return instance;
063 // }
064
065 /**
066 * Creates a new <code>RandomImageFactory</code> instance.
067 */
068 public RandomImageFactory()
069 {
070 super();
071 zinit();
072 }
073
074 private void zinit() {
075
076 loader = new ImageCache(this,50);
077 loader.loadObjects(10);
078 }
079
080 /**
081 * Creates a new <code>RandomImageFactory</code> instance.
082 * @param rootDir a <code>String</code> value
083 */
084 public RandomImageFactory(String rootDir)
085 {
086 super(rootDir);
087 zinit();
088 }
089
090
091 // this being synched is new
092 /**
093 * Clears the cache maintained in the ImageLoader. Useful
094 * after enabling or disabling directories and changing the
095 * root directory.
096 *
097 */
098 public synchronized void clearCache()
099 {
100 if(loader==null) return;
101 loader.clearCache();
102 }
103
104 /**
105 * Gets an image embedded in a CacheObject, which provides some
106 * additional information about the image.
107 *
108 * @return a <code>CacheObject</code> value with an random ImageIcon as its
109 * CacheObject.object field.
110 */
111 public synchronized CacheObject getRandomCacheObject()
112 {
113 assert loader!=null : "Loader is null";
114 CacheObject foo = loader.getRandomCacheObject();
115 return foo;
116 }
117
118 /**
119 * Gets a random image as an ImageIcon.
120 *
121 * @param size an <code>int</code> value
122 * @return an <code>ImageIcon</code> value
123 */
124 public synchronized ImageIcon getRandomImageIcon(int size)
125 {
126 assert loader!=null : "Loader is null";
127 CacheObject foo = loader.getRandomCacheObject();
128 if(foo==null) return null;
129 return (ImageIcon)foo.object;
130 }
131
132
133 public static int getGlobalCacheObjectCount()
134 {
135 return CacheObject.count;
136 }
137
138
139
140 /**
141 * Describe <code>getFileFilter</code> method here.
142 *
143 * @return a <code>ZipCapableFileFilter</code> value
144 */
145 protected ZipCapableFileFilter getFileFilter()
146 {
147 if (filter!=null) return filter;
148 filter = new JPEGFileFilter();
149 return filter;
150 }
151 }
152
153
154
155
156
157
158
159