/**
 * 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.ArchiPlugin;
import com.archimatetool.editor.preferences.IPreferenceConstants;
import com.archimatetool.editor.utils.StringUtils;



/**
 * Check for New Action
 * 
 * @author Phillip Beauvoir
 */
public class CheckForNewVersionAction extends Action {
    
    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 {
            String versionFile = ArchiPlugin.PREFERENCES.getString(IPreferenceConstants.UPDATE_URL);
            
            if(!StringUtils.isSet(versionFile)) {
                return;
            }
            
            URL url = new URL(versionFile);
            String newVersion = getOnlineVersion(url);
            
            // Get this app's main version number
            String thisVersion = ArchiPlugin.INSTANCE.getVersion();
            
            if(StringUtils.compareVersionNumbers(newVersion, thisVersion) > 0) {
                String downloadURL = ArchiPlugin.PREFERENCES.getString(IPreferenceConstants.DOWNLOAD_URL);
                
                // No download URL
                if(!StringUtils.isSet(downloadURL)) {
                    MessageDialog.openInformation(null, Messages.CheckForNewVersionAction_1,
                            Messages.CheckForNewVersionAction_2 + " (" + newVersion + "). "); //$NON-NLS-1$ //$NON-NLS-2$
                    return;
                }

                // Does have download URL
                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(downloadURL);
                        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 + " " + ex.getMessage()); //$NON-NLS-1$
            return;
        }
        catch(PartInitException ex) {
            ex.printStackTrace();
        }

    };
    
    @Override
    public boolean isEnabled() {
        String versionFile = ArchiPlugin.PREFERENCES.getString(IPreferenceConstants.UPDATE_URL);
        return StringUtils.isSet(versionFile);
    }
    
    private void showErrorMessage(String message) {
        MessageDialog.openError(null, Messages.CheckForNewVersionAction_6, message);
    }
}

@@ -347,37 +347,206 @@
 ava.net.
-MalformedURLException
+Authenticator;%0D%0Aimport java.net.HttpURLConnection;%0D%0Aimport java.net.InetSocketAddress;%0D%0Aimport java.net.MalformedURLException;%0D%0Aimport java.net.PasswordAuthentication;%0D%0Aimport java.net.Proxy
 ;%0D%0Aimpor
@@ -593,16 +593,45 @@
 ection;%0D
+%0Aimport java.net.Proxy.Type;%0D
 %0A%0D%0Aimpor
@@ -1933,24 +1933,1177 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Get the connection through a Proxy%0D%0A     * Should we ever implement Proxy support in Archi this is the pattern to use%0D%0A     * %0D%0A     * coArchi sets Authenticator.setDefault(a) and resets it with Authenticator.setDefault(null)%0D%0A     * but this does not clear Java's cached authentication once Authenticator.setDefault(a) has been called%0D%0A     * so we need to explicitly set one on the connection here%0D%0A     */%0D%0A    @SuppressWarnings(%22unused%22)%0D%0A    private URLConnection getProxyConnection(URL url) throws IOException %7B%0D%0A        InetSocketAddress socketAddress = new InetSocketAddress(%221.1.1.1%22, 1234); //$NON-NLS-1$%0D%0A        Proxy proxy = new Proxy(Type.HTTP, socketAddress);%0D%0A        %0D%0A        HttpURLConnection connection = (HttpURLConnection)url.openConnection(proxy);%0D%0A        %0D%0A        connection.setAuthenticator(new Authenticator() %7B%0D%0A            @Override%0D%0A            protected PasswordAuthentication getPasswordAuthentication() %7B%0D%0A                return new PasswordAuthentication(%22username%22, %22password%22.toCharArray()); //$NON-NLS-1$ //$NON-NLS-2$%0D%0A            %7D%0D%0A        %7D);%0D%0A        %0D%0A        return connection;%0D%0A    %7D%0D%0A    %0D%0A
     @Overrid

Add placeholder code for getting a URLConnection with a Proxy