001         package com.croftsoft.apps.slideshow;
002         
003         import java.awt.*;
004         import java.io.*;
005         import java.net.*;
006         import java.util.*;
007         import java.util.concurrent.*;
008         import java.util.logging.*;
009         import javax.imageio.ImageIO;
010    
011         import com.croftsoft.core.CroftSoftConstants;
012         import com.croftsoft.core.math.MathConstants;
013         
014         /*********************************************************************
015         * Configuration.
016         *  
017         * Can be modified to be persistent.
018         * 
019         * @version
020         *   $Id: SlideshowConfig.java,v 1.4 2006/12/17 09:42:39 croft Exp $
021         * @since
022         *   2006-10-30
023         * @author
024         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
025         *********************************************************************/
026    
027         public final class  SlideshowConfig
028         //////////////////////////////////////////////////////////////////////
029         //////////////////////////////////////////////////////////////////////
030         {
031           
032         private static final String  CLASS_NAME
033           = SlideshowConfig.class.getName ( );
034           
035         private static final String  VERSION
036           = "$Date: 2006/12/17 09:42:39 $";
037       
038         private static final String  TITLE
039           = "CroftSoft Slideshow";
040       
041         private static final String  INFO
042           = TITLE + "\n"
043           + "Version " + VERSION + "\n"
044           + CroftSoftConstants.COPYRIGHT + "\n"
045           + CroftSoftConstants.DEFAULT_LICENSE + "\n"
046           + CroftSoftConstants.HOME_PAGE + "\n";
047    
048         private static final int
049           FRAME_WIDTH  = 600,
050           FRAME_HEIGHT = 400;
051       
052         private static final double  UPDATE_RATE = 1.0;
053         
054         private static final String  SHUTDOWN_CONFIRMATION_PROMPT = null;
055         
056         private static final Font    FONT
057           = new Font ( "Arioso", Font.BOLD, 20 );
058         
059         private static final Color
060           BACKGROUND_COLOR = Color.BLACK,
061           FOREGROUND_COLOR = Color.GREEN;
062         
063         //
064         
065         private static final String
066           PROPERTY_SHOW  = "show",
067           PROPERTY_SPEED = "speed";
068           
069         private static final String [ ]  PROPERTY_SHOW_VALUES = {
070           "off",
071           "on" };
072         
073         private static final String [ ]  PROPERTY_SPEED_VALUES = {
074           "slow",
075           "medium",
076           "fast" };
077         
078         private static final int [ ]  DISPLAY_TIMES = {
079           600,
080           60,
081           6 };
082         
083         private static final boolean [ ]  SHOW_VALUES = {
084           false,
085           true };
086         
087         private static final long  DEFAULT_DISPLAY_TIME_NANOS
088           = DISPLAY_TIMES [ 1 ] * MathConstants.NANOSECONDS_PER_SECOND;
089         
090         private static final boolean  DEFAULT_SHOW
091           = false;
092         
093         //
094         
095         private final Logger                logger;
096         
097         private final Map<String, Integer>  speedMap;
098         
099         private final Map<String, Boolean>  showMap;
100         
101         //
102         
103         private boolean     displayImageFilename;
104           
105         private String [ ]  imageFilenames;
106         
107         private long        displayTimeNanos;
108    
109         //////////////////////////////////////////////////////////////////////
110         //////////////////////////////////////////////////////////////////////
111         
112         public static SlideshowConfig  load ( String [ ]  args )
113         //////////////////////////////////////////////////////////////////////
114         {
115           // Could load from a persistent XML file.
116           
117           return new SlideshowConfig ( );
118         }
119         
120         //////////////////////////////////////////////////////////////////////
121         //////////////////////////////////////////////////////////////////////
122         
123         public  SlideshowConfig ( )
124         //////////////////////////////////////////////////////////////////////
125         {
126           logger = Logger.getLogger ( CLASS_NAME );
127           
128           speedMap = new HashMap<String, Integer> ( );
129    
130           showMap = new HashMap<String, Boolean> ( );
131    
132           for ( int  i = 0; i < PROPERTY_SPEED_VALUES.length; i++ )
133           {
134             speedMap.put (
135               PROPERTY_SPEED_VALUES [ i ],
136               new Integer ( DISPLAY_TIMES [ i ] ) );
137           }
138    
139           displayTimeNanos = DEFAULT_DISPLAY_TIME_NANOS;
140    
141           for ( int  i = 0; i < PROPERTY_SHOW_VALUES.length; i++ )
142           {
143             showMap.put (
144               PROPERTY_SHOW_VALUES [ i ],
145               new Boolean ( SHOW_VALUES [ i ] ) );
146           }
147    
148           displayImageFilename = DEFAULT_SHOW;
149    /*
150           final Integer  displayTimeInteger
151             = ( Integer ) getPropertyValue ( PROPERTY_SPEED, speedMap );
152    
153           if ( displayTimeInteger != null )
154           {
155             displayTimeNanos = displayTimeInteger.intValue ( )
156               * MathConstants.NANOSECONDS_PER_SECOND;
157           }
158    
159           final Boolean  showBoolean
160             = ( Boolean ) getPropertyValue ( PROPERTY_SHOW, showMap );
161    
162           if ( showBoolean != null )
163           {
164             displayImageFilename = showBoolean.booleanValue ( );
165           }
166    */
167           final Executor  executor = Executors.newSingleThreadExecutor ( );
168    
169           executor.execute (
170             new Runnable ( )
171             {
172               public void  run ( )
173               {
174                 imageFilenames = loadImageFilenames ( );
175               }
176             } );
177         }
178         
179         //////////////////////////////////////////////////////////////////////
180         //////////////////////////////////////////////////////////////////////
181         
182         public Color  getBackgroundColor ( ) { return BACKGROUND_COLOR; }
183         
184         public Color  getForegroundColor ( ) { return FOREGROUND_COLOR; }
185         
186         public String  getInfo ( ) { return INFO; }
187         
188         public Font  getFont ( ) { return FONT; }
189         
190         public Dimension  getFrameSize ( )
191           { return new Dimension ( FRAME_WIDTH, FRAME_HEIGHT ); }
192         
193         public String  getFrameTitle ( ) { return TITLE; }
194    
195         public String  getShutdownConfirmationPrompt ( )
196           { return SHUTDOWN_CONFIRMATION_PROMPT; }
197         
198         public String  getThreadName ( ) { return TITLE; }
199         
200         public double  getUpdateRate ( ) { return UPDATE_RATE; }
201         
202         public String [ ]  getImageFilenames ( ) { return imageFilenames; }
203         
204         public long  getDisplayTimeNanos ( ) { return displayTimeNanos; }
205         
206         public boolean  getDisplayImageFilename ( )
207           { return displayImageFilename; }
208         
209         //////////////////////////////////////////////////////////////////////
210         // private methods
211         //////////////////////////////////////////////////////////////////////
212         
213         private static void  initLoggingToFiles ( final Logger  logger )
214         //////////////////////////////////////////////////////////////////////
215         {
216           try
217           {
218             final String  userHome = System.getProperty ( "user.home" );
219    
220             if ( userHome == null )
221             {
222               return;
223             }
224    
225             final File  homeDirectory = new File ( userHome );
226    
227             if ( !homeDirectory.exists ( )
228               || !homeDirectory.isDirectory ( ) )
229             {
230               return;
231             }
232    
233             final File  logDirectory
234               = new File ( homeDirectory, ".croftsoft/savor" );
235    
236             logDirectory.mkdirs ( );
237    
238             final FileHandler  fileHandler = new FileHandler (
239               "%h/.croftsoft/savor/savor%g.log",
240               10000,
241               2,
242               false );
243    
244             logger.addHandler ( fileHandler );
245           }
246           catch ( final Exception  ex )
247           {
248             ex.printStackTrace ( );
249           }
250         }
251         
252         private static String [ ]  loadImageFilenames ( )
253         //////////////////////////////////////////////////////////////////////
254         {
255           try
256           {
257             final String [ ]  suffixes = ImageIO.getReaderFileSuffixes ( );
258             
259             final String  userHome = System.getProperty ( "user.home" );
260           
261             if ( userHome == null )
262             {
263               return null;
264             }
265             
266             final File  homeDirectory = new File ( userHome );
267             
268             if ( !homeDirectory.exists ( )
269               || !homeDirectory.isDirectory ( ) )
270             {
271               return null;
272             }
273           
274             File  picturesDirectory
275               = new File ( homeDirectory, "My Documents/My Pictures" );
276             
277             if ( !picturesDirectory.exists ( )
278               || !picturesDirectory.isDirectory ( ) )
279             {
280               picturesDirectory = homeDirectory;
281             }
282             
283             final Set<String>  imageFilenameSet = new HashSet<String> ( );
284             
285             final Stack<File>  directoryStack = new Stack<File> ( );
286             
287             directoryStack.add ( picturesDirectory );
288             
289             File  directory = null;
290             
291             while ( !directoryStack.isEmpty ( ) )
292             {
293               directory = directoryStack.pop ( );
294               
295               final String [ ]  list = directory.list ( );
296               
297               for ( String  filename : list )
298               {
299                 final File  file = new File ( directory, filename );
300                 
301                 if ( file.isDirectory ( ) )
302                 {
303                   directoryStack.add ( file );
304                   
305                   continue;
306                 }
307                 
308                 filename = filename.toLowerCase ( );
309                 
310                 for ( final String  suffix : suffixes )
311                 {
312                   if ( filename.endsWith ( "." + suffix ) )
313                   {
314                     filename = file.getCanonicalPath ( );
315                 
316                     imageFilenameSet.add ( filename );
317                     
318                     break;
319                   }
320                 }
321               }
322             }
323             
324             return imageFilenameSet.toArray ( new String [ 0 ] );
325           }
326           catch ( final Exception  ex )
327           {
328             ex.printStackTrace ( );
329           }
330           
331           return null;
332         }
333    /*
334         private Object  getPropertyValue (
335           final String  propertyName,
336           final Map     propertyMap )
337         //////////////////////////////////////////////////////////////////////
338         {
339           final ScreensaverContext  screensaverContext = getContext ( );
340           
341           final ScreensaverSettings  screensaverSettings
342             = screensaverContext.getSettings ( );
343           
344           String  propertyValue
345             = screensaverSettings.getProperty ( propertyName );
346           
347           if ( propertyValue == null )
348           {
349             return null;
350           }
351           
352           propertyValue = propertyValue.trim ( ).toLowerCase ( );
353           
354           return propertyMap.get ( propertyValue );
355         }
356    */     
357         //////////////////////////////////////////////////////////////////////
358         //////////////////////////////////////////////////////////////////////
359         }