Search in sources :

Example 6 with EditingTarget

use of org.rstudio.studio.client.workbench.views.source.editors.EditingTarget in project rstudio by rstudio.

the class Source method openProjectDocs.

private void openProjectDocs(final Session session) {
    JsArrayString openDocs = session.getSessionInfo().getProjectOpenDocs();
    if (openDocs.length() > 0) {
        // set new tab pending for the duration of the continuation
        newTabPending_++;
        // create a continuation for opening the source docs
        SerializedCommandQueue openCommands = new SerializedCommandQueue();
        for (int i = 0; i < openDocs.length(); i++) {
            String doc = openDocs.get(i);
            final FileSystemItem fsi = FileSystemItem.createFile(doc);
            openCommands.addCommand(new SerializedCommand() {

                @Override
                public void onExecute(final Command continuation) {
                    openFile(fsi, fileTypeRegistry_.getTextTypeForFile(fsi), new CommandWithArg<EditingTarget>() {

                        @Override
                        public void execute(EditingTarget arg) {
                            continuation.execute();
                        }
                    });
                }
            });
        }
        // decrement newTabPending and select first tab when done
        openCommands.addCommand(new SerializedCommand() {

            @Override
            public void onExecute(Command continuation) {
                newTabPending_--;
                onFirstTab();
                continuation.execute();
            }
        });
        // execute the continuation
        openCommands.run();
    }
}
Also used : FileSystemItem(org.rstudio.core.client.files.FileSystemItem) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Command(com.google.gwt.user.client.Command) AppCommand(org.rstudio.core.client.command.AppCommand) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) JsArrayString(com.google.gwt.core.client.JsArrayString) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 7 with EditingTarget

use of org.rstudio.studio.client.workbench.views.source.editors.EditingTarget in project rstudio by rstudio.

the class Source method revertUnsavedTargets.

private void revertUnsavedTargets(Command onCompleted) {
    // collect up unsaved targets
    ArrayList<EditingTarget> unsavedTargets = new ArrayList<EditingTarget>();
    for (EditingTarget target : editors_) if (isUnsavedTarget(target, TYPE_FILE_BACKED))
        unsavedTargets.add(target);
    // revert all of them
    cpsExecuteForEachEditor(// targets the user chose not to save
    unsavedTargets, // save each editor
    new CPSEditingTargetCommand() {

        @Override
        public void execute(EditingTarget saveTarget, Command continuation) {
            if (saveTarget.getPath() != null) {
                // file backed document -- revert it
                saveTarget.revertChanges(continuation);
            } else {
                // untitled document -- just close the tab non-interactively
                view_.closeTab(saveTarget.asWidget(), false, continuation);
            }
        }
    }, // onCompleted at the end
    onCompleted);
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) Command(com.google.gwt.user.client.Command) AppCommand(org.rstudio.core.client.command.AppCommand) ArrayList(java.util.ArrayList) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget)

Example 8 with EditingTarget

use of org.rstudio.studio.client.workbench.views.source.editors.EditingTarget in project rstudio by rstudio.

the class Source method addTab.

private EditingTarget addTab(SourceDocument doc, Integer position, int mode) {
    final String defaultNamePrefix = editingTargetSource_.getDefaultNamePrefix(doc);
    final EditingTarget target = editingTargetSource_.getEditingTarget(doc, fileContext_, new Provider<String>() {

        public String get() {
            return getNextDefaultName(defaultNamePrefix);
        }
    });
    final Widget widget = createWidget(target);
    if (position == null) {
        editors_.add(target);
    } else {
        // we're inserting into an existing permuted tabset -- push aside 
        // any tabs physically to the right of this tab
        editors_.add(position, target);
        for (int i = 0; i < tabOrder_.size(); i++) {
            int pos = tabOrder_.get(i);
            if (pos >= position)
                tabOrder_.set(i, pos + 1);
        }
        // add this tab in its "natural" position
        tabOrder_.add(position, position);
    }
    view_.addTab(widget, target.getIcon(), target.getId(), target.getName().getValue(), // used as tooltip, if non-null
    target.getTabTooltip(), position, true);
    fireDocTabsChanged();
    target.getName().addValueChangeHandler(new ValueChangeHandler<String>() {

        public void onValueChange(ValueChangeEvent<String> event) {
            view_.renameTab(widget, target.getIcon(), event.getValue(), target.getPath());
            fireDocTabsChanged();
        }
    });
    view_.setDirty(widget, target.dirtyState().getValue());
    target.dirtyState().addValueChangeHandler(new ValueChangeHandler<Boolean>() {

        public void onValueChange(ValueChangeEvent<Boolean> event) {
            view_.setDirty(widget, event.getValue());
            manageCommands();
        }
    });
    target.addEnsureVisibleHandler(new EnsureVisibleHandler() {

        public void onEnsureVisible(EnsureVisibleEvent event) {
            view_.selectTab(widget);
        }
    });
    target.addCloseHandler(new CloseHandler<Void>() {

        public void onClose(CloseEvent<Void> voidCloseEvent) {
            view_.closeTab(widget, false);
        }
    });
    events_.fireEvent(new SourceDocAddedEvent(doc, mode));
    // multiple documents are open; if this is the second document, go check
    if (editors_.size() == 2)
        manageMultiTabCommands();
    // if the target had an editing session active, attempt to resume it
    if (doc.getCollabParams() != null)
        target.beginCollabSession(doc.getCollabParams());
    return target;
}
Also used : TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) IsWidget(com.google.gwt.user.client.ui.IsWidget) Widget(com.google.gwt.user.client.ui.Widget) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString)

Example 9 with EditingTarget

use of org.rstudio.studio.client.workbench.views.source.editors.EditingTarget in project rstudio by rstudio.

the class Source method doOpenSourceFile.

private void doOpenSourceFile(final FileSystemItem file, final TextFileType fileType, final FilePosition position, final String pattern, final int navMethod, final boolean forceHighlightMode) {
    // if the navigation should happen in another window, do that instead
    NavigationResult navResult = windowManager_.navigateToFile(file, position, navMethod);
    // we navigated externally, just skip this
    if (navResult.getType() == NavigationResult.RESULT_NAVIGATED)
        return;
    // we're about to open in this window--if it's the main window, focus it
    if (SourceWindowManager.isMainSourceWindow() && Desktop.isDesktop())
        Desktop.getFrame().bringMainFrameToFront();
    final boolean isDebugNavigation = navMethod == NavigationMethods.DEBUG_STEP || navMethod == NavigationMethods.DEBUG_END;
    final CommandWithArg<EditingTarget> editingTargetAction = new CommandWithArg<EditingTarget>() {

        @Override
        public void execute(EditingTarget target) {
            if (position != null) {
                SourcePosition endPosition = null;
                if (isDebugNavigation) {
                    DebugFilePosition filePos = (DebugFilePosition) position.cast();
                    endPosition = SourcePosition.create(filePos.getEndLine() - 1, filePos.getEndColumn() + 1);
                    if (Desktop.isDesktop() && navMethod != NavigationMethods.DEBUG_END)
                        Desktop.getFrame().bringMainFrameToFront();
                }
                navigate(target, SourcePosition.create(position.getLine() - 1, position.getColumn() - 1), endPosition);
            } else if (pattern != null) {
                Position pos = target.search(pattern);
                if (pos != null) {
                    navigate(target, SourcePosition.create(pos.getRow(), 0), null);
                }
            }
        }

        private void navigate(final EditingTarget target, final SourcePosition srcPosition, final SourcePosition srcEndPosition) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                @Override
                public void execute() {
                    if (navMethod == NavigationMethods.DEBUG_STEP) {
                        target.highlightDebugLocation(srcPosition, srcEndPosition, true);
                    } else if (navMethod == NavigationMethods.DEBUG_END) {
                        target.endDebugHighlighting();
                    } else {
                        // force highlight mode if requested
                        if (forceHighlightMode)
                            target.forceLineHighlighting();
                        // now navigate to the new position
                        boolean highlight = navMethod == NavigationMethods.HIGHLIGHT_LINE && !uiPrefs_.highlightSelectedLine().getValue();
                        target.navigateToPosition(srcPosition, false, highlight);
                    }
                }
            });
        }
    };
    if (navResult.getType() == NavigationResult.RESULT_RELOCATE) {
        server_.getSourceDocument(navResult.getDocId(), new ServerRequestCallback<SourceDocument>() {

            @Override
            public void onResponseReceived(final SourceDocument doc) {
                editingTargetAction.execute(addTab(doc, OPEN_REPLAY));
            }

            @Override
            public void onError(ServerError error) {
                globalDisplay_.showErrorMessage("Document Tab Move Failed", "Couldn't move the tab to this window: \n" + error.getMessage());
            }
        });
        return;
    }
    final CommandWithArg<FileSystemItem> action = new CommandWithArg<FileSystemItem>() {

        @Override
        public void execute(FileSystemItem file) {
            openFile(file, fileType, editingTargetAction);
        }
    };
    // highlight in place.
    if (isDebugNavigation) {
        setPendingDebugSelection();
        for (int i = 0; i < editors_.size(); i++) {
            EditingTarget target = editors_.get(i);
            String path = target.getPath();
            if (path != null && path.equalsIgnoreCase(file.getPath())) {
                // the file's open; just update its highlighting 
                if (navMethod == NavigationMethods.DEBUG_END) {
                    target.endDebugHighlighting();
                } else {
                    view_.selectTab(i);
                    editingTargetAction.execute(target);
                }
                return;
            }
        }
        // open a file just to turn off debug highlighting in the file!
        if (navMethod == NavigationMethods.DEBUG_END)
            return;
    }
    // Warning: event.getFile() can be null (e.g. new Sweave document)
    if (file != null && file.getLength() < 0) {
        // If the file has no size info, stat the file from the server. This
        // is to prevent us from opening large files accidentally.
        server_.stat(file.getPath(), new ServerRequestCallback<FileSystemItem>() {

            @Override
            public void onResponseReceived(FileSystemItem response) {
                action.execute(response);
            }

            @Override
            public void onError(ServerError error) {
                // Couldn't stat the file? Proceed anyway. If the file doesn't
                // exist, we'll let the downstream code be the one to show the
                // error.
                action.execute(file);
            }
        });
    } else {
        action.execute(file);
    }
}
Also used : SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) SourceDocument(org.rstudio.studio.client.workbench.views.source.model.SourceDocument) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) FileSystemItem(org.rstudio.core.client.files.FileSystemItem) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) NavigationResult(org.rstudio.studio.client.workbench.views.source.SourceWindowManager.NavigationResult)

Example 10 with EditingTarget

use of org.rstudio.studio.client.workbench.views.source.editors.EditingTarget in project rstudio by rstudio.

the class Source method closeTabIndex.

private void closeTabIndex(int idx, boolean closeDocument) {
    EditingTarget target = editors_.remove(idx);
    tabOrder_.remove(new Integer(idx));
    for (int i = 0; i < tabOrder_.size(); i++) {
        if (tabOrder_.get(i) > idx) {
            tabOrder_.set(i, tabOrder_.get(i) - 1);
        }
    }
    target.onDismiss(closeDocument ? EditingTarget.DISMISS_TYPE_CLOSE : EditingTarget.DISMISS_TYPE_MOVE);
    if (activeEditor_ == target) {
        activeEditor_.onDeactivate();
        activeEditor_ = null;
    }
    if (closeDocument) {
        events_.fireEvent(new DocTabClosedEvent(target.getId()));
        server_.closeDocument(target.getId(), new VoidServerRequestCallback());
    }
    manageCommands();
    fireDocTabsChanged();
    if (view_.getTabCount() == 0) {
        sourceNavigationHistory_.clear();
        events_.fireEvent(new LastSourceDocClosedEvent());
    }
}
Also used : TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) VoidServerRequestCallback(org.rstudio.studio.client.server.VoidServerRequestCallback)

Aggregations

EditingTarget (org.rstudio.studio.client.workbench.views.source.editors.EditingTarget)22 CodeBrowserEditingTarget (org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget)22 DataEditingTarget (org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget)22 TextEditingTarget (org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget)22 JsArrayString (com.google.gwt.core.client.JsArrayString)10 JSONString (com.google.gwt.json.client.JSONString)10 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)9 Command (com.google.gwt.user.client.Command)7 AppCommand (org.rstudio.core.client.command.AppCommand)7 ServerError (org.rstudio.studio.client.server.ServerError)7 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)4 VoidServerRequestCallback (org.rstudio.studio.client.server.VoidServerRequestCallback)4 SourceDocument (org.rstudio.studio.client.workbench.views.source.model.SourceDocument)4 ArrayList (java.util.ArrayList)3 ProgressIndicator (org.rstudio.core.client.widget.ProgressIndicator)3 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)2 CloseHandler (com.google.gwt.event.logical.shared.CloseHandler)2 SelectionHandler (com.google.gwt.event.logical.shared.SelectionHandler)2 ValueChangeHandler (com.google.gwt.event.logical.shared.ValueChangeHandler)2 NativePreviewHandler (com.google.gwt.user.client.Event.NativePreviewHandler)2