/**
 * This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 */
package com.archimatetool.editor.actions;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;

import com.archimatetool.editor.Application;
import com.archimatetool.editor.utils.StringUtils;



/**
 * Check for New Action
 * 
 * @author Phillip Beauvoir
 */
public class CheckForNewVersionAction extends Action {
    
    String versionFile = "http://www.archimatetool.com/archi-version.txt"; //$NON-NLS-1$
    
    String downloadPage = "http://www.archimatetool.com/download"; //$NON-NLS-1$
    
    public CheckForNewVersionAction() {
        super(Messages.CheckForNewVersionAction_0);
    }
    
    String getOnlineVersion(URL url) throws IOException {
        URLConnection connection = url.openConnection();
        connection.connect();
        
        InputStream is = connection.getInputStream();
        char[] buf = new char[32];
        Reader r = new InputStreamReader(is, "UTF-8"); //$NON-NLS-1$
        StringBuilder s = new StringBuilder();
        while(true) {
            int n = r.read(buf);
            if(n < 0) {
                break;
            }
            s.append(buf, 0, n);
        }
        
        is.close();
        r.close();
        
        return s.toString();
    }

    @Override
    public void run() {
        try {
            URL url = new URL(versionFile);
            String newVersion = getOnlineVersion(url);
            
            // Get this app's main version number
            String thisVersion = System.getProperty(Application.APPLICATION_VERSIONID);
            
            if(StringUtils.compareVersionNumbers(newVersion, thisVersion) > 0) {
                boolean reply = MessageDialog.openQuestion(null, Messages.CheckForNewVersionAction_1,
                        Messages.CheckForNewVersionAction_2 +
                        " (" + newVersion + "). " + //$NON-NLS-1$ //$NON-NLS-2$
                        Messages.CheckForNewVersionAction_3);
                
                if(reply) {
                    IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
                    IWebBrowser browser = support.getExternalBrowser();
                    if(browser != null) {
                        URL url2 = new URL(downloadPage);
                        browser.openURL(url2);
                    }
                }
            }
            else {
                MessageDialog.openInformation(null, Messages.CheckForNewVersionAction_1, Messages.CheckForNewVersionAction_4);
            }
        }
        catch(MalformedURLException ex) {
            ex.printStackTrace();
        }
        catch(IOException ex) {
            ex.printStackTrace();
            showErrorMessage(Messages.CheckForNewVersionAction_5);
            return;
        }
        catch(PartInitException ex) {
            ex.printStackTrace();
        }

    };
    
    private void showErrorMessage(String message) {
        MessageDialog.openError(null, Messages.CheckForNewVersionAction_6, message);
    }
}

@@ -954,32 +954,33 @@
 sionFile = %22http
+s
 ://www.archimate
@@ -1060,16 +1060,17 @@
  = %22http
+s
 ://www.a

Check for new version uses HTTPS URL