同时选择目录和文件

时间:2017-03-14 05:39:24

标签: java swt

我在复制文件和目录方面遇到了一些问题。我有一个按钮和一个源路径和目标路径的文本框。

我想知道如果我点击源路径我可以选择目录是否可能,我也可以选择要复制的文件并将其放到目标路径。

这是我目前的代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
//import org.eclipse.swt.graphics.Path;
//import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.DateTime;
//import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.DirectoryDialog;

//import java.io.BufferedInputStream;
//import java.io.BufferedOutputStream;
import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
//import java.util.ArrayList;
//import java.util.Collection;

//import javax.swing.SwingWorker;
//import org.eclipse.swt.custom.CLabel;

public class FortryApplication {

    protected static Shell shell;
    private static Text txtSource;
    private static Text txtDestination;
    private static Text txt1;
    InputStream inStream = null;
    OutputStream outStream = null;
    public Display display;

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 400);
        shell.setText("Connection Manager");

        Label lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setFont(SWTResourceManager.getFont("Segoe UI", 12, SWT.NORMAL));
        lblNewLabel.setBounds(10, 0, 107, 25);
        lblNewLabel.setText("Source File ");

        Button btnSourceSearch = new Button(shell, SWT.NONE);
        btnSourceSearch.setBounds(10, 31, 75, 25);
        btnSourceSearch.setText("Search");


        btnSourceSearch.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {

                DirectoryDialog dlg = new DirectoryDialog(shell);
                dlg.setFilterPath(txtSource.getText());
                dlg.setMessage("Select a source file to transfer");
                String dir = dlg.open();
                if (dir != null) {
                    txtSource.setText(dir);
                }
            }
        });
        txtSource = new Text(shell, SWT.BORDER);
        txtSource.setBounds(91, 31, 333, 25);

        Label lblNewLabel_1 = new Label(shell, SWT.NONE);
        lblNewLabel_1.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
        lblNewLabel_1.setBounds(10, 62, 107, 25);
        lblNewLabel_1.setText("Destination File");

        Button btnDestinationSearch = new Button(shell, SWT.NONE);
        btnDestinationSearch.setBounds(10, 93, 75, 25);
        btnDestinationSearch.setText("Search");

        btnDestinationSearch.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                DirectoryDialog dialog = new DirectoryDialog(shell, SWT.SAVE);
                dialog.setFilterPath(txtDestination.getText());
                String dir = dialog.open();
                if(dir != null){
                    txtDestination.setText(dir);
                }
            }
        });
        txtDestination = new Text(shell, SWT.BORDER);
        txtDestination.setBounds(91, 93, 333, 25);

        Label lblDateRange = new Label(shell, SWT.NONE);
        lblDateRange.setBounds(10, 147, 81, 15);
        lblDateRange.setText("Date Range:");

        Button btnStartCopy = new Button(shell, SWT.NONE);
        btnStartCopy.setBounds(111, 249, 75, 25);
        btnStartCopy.setText("Start Copy");


        btnStartCopy.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                File srcFolder = new File(txtSource.getText());
                File destFolder = new File(txtDestination.getText());

                if(!srcFolder.exists()){
                    MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
                    msgBox.setText("Information");
                    msgBox.setMessage("Directory does not exist.");
                    msgBox.open();
                }else{
                    try{
                        copyFolder(srcFolder,destFolder);
                    }catch(IOException e1){
                        e1.printStackTrace();
                    }
                    txt1.append("Finished Copying.....\n");
                    MessageBox msgBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
                    msgBox.setText("Information");
                    msgBox.setMessage("Done Copying...");
                    msgBox.open();
                }
            }
        });


        Button btnCancel = new Button(shell, SWT.NONE);
        btnCancel.setBounds(233, 249, 75, 25);
        btnCancel.setText("Cancel");

        btnCancel.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {

            }
        });

        Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
        label.setBounds(0, 301, 434, 2);

        Label lblFrom = new Label(shell, SWT.NONE);
        lblFrom.setBounds(48, 172, 55, 15);
        lblFrom.setText("From");

        DateTime ddFrom = new DateTime(shell, SWT.NONE);
        ddFrom.setBounds(48, 193, 119, 23);

        Label label_1 = new Label(shell, SWT.NONE);
        label_1.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
        label_1.setBounds(207, 190, 22, 25);
        label_1.setText(":");

        DateTime ddTo = new DateTime(shell, SWT.NONE);
        ddTo.setBounds(253, 193, 119, 23);

        Label lblTo = new Label(shell, SWT.NONE);
        lblTo.setText("To");
        lblTo.setBounds(253, 172, 55, 15);

        txt1 = new Text(shell, SWT.BORDER | SWT.READ_ONLY | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
        txt1.setBounds(0,301,434,59);
        txt1.setEnabled(true);
        txt1.setText("Progress\n");

        Label lblStatus = new Label(shell, SWT.NONE);
        lblStatus.setBounds(6, 285, 55, 15);
        lblStatus.setText("Status:");

    }

    public static void copyFolder(File src, File dest)
            throws IOException{
        if(src.isDirectory()){
            if (!dest.exists())
            {
                dest.mkdir();
                txt1.append("Directory created : " + dest + "\n");
            }
            String files[] = src.list();
            for (String file : files)
            {
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);

                //Recursive function call
                copyFolder(srcFile, destFile);
            }
        }
        else{
            Files.copy(src.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
            txt1.append("Copying " + src.getAbsolutePath() + "\n");

        }
//      if (src.exists()) {
//          src.delete();
//      }
    }
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {

        try {
            FortryApplication window = new FortryApplication();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

PS:我使用的是SWT和WindowsBuilder,我只能选择目录而不是文件。

1 个答案:

答案 0 :(得分:0)

查看FileDialog而不是使用DirectoryDialog

示例:

   FileDialog dialog = new FileDialog(shell, SWT.OPEN);
   dialog.setFilterExtensions(new String [] {"*.html"});
   dialog.setFilterPath("c:\\temp");
   String result = dialog.open();

FAQ How do I prompt the user to select a file or a directory?