<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package usda.weru.weps;

import de.schlichtherle.truezip.file.TFile;
import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import usda.weru.util.Util;
import usda.weru.wmrm.Wmrm;

/**
 *
 */
public class RunExporter extends usda.weru.weps.gui.RunExporter_n implements PropertyChangeListener { 
    
    private static final long serialVersionUID = 1L;
    private boolean zipChecked = true;
    private boolean removeSource = false;
    private TFile c_sourceRun;
    private TFile c_dest;
     
     /**
     *
     * @param source
     * @param dest
     * @param removeSource
     */
    public RunExporter(TFile source, TFile dest, boolean removeSource, Weps parent){
        super(source);
        c_sourceRun = source;
        c_dest = dest;
        setRemoveCheckBox(removeSource);
        display(parent);
    }
    private void display(Component parent) {
        JOptionPane pane = new JOptionPane(this, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION,null, new String[]{"Export", "Cancel"}, "Export");
        JDialog dialog = pane.createDialog(parent, "Run Exporter");
        dialog.setResizable(true);
        dialog.setVisible(true);
        
        //error checking after clicking export
        if ("Export".equals(pane.getValue())) {
            c_sourceRun = g_source.getFile();
            c_dest = g_dest.getFile();
            
            
             
            if (c_sourceRun == null || !c_sourceRun.exists() || !c_sourceRun.getAbsolutePath().contains(".wjr")) {
                JOptionPane.showMessageDialog(this, "The specified source run does not exist.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                display(parent);
                g_source.requestFocus();
                return;
            }

            if (c_dest == null || !c_dest.isDirectory()) {
                JOptionPane.showMessageDialog(this, "The specified export location does not exist.",
                        "Error", JOptionPane.ERROR_MESSAGE);
                display(parent);
                g_source.requestFocus();
                return;
            }
            TFile destRun;
            if(zipChecked){
                File f = new File(c_dest.getAbsolutePath());
                String [] pathnames = f.list();
                for(String pathname : pathnames) {
                    if(pathname.equals(c_sourceRun.getName() + ".zip")){
                        int overwrite = JOptionPane.showOptionDialog(this, "The file: \"" + pathname
                        + "\" already exists as a zip file.\nDo you want to overwrite the file?", "Confirm Overwrite",
                        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                        null, new String[]{"Overwrite", "No"}, "No");
                        if (overwrite != JOptionPane.YES_OPTION) {
                            display(parent);
                            return;

                        }
                    }
                }
                
                boolean compressed = compress(c_sourceRun.getAbsolutePath(), c_dest.getAbsolutePath() + "\\"+ c_sourceRun.getName());
                if (!compressed) {
                     //tell user we couldn't remove the source run.
                     JOptionPane.showMessageDialog(this, "Unable to successfully zip run to export directory",
                             "Error", JOptionPane.ERROR_MESSAGE);
                             display(parent);
                }
            }else{
                destRun = new TFile(c_dest, c_sourceRun.getName());
            

                if (destRun.exists()) {
                    //Confirm the overwrite.
                    String cutPath = destRun.getAbsolutePath();
                    int sub = cutPath.length() - 40;
                    if (sub &gt; 0) {
                        cutPath = cutPath.substring(sub);
                        cutPath = "..." + cutPath;
                    }

                    int overwrite = JOptionPane.showOptionDialog(this, "The file: \"" + cutPath
                            + "\" already exist.\nDo you want to overwrite the file?", "Confirm Overwrite",
                            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                            null, new String[]{"Overwrite", "No"}, "No");
                    if (overwrite != JOptionPane.YES_OPTION) {
                        display(parent);
                        return;
                    }
                }

                boolean copied = Util.copyDirectory(c_sourceRun, destRun, true);
                if (!copied) {
                    //Tell the user we couldn't copy everything
                    JOptionPane.showMessageDialog(this, "Unable to successfully export run.",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    display(parent);
                    g_source.requestFocus();
                    return;
                }
            }
            if (removeSource) {
                boolean removed = Util.deleteAll(c_sourceRun);
                if (!removed) {
                    //tell user we couldn't remove the source run.
                    JOptionPane.showMessageDialog(this, "Unable to successfully remove source run.",
                            "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
         
        }
    }
    public static boolean compress(String dirPath, String zipName) {
        final Path sourceDir = Paths.get(dirPath);
        String zipFileName = zipName.concat(".zip");
        try {
            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor&lt;Path&gt;() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
    }
    
    public void setRemoveCheckBox(boolean removeSource) {
        this.removeSource = removeSource;
    }
    
    @Override
    protected void zipContentsActionPerformed(java.awt.event.ActionEvent evt){
        zipChecked =  g_zipContents.isSelected();      
    }
    @Override
    protected void removeCheckBoxActionPerformed(java.awt.event.ActionEvent evt){
        removeSource = g_removeCheckBox.isSelected();
    }
 
   
}
</pre></body></html>