Example #1
0
  /**
   * 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();
    }
  }