// 2012-06-07 johnpfeiffer requires httpclient-4.2 , httpcore-4.2, o2javasdk-0.0.233
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.oxygen.sdk.O2Agent;
public class Main extends JPanel
{
private static final long serialVersionUID = 1L;
private static final String CLASSVERSIONNUMBER = "0.3";
private static final int SECONDSTOSLEEP = 3000;
private int screenHeight = 0;
private int screenWidth = 0;
private BufferedImage img = null;
private int imageHeight = 0;
private int imageWidth = 0;
private static final String CORRECTUSAGE = " CORRECT Usage: java -jar OxygenSpaceJPGKiosk-0.3.jar URL APIKEY oxygenUserId oxygenPassword spaceName destinationFolder repetitionCount";
private String accessUrl;
private String ApiKey;
private String oxygenUserId;
private String oxygenPassword;
private String remoteSpaceName;
private String localDestinationPath;
private ArrayList <String> localFileNames;
Main( String args[] ) throws Exception
{
initializeDisplays();
}
protected String getVersion()
{
return CLASSVERSIONNUMBER;
}
public void paint( Graphics g )
{
g.drawImage( img , 0 , 0 , null );
}
public void LoadImage( String fileName )
{
try
{
img = ImageIO.read( new File( fileName ) );
imageHeight = img.getHeight();
imageWidth = img.getWidth();
System.out.println( "DEBUG: " + fileName + " image size width,height = " + imageHeight + "," + imageWidth );
}catch( IOException e )
{
e.printStackTrace();
}
}
private void initializeDisplays()
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle r = ge.getMaximumWindowBounds();
screenHeight = r.height;
screenWidth = r.width;
System.out.println( "DEBUG: default display size " + screenHeight + " x " + screenWidth );
}
private void initializeLocalFileListing()
{
System.out.println( "DEBUG: begin local file listing from: " + localDestinationPath );
localFileNames = new ArrayList <String>();
File destinationDirectory = new File( localDestinationPath );
try
{
System.out.println( "DEBUG: dest: " + destinationDirectory.getPath() + " " + destinationDirectory.getCanonicalPath() );
}catch( Exception e )
{
e.printStackTrace();
}
File[] files = destinationDirectory.listFiles( new ImageFileFilter() );
if( files == null )
{
System.out.println( destinationDirectory.list() );
System.exit( 1 );
}
for( File f : files )
{
localFileNames.add( f.toString() );
}
}
private void getFiles( O2Agent agent ) throws Exception
{
SpaceContains oxygenfiles = new SpaceContains( agent , remoteSpaceName );
ArrayList <OxygenFile> fileListing = oxygenfiles.getFileListing();
for( OxygenFile current : fileListing )
{
oxygenfiles.download( current , localDestinationPath );
}
}
// TODO: verify these are the correct Files by using the ID
private void removeLocalFiles()
{
localFileNames = new ArrayList <String>();
File tempDirectory = new File( localDestinationPath );
File[] files = tempDirectory.listFiles( new ImageFileFilter() );
for( File f : files )
{
f.delete();
// System.out.println( "DEBUG: removing image file " + f );
}
}
// TODO: nasty windows hack to catch %tmp% or %userprofile%
private String validArguments( String args[] )
{
StringBuilder result = new StringBuilder();
if( args.length != 7 )
{
result.append( "ERROR: " + args.length + " is the incorrect number of arguments" );
}else
{
for( int i = 0 ; i < args.length ; i++ )
{
if( args[i] == null || args[i].isEmpty() )
{
result.append( "ERROR: argument " + i + " is null or empty" );
break;
}
}
try
{
Integer.parseInt( args[6] );
}catch( NumberFormatException ne )
{
result.append( "ERROR: the last argument should be a positive integer, not : " + args[6] );
}
try
{
new URL( args[0] );
}catch( MalformedURLException e )
{
result.append( "ERROR: malformed URL: " + args[0] );
}
}
if( !result.toString().isEmpty() )
{
result.append( CORRECTUSAGE );
}else
{
accessUrl = args[0];
ApiKey = args[1];
oxygenUserId = args[2];
oxygenPassword = args[3];
remoteSpaceName = args[4];
if( args[5].equals( "%tmp%" ) || args[5].equals( "%temp%" ) )
{
localDestinationPath = System.getProperty( "java.io.tmpdir" );
}else
{
localDestinationPath = args[5];
}
if( !localDestinationPath.endsWith( System.getProperty( "file.separator" ) ) )
{
localDestinationPath = localDestinationPath + System.getProperty( "file.separator" );
System.out.println( "DEBUG: fixed path to end with a trailing slash " + localDestinationPath );
}
}
return result.toString();
}
public static void main( String args[] ) throws Exception
{
Main panel = new Main( args );
String message = panel.validArguments( args );
if( !message.isEmpty() )
{
System.err.println( message );
System.exit( 1 );
}
JFrame frame = new JFrame( "ImageViewer" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
int repetitionCount = Integer.parseInt( args[6] );
for( int i = 0 ; i < repetitionCount ; i++ )
{
OxygenLogin login = new OxygenLogin( panel.accessUrl , panel.ApiKey , panel.oxygenUserId , panel.oxygenPassword );
O2Agent agent = login.getAgent();
panel.getFiles( agent );
System.out.println( "DEBUG: getFiles complete" );
panel.initializeLocalFileListing();
System.out.println( "DEBUG: localFiles complete" );
for( String current : panel.localFileNames )
{
panel.LoadImage( current );
frame.getContentPane().add( panel );
frame.pack();
frame.setLocation( 0 , 0 );
frame.setSize( panel.screenWidth , panel.screenHeight );
frame.validate();
frame.setVisible( true );
try
{
Thread.sleep( SECONDSTOSLEEP );
}catch( Exception e )
{
e.printStackTrace();
}
frame.invalidate();
panel.removeAll();
}
panel.removeLocalFiles();
} // end for loop
System.out.println( "finished" );
System.exit( 0 );
}
class ImageFileFilter implements FileFilter
{
private final String[] okFileExtensions = new String[]
{ "jpg" };
public boolean accept( File file )
{
for( String extension : okFileExtensions )
{
if( file.getName().toLowerCase().endsWith( extension ) )
{
return true;
}
}
return false;
}
}
} // end class
/*
* EXTRA STUFF
* GraphicsDevice[] gs = ge.getScreenDevices();
* for( int i = 0 ; i < gs.length ; i++ )
* {
* DisplayMode dm = gs[i].getDisplayMode();
* int screenWidth = dm.getWidth();
* int screenHeight = dm.getHeight();
* System.out.println( "DEBUG: Display " + i + " with size " + screenWidth +
* " x " + screenHeight );
* }
* GraphicsDevice defaultDevice = ge.getDefaultScreenDevice();
* DisplayMode targetDisplay = defaultDevice.getDisplayMode();
*/