Search in sources :

Example 26 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException 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 27 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class SourceViewer method rememberSelection.

/**
 * Remembers and returns the current selection. The saved selection can be restored
 * by calling <code>restoreSelection()</code>.
 *
 * @return the current selection
 * @see org.eclipse.jface.text.ITextViewer#getSelectedRange()
 * @since 3.0
 */
protected Point rememberSelection() {
    final ITextSelection selection = (ITextSelection) getSelection();
    final IDocument document = getDocument();
    if (fSelections.isEmpty()) {
        fSelectionCategory = _SELECTION_POSITION_CATEGORY + hashCode();
        fSelectionUpdater = new NonDeletingPositionUpdater(fSelectionCategory);
        document.addPositionCategory(fSelectionCategory);
        document.addPositionUpdater(fSelectionUpdater);
    }
    try {
        final Position position;
        if (selection instanceof IBlockTextSelection)
            position = new ColumnPosition(selection.getOffset(), selection.getLength(), ((IBlockTextSelection) selection).getStartColumn(), ((IBlockTextSelection) selection).getEndColumn());
        else
            position = new Position(selection.getOffset(), selection.getLength());
        document.addPosition(fSelectionCategory, position);
        fSelections.push(position);
    } catch (BadLocationException exception) {
    // Should not happen
    } catch (BadPositionCategoryException exception) {
    // Should not happen
    }
    return new Point(selection.getOffset(), selection.getLength());
}
Also used : Position(org.eclipse.jface.text.Position) NonDeletingPositionUpdater(org.eclipse.jface.internal.text.NonDeletingPositionUpdater) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IBlockTextSelection(org.eclipse.jface.text.IBlockTextSelection) Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 28 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class SourceViewer method restoreSelection.

/**
 * Restores a previously saved selection in the document.
 * <p>
 * If no selection was previously saved, nothing happens.
 *
 * @since 3.0
 */
protected void restoreSelection() {
    if (!fSelections.isEmpty()) {
        final IDocument document = getDocument();
        final Position position = fSelections.pop();
        try {
            document.removePosition(fSelectionCategory, position);
            Point currentSelection = getSelectedRange();
            if (currentSelection == null || currentSelection.x != position.getOffset() || currentSelection.y != position.getLength()) {
                if (position instanceof ColumnPosition && getTextWidget().getBlockSelection()) {
                    setSelection(new BlockTextSelection(document, document.getLineOfOffset(position.getOffset()), ((ColumnPosition) position).fStartColumn, document.getLineOfOffset(position.getOffset() + position.getLength()), ((ColumnPosition) position).fEndColumn, getTextWidget().getTabs()));
                } else {
                    setSelectedRange(position.getOffset(), position.getLength());
                }
            }
            if (fSelections.isEmpty())
                clearRememberedSelection();
        } catch (BadPositionCategoryException exception) {
        // Should not happen
        } catch (BadLocationException x) {
        // Should not happen
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) Point(org.eclipse.swt.graphics.Point) IDocument(org.eclipse.jface.text.IDocument) BlockTextSelection(org.eclipse.jface.text.BlockTextSelection) IBlockTextSelection(org.eclipse.jface.text.IBlockTextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 29 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class SourceViewer method clearRememberedSelection.

protected void clearRememberedSelection() {
    if (!fSelections.isEmpty())
        fSelections.clear();
    IDocument document = getDocument();
    if (document != null && fSelectionUpdater != null) {
        document.removePositionUpdater(fSelectionUpdater);
        try {
            document.removePositionCategory(fSelectionCategory);
        } catch (BadPositionCategoryException e) {
        // ignore
        }
    }
    fSelectionUpdater = null;
    fSelectionCategory = null;
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument)

Example 30 with BadPositionCategoryException

use of org.eclipse.jface.text.BadPositionCategoryException in project eclipse.platform.text by eclipse.

the class ProjectionMappingTest method addProjection.

private void addProjection(int fragmentOffset, int segmentOffset, int length) {
    Fragment fragment = new Fragment(fragmentOffset, length);
    Segment segment = new Segment(segmentOffset, length);
    fragment.segment = segment;
    segment.fragment = fragment;
    try {
        fMasterDocument.addPosition(fFragmentsCategory, fragment);
        fSlaveDocument.addPosition(fSegmentsCategory, segment);
    } catch (BadLocationException e) {
        assertTrue(false);
    } catch (BadPositionCategoryException e) {
        assertTrue(false);
    }
}
Also used : BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) Fragment(org.eclipse.jface.text.projection.Fragment) Segment(org.eclipse.jface.text.projection.Segment) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)42 Position (org.eclipse.jface.text.Position)30 BadLocationException (org.eclipse.jface.text.BadLocationException)22 TypedPosition (org.eclipse.jface.text.TypedPosition)18 IDocument (org.eclipse.jface.text.IDocument)14 ITypedRegion (org.eclipse.jface.text.ITypedRegion)10 TypedRegion (org.eclipse.jface.text.TypedRegion)8 ArrayList (java.util.ArrayList)7 IRegion (org.eclipse.jface.text.IRegion)7 Point (org.eclipse.swt.graphics.Point)4 Region (org.eclipse.jface.text.Region)3 Iterator (java.util.Iterator)2 List (java.util.List)2 DefaultPositionUpdater (org.eclipse.jface.text.DefaultPositionUpdater)2 IBlockTextSelection (org.eclipse.jface.text.IBlockTextSelection)2 IPositionUpdater (org.eclipse.jface.text.IPositionUpdater)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1