Skip to content

Commit

Permalink
infrastructure required for creating new docs with initial contents
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Mar 16, 2012
1 parent bfb388e commit c180b0e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/cpp/session/modules/SessionSource.cpp
Expand Up @@ -141,16 +141,21 @@ Error newDocument(const json::JsonRpcRequest& request,
{
// params
std::string type;
json::Value jsonContents;
json::Object properties;
Error error = json::readParams(request.params,
&type,
&jsonContents,
&properties);
if (error)
return error ;

// create the new doc and write it to the database
boost::shared_ptr<SourceDocument> pDoc(new SourceDocument(type)) ;

if (json::isType<std::string>(jsonContents))
pDoc->setContents(jsonContents.get_str());

pDoc->editProperties(properties);

error = source_database::put(pDoc);
Expand Down
Expand Up @@ -980,12 +980,15 @@ public void writeProjectVcsOptions(RProjectVcsOptions options,
}

public void newDocument(String filetype,
String contents,
JsObject properties,
ServerRequestCallback<SourceDocument> requestCallback)
{
JSONArray params = new JSONArray();
params.set(0, new JSONString(filetype));
params.set(1, new JSONObject(properties));
params.set(1, contents != null ? new JSONString(contents) :
JSONNull.getInstance());
params.set(2, new JSONObject(properties));
sendRequest(RPC_SCOPE, NEW_DOCUMENT, params, requestCallback);
}

Expand Down
Expand Up @@ -382,7 +382,7 @@ well as menu structures (for main menu and popup menus).
desc="Create a new R script"/>

<cmd id="newSweaveDoc"
menuLabel="_Sweave..."
menuLabel="_Sweave"
desc="Create a new Sweave document"/>

<cmd id="openSourceDoc"
Expand Down
Expand Up @@ -234,6 +234,7 @@ public void onViewData(ViewDataEvent event)
{
server_.newDocument(
FileTypeRegistry.DATAFRAME.getTypeId(),
null,
JsObject.createJsObject(),
new SimpleRequestCallback<SourceDocument>("Edit Data Frame") {
public void onResponseReceived(SourceDocument response)
Expand Down Expand Up @@ -390,6 +391,7 @@ public void onShowContent(ShowContentEvent event)
ContentItem content = event.getContent();
server_.newDocument(
FileTypeRegistry.URLCONTENT.getTypeId(),
null,
(JsObject) content.cast(),
new SimpleRequestCallback<SourceDocument>("Show")
{
Expand Down Expand Up @@ -421,6 +423,7 @@ public void onShowData(ShowDataEvent event)
ensureVisible(true);
server_.newDocument(
FileTypeRegistry.DATAFRAME.getTypeId(),
null,
(JsObject) data.cast(),
new SimpleRequestCallback<SourceDocument>("Show Data Frame")
{
Expand All @@ -442,15 +445,40 @@ public void onNewSourceDoc()
@Handler
public void onNewSweaveDoc()
{
newDoc(FileTypeRegistry.SWEAVE, null);
String template = null;
/*
String template =
"\n" +
"\\documentclass{article}\n" +
"\n" +
"\\title{Sweave Example 1}\n" +
"\\author{Friedrich Leisch}\n" +
"\n" +
"\\begin{document}\n" +
"\\SweaveOpts{concordance=TRUE}" +
"\n\n" +
"\\maketitle" +
"\n\n\n" +
"\\end{document}";
*/

newDoc(FileTypeRegistry.SWEAVE, template, null);
}

private void newDoc(EditableFileType fileType,
ResultCallback<EditingTarget, ServerError> callback)
{
newDoc(fileType, null, callback);
}

private void newDoc(EditableFileType fileType,
String contents,
final ResultCallback<EditingTarget, ServerError> resultCallback)
{
ensureVisible(true);
server_.newDocument(
fileType.getTypeId(),
contents,
JsObject.createJsObject(),
new SimpleRequestCallback<SourceDocument>(
"Error Creating New Document")
Expand Down
Expand Up @@ -43,6 +43,7 @@ public interface SourceServerOperations extends FilesServerOperations,
* have never been saved.
*/
void newDocument(String fileType,
String contents,
JsObject properties,
ServerRequestCallback<SourceDocument> requestCallback);

Expand Down

0 comments on commit c180b0e

Please sign in to comment.