diff --git a/src/gwt/src/org/rstudio/studio/client/RStudioGinModule.java b/src/gwt/src/org/rstudio/studio/client/RStudioGinModule.java index 88217dad2bc..7d893896438 100644 --- a/src/gwt/src/org/rstudio/studio/client/RStudioGinModule.java +++ b/src/gwt/src/org/rstudio/studio/client/RStudioGinModule.java @@ -21,6 +21,7 @@ import org.rstudio.studio.client.application.events.EventBus; import org.rstudio.studio.client.application.model.ApplicationServerOperations; import org.rstudio.studio.client.application.ui.ApplicationWindow; +import org.rstudio.studio.client.common.ConsoleDispatcher; import org.rstudio.studio.client.common.DefaultGlobalDisplay; import org.rstudio.studio.client.common.GlobalDisplay; import org.rstudio.studio.client.common.codetools.CodeToolsServerOperations; @@ -95,7 +96,7 @@ protected void configure() bind(ClientStateUpdater.class).asEagerSingleton(); bind(Commands.class).in(Singleton.class); bind(DefaultCRANMirror.class).in(Singleton.class); - + bind(ConsoleDispatcher.class).in(Singleton.class); bind(ChooseFile.class).asEagerSingleton(); bind(AceThemes.class).asEagerSingleton(); diff --git a/src/gwt/src/org/rstudio/studio/client/common/ConsoleDispatcher.java b/src/gwt/src/org/rstudio/studio/client/common/ConsoleDispatcher.java new file mode 100644 index 00000000000..7823ba7845f --- /dev/null +++ b/src/gwt/src/org/rstudio/studio/client/common/ConsoleDispatcher.java @@ -0,0 +1,114 @@ +package org.rstudio.studio.client.common; + +import org.rstudio.core.client.files.FileSystemItem; +import org.rstudio.core.client.files.FilenameTransform; +import org.rstudio.core.client.widget.ProgressIndicator; +import org.rstudio.core.client.widget.ProgressOperationWithInput; +import org.rstudio.studio.client.application.events.EventBus; +import org.rstudio.studio.client.workbench.WorkbenchContext; +import org.rstudio.studio.client.workbench.model.RemoteFileSystemContext; +import org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent; + +import com.google.inject.Inject; +import com.google.inject.Singleton; + + +@Singleton +public class ConsoleDispatcher +{ + @Inject + public ConsoleDispatcher(EventBus eventBus, + FileDialogs fileDialogs, + WorkbenchContext workbenchContext, + RemoteFileSystemContext fsContext) + { + eventBus_ = eventBus; + fileDialogs_ = fileDialogs; + workbenchContext_ = workbenchContext; + fsContext_ = fsContext; + } + + public void executeSetWd(FileSystemItem dir) + { + String escaped = dir.getPath().replaceAll("\\\\", "\\\\\\\\"); + if (escaped.equals("~")) + escaped = "~/"; + eventBus_.fireEvent( + new SendToConsoleEvent("setwd(\"" + escaped + "\")", true)); + } + + public void executeCommand(String command, FileSystemItem targetFile) + { + String code = command + "(\"" + targetFile.getPath() + "\")"; + eventBus_.fireEvent(new SendToConsoleEvent(code, true)); + } + + + public void saveFileAsThenExecuteCommand(String caption, + final String defaultExtension, + final String command) + { + fileDialogs_.saveFile( + caption, + fsContext_, + workbenchContext_.getCurrentWorkingDir(), + new FilenameTransform() + { + public String transform(String filename) + { + if (defaultExtension != null) + { + // auto-append .RData if that isn't the extension + String ext = FileSystemItem.getExtensionFromPath(filename); + return ext.equalsIgnoreCase(defaultExtension) + ? filename + : filename + defaultExtension; + } + else + { + return filename; + } + } + }, + new ProgressOperationWithInput() + { + public void execute( + FileSystemItem input, + ProgressIndicator indicator) + { + if (input == null) + return; + + executeCommand(command, input); + indicator.onCompleted(); + } + }); + } + + public void chooseFileThenExecuteCommand(String caption, + final String command) + { + fileDialogs_.openFile( + caption, + fsContext_, + workbenchContext_.getCurrentWorkingDir(), + new ProgressOperationWithInput() + { + public void execute(FileSystemItem input, ProgressIndicator indicator) + { + if (input == null) + return; + + executeCommand(command, input); + indicator.onCompleted(); + } + }); + + } + + + private final EventBus eventBus_; + private final FileDialogs fileDialogs_; + private final WorkbenchContext workbenchContext_; + private final RemoteFileSystemContext fsContext_; +} diff --git a/src/gwt/src/org/rstudio/studio/client/common/WorkbenchHelper.java b/src/gwt/src/org/rstudio/studio/client/common/WorkbenchHelper.java deleted file mode 100644 index eaf663a131b..00000000000 --- a/src/gwt/src/org/rstudio/studio/client/common/WorkbenchHelper.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * WorkbenchEventHelper.java - * - * Copyright (C) 2009-11 by RStudio, Inc. - * - * This program is licensed to you under the terms of version 3 of the - * GNU Affero General Public License. This program is distributed WITHOUT - * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the - * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. - * - */ -package org.rstudio.studio.client.common; - -import org.rstudio.core.client.files.FileSystemItem; -import org.rstudio.studio.client.application.events.EventBus; -import org.rstudio.studio.client.workbench.views.console.events.SendToConsoleEvent; - -public class WorkbenchHelper -{ - public static void sendSetWdToConsole(FileSystemItem dir, EventBus eventBus) - { - String escaped = dir.getPath().replaceAll("\\\\", "\\\\\\\\"); - if (escaped.equals("~")) - escaped = "~/"; - eventBus.fireEvent( - new SendToConsoleEvent("setwd(\"" + escaped + "\")", true)); - } - - public static void sendFileCommandToConsole(String command, - FileSystemItem file, - EventBus eventBus) - { - String code = command + "(\"" + file.getPath() + "\")"; - eventBus.fireEvent(new SendToConsoleEvent(code, true)); - } -} diff --git a/src/gwt/src/org/rstudio/studio/client/workbench/Workbench.java b/src/gwt/src/org/rstudio/studio/client/workbench/Workbench.java index 13339cdf1ab..77e674422c3 100644 --- a/src/gwt/src/org/rstudio/studio/client/workbench/Workbench.java +++ b/src/gwt/src/org/rstudio/studio/client/workbench/Workbench.java @@ -23,9 +23,9 @@ import org.rstudio.core.client.widget.ProgressIndicator; import org.rstudio.core.client.widget.ProgressOperationWithInput; import org.rstudio.studio.client.application.events.EventBus; +import org.rstudio.studio.client.common.ConsoleDispatcher; import org.rstudio.studio.client.common.FileDialogs; import org.rstudio.studio.client.common.GlobalDisplay; -import org.rstudio.studio.client.common.WorkbenchHelper; import org.rstudio.studio.client.common.GlobalDisplay.NewWindowOptions; import org.rstudio.studio.client.server.Server; import org.rstudio.studio.client.server.VoidServerRequestCallback; @@ -52,7 +52,8 @@ public Workbench(WorkbenchMainView view, Server server, Commands commands, RemoteFileSystemContext fsContext, - FileDialogs fileDialogs) + FileDialogs fileDialogs, + ConsoleDispatcher consoleDispatcher) { view_ = view; workbenchContext_ = workbenchContext; @@ -61,7 +62,8 @@ public Workbench(WorkbenchMainView view, server_ = server; fsContext_ = fsContext; fileDialogs_ = fileDialogs; - + consoleDispatcher_ = consoleDispatcher; + ((Binder)GWT.create(Binder.class)).bind(commands, this); // edit @@ -202,7 +204,7 @@ public void execute(FileSystemItem input, return; // set console - WorkbenchHelper.sendSetWdToConsole(input, eventBus_); + consoleDispatcher_.executeSetWd(input); // set files pane eventBus_.fireEvent(new DirectoryNavigateEvent(input)); @@ -215,10 +217,11 @@ public void execute(FileSystemItem input, private final Server server_; private final EventBus eventBus_; private final WorkbenchMainView view_; - GlobalDisplay globalDisplay_; + private final GlobalDisplay globalDisplay_; private final RemoteFileSystemContext fsContext_; private final FileDialogs fileDialogs_; private final WorkbenchContext workbenchContext_; + private final ConsoleDispatcher consoleDispatcher_; private final TimeBufferedCommand metricsChangedCommand_; private WorkbenchMetrics lastWorkbenchMetrics_; private boolean nearQuotaWarningShown_ = false; diff --git a/src/gwt/src/org/rstudio/studio/client/workbench/views/files/Files.java b/src/gwt/src/org/rstudio/studio/client/workbench/views/files/Files.java index 5122adac5fe..1246394ecf5 100644 --- a/src/gwt/src/org/rstudio/studio/client/workbench/views/files/Files.java +++ b/src/gwt/src/org/rstudio/studio/client/workbench/views/files/Files.java @@ -29,8 +29,8 @@ import org.rstudio.core.client.widget.ProgressOperation; import org.rstudio.core.client.widget.ProgressOperationWithInput; import org.rstudio.studio.client.application.events.EventBus; +import org.rstudio.studio.client.common.ConsoleDispatcher; import org.rstudio.studio.client.common.GlobalDisplay; -import org.rstudio.studio.client.common.WorkbenchHelper; import org.rstudio.studio.client.common.filetypes.FileTypeRegistry; import org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserEvent; import org.rstudio.studio.client.common.filetypes.events.OpenFileInBrowserHandler; @@ -121,12 +121,14 @@ public Files(Display view, Commands commands, Provider pFilesCopy, Provider pFilesUpload, - FileTypeRegistry fileTypeRegistry) + FileTypeRegistry fileTypeRegistry, + ConsoleDispatcher consoleDispatcher) { super(view); view_ = view ; view_.setObserver(new DisplayObserver()); fileTypeRegistry_ = fileTypeRegistry; + consoleDispatcher_ = consoleDispatcher; eventBus_ = eventBus; server_ = server; @@ -520,7 +522,7 @@ public void execute(final ProgressIndicator progress) @Handler void onSyncWorkingDir() { - WorkbenchHelper.sendSetWdToConsole(currentPath_, eventBus_); + consoleDispatcher_.executeSetWd(currentPath_); } @Handler @@ -573,6 +575,7 @@ public void requestData( private final Display view_ ; private final FileTypeRegistry fileTypeRegistry_; + private final ConsoleDispatcher consoleDispatcher_; private final FilesServerOperations server_; private final EventBus eventBus_; private final GlobalDisplay globalDisplay_ ; diff --git a/src/gwt/src/org/rstudio/studio/client/workbench/views/history/History.java b/src/gwt/src/org/rstudio/studio/client/workbench/views/history/History.java index d713d929e74..76cdc11593e 100644 --- a/src/gwt/src/org/rstudio/studio/client/workbench/views/history/History.java +++ b/src/gwt/src/org/rstudio/studio/client/workbench/views/history/History.java @@ -33,24 +33,18 @@ import org.rstudio.core.client.events.HasSelectionCommitHandlers; import org.rstudio.core.client.events.SelectionCommitEvent; import org.rstudio.core.client.events.SelectionCommitHandler; -import org.rstudio.core.client.files.FileSystemItem; -import org.rstudio.core.client.files.FilenameTransform; import org.rstudio.core.client.jsonrpc.RpcObjectList; import org.rstudio.core.client.widget.ProgressIndicator; import org.rstudio.core.client.widget.ProgressOperation; -import org.rstudio.core.client.widget.ProgressOperationWithInput; import org.rstudio.studio.client.application.events.EventBus; -import org.rstudio.studio.client.common.FileDialogs; +import org.rstudio.studio.client.common.ConsoleDispatcher; import org.rstudio.studio.client.common.GlobalDisplay; import org.rstudio.studio.client.common.SimpleRequestCallback; -import org.rstudio.studio.client.common.WorkbenchHelper; import org.rstudio.studio.client.server.ServerError; import org.rstudio.studio.client.server.ServerRequestCallback; import org.rstudio.studio.client.server.VoidServerRequestCallback; -import org.rstudio.studio.client.workbench.WorkbenchContext; import org.rstudio.studio.client.workbench.WorkbenchView; import org.rstudio.studio.client.workbench.commands.Commands; -import org.rstudio.studio.client.workbench.model.RemoteFileSystemContext; import org.rstudio.studio.client.workbench.model.Session; import org.rstudio.studio.client.workbench.model.helper.StringStateValue; import org.rstudio.studio.client.workbench.views.BasePresenter; @@ -68,7 +62,8 @@ import java.util.ArrayList; -// TODO: Make History and Workspace Save/Load stuff share codepath +// TODO: Save Workspace As... +// TODO: editor should open .Rhistory files // TODO: History menu items // TODO: Search History command (only if menu_ // TODO: Consider history restored message at startup @@ -207,9 +202,7 @@ public void dismissResults() public History(final Display view, HistoryServerOperations server, final GlobalDisplay globalDisplay, - FileDialogs fileDialogs, - WorkbenchContext workbenchContext, - RemoteFileSystemContext fsContext, + ConsoleDispatcher consoleDispatcher, EventBus events, final Session session, Commands commands, @@ -219,9 +212,7 @@ public History(final Display view, view_ = view; events_ = events; globalDisplay_ = globalDisplay; - fileDialogs_ = fileDialogs; - workbenchContext_ = workbenchContext; - fsContext_ = fsContext; + consoleDispatcher_ = consoleDispatcher; searchCommand_ = new SearchCommand(session); session_ = session; @@ -447,60 +438,17 @@ void onLoadHistory() { view_.bringToFront(); - fileDialogs_.openFile( - "Load History", - fsContext_, - workbenchContext_.getCurrentWorkingDir(), - new ProgressOperationWithInput() - { - public void execute(FileSystemItem input, ProgressIndicator indicator) - { - if (input == null) - return; - - WorkbenchHelper.sendFileCommandToConsole("loadhistory", - input, - events_); - indicator.onCompleted(); - } - }); + consoleDispatcher_.chooseFileThenExecuteCommand("Load History", + "loadhistory"); } void onSaveHistory() { view_.bringToFront(); - fileDialogs_.saveFile( - "Save History As", - fsContext_, - workbenchContext_.getCurrentWorkingDir(), - new FilenameTransform() - { - public String transform(String filename) - { - // auto-append .RData if that isn't the extension - String ext = FileSystemItem.getExtensionFromPath(filename); - return ext.equalsIgnoreCase(".rhistory") - ? filename - : filename + ".Rhistory"; - } - }, - new ProgressOperationWithInput() - { - public void execute( - FileSystemItem input, - ProgressIndicator indicator) - { - if (input == null) - return; - - WorkbenchHelper.sendFileCommandToConsole("savehistory", - input, - events_); - - indicator.onCompleted(); - } - }); + consoleDispatcher_.saveFileAsThenExecuteCommand("Save History As", + ".Rhistory", + "savehistory"); } @Handler @@ -706,7 +654,5 @@ public void onError(ServerError error) private final SearchCommand searchCommand_; private HistoryServerOperations server_; private final Session session_; - private final WorkbenchContext workbenchContext_; - private final RemoteFileSystemContext fsContext_; - private final FileDialogs fileDialogs_; + private final ConsoleDispatcher consoleDispatcher_; } diff --git a/src/gwt/src/org/rstudio/studio/client/workbench/views/workspace/Workspace.java b/src/gwt/src/org/rstudio/studio/client/workbench/views/workspace/Workspace.java index 70c8f36a6db..d43896f5c78 100644 --- a/src/gwt/src/org/rstudio/studio/client/workbench/views/workspace/Workspace.java +++ b/src/gwt/src/org/rstudio/studio/client/workbench/views/workspace/Workspace.java @@ -18,13 +18,12 @@ import org.rstudio.core.client.command.CommandBinder; import org.rstudio.core.client.command.Handler; import org.rstudio.core.client.files.FileSystemItem; -import org.rstudio.core.client.files.FilenameTransform; import org.rstudio.core.client.jsonrpc.RpcObjectList; import org.rstudio.core.client.widget.*; import org.rstudio.studio.client.application.events.EventBus; +import org.rstudio.studio.client.common.ConsoleDispatcher; import org.rstudio.studio.client.common.FileDialogs; import org.rstudio.studio.client.common.GlobalDisplay; -import org.rstudio.studio.client.common.WorkbenchHelper; import org.rstudio.studio.client.common.filetypes.events.OpenDataFileEvent; import org.rstudio.studio.client.common.filetypes.events.OpenDataFileHandler; import org.rstudio.studio.client.server.ServerError; @@ -72,6 +71,7 @@ public Workspace(Workspace.Display view, GlobalDisplay globalDisplay, FileDialogs fileDialogs, WorkbenchContext workbenchContext, + ConsoleDispatcher consoleDispatcher, RemoteFileSystemContext fsContext, Commands commands, Binder binder) @@ -84,6 +84,7 @@ public Workspace(Workspace.Display view, server_ = server; globalDisplay_ = globalDisplay ; workbenchContext_ = workbenchContext; + consoleDispatcher_ = consoleDispatcher; fsContext_ = fsContext; objects_ = view_.getWorkspaceObjectTable(); @@ -187,34 +188,10 @@ public void execute(final ProgressIndicator indicator) void onSaveWorkspace() { view_.bringToFront(); - fileDialogs_.saveFile( - "Save Workspace", - fsContext_, - workbenchContext_.getCurrentWorkingDir(), - new FilenameTransform() - { - public String transform(String filename) - { - // auto-append .RData if that isn't the extension - String ext = FileSystemItem.getExtensionFromPath(filename); - return ext.equalsIgnoreCase(".rdata") - ? filename - : filename + ".RData"; - } - }, - new ProgressOperationWithInput() - { - public void execute( - FileSystemItem input, - ProgressIndicator indicator) - { - if (input == null) - return; - - sendWorkspaceCommandToConsole("save.image", input); - indicator.onCompleted(); - } - }); + + consoleDispatcher_.saveFileAsThenExecuteCommand("Save Workspace", + ".RData", + "save.image"); } @@ -228,21 +205,7 @@ void onSaveDefaultWorkspace() void onLoadWorkspace() { view_.bringToFront(); - fileDialogs_.openFile( - "Load Workspace", - fsContext_, - workbenchContext_.getCurrentWorkingDir(), - new ProgressOperationWithInput() - { - public void execute(FileSystemItem input, ProgressIndicator indicator) - { - if (input == null) - return; - - sendWorkspaceCommandToConsole("load", input); - indicator.onCompleted(); - } - }); + consoleDispatcher_.chooseFileThenExecuteCommand("Load Workspace", "load"); } @@ -265,9 +228,9 @@ public void onOpenDataFile(OpenDataFileEvent event) new ProgressOperation() { public void execute(ProgressIndicator indicator) { - sendWorkspaceCommandToConsole( - "load", - FileSystemItem.createFile(dataFilePath)); + consoleDispatcher_.executeCommand( + "load", + FileSystemItem.createFile(dataFilePath)); indicator.onCompleted(); } @@ -280,18 +243,9 @@ private void sendDefaultWorkspaceCommandToConsole(String command) { FileSystemItem cwd = workbenchContext_.getCurrentWorkingDir(); FileSystemItem wsPath = FileSystemItem.createFile(cwd.completePath(".RData")); - sendWorkspaceCommandToConsole(command, wsPath); + consoleDispatcher_.executeCommand(command, wsPath); } - private void sendWorkspaceCommandToConsole(String command, - FileSystemItem workspaceFile) - { - WorkbenchHelper.sendFileCommandToConsole(command, - workspaceFile, - eventBus_); - } - - void onImportDatasetFromFile() { @@ -537,5 +491,6 @@ public boolean containsObject(RpcObjectList objects, private final WorkspaceObjectTable objects_; private final WorkbenchContext workbenchContext_; private final RemoteFileSystemContext fsContext_; + private final ConsoleDispatcher consoleDispatcher_; private final FileDialogs fileDialogs_; }