Search in sources :

Example 1 with SourcePathChangedEvent

use of org.rstudio.studio.client.workbench.views.source.events.SourcePathChangedEvent in project rstudio by rstudio.

the class Files method onRenameFile.

@Handler
void onRenameFile() {
    // get currently selected files
    ArrayList<FileSystemItem> selectedFiles = view_.getSelectedFiles();
    // validation: some selection exists
    if (selectedFiles.size() == 0)
        return;
    // validation: no more than one file selected
    if (selectedFiles.size() > 1) {
        globalDisplay_.showErrorMessage("Invalid Selection", "Please select only one file to rename");
        return;
    }
    // validation -- not prohibited move of public folder
    if (!validateNotRestrictedFolder(selectedFiles, "renamed"))
        return;
    // prompt for new file name then execute the rename
    final FileSystemItem file = selectedFiles.get(0);
    globalDisplay_.promptForText("Rename File", "Please enter the new file name:", file.getName(), 0, file.getStem().length(), null, new ProgressOperationWithInput<String>() {

        public void execute(String input, final ProgressIndicator progress) {
            progress.onProgress("Renaming file...");
            String path = file.getParentPath().completePath(input);
            final FileSystemItem target = file.isDirectory() ? FileSystemItem.createDir(path) : FileSystemItem.createFile(path);
            // clear selection
            view_.selectNone();
            // premptively rename in the UI then fallback to refreshing
            // the view if there is an error
            view_.renameFile(file, target);
            // execute on the server
            server_.renameFile(file, target, new VoidServerRequestCallback(progress) {

                @Override
                protected void onSuccess() {
                    // if we were successful, let editor know
                    if (!file.isDirectory()) {
                        eventBus_.fireEvent(new SourcePathChangedEvent(file.getPath(), target.getPath()));
                    }
                }

                @Override
                protected void onFailure() {
                    onRefreshFiles();
                }
            });
        }
    });
}
Also used : FileSystemItem(org.rstudio.core.client.files.FileSystemItem) SourcePathChangedEvent(org.rstudio.studio.client.workbench.views.source.events.SourcePathChangedEvent) OpenFileInBrowserHandler(org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserHandler) Handler(org.rstudio.core.client.command.Handler)

Aggregations

Handler (org.rstudio.core.client.command.Handler)1 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)1 OpenFileInBrowserHandler (org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserHandler)1 SourcePathChangedEvent (org.rstudio.studio.client.workbench.views.source.events.SourcePathChangedEvent)1