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/ImageCache.java,v 1.3 2004/05/10 14:58:54 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.randommedia; 021 022 023 024 import javax.swing.ImageIcon; 025 import java.net.URL; 026 import org.apache.log4j.*; 027 028 /** 029 * Loads and holds a bunch of image files whose locations are specified 030 * relative to a fixed base URL. 031 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a> 032 * @version 1.0 033 */ 034 public class ImageCache extends MediaCache { 035 CacheObject last = null; 036 037 /** 038 * Creates a new <code>ImageCache</code> instance. 039 * 040 * @param p a <code>RandomURLProvider</code> value 041 * @param size an <code>int</code> value 042 */ 043 public ImageCache(RandomURLProvider p,int size) { 044 super(p,size); 045 } 046 047 /** 048 * Describe <code>loadCacheObject</code> method here. 049 * 050 * @return a <code>CacheObject</code> value 051 */ 052 public CacheObject loadCacheObject() 053 { 054 return (CacheObject)getObject(); // def'd in MediaCache 055 } 056 057 // protected void createMediaLoader(MediaCache m,URL u, int pri) 058 // { 059 // new ImageContentLoader(m,u,pri); 060 // }; 061 062 /** 063 * Describe <code>getUncachedRandomImageIcon</code> method here. 064 * 065 * @param size an <code>int</code> value 066 * @return an <code>ImageIcon</code> value 067 */ 068 public ImageIcon getUncachedRandomImageIcon (int size) 069 { 070 CacheObject foo = getRandomUncachedObject(); 071 if(foo==null) return null; 072 return (ImageIcon)foo.object; 073 } 074 075 076 /** 077 * Describe <code>getRandomUncachedObject</code> method here. 078 * 079 * @return a <code>CacheObject</code> value 080 */ 081 public CacheObject getRandomUncachedObject() 082 { 083 ImageContentLoader ld = new ImageContentLoader(); 084 assert provider!=null : "Can't get a content loader"; 085 URL u = provider.getRandomURL(); 086 assert u!=null : "Can't get a url from the provider"; 087 CacheObject foo = (CacheObject)ld.loadContent(u); 088 return foo; 089 } 090 091 /** 092 * Describe <code>getRandomCacheObject</code> method here. 093 * 094 * @return a <code>CacheObject</code> value 095 */ 096 public CacheObject getRandomCacheObject() 097 { 098 return loadCacheObject(); 099 } 100 101 /** 102 * Gets a random image icon of the given size from the cache. 103 * Initializes or loads cache objects as necessary. 104 * @param size an <code>int</code> value 105 * @return an <code>ImageIcon</code> value 106 */ 107 public ImageIcon getRandomImageIcon( int size ) 108 { 109 if(size<1) return null; 110 CacheObject ob; 111 ob = getRandomCacheObject(); 112 if(ob==null){ 113 ob = getRandomUncachedObject(); 114 } 115 if(ob==null) return null; 116 ImageIcon foo = (ImageIcon)ob.object; 117 if(foo==null) return null; 118 last = ob; 119 ImageIcon ret = ImageIconUtilities.formatImage(foo, size); 120 return ret; 121 } 122 123 /** 124 * If there's a null in an intermediate step, let the user know. 125 * This is useful for debugging. 126 * @param msg a <code>String</code> value 127 * @return an <code>Object</code> value 128 */ 129 protected static Object nullNotifier(String msg) 130 { 131 System.out.println("NULLNOTIFIER: " +msg); 132 return null; 133 } 134 135 /** 136 * Describe <code>getContentLoader</code> method here. 137 * 138 * @return a <code>ContentLoader</code> value 139 */ 140 public ContentLoader getContentLoader() 141 { 142 return new ImageContentLoader(); 143 } 144 }