/** Restores the code folding state information from the given memento. */ public void restoreCodeFolding(org.eclipse.ui.IMemento memento) { if (memento == null) { return; } org.eclipse.ui.IMemento[] annotationMementos = memento.getChildren(ANNOTATION); if (annotationMementos == null) { return; } java.util.Map<org.eclipse.jface.text.source.projection.ProjectionAnnotation, Boolean> collapsedStates = new java.util.LinkedHashMap< org.eclipse.jface.text.source.projection.ProjectionAnnotation, Boolean>(); for (org.eclipse.ui.IMemento annotationMemento : annotationMementos) { org.eclipse.jface.text.source.projection.ProjectionAnnotation annotation = new org.eclipse.jface.text.source.projection.ProjectionAnnotation(); collapsedStates.put(annotation, annotationMemento.getBoolean(IS_COLLAPSED)); int offset = annotationMemento.getInteger(OFFSET); int length = annotationMemento.getInteger(LENGTH); org.eclipse.jface.text.Position position = new org.eclipse.jface.text.Position(offset, length); projectionAnnotationModel.addAnnotation(annotation, position); } // postset collapse state to prevent wrong displaying folding code. for (org.eclipse.jface.text.source.projection.ProjectionAnnotation annotation : collapsedStates.keySet()) { Boolean isCollapsed = collapsedStates.get(annotation); if (isCollapsed != null && isCollapsed.booleanValue()) { projectionAnnotationModel.collapse(annotation); } } }
/** update the display folding */ private void updateFolding(final DFPropModel dfmodel) { ISourceViewer viewer = getSourceViewer(); if (viewer instanceof ProjectionViewer) { ProjectionAnnotationModel annotationModel = ((ProjectionViewer) viewer).getProjectionAnnotationModel(); Iterator<?> annotations = annotationModel.getAnnotationIterator(); Map<Integer, ProjectionAnnotation> annotationsMap = new HashMap<Integer, ProjectionAnnotation>(); while (annotations.hasNext()) { Object next = annotations.next(); if (next instanceof ProjectionAnnotation) { ProjectionAnnotation annotation = (ProjectionAnnotation) next; Position position = annotationModel.getPosition(annotation); if (position != null) { int offset = position.getOffset(); annotationsMap.put(offset, annotation); } } } Map<Annotation, Position> additions = applyFolding(annotationModel, dfmodel, annotationsMap); annotationModel.modifyAnnotations( annotationsMap.values().toArray(new ProjectionAnnotation[annotationsMap.size()]), additions, new ProjectionAnnotation[0]); } }
/** Saves the code folding state into the given memento. */ public void saveCodeFolding(org.eclipse.ui.IMemento memento) { java.util.Iterator<?> annotationIt = projectionAnnotationModel.getAnnotationIterator(); while (annotationIt.hasNext()) { org.eclipse.jface.text.source.projection.ProjectionAnnotation annotation = (org.eclipse.jface.text.source.projection.ProjectionAnnotation) annotationIt.next(); org.eclipse.ui.IMemento annotationMemento = memento.createChild(ANNOTATION); org.eclipse.jface.text.Position position = projectionAnnotationModel.getPosition(annotation); annotationMemento.putBoolean(IS_COLLAPSED, annotation.isCollapsed()); annotationMemento.putInteger(OFFSET, position.offset); annotationMemento.putInteger(LENGTH, position.length); } }
protected void calculatePositions() { ProjectionAnnotationModel annotationModel = editor.getAnnotationModel(); if (annotationModel == null) { return; } Set<SQLScriptPosition> removedPositions = editor.getRuleManager().getRemovedPositions(true); Set<SQLScriptPosition> addedPositions = editor.getRuleManager().getAddedPositions(true); Annotation[] removedAnnotations = null; if (!removedPositions.isEmpty()) { removedAnnotations = new Annotation[removedPositions.size()]; int index = 0; for (SQLScriptPosition pos : removedPositions) { removedAnnotations[index++] = pos.getFoldingAnnotation(); } } Map<Annotation, Position> addedAnnotations = null; if (!addedPositions.isEmpty()) { addedAnnotations = new HashMap<>(); for (SQLScriptPosition pos : addedPositions) { addedAnnotations.put(pos.getFoldingAnnotation(), pos); } } if (removedAnnotations != null || addedAnnotations != null) { annotationModel.modifyAnnotations(removedAnnotations, addedAnnotations, null); } /* final List<Position> positions; try { positions = parseRegion(); } catch (BadLocationException e) { e.printStackTrace(); return; } */ /* Display.getDefault().asyncExec(new Runnable() { public void run() { editor.updateFoldingStructure(regionOffset, regionLength, positions); } }); */ }
/** * Updates the annotations registered with this editor. * * @param positions */ public void updateFoldingStructure(ArrayList<Position> newPositions) { Map<ProjectionAnnotation, Position> saveAllAnnotations = new HashMap<ProjectionAnnotation, Position>(); saveAllAnnotations.putAll(curAnnotations); try { String setDefaultCollapsedProperty = EclipseUtil.getPreferenceStore().getString(PluginConstants.T24_ALWAYS_COLLAPSE_REGION); boolean setDefaultCollapsed = Boolean.parseBoolean(setDefaultCollapsedProperty); // this will hold the new annotations along // with their corresponding positions Map<ProjectionAnnotation, Position> newAnnotations = new HashMap<ProjectionAnnotation, Position>(); for (int i = 0; i < newPositions.size(); i++) { ProjectionAnnotation an = annotationAlreadyExists((Position) newPositions.get(i)); ProjectionAnnotation annotation; if (an == null) { // The annotation doesn't exist yet, so create it brand new. annotation = new ProjectionAnnotation(); annotation.setRangeIndication(false); annotation.setText("Annotation " + i); if (setDefaultCollapsed) { annotation.markCollapsed(); } else { annotation.markExpanded(); } Position pos = newPositions.get(i); newAnnotations.put(annotation, pos); saveAllAnnotations.put(annotation, pos); } else { if (setDefaultCollapsed) { an.markCollapsed(); } else { an.markExpanded(); } newAnnotations.put(an, newPositions.get(i)); saveAllAnnotations.put(an, newPositions.get(i)); } } Set<ProjectionAnnotation> keys = curAnnotations.keySet(); Iterator<ProjectionAnnotation> keyIt = keys.iterator(); Annotation[] annotations = new Annotation[curAnnotations.size()]; int annotationCount = 0; while (keyIt.hasNext()) { ProjectionAnnotation an = (ProjectionAnnotation) keyIt.next(); annotations[annotationCount++] = an; saveAllAnnotations.remove(an); } if (annotationModel != null) { annotationModel.modifyAnnotations(annotations, newAnnotations, null); } else { createAnnotationModel(); } curAnnotations = saveAllAnnotations; } catch (Exception e) { e.printStackTrace(); } }
/** * @param element * @param elements * @param model * @return */ protected boolean isInsideLast( PyProjectionAnnotation element, List elements, ProjectionAnnotationModel model) { if (elements.size() == 0) { return false; } PyProjectionAnnotation top = (PyProjectionAnnotation) elements.get(elements.size() - 1); Position p1 = model.getPosition(element); Position pTop = model.getPosition(top); int p1Offset = p1.getOffset(); int pTopoffset = pTop.getOffset(); int pTopLen = pTopoffset + pTop.getLength(); if (p1Offset > pTopoffset && p1Offset < pTopLen) { return true; } return false; }
/** * Checks whether the given positions are in the <code> * org.eclipse.jface.text.source.projection.ProjectionAnnotationModel</code> or in the addition * set. If not it tries to add into <code>additions</code>. Deletes old * org.eclipse.jface.text.source.projection.ProjectionAnnotation with line count less than 2. * * @param positions a list of available foldable positions */ public void updateCodefolding(java.util.List<org.eclipse.jface.text.Position> positions) { org.eclipse.jface.text.IDocument document = sourceViewer.getDocument(); if (document == null) { return; } oldAnnotations.clear(); java.util.Iterator<?> annotationIterator = projectionAnnotationModel.getAnnotationIterator(); while (annotationIterator.hasNext()) { oldAnnotations.add( (org.eclipse.jface.text.source.projection.ProjectionAnnotation) annotationIterator.next()); } // Add new Position with a unique line offset for (org.eclipse.jface.text.Position position : positions) { if (!isInAdditions(position)) { addPosition(position); } } projectionAnnotationModel.modifyAnnotations( oldAnnotations.toArray(new org.eclipse.jface.text.source.Annotation[0]), additions, null); additions.clear(); }
/** * Tries to add this position into the model. Only positions with more than 3 lines can be taken * in. If multiple positions exist on the same line, the longest will be chosen. The shorter ones * will be deleted. * * @param position the position to be added. */ private void addPosition(org.eclipse.jface.text.Position position) { org.eclipse.jface.text.IDocument document = sourceViewer.getDocument(); int lines = 0; try { lines = document.getNumberOfLines(position.offset, position.length); } catch (org.eclipse.jface.text.BadLocationException e) { e.printStackTrace(); return; } if (lines < 3) { return; } // if a position to add existed on the same line, the longest one will be chosen try { for (org.eclipse.jface.text.source.projection.ProjectionAnnotation annotationToAdd : additions.keySet()) { org.eclipse.jface.text.Position positionToAdd = additions.get(annotationToAdd); if (document.getLineOfOffset(position.offset) == document.getLineOfOffset(positionToAdd.offset)) { if (positionToAdd.length < position.length) { additions.remove(annotationToAdd); } else { return; } } } } catch (org.eclipse.jface.text.BadLocationException e) { return; } for (org.eclipse.jface.text.source.projection.ProjectionAnnotation annotationInModel : oldAnnotations) { org.eclipse.jface.text.Position positionInModel = projectionAnnotationModel.getPosition(annotationInModel); if (position.offset == positionInModel.offset && position.length == positionInModel.length) { oldAnnotations.remove(annotationInModel); return; } } additions.put(new org.eclipse.jface.text.source.projection.ProjectionAnnotation(), position); }
/** * @param model * @return */ protected Iterator getAnnotationsIterator( final ProjectionAnnotationModel model, boolean useExpanded) { // put annotations in array list. Iterator iter = model.getAnnotationIterator(); if (iter != null) { // get the not collapsed (expanded) and sort them ArrayList expanded = new ArrayList(); while (iter.hasNext()) { PyProjectionAnnotation element = (PyProjectionAnnotation) iter.next(); if (element.isCollapsed() == useExpanded) { expanded.add(element); } } Collections.sort( expanded, new Comparator() { 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; }