/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

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