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