001package apps.TrainCrew;
002
003import java.awt.event.ActionEvent;
004import java.io.File;
005import java.io.InputStream;
006import java.net.URI;
007import java.net.URL;
008import javax.swing.Icon;
009import javax.swing.JPanel;
010import jmri.util.swing.JmriAbstractAction;
011import jmri.util.swing.WindowInterface;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Install TrainCrew app from a URL
017 *
018 * @author Bob Jacobsen Copyright (C) 2018
019 */
020public class InstallFromURL extends JmriAbstractAction {
021
022    public InstallFromURL(String s, WindowInterface wi) {
023        super(s, wi);
024    }
025
026    public InstallFromURL(String s, Icon i, WindowInterface wi) {
027        super(s, i, wi);
028    }
029
030    public InstallFromURL(String s) {
031        super(s);
032    }
033
034    public InstallFromURL(String s, JPanel who) {
035        super(s);
036    }
037
038    public InstallFromURL() {
039        super(Bundle.getMessage("TrainCrewInstallMenu"));
040    }
041
042    JPanel _who;
043
044    @Override
045    public void actionPerformed(ActionEvent e) {
046
047        // get the input URL
048        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("apps.TrainCrew.InstallFromURL");
049        String urlString = bundle.getString("TrainCrewZipURL");
050        String targetDirectory = bundle.getString("TrainCrewInstallDirectory");
051        
052        log.info("Will install from {} to {}", urlString, targetDirectory);
053        
054        try {
055            // create URL
056            URL url = new URI(urlString).toURL();
057
058            try {
059                // open connection
060                InputStream inStream = url.openConnection().getInputStream();
061                
062                // transfer and unpack
063                jmri.util.UnzipFileClass.unzipFunction(new File(targetDirectory), inStream);
064                
065                log.info("Complete!");
066            } catch (java.io.IOException ex) {
067                log.error("Error in transfer", ex);
068            }
069        } catch (java.net.MalformedURLException | java.net.URISyntaxException ex) {
070            log.error("Invalid URL", ex);
071        }
072    }
073
074    // never invoked, because we overrode actionPerformed above
075    @Override
076    public jmri.util.swing.JmriPanel makePanel() {
077        throw new IllegalArgumentException("Should not be invoked");
078    }
079
080    // initialize logging
081    private final static Logger log = LoggerFactory.getLogger(InstallFromURL.class);
082
083}