Java Code Examples for org.eclipse.jface.text.source.projection.ProjectionAnnotationModel#getAnnotationIterator()

The following examples show how to use org.eclipse.jface.text.source.projection.ProjectionAnnotationModel#getAnnotationIterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TexCollapseAction.java    From texlipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Collapses the deepest annotation that contains the given offset
 * 
 * @param model The annotation model to use
 * @param offset The offset inside the document
 */
private void collapseDeepestMatching(ProjectionAnnotationModel model, int offset) {
    TexProjectionAnnotation toCollapse = null;
    for (Iterator iter = model.getAnnotationIterator(); iter.hasNext();) {
        TexProjectionAnnotation tpa = (TexProjectionAnnotation) iter.next();
        if (tpa.contains(offset)) {
            if (toCollapse != null) {
                if (tpa.isDeeperThan(toCollapse))
                    toCollapse = tpa;
            } else {
                toCollapse = tpa;
            }
        }
    }
    if (toCollapse != null) {
        model.collapse(toCollapse);
    }
}
 
Example 2
Source File: RepositionHandler.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * A semi-hack... This uses stuff that may change at any time in Eclipse.  
 * In the java editor, the projection annotation model contains the collapsible regions which correspond to methods (and other areas
 * such as import groups).
 * 
 * This may work in other editor types as well... TBD
 */
protected int transform(ITextEditor editor, IDocument document, ITextSelection currentSelection,
		ExecutionEvent event) throws BadLocationException {

	ITextViewerExtension viewer = MarkUtils.getITextViewer(editor);
	if (viewer instanceof ProjectionViewer) {
		ProjectionAnnotationModel projection = ((ProjectionViewer)viewer).getProjectionAnnotationModel();
		Iterator<Annotation> pit = projection.getAnnotationIterator();
		while (pit.hasNext()) {
			Position p = projection.getPosition(pit.next());
			if (p.includes(currentSelection.getOffset())) {
				if (isUniversalPresent()) {
					// Do this here to prevent subsequent scrolling once range is revealed
					MarkUtils.setSelection(editor, new TextSelection(document, p.offset, 0));
				}
				// the viewer is pretty much guaranteed to be a TextViewer
				if (viewer instanceof TextViewer) {
					((TextViewer)viewer).revealRange(p.offset, p.length);
				}
				break;
			}
		}
	}
	return NO_OFFSET;		
}
 
Example 3
Source File: DefaultFoldingStructureProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Annotation[] mergeFoldingRegions(Collection<FoldedPosition> foldedPositions,
		ProjectionAnnotationModel projectionAnnotationModel) {
	List<Annotation> deletions = new ArrayList<Annotation>();
	for (Iterator<Annotation> iterator = projectionAnnotationModel.getAnnotationIterator(); iterator.hasNext();) {
		Annotation annotation = iterator.next();
		if (annotation instanceof ProjectionAnnotation) {
			Position position = projectionAnnotationModel.getPosition(annotation);
			if (!foldedPositions.remove(position)) {
				deletions.add(annotation);
			}
		}
	}
	return deletions.toArray(new Annotation[deletions.size()]);
}
 
Example 4
Source File: TexCollapseAction.java    From texlipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collapses all annotations that are completely contained in the interval
 * defined by <code>startOffset</code> and <code>endOffset</code>.
 * 
 * @param model The annotation model to use
 * @param startOffset The document offset of the start of the interval
 * @param endOffset The document offset of the end of the interval
 */
private void collapseAllContained(ProjectionAnnotationModel model,
        int startOffset, int endOffset) {
    for (Iterator iter = model.getAnnotationIterator(); iter.hasNext();) {
        TexProjectionAnnotation tpa = (TexProjectionAnnotation) iter.next();
        if (tpa.isBetween(startOffset, endOffset)) {
            model.collapse(tpa);
        }
    }
}
 
Example 5
Source File: AbstractFoldingEditor.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public void updateFoldingStructure(Map<ProjectionAnnotation, Position> annotations)
{
	synchronized (lockUpdateFoldingStructure)
	{
		List<Annotation> deletions = new ArrayList<Annotation>();
		Collection<Position> additions = annotations.values();
		ProjectionAnnotationModel currentModel = getAnnotationModel();
		if (currentModel == null)
		{
			return;
		}
		for (@SuppressWarnings("rawtypes")
		Iterator iter = currentModel.getAnnotationIterator(); iter.hasNext();)
		{
			Object annotation = iter.next();
			if (annotation instanceof ProjectionAnnotation)
			{
				Position position = currentModel.getPosition((Annotation) annotation);
				if (additions.contains(position))
				{
					additions.remove(position);
				}
				else
				{
					deletions.add((Annotation) annotation);
				}
			}
		}
		if (annotations.size() != 0 || deletions.size() != 0)
		{
			currentModel.modifyAnnotations(deletions.toArray(new Annotation[deletions.size()]), annotations, null);
		}
	}
}
 
Example 6
Source File: DefaultJavaFoldingStructureProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collapses or expands all annotations matched by the passed filter.
 *
 * @param filter the filter to use to select which annotations to collapse
 * @param expand <code>true</code> to expand the matched annotations, <code>false</code> to
 *        collapse them
 */
private void modifyFiltered(Filter filter, boolean expand) {
	if (!isInstalled())
		return;

	ProjectionAnnotationModel model= getModel();
	if (model == null)
		return;

	List<JavaProjectionAnnotation> modified= new ArrayList<JavaProjectionAnnotation>();
	Iterator<Annotation> iter= model.getAnnotationIterator();
	while (iter.hasNext()) {
		Object annotation= iter.next();
		if (annotation instanceof JavaProjectionAnnotation) {
			JavaProjectionAnnotation java= (JavaProjectionAnnotation) annotation;

			if (expand == java.isCollapsed() && filter.match(java)) {
				if (expand)
					java.markExpanded();
				else
					java.markCollapsed();
				modified.add(java);
			}

		}
	}

	model.modifyAnnotations(null, null, modified.toArray(new Annotation[modified.size()]));
}
 
Example 7
Source File: DecoratedScriptEditor.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collapses all item with the specified style.
 * 
 * @param style
 *            the style to collapse
 */
private void collapseStyle( int style )
{
	ISourceViewer viewer = getViewer( );

	if ( !( viewer instanceof ProjectionViewer ) )
	{
		return;
	}

	ProjectionAnnotationModel model = ( (ProjectionViewer) viewer ).getProjectionAnnotationModel( );

	if ( model == null )
	{
		return;
	}

	List modified = new ArrayList( );
	Iterator iter = model.getAnnotationIterator( );

	while ( iter.hasNext( ) )
	{
		Object annotation = iter.next( );

		if ( annotation instanceof ScriptProjectionAnnotation )
		{
			ScriptProjectionAnnotation scriptAnnotation = (ScriptProjectionAnnotation) annotation;

			if ( !scriptAnnotation.isCollapsed( )
					&& scriptAnnotation.isStyle( style ) )
			{
				scriptAnnotation.markCollapsed( );
				modified.add( scriptAnnotation );
			}
		}
	}
	model.modifyAnnotations( null,
			null,
			(Annotation[]) modified.toArray( new Annotation[modified.size( )] ) );
}
 
Example 8
Source File: PyFoldingAction.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param model
 * @return
 */
protected Iterator<Annotation> getAnnotationsIterator(final ProjectionAnnotationModel model,
        boolean useExpanded) {
    //put annotations in array list.
    Iterator<Annotation> iter = model.getAnnotationIterator();
    if (iter != null) {

        //get the not collapsed (expanded) and sort them
        ArrayList<Annotation> expanded = new ArrayList<Annotation>();
        while (iter.hasNext()) {
            PyProjectionAnnotation element = (PyProjectionAnnotation) iter.next();
            if (element.isCollapsed() == useExpanded) {
                expanded.add(element);
            }
        }

        Collections.sort(expanded, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                PyProjectionAnnotation e1 = (PyProjectionAnnotation) o1;
                PyProjectionAnnotation e2 = (PyProjectionAnnotation) o2;
                int e1Off = model.getPosition(e1).getOffset();
                int e2Off = model.getPosition(e2).getOffset();
                if (e1Off < e2Off) {
                    return -1;
                }
                if (e1Off > e2Off) {
                    return 1;
                }
                return 0;
            }
        });

        iter = expanded.iterator();
    }
    return iter;
}
 
Example 9
Source File: PyCollapse.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void run(IAction action) {
    PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(getTextEditor());

    ProjectionAnnotationModel model = getTextEditor().getAdapter(
            ProjectionAnnotationModel.class);
    try {
        if (model != null) {
            //put annotations in array list.
            Iterator iter = model.getAnnotationIterator();
            while (iter != null && iter.hasNext()) {
                PyProjectionAnnotation element = (PyProjectionAnnotation) iter.next();
                Position position = model.getPosition(element);

                int line = ps.getDoc().getLineOfOffset(position.offset);

                int start = ps.getStartLineIndex();
                int end = ps.getEndLineIndex();

                for (int i = start; i <= end; i++) {
                    if (i == line) {
                        model.collapse(element);
                        break;
                    }
                }
            }

        }
    } catch (BadLocationException e) {
        Log.log(IStatus.ERROR, "Unexpected error collapsing", e);
    }
}
 
Example 10
Source File: CodeFoldingSetter.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Given the ast, create the needed marks and set them in the passed model.
 */
private synchronized void addMarksToModel(SimpleNode root2, ProjectionAnnotationModel model) {
    try {
        if (model != null) {
            ArrayList<Annotation> existing = new ArrayList<Annotation>();

            //get the existing annotations
            Iterator<Annotation> iter = model.getAnnotationIterator();
            while (iter != null && iter.hasNext()) {
                Annotation element = iter.next();
                existing.add(element);
            }

            //now, remove the annotations not used and add the new ones needed
            IDocument doc = editor.getDocument();
            if (doc != null) { //this can happen if we change the input of the editor very quickly.
                boolean foldInitial = initialFolding;
                initialFolding = false;
                List<FoldingEntry> marks = getMarks(doc, root2, foldInitial);
                Map<ProjectionAnnotation, Position> annotationsToAdd;
                if (marks.size() > OptimizationRelatedConstants.MAXIMUM_NUMBER_OF_CODE_FOLDING_MARKS) {
                    annotationsToAdd = new HashMap<ProjectionAnnotation, Position>();

                } else {
                    annotationsToAdd = getAnnotationsToAdd(marks, model, existing);
                }

                model.replaceAnnotations(existing.toArray(new Annotation[existing.size()]), annotationsToAdd);
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
}