Search in sources :

Example 1 with DefaultPositionUpdater

use of org.eclipse.jface.text.DefaultPositionUpdater in project che by eclipse.

the class JavaContext method rewriteImports.

private void rewriteImports() {
    if (fImportRewrite == null)
        return;
    if (isReadOnly())
        return;
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null)
        return;
    try {
        Position position = new Position(getCompletionOffset(), 0);
        IDocument document = getDocument();
        //$NON-NLS-1$
        final String category = "__template_position_importer" + System.currentTimeMillis();
        IPositionUpdater updater = new DefaultPositionUpdater(category);
        document.addPositionCategory(category);
        document.addPositionUpdater(updater);
        document.addPosition(position);
        try {
            JavaModelUtil.applyEdit(cu, fImportRewrite.rewriteImports(null), false, null);
            setCompletionOffset(position.getOffset());
        } catch (CoreException e) {
            handleException(null, e);
        } finally {
            document.removePosition(position);
            document.removePositionUpdater(updater);
            document.removePositionCategory(category);
        }
    } catch (BadLocationException e) {
        handleException(null, e);
    } catch (BadPositionCategoryException e) {
        handleException(null, e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) Position(org.eclipse.jface.text.Position) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) IPositionUpdater(org.eclipse.jface.text.IPositionUpdater) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with DefaultPositionUpdater

use of org.eclipse.jface.text.DefaultPositionUpdater in project che by eclipse.

the class DocumentCommand method execute.

/**
	 * Executes the document commands on a document.
	 *
	 * @param document the document on which to execute the commands
	 * @throws BadLocationException in case access to the given document fails
	 * @since 2.1
	 */
void execute(IDocument document) throws BadLocationException {
    if (length == 0 && text == null && fCommands.size() == 0)
        return;
    DefaultPositionUpdater updater = new DefaultPositionUpdater(getCategory());
    Position caretPosition = null;
    try {
        if (updateCaret()) {
            document.addPositionCategory(getCategory());
            document.addPositionUpdater(updater);
            caretPosition = new Position(caretOffset);
            document.addPosition(getCategory(), caretPosition);
        }
        final Command originalCommand = new Command(offset, length, text, owner);
        for (final Iterator iterator = new CommandIterator(fCommands, originalCommand, false); iterator.hasNext(); ) ((Command) iterator.next()).execute(document);
    } catch (BadLocationException e) {
    // ignore
    } catch (BadPositionCategoryException e) {
    // ignore
    } finally {
        if (updateCaret()) {
            document.removePositionUpdater(updater);
            try {
                document.removePositionCategory(getCategory());
            } catch (BadPositionCategoryException e) {
                Assert.isTrue(false);
            }
            caretOffset = caretPosition.getOffset();
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) DefaultPositionUpdater(org.eclipse.jface.text.DefaultPositionUpdater) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 Position (org.eclipse.jface.text.Position)2 Iterator (java.util.Iterator)1 ListIterator (java.util.ListIterator)1 CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IDocument (org.eclipse.jface.text.IDocument)1 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)1