package jas2.util.pluginmanager;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.net.*;

/**
 * This class works with the VersionNumberComparison class to check if current version of an application is the latest version.
 * A JPane is created which when added to a JFrame prompts the user to select if they would like to 
 * check the application version now, or every time the application starts, or both. If they wish to check the
 * version then they need to decide if they are looking for new developement or production releases.
 * The latest developement and production version numbers are read from the web page referenced by the
 * constructor argument url and is compared to constructor
 * argument current version. Appropriate output messages are generated.    
 */
public class AppVersionCheck implements ActionListener {
    
    private JButton checknow;
	private JCheckBox checkdevelopers;
	private JCheckBox checknondevelopers;
	private JCheckBox checkstartno;
	private JCheckBox checkstartyes;
	private JOptionPane updatepane;
	private JPanel panel;
	private Container container;
	
	private String latestversion;
	private String appname;
	private String versionURL;
	private String currentversion;
	private Properties userprops;
	
	private int whichmessage;
	boolean developers;
	/**The Constructor takes two arguments:
	 * @param c The container argument is of special interest because it is the container that the
     * JOptionPanes generated in this class are attatched to. If the VersionOptionPane is created then its
     * container should be passed in, other wise the application container should be used. 
     * @param versionprops The Properties object is used to set the current application properties. It should include the
	 * following keys:  
	 * @param appname is the name of the application
	 * @param url should refer to the location of a web page which contains the version
     * number of the latest developers release on the first line and the latest production release on the second line.
     * @param currentversion should be the current running version of
     * the application. 
     * @param versionselection sets the current selection for the 'which version' check boxes. 
     * @param updateonstartselection sets whether the user wants to automatically look for updates on start up. 
     * @param versioncheckoptionpane controls whether the JOptionPane which notifies 
     * the user that they are currnetly running the latest version should be displayed, if the container used
     * is the one which contains the VersionCheckOptionPane then the message will be displayed, otherwise not.
     */
	
    public AppVersionCheck(String appname, String versionURL, String currentversion, Properties userprops)
	{
        	
		this.userprops = userprops;
		
		if(appname != null)this.appname = appname;
		else this.appname=unknown;
		
		if(versionURL != null)this.versionURL = versionURL;
		else this.versionURL=unknown;
		
		
		if(currentversion != null)this.currentversion=currentversion;
		else this.currentversion = unknown;
			
		   
	}
    
    /**Returns a JPane which contains three check boxes that allow the user to chose when to check
     * for a new version of the application and if they are looking for develpement or production releases. 
     */
    public JPanel getVersionCheckOptionPane()
	{
        panel = new JPanel(new GridLayout(7,0));
		checknow=new JButton("Check For New Version Now...");
		checknow.addActionListener(this);
		Box box = new Box(BoxLayout.X_AXIS);
		box.add(Box.createGlue());
		box.add(checknow);
		box.add(Box.createGlue());
		JLabel checkstartlabel = new JLabel("Would you like "+appname+" to check for a new application version each time the application starts?");
 		JLabel checkdevelopmentlabel = new JLabel("When checking for a new version would you like to check for production or development releases?"); 
		checknondevelopers= new JCheckBox("Only check for new "+appname+" production release versions.");
		checkdevelopers= new JCheckBox("Check for new development and production releases of "+appname+".");
		checkstartno = new JCheckBox("No, do not check at start up.");
		checkstartno.addActionListener(this);
		checkstartyes = new JCheckBox("Yes, check at start up");
		checkstartyes.addActionListener(this);
		ButtonGroup developergroup = new ButtonGroup();
		developergroup.add(checkdevelopers);
		developergroup.add(checknondevelopers);
		ButtonGroup startgroup = new ButtonGroup();
		startgroup.add(checkstartno);
		startgroup.add(checkstartyes);
		if(userprops.getProperty("DeveloperVersion","false").equals("true"))checkdevelopers.setSelected(true);
		else checknondevelopers.setSelected(true);
		if(userprops.getProperty("UpdateOnStart","true").equals("true")) checkstartyes.setSelected(true);
		else checkstartno.setSelected(true);
		panel.add(checkstartlabel);
		panel.add(checkstartno);
		panel.add(checkstartyes);
		panel.add(checkdevelopmentlabel);
		panel.add(checknondevelopers);
		panel.add(checkdevelopers);
		panel.add(box);
		return panel;
    }
    
	
	/**
	 * Reads the file containing the current dev and production versions of 
	 * the application. Production version should be on the first (non commented) line
	 * and Developers version should be on the second line. Blank lines or lines beginning
	 * with # are treated as comments.
	 * @param developers if true check the developers version, else the production version
	 * @return The version number, or unknown if it cannot be determined
	 */
	private String getLatestVersion(boolean developers)
	{   
	    if(versionURL.equals(unknown)) return unknown;
	    try
		{
		    URL location = new URL(versionURL);
		    URLConnection con = location.openConnection();
			String productionversion = unknown;
	        String developersversion = unknown;
					
		    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			for (;;)
			{
				String line = in.readLine();
				if (line == null) break;
				if (line.length() == 0 || line.charAt(0) == '#') continue;
				if (productionversion == unknown) productionversion = line;
				else                              developersversion = line;
			}
			in.close();
									
			return developers ?  developersversion : productionversion;
		   		
		}
		catch(Exception x)
		{
	        return unknown;
	    }
	}
	
	public int testCurrentVersion(){
		developers = false;
		if(checkdevelopers != null)developers = checkdevelopers.isSelected();
		else  developers  = new Boolean(userprops.getProperty("DeveloperVersion")).booleanValue();
		
		latestversion = getLatestVersion(developers);
		VersionNumberCompare versioncompare = new VersionNumberCompare();	
		try{
		whichmessage = versioncompare.compareVersions(currentversion, latestversion, true);
		}catch(VersionNumberCompare.VersionCompareException vce){
			whichmessage = VersionNumberCompare.VERSIONCHECKERROR;
		}
		return whichmessage;		
	}
	
    /**
     * Call this method to compare versions and generate the resulting dialog panes
     */	
	public void showUpdateDialog(Container c,boolean ignoreErrors, boolean ignoreRunningCurrentVersion)
	{	
		container = c;		
		if (latestversion.equals(unknown) && ignoreErrors) return; 
		
		Object updatemessage = null;
		String updatetitle = "Application Version Check Dialog";
		int optiontype; 
		int messagetype;
		int selectedbutton;
		
		if(whichmessage == VersionNumberCompare.UPDATEVERSION & developers){
			updatemessage =appname +" developement release version "+latestversion+" is available. You are currently running "+appname +" version " + currentversion+ ". Would you like to update "+appname+ "?";
			optiontype = JOptionPane.YES_NO_CANCEL_OPTION;
			messagetype = JOptionPane.QUESTION_MESSAGE;
			selectedbutton = JOptionPane.showConfirmDialog(container,updatemessage, updatetitle, optiontype, messagetype);
			if(selectedbutton == JOptionPane.YES_OPTION) createDownloadUpdatesDialog();
		}else if(whichmessage == VersionNumberCompare.UPDATEVERSION & !developers){
			updatemessage =appname +" production release version "+latestversion+" is available. You are currently running "+appname +" version " + currentversion+ ". Would you like to update "+appname+ "?";
			optiontype = JOptionPane.YES_NO_CANCEL_OPTION;
			messagetype = JOptionPane.QUESTION_MESSAGE;
			selectedbutton = JOptionPane.showConfirmDialog(container,updatemessage, updatetitle, optiontype, messagetype);
			if(selectedbutton == JOptionPane.YES_OPTION) createDownloadUpdatesDialog();
		}else if(whichmessage == VersionNumberCompare.NOUPDATEVERSION && !ignoreRunningCurrentVersion){
			if(developers)updatemessage ="You are currently running "+appname +" version "+ currentversion+ ", which is the latest developers version.";
			else updatemessage ="You are currently running "+appname +" version "+ currentversion+ ", which is the latest production release version.";
			optiontype = JOptionPane.OK_CANCEL_OPTION;
			messagetype = JOptionPane.INFORMATION_MESSAGE;
			selectedbutton = JOptionPane.showConfirmDialog(container,updatemessage, updatetitle, optiontype, messagetype);
		}else if(whichmessage == VersionNumberCompare.VERSIONCHECKERROR){
			if (ignoreErrors) return;
		    updatemessage ="User version: " + currentversion+ ". Newest version: " + latestversion+". "+appname+" version comparison error!!";
			optiontype = JOptionPane.OK_CANCEL_OPTION;
			messagetype = JOptionPane.INFORMATION_MESSAGE;
			selectedbutton = JOptionPane.showConfirmDialog(container,updatemessage, updatetitle, optiontype, messagetype);
		}
		
	}
    
	private void createDownloadUpdatesDialog(){
			Object downloadmessage ="To update "+appname +" to version "+latestversion+" go to http://jas.freehep.org/downloads.htm.";
			String downloadtitle = "Retrieving Updates Dialog";
			int optionType = JOptionPane.CANCEL_OPTION;
			int messagetype = JOptionPane.INFORMATION_MESSAGE;
			int selectedbutton = JOptionPane.showConfirmDialog(container,downloadmessage, downloadtitle, optionType, messagetype);
	}
	
	private void createCheckNowButtonPane(){
		String updatetitle = "Check Now Dialog";
		int optiontype; 
		int messagetype;
		int selectedbutton;
		Object updatemessage ="Before "+ appname+" can check for new releases you need to select either the developer or production release version check box.";
		optiontype = JOptionPane.OK_CANCEL_OPTION;
		messagetype = JOptionPane.INFORMATION_MESSAGE;
		selectedbutton = JOptionPane.showConfirmDialog(container,updatemessage, updatetitle, optiontype, messagetype);

	}
	public void setUserProperties(){
		if(checkstartno.isSelected()== true){
			userprops.put("UpdateOnStart", "false");
		}else if(checkstartyes.isSelected()== true){
			userprops.put("UpdateOnStart", "true");
		} 
		
		if(checknondevelopers.isSelected()== true){
			userprops.put("DeveloperVersion", "false");
		}else if(checkdevelopers.isSelected()== true){
			userprops.put("DeveloperVersion", "true");
		}
	}
	
	public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(checknow)){
				if(checkdevelopers.isSelected() | checknondevelopers.isSelected()){
					testCurrentVersion();
					showUpdateDialog(checknow.getParent(),false,false);
				}
				else createCheckNowButtonPane();
			}else if(e.getSource().equals(checkstartyes)){
				if(!checkdevelopers.isSelected()&& !checknondevelopers.isSelected()) checknondevelopers.setSelected(true);
			}
	}
   private static final String unknown = "unknown";
}

 

Ads help maintain this website.