/home/wpollock1/public_html/AJava/webstart/Demo.java
// WebStart Demo - A simple demo of using Java WebStart
// Written 1/2012 by Wayne Pollock, Tampa Florida USA
//
// This app should be bundled in WebStartDemo.jar, and is launched
// by putting a link to the associated Demo.jnlp file.
// Compile with the file javaws.jar listed on CLASSPATH, or it won't work.
// You can run from the command line with:
// javaws Demo.jnlp
//
// (See tutorial at:
// http://docs.oracle.com/javase/tutorial/deployment/webstart/
// The JNLP API is documented at:
// http://docs.oracle.com/javase/7/docs/jre/api/javaws/jnlp/
// The JNLP (XML) file syntax is documented at:
// http://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/syntax.html
// There are XML schema files for jnlp files, at:
// https://code.google.com/p/ivy-tools/source/browse/trunk/src/app/web/jnlp.xsd
// and:
// http://mindprod.com/jgloss/jnlp.html#XSDSCHEMAS (the latest one)
package webstart;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.jnlp.*;
import static javax.swing.JFrame.*;
public class Demo {
final static JFrame frame = new JFrame( "WebStartDemo" );
final static JPanel titlePanel = new JPanel();
final static JTextArea fileNameLabel = new JTextArea( "<No File Selected>" );
final static JPanel btnPanel = new JPanel();
final static JTextArea fileTextArea = new JTextArea();
public static void main ( String [] args ) {
frame.setSize( 600, 400 );
frame.setLocationRelativeTo( null ); // Center on screen
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
// Draw the initial UI on the Event Dispatch Thread:
try {
SwingUtilities.invokeAndWait( new Runnable() {
public void run () {
drawUI( frame );
}
});
} catch ( Exception e ) {
e.printStackTrace();
System.exit( 1 );
}
frame.setVisible( true );
}
private static void drawUI ( JFrame frame ) {
// Add and layout components:
frame.setLayout( new BorderLayout() );
frame.add( titlePanel, "North" );
fileNameLabel.setFont( new Font("SanSerif", Font.BOLD, 18 ) );
titlePanel.add( fileNameLabel );
frame.add( new JScrollPane( fileTextArea ), "Center" );
frame.add( btnPanel, "South" );
JButton btn = new JButton( "Open File..." );
btnPanel.add( btn );
// Hook up Event handling:
btn.addActionListener( new ActionListener() {
public void actionPerformed ( ActionEvent ae ) {
displayFile();
}
});
}
private static void displayFile () {
FileOpenService fos;
// Get reference to JNLP (WebStart) file open service:
try {
fos = (FileOpenService) ServiceManager.lookup(
"javax.jnlp.FileOpenService" );
} catch ( UnavailableServiceException e ) {
fos = null;
}
if ( fos != null ) {
try {
// Ask user to select a text file through JNLP service:
// (Note, JNLP may ignore the suggested extensions, so any
// file might be returned. TODO: Check file type is text.
FileContents fileContents =
fos.openFileDialog( null, new String[] { "txt", "java" } );
// Update the file name label:
String fileName = fileContents.getName();
// JNLP may hide the file name, so:
if ( fileName == null || fileName == "" )
fileName = "<Unknown file name>";
fileNameLabel.setText( fileName );
// Read and display file contents:
InputStreamReader isr =
new InputStreamReader( fileContents.getInputStream() );
fileTextArea.read( isr, null );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
}