Skip to content

Commit

Permalink
refactor load/save console commands into common codepath
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed May 18, 2011
1 parent bec0178 commit d0ca795
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 169 deletions.
3 changes: 2 additions & 1 deletion src/gwt/src/org/rstudio/studio/client/RStudioGinModule.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
114 changes: 114 additions & 0 deletions 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<FileSystemItem>()
{
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<FileSystemItem>()
{
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_;
}
37 changes: 0 additions & 37 deletions src/gwt/src/org/rstudio/studio/client/common/WorkbenchHelper.java

This file was deleted.

13 changes: 8 additions & 5 deletions src/gwt/src/org/rstudio/studio/client/workbench/Workbench.java
Expand Up @@ -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;
Expand All @@ -52,7 +52,8 @@ public Workbench(WorkbenchMainView view,
Server server,
Commands commands,
RemoteFileSystemContext fsContext,
FileDialogs fileDialogs)
FileDialogs fileDialogs,
ConsoleDispatcher consoleDispatcher)
{
view_ = view;
workbenchContext_ = workbenchContext;
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -121,12 +121,14 @@ public Files(Display view,
Commands commands,
Provider<FilesCopy> pFilesCopy,
Provider<FilesUpload> pFilesUpload,
FileTypeRegistry fileTypeRegistry)
FileTypeRegistry fileTypeRegistry,
ConsoleDispatcher consoleDispatcher)
{
super(view);
view_ = view ;
view_.setObserver(new DisplayObserver());
fileTypeRegistry_ = fileTypeRegistry;
consoleDispatcher_ = consoleDispatcher;

eventBus_ = eventBus;
server_ = server;
Expand Down Expand Up @@ -520,7 +522,7 @@ public void execute(final ProgressIndicator progress)
@Handler
void onSyncWorkingDir()
{
WorkbenchHelper.sendSetWdToConsole(currentPath_, eventBus_);
consoleDispatcher_.executeSetWd(currentPath_);
}

@Handler
Expand Down Expand Up @@ -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_ ;
Expand Down
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -447,60 +438,17 @@ void onLoadHistory()
{
view_.bringToFront();

fileDialogs_.openFile(
"Load History",
fsContext_,
workbenchContext_.getCurrentWorkingDir(),
new ProgressOperationWithInput<FileSystemItem>()
{
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<FileSystemItem>()
{
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
Expand Down Expand Up @@ -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_;
}

0 comments on commit d0ca795

Please sign in to comment.