⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copyfilesandfoldersoperation.java

📁 Python Development Environment (Python IDE plugin for Eclipse). Features editor, code completion, re
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * @param monitor
     *            the monitor that information will be sent to.
     * @see WorkspaceModifyOperation
     * @see WorkspaceJob
     * @since 3.2
     */
    public void copyFilesInCurrentThread(URI[] uris, IContainer destination,
            IProgressMonitor monitor) {
        IFileStore[] stores = buildFileStores(uris);
        if (stores == null) {
            return;
        }

        copyFileStores(destination, stores, false, monitor);
    }

    /**
     * Build the collection of fileStores that map to fileNames. If any of them
     * cannot be found then match then return <code>null</code>.
     * 
     * @param uris
     * @return IFileStore[]
     */
    private IFileStore[] buildFileStores(URI[] uris) {
        IFileStore[] stores = new IFileStore[uris.length];
        for (int i = 0; i < uris.length; i++) {
            IFileStore store;
            try {
                store = EFS.getStore(uris[i]);
            } catch (CoreException e) {
                IDEWorkbenchPlugin.log(e.getMessage(), e);
                reportFileInfoNotFound(uris[i].toString());
                return null;
            }
            if (store == null) {
                reportFileInfoNotFound(uris[i].toString());
                return null;
            }
            stores[i] = store;
        }
        return stores;

    }

    /**
     * Copies the given files and folders to the destination. The current Thread
     * is halted while the resources are copied using a
     * WorkspaceModifyOperation. This method should be called from the UI
     * Thread.
     * 
     * @param fileNames
     *            names of the files to copy
     * @param destination
     *            destination to which files will be copied
     * @see WorkspaceModifyOperation
     * @see Display#getThread()
     * @see Thread#currentThread()
     * @since 3.2
     */
    public void copyFiles(final String[] fileNames, IContainer destination) {
        IFileStore[] stores = buildFileStores(fileNames);
        if (stores == null) {
            return;
        }

        copyFileStores(destination, stores, true, null);
    }

    /**
     * Copies the given files and folders to the destination without forking a
     * new Thread or blocking using a WorkspaceModifyOperation. It is
     * recommended that this method only be called from a {@link WorkspaceJob}
     * to avoid possible deadlock.
     * 
     * @param fileNames
     *            names of the files to copy
     * @param destination
     *            destination to which files will be copied
     * @param monitor
     *            the monitor that information will be sent to.
     * @see WorkspaceModifyOperation
     * @see WorkspaceJob
     * @since 3.2
     */
    public void copyFilesInCurrentThread(final String[] fileNames,
            IContainer destination, IProgressMonitor monitor) {
        IFileStore[] stores = buildFileStores(fileNames);
        if (stores == null) {
            return;
        }

        copyFileStores(destination, stores, false, monitor);
    }

    /**
     * Build the collection of fileStores that map to fileNames. If any of them
     * cannot be found then match then return null.
     * 
     * @param fileNames
     * @return IFileStore[]
     */
    private IFileStore[] buildFileStores(final String[] fileNames) {
        IFileStore[] stores = new IFileStore[fileNames.length];
        for (int i = 0; i < fileNames.length; i++) {
            IFileStore store = IDEResourceInfoUtils.getFileStore(fileNames[i]);
            if (store == null) {
                reportFileInfoNotFound(fileNames[i]);
                return null;
            }
            stores[i] = store;
        }
        return stores;
    }

    /**
     * Report that a file info could not be found.
     * 
     * @param fileName
     */
    private void reportFileInfoNotFound(final String fileName) {

        messageShell.getDisplay().syncExec(new Runnable() {
            public void run() {
                ErrorDialog
                        .openError(
                                messageShell,
                                getProblemsTitle(),
                                NLS
                                        .bind(
                                                IDEWorkbenchMessages.CopyFilesAndFoldersOperation_infoNotFound,
                                                fileName), null);
            }
        });
    }

    /**
     * Copies the given files and folders to the destination.
     * 
     * @param stores
     *            the file stores to copy
     * @param destination
     *            destination to which files will be copied
     */
    private void copyFileStores(IContainer destination,
            final IFileStore[] stores, boolean fork, IProgressMonitor monitor) {
        // test files for existence separate from validate API
        // because an external file may not exist until the copy actually
        // takes place (e.g., WinZip contents).
        IStatus fileStatus = checkExist(stores);
        if (fileStatus.getSeverity() != IStatus.OK) {
            displayError(fileStatus);
            return;
        }
        String errorMsg = validateImportDestinationInternal(destination, stores);
        if (errorMsg != null) {
            displayError(errorMsg);
            return;
        }
        final IPath destinationPath = destination.getFullPath();

        if (fork) {
            WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
                public void execute(IProgressMonitor monitor) {
                    copyFileStores(stores, destinationPath, monitor);
                }
            };
            try {
                PlatformUI.getWorkbench().getProgressService().run(true, true,
                        op);
            } catch (InterruptedException e) {
                return;
            } catch (InvocationTargetException exception) {
                display(exception);
            }
        } else {
            copyFileStores(stores, destinationPath, monitor);
        }

        // If errors occurred, open an Error dialog
        if (errorStatus != null) {
            displayError(errorStatus);
            errorStatus = null;
        }
    }

    /**
     * Display the supplied status in an error dialog.
     * 
     * @param status
     *            The status to display
     */
    private void displayError(final IStatus status) {
        messageShell.getDisplay().syncExec(new Runnable() {
            public void run() {
                ErrorDialog.openError(messageShell, getProblemsTitle(), null,
                        status);
            }
        });
    }

    /**
     * Creates a file or folder handle for the source resource as if it were to
     * be created in the destination container.
     * 
     * @param destination
     *            destination container
     * @param source
     *            source resource
     * @return IResource file or folder handle, depending on the source type.
     */
    IResource createLinkedResourceHandle(IContainer destination,
            IResource source) {
        IWorkspace workspace = destination.getWorkspace();
        IWorkspaceRoot workspaceRoot = workspace.getRoot();
        IPath linkPath = destination.getFullPath().append(source.getName());
        IResource linkHandle;

        if (source.getType() == IResource.FOLDER) {
            linkHandle = workspaceRoot.getFolder(linkPath);
        } else {
            linkHandle = workspaceRoot.getFile(linkPath);
        }
        return linkHandle;
    }

    /**
     * Removes the given resource from the workspace.
     * 
     * @param resource
     *            resource to remove from the workspace
     * @param monitor
     *            a progress monitor for showing progress and for cancelation
     * @return true the resource was deleted successfully false the resource was
     *         not deleted because a CoreException occurred
     */
    boolean delete(IResource resource, IProgressMonitor monitor) {
        boolean force = false; // don't force deletion of out-of-sync resources

        if (resource.getType() == IResource.PROJECT) {
            // if it's a project, ask whether content should be deleted too
            IProject project = (IProject) resource;
            try {
                project.delete(true, force, monitor);
            } catch (CoreException e) {
                recordError(e); // log error
                return false;
            }
        } else {
            // if it's not a project, just delete it
            int flags = IResource.KEEP_HISTORY;
            if (force) {
                flags = flags | IResource.FORCE;
            }
            try {
                resource.delete(flags, monitor);
            } catch (CoreException e) {
                recordError(e); // log error
                return false;
            }
        }
        return true;
    }

    /**
     * Opens an error dialog to display the given message.
     * 
     * @param message
     *            the error message to show
     */
    private void displayError(final String message) {
        messageShell.getDisplay().syncExec(new Runnable() {
            public void run() {
                MessageDialog.openError(messageShell, getProblemsTitle(),
                        message);
            }
        });
    }

    /**
     * Returns the resource either casted to or adapted to an IFile.
     * 
     * @param resource
     *            resource to cast/adapt
     * @return the resource either casted to or adapted to an IFile.
     *         <code>null</code> if the resource does not adapt to IFile
     */
    protected IFile getFile(IResource resource) {
        if (resource instanceof IFile) {
            return (IFile) resource;
        }
        return (IFile) ((IAdaptable) resource).getAdapter(IFile.class);
    }

    /**
     * Returns java.io.File objects for the given file names.
     * 
     * @param fileNames
     *            files to return File object for.
     * @return java.io.File objects for the given file names.
     * @deprecated This method is not longer in use anywhere in this class and
     *             is only provided for backwards compatability with subclasses
     *             of the receiver.
     */
    protected File[] getFiles(String[] fileNames) {
        File[] files = new File[fileNames.length];

        for (int i = 0; i < fileNames.length; i++) {
            files[i] = new File(fileNames[i]);
        }
        return files;
    }

    /**
     * Returns the resource either casted to or adapted to an IFolder.
     * 
     * @param resource
     *            resource to cast/adapt
     * @return the resource either casted to or adapted to an IFolder.
     *         <code>null</code> if the resource does not adapt to IFolder
     */
    protected IFolder getFolder(IResource resource) {
        if (resource instanceof IFolder) {
            return (IFolder) resource;
        }
        return (IFolder) ((IAdaptable) resource).getAdapter(IFolder.class);
    }

    /**
     * Returns a new name for a copy of the resource at the given path in the
     * given workspace.
     * 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -