PageRenderTime 34ms CodeModel.GetById 21ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/edu/uncc/parsets/gui/VersionCheck.java

https://code.google.com/p/parsets/
Java | 128 lines | 86 code | 14 blank | 28 comment | 8 complexity | f9ba17159c7aa005a68fb6820c52d719 MD5 | raw file
  1package edu.uncc.parsets.gui;
  2
  3import java.awt.Desktop;
  4import java.awt.Dimension;
  5import java.io.BufferedReader;
  6import java.io.IOException;
  7import java.io.InputStream;
  8import java.io.InputStreamReader;
  9import java.net.URI;
 10import java.net.URL;
 11
 12import javax.swing.Box;
 13import javax.swing.BoxLayout;
 14import javax.swing.JFrame;
 15import javax.swing.JLabel;
 16import javax.swing.JOptionPane;
 17import javax.swing.JScrollPane;
 18import javax.swing.JTextPane;
 19
 20import org.json.simple.JSONArray;
 21import org.json.simple.JSONObject;
 22import org.json.simple.parser.JSONParser;
 23
 24import edu.uncc.parsets.ParallelSets;
 25import edu.uncc.parsets.util.PSLogging;
 26import edu.uncc.parsets.util.osabstraction.AbstractOS;
 27
 28/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 29 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 30 *                     and others (see Authors.txt for full list)
 31 * All rights reserved.
 32 * 
 33 * Redistribution and use in source and binary forms, with or without
 34 * modification, are permitted provided that the following conditions are met:
 35 * 
 36 *    * Redistributions of source code must retain the above copyright
 37 *      notice, this list of conditions and the following disclaimer.
 38 *    * Redistributions in binary form must reproduce the above copyright
 39 *      notice, this list of conditions and the following disclaimer in the
 40 *      documentation and/or other materials provided with the distribution.
 41 *    * Neither the name of UNC Charlotte nor the names of its contributors
 42 *      may be used to endorse or promote products derived from this software
 43 *      without specific prior written permission.
 44 *      
 45 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 46 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 48 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 49 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 50 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 51 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 52 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 55\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 56
 57public class VersionCheck extends Thread {
 58
 59	private static final String VERSIONURL = "http://data.eagereyes.org/parsets/version.php?current="+ParallelSets.VERSION+"&os=";
 60	
 61	private static final String MESSAGE1 = "A new version of this program is available:";
 62	
 63	private static final String MESSAGE2 = "<html><b>Do you want to upgrade?</b></html>";
 64	
 65	private static final String TITLE = "New Version!";
 66	
 67	private JFrame frame;
 68	
 69	public VersionCheck(JFrame f) {
 70		frame = f;
 71		setDaemon(true);
 72	}
 73	
 74	@Override
 75	public void run() {
 76		BufferedReader reader = null;
 77		try {
 78			URL url = new URL(VERSIONURL+AbstractOS.getCurrentOS().shortName());
 79			InputStream stream = url.openStream();
 80
 81			reader = new BufferedReader(new InputStreamReader(stream));
 82			JSONParser p = new JSONParser();
 83			JSONObject versionInfo = (JSONObject) p.parse(reader);
 84			String latestVersion = (String) versionInfo.get("latest");
 85			String versionParts[] = latestVersion.split("\\.");
 86			int majorVersion = Integer.parseInt(versionParts[0]);
 87			int minorVersion = Integer.parseInt(versionParts[1]);
 88			if (majorVersion > ParallelSets.MAJOR_VERSION ||
 89				(majorVersion == ParallelSets.MAJOR_VERSION && minorVersion > ParallelSets.MINOR_VERSION)) {
 90				JSONArray versions = (JSONArray) versionInfo.get("versions");
 91				StringBuilder versionsHTML = new StringBuilder("<html>");
 92				for (Object v : versions) {
 93					JSONObject version = (JSONObject) v;
 94					versionsHTML.append("<p><b>Version ");
 95					versionsHTML.append(version.get("version")+"</b> (released ");
 96					versionsHTML.append(version.get("release_date")+")</p>");
 97					versionsHTML.append(version.get("notes"));
 98				}
 99				versionsHTML.append("</html>");
100				JTextPane textPane = new JTextPane();
101				textPane.setContentType("text/html");
102				textPane.setText(versionsHTML.toString());
103				textPane.setEditable(false);
104				Box b = new Box(BoxLayout.Y_AXIS);
105				b.add(new JLabel(MESSAGE1));
106				b.add(Box.createVerticalStrut(5));
107				JScrollPane scrollPane = new JScrollPane(textPane);
108				scrollPane.setPreferredSize(new Dimension(300, 300));
109				b.add(scrollPane);
110				b.add(new JLabel(MESSAGE2));
111				int response = JOptionPane.showConfirmDialog(frame, b, TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
112				if (response == 0)
113					Desktop.getDesktop().browse(new URI(ParallelSets.WEBSITE));
114			}
115		} catch (RuntimeException e) { // to make FindBugs happy
116			PSLogging.logger.info("Could not check for new version.", e);
117		} catch (Exception e) {
118			PSLogging.logger.info("Could not check for new version.", e);
119		} finally {
120			if (reader != null)
121				try {
122					reader.close();
123				} catch (IOException e) {
124					PSLogging.logger.warn("Error closing stream.", e);
125				}
126		}
127	}
128}