Search in sources :

Example 46 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method validateEditorInputState.

@Override
public boolean validateEditorInputState() {
    boolean enabled = false;
    synchronized (this) {
        enabled = fIsStateValidationEnabled;
    }
    if (enabled) {
        ISourceViewer viewer = fSourceViewer;
        if (viewer == null)
            return false;
        fTextInputListener.inputChanged = false;
        viewer.addTextInputListener(fTextInputListener);
        try {
            final IEditorInput input = getEditorInput();
            BusyIndicator.showWhile(getSite().getShell().getDisplay(), new Runnable() {

                @Override
                public void run() {
                    validateState(input);
                }
            });
            sanityCheckState(input);
            return !isEditorInputReadOnly() && !fTextInputListener.inputChanged;
        } finally {
            viewer.removeTextInputListener(fTextInputListener);
        }
    }
    return !isEditorInputReadOnly();
}
Also used : SafeRunnable(org.eclipse.jface.util.SafeRunnable) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IEditorInput(org.eclipse.ui.IEditorInput)

Example 47 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method doSetInput.

/**
 * Called directly from <code>setInput</code> and from within a workspace
 * runnable from <code>init</code>, this method does the actual setting
 * of the editor input. Closes the editor if <code>input</code> is
 * <code>null</code>. Disconnects from any previous editor input and its
 * document provider and connects to the new one.
 * <p>
 * Subclasses may extend.
 * </p>
 *
 * @param input the input to be set
 * @exception CoreException if input cannot be connected to the document
 *            provider
 */
protected void doSetInput(IEditorInput input) throws CoreException {
    ISaveablesLifecycleListener listener = getSite().getService(ISaveablesLifecycleListener.class);
    if (listener == null)
        fSavable = null;
    if (input == null) {
        close(isSaveOnCloseNeeded());
        if (fSavable != null) {
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false));
            fSavable.disconnectEditor();
            fSavable = null;
        }
    } else {
        boolean mustSendLifeCycleEvent = false;
        if (fSavable != null) {
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false));
            fSavable.disconnectEditor();
            fSavable = null;
            mustSendLifeCycleEvent = true;
        }
        IEditorInput oldInput = getEditorInput();
        if (oldInput != null)
            getDocumentProvider().disconnect(oldInput);
        super.setInput(input);
        updateDocumentProvider(input);
        IDocumentProvider provider = getDocumentProvider();
        if (provider == null) {
            IStatus s = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, EditorMessages.Editor_error_no_provider, null);
            throw new CoreException(s);
        }
        provider.connect(input);
        initializeTitle(input);
        if (fSourceViewer != null) {
            initializeSourceViewer(input);
            // Reset the undo context for the undo and redo action handlers
            IAction undoAction = getAction(ITextEditorActionConstants.UNDO);
            IAction redoAction = getAction(ITextEditorActionConstants.REDO);
            boolean areOperationActionHandlersInstalled = undoAction instanceof OperationHistoryActionHandler && redoAction instanceof OperationHistoryActionHandler;
            IUndoContext undoContext = getUndoContext();
            if (undoContext != null && areOperationActionHandlersInstalled) {
                ((OperationHistoryActionHandler) undoAction).setContext(undoContext);
                ((OperationHistoryActionHandler) redoAction).setContext(undoContext);
            } else {
                createUndoRedoActions();
            }
        }
        if (fIsOverwriting)
            toggleOverwriteMode();
        setInsertMode(getLegalInsertModes().get(0));
        updateCaret();
        updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE);
        if (fSelectionListener != null)
            fSelectionListener.setDocument(getDocumentProvider().getDocument(input));
        IVerticalRuler ruler = getVerticalRuler();
        if (ruler instanceof CompositeRuler)
            updateContributedRulerColumns((CompositeRuler) ruler);
        // Send savable life-cycle if needed.
        if (mustSendLifeCycleEvent && listener != null)
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, getSaveables(), false));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) OperationHistoryActionHandler(org.eclipse.ui.operations.OperationHistoryActionHandler) IStatus(org.eclipse.core.runtime.IStatus) IAction(org.eclipse.jface.action.IAction) IVerticalRuler(org.eclipse.jface.text.source.IVerticalRuler) CompositeRuler(org.eclipse.jface.text.source.CompositeRuler) ISaveablesLifecycleListener(org.eclipse.ui.ISaveablesLifecycleListener) IUndoContext(org.eclipse.core.commands.operations.IUndoContext) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) SaveablesLifecycleEvent(org.eclipse.ui.SaveablesLifecycleEvent)

Example 48 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method handleEditorInputChanged.

/**
 * Handles an external change of the editor's input element. Subclasses may
 * extend.
 */
protected void handleEditorInputChanged() {
    String title;
    String msg;
    Shell shell = getSite().getShell();
    final IDocumentProvider provider = getDocumentProvider();
    if (provider == null) {
        // fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=15066
        close(false);
        return;
    }
    final IEditorInput input = getEditorInput();
    final String inputName = input.getToolTipText();
    if (provider.isDeleted(input)) {
        if (isSaveAsAllowed()) {
            title = EditorMessages.Editor_error_activated_deleted_save_title;
            msg = NLSUtility.format(EditorMessages.Editor_error_activated_deleted_save_message, inputName);
            String[] buttons = { EditorMessages.Editor_error_activated_deleted_save_button_save, EditorMessages.Editor_error_activated_deleted_save_button_close };
            MessageDialog dialog = new MessageDialog(shell, title, null, msg, MessageDialog.QUESTION, buttons, 0);
            if (dialog.open() == 0) {
                IProgressMonitor pm = getProgressMonitor();
                try {
                    performSaveAs(pm);
                    if (pm.isCanceled())
                        handleEditorInputChanged();
                } finally {
                    pm.done();
                }
            } else {
                close(false);
            }
        } else {
            title = EditorMessages.Editor_error_activated_deleted_close_title;
            msg = NLSUtility.format(EditorMessages.Editor_error_activated_deleted_close_message, inputName);
            if (MessageDialog.openConfirm(shell, title, msg))
                close(false);
        }
    } else if (fHasBeenActivated) {
        title = EditorMessages.Editor_error_activated_outofsync_title;
        msg = NLSUtility.format(EditorMessages.Editor_error_activated_outofsync_message, inputName);
        if (MessageDialog.openQuestion(shell, title, msg)) {
            try {
                if (provider instanceof IDocumentProviderExtension) {
                    IDocumentProviderExtension extension = (IDocumentProviderExtension) provider;
                    extension.synchronize(input);
                } else {
                    doSetInput(input);
                }
            } catch (CoreException x) {
                IStatus status = x.getStatus();
                if (status == null || status.getSeverity() != IStatus.CANCEL) {
                    title = EditorMessages.Editor_error_refresh_outofsync_title;
                    msg = NLSUtility.format(EditorMessages.Editor_error_refresh_outofsync_message, inputName);
                    ErrorDialog.openError(shell, title, msg, x.getStatus());
                }
            }
        } else if (!isDirty()) {
            // Trigger dummy change to dirty the editor, for details see https://bugs.eclipse.org/344101 .
            try {
                IDocument document = provider.getDocument(input);
                if (document != null)
                    // $NON-NLS-1$
                    document.replace(0, 0, "");
            } catch (BadLocationException e) {
            // Ignore as this can't happen
            }
        }
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 49 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class HippieProposalProcessor method getSuggestions.

/**
 * Create the array of suggestions. It scans all open text editors and prefers suggestions from
 * the currently open editor. It also adds the empty suggestion at the end.
 *
 * @param viewer the viewer
 * @param offset the offset
 * @param prefix the prefix to search for
 * @return the list of all possible suggestions in the currently open editors
 * @throws BadLocationException if accessing the current document fails
 */
private List<String> getSuggestions(ITextViewer viewer, int offset, String prefix) throws BadLocationException {
    ArrayList<String> suggestions = createSuggestionsFromOpenDocument(viewer, offset, prefix);
    IDocument currentDocument = viewer.getDocument();
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IEditorReference[] editorReferences = window.getActivePage().getEditorReferences();
    for (int i = 0; i < editorReferences.length; i++) {
        // don't create!
        IEditorPart editor = editorReferences[i].getEditor(false);
        if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;
            IEditorInput input = textEditor.getEditorInput();
            IDocument doc = textEditor.getDocumentProvider().getDocument(input);
            if (!currentDocument.equals(doc))
                suggestions.addAll(fEngine.getCompletionsForward(doc, prefix, 0, false));
        }
    }
    // add the empty suggestion
    // $NON-NLS-1$
    suggestions.add("");
    List<String> uniqueSuggestions = fEngine.makeUnique(suggestions);
    return uniqueSuggestions;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IEditorReference(org.eclipse.ui.IEditorReference) StyledString(org.eclipse.jface.viewers.StyledString) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument) Point(org.eclipse.swt.graphics.Point) IEditorInput(org.eclipse.ui.IEditorInput)

Example 50 with IEditorInput

use of org.eclipse.ui.IEditorInput in project eclipse.platform.text by eclipse.

the class AbstractDecoratedTextEditor method editorContextMenuAboutToShow.

@Override
protected void editorContextMenuAboutToShow(IMenuManager menu) {
    super.editorContextMenuAboutToShow(menu);
    IAction preferencesAction = getAction(ITextEditorActionConstants.CONTEXT_PREFERENCES);
    menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(ITextEditorActionConstants.GROUP_SETTINGS));
    menu.appendToGroup(ITextEditorActionConstants.GROUP_SETTINGS, preferencesAction);
    menu.appendToGroup(ITextEditorActionConstants.GROUP_SAVE, new Separator(ITextEditorActionConstants.GROUP_OPEN));
    IEditorInput editorInput = getEditorInput();
    if ((editorInput.getAdapter(IResource.class)) instanceof IFile) {
        MenuManager openWithSubMenu = new MenuManager(TextEditorMessages.AbstractDecoratedTextEditor_openWith_menu);
        final IWorkbenchPage page = getEditorSite().getPage();
        // XXX: Internal reference will get fixed during 3.7, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=307026
        openWithSubMenu.add(new OpenWithMenu(page, editorInput) {

            @Override
            protected void openEditor(IEditorDescriptor editorDescriptor, boolean openUsingDescriptor) {
                super.openEditor(editorDescriptor, openUsingDescriptor);
                ISelection selection = getSelectionProvider().getSelection();
                if (selection instanceof ITextSelection) {
                    revealInEditor(page.getActiveEditor(), ((ITextSelection) selection).getOffset(), ((ITextSelection) selection).getLength());
                }
            }
        });
        menu.appendToGroup(ITextEditorActionConstants.GROUP_OPEN, openWithSubMenu);
    }
    MenuManager showInSubMenu = new MenuManager(getShowInMenuLabel());
    showInSubMenu.add(ContributionItemFactory.VIEWS_SHOW_IN.create(getEditorSite().getWorkbenchWindow()));
    menu.appendToGroup(ITextEditorActionConstants.GROUP_OPEN, showInSubMenu);
}
Also used : IFile(org.eclipse.core.resources.IFile) IAction(org.eclipse.jface.action.IAction) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) MenuManager(org.eclipse.jface.action.MenuManager) IMenuManager(org.eclipse.jface.action.IMenuManager) ISelection(org.eclipse.jface.viewers.ISelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) OpenWithMenu(org.eclipse.ui.actions.OpenWithMenu) Separator(org.eclipse.jface.action.Separator) IEditorInput(org.eclipse.ui.IEditorInput) IURIEditorInput(org.eclipse.ui.IURIEditorInput) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

IEditorInput (org.eclipse.ui.IEditorInput)135 IFile (org.eclipse.core.resources.IFile)38 IEditorPart (org.eclipse.ui.IEditorPart)37 PartInitException (org.eclipse.ui.PartInitException)31 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)28 CoreException (org.eclipse.core.runtime.CoreException)25 IFileEditorInput (org.eclipse.ui.IFileEditorInput)22 IDocument (org.eclipse.jface.text.IDocument)17 IEditorReference (org.eclipse.ui.IEditorReference)17 FileEditorInput (org.eclipse.ui.part.FileEditorInput)16 File (java.io.File)14 ArrayList (java.util.ArrayList)14 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)14 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)14 IResource (org.eclipse.core.resources.IResource)13 Shell (org.eclipse.swt.widgets.Shell)13 IViewPart (org.eclipse.ui.IViewPart)13 IPath (org.eclipse.core.runtime.IPath)12 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)12 FileStoreEditorInput (org.eclipse.ui.ide.FileStoreEditorInput)11