private void draw(
      Object parentGroup, GeometryIndex parentIndex, Polygon polygon, GraphicsContext graphics) {
    String groupName = baseName;
    if (parentIndex != null) {
      groupName += "." + editingService.getIndexService().format(parentIndex);
    }

    Composite bgGroup = getOrCreateGroup(parentGroup, groupName + ".background");
    Composite geometryGroup = getOrCreateGroup(parentGroup, groupName + ".geometries");

    // Draw the exterior ring:
    GeometryIndex shellIndex =
        editingService
            .getIndexService()
            .addChildren(parentIndex, GeometryIndexType.TYPE_GEOMETRY, 0);
    if (!polygon.isEmpty()) {
      if (styleService.getBackgroundStyle() != null
          && styleService.getBackgroundStyle().getFillOpacity() > 0) {
        graphics.drawPolygon(bgGroup, "background", polygon, findGeometryStyle(shellIndex));
      }
      draw(geometryGroup, shellIndex, polygon.getExteriorRing(), graphics);
    }

    // Draw the interior rings:
    for (int i = 0; i < polygon.getNumInteriorRing(); i++) {
      GeometryIndex holeIndex =
          editingService
              .getIndexService()
              .addChildren(parentIndex, GeometryIndexType.TYPE_GEOMETRY, i + 1);
      draw(geometryGroup, holeIndex, polygon.getInteriorRingN(i), graphics);
    }
  }
 private ShapeStyle findGeometryStyle(GeometryIndex index) {
   if (!editingService.getIndexStateService().isEnabled(index)) {
     return styleService.getBackgroundDisabledStyle();
   } else if (editingService.getIndexStateService().isMarkedForDeletion(index)) {
     return styleService.getBackgroundMarkedForDeletionStyle();
   }
   return styleService.getBackgroundStyle();
 }
  private void draw(
      Object parentGroup,
      GeometryIndex parentIndex,
      LinearRing linearRing,
      GraphicsContext graphics) {
    String groupName = baseName;
    if (parentIndex != null) {
      groupName += "." + editingService.getIndexService().format(parentIndex);
    }
    Composite edgeGroup = getOrCreateGroup(parentGroup, groupName + ".edges");
    Composite vertexGroup = getOrCreateGroup(parentGroup, groupName + ".vertices");

    Coordinate[] coordinates = linearRing.getCoordinates();
    if (coordinates != null) {
      // Check if we have to draw the background as well (if there are controllers defined for it):
      GraphicsController controller = createGeometryController(parentIndex);
      if (controller != null) {
        Polygon polygon =
            mapWidget.getMapModel().getGeometryFactory().createPolygon(linearRing, null);
        graphics.drawPolygon(parentGroup, groupName + ".background", polygon, new ShapeStyle());
        graphics.setController(parentGroup, groupName + ".background", controller);
      }

      // Draw individual edges:
      int max = coordinates.length;
      if (!styleService.isCloseRingWhileInserting()
          && editingService.getEditingState() == GeometryEditState.INSERTING
          && editingService
              .getIndexService()
              .isChildOf(parentIndex, editingService.getInsertIndex())) {
        max--;
      }
      for (int i = 1; i < max; i++) {
        GeometryIndex edgeIndex =
            editingService
                .getIndexService()
                .addChildren(parentIndex, GeometryIndexType.TYPE_EDGE, i - 1);
        String identifier = baseName + "." + editingService.getIndexService().format(edgeIndex);

        LineString edge =
            linearRing
                .getGeometryFactory()
                .createLineString(new Coordinate[] {coordinates[i - 1], coordinates[i]});
        graphics.drawLine(edgeGroup, identifier, edge, findEdgeStyle(edgeIndex));
        graphics.setController(edgeGroup, identifier, createEdgeController(edgeIndex));
      }

      addInivisibleShapeToGraphicsContext(graphics, vertexGroup);
      for (int i = 0; i < coordinates.length - 1; i++) {
        GeometryIndex vertexIndex =
            editingService
                .getIndexService()
                .addChildren(parentIndex, GeometryIndexType.TYPE_VERTEX, i);
        String identifier = baseName + "." + editingService.getIndexService().format(vertexIndex);
        addShapeToGraphicsContext(
            graphics, vertexGroup, identifier, coordinates[i], findVertexStyle(vertexIndex));
        graphics.setController(vertexGroup, identifier, createVertexController(vertexIndex));
      }
    }
  }
  public void onCoordinateSnapAttempt(CoordinateSnapEvent event) {
    if (editingService.getEditingState() == GeometryEditState.INSERTING) {
      String identifier =
          baseName + "." + editingService.getIndexService().format(editingService.getInsertIndex());
      Object parentGroup =
          groups.get(identifier.substring(0, identifier.lastIndexOf('.')) + ".vertices");

      Coordinate temp = event.getTo();
      Coordinate coordinate =
          mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp);
      addShapeToGraphicsContext(
          mapWidget.getVectorContext(),
          parentGroup,
          identifier,
          coordinate,
          event.hasSnapped() ? styleService.getVertexSnappedStyle() : new ShapeStyle());
    }
  }
  private ShapeStyle findVertexStyle(GeometryIndex index) {
    if (editingService.getIndexStateService().isMarkedForDeletion(index)) {
      return styleService.getVertexMarkForDeletionStyle();
    } else if (!editingService.getIndexStateService().isEnabled(index)) {
      return styleService.getVertexDisabledStyle();
    } else if (editingService.getIndexStateService().isSnapped(index)) {
      return styleService.getVertexSnappedStyle();
    }

    boolean selected = editingService.getIndexStateService().isSelected(index);
    boolean highlighted = editingService.getIndexStateService().isHightlighted(index);
    if (selected && highlighted) {
      return styleService.getVertexSelectHoverStyle();
    } else if (selected) {
      return styleService.getVertexSelectStyle();
    } else if (highlighted) {
      return styleService.getVertexHoverStyle();
    }
    return styleService.getVertexStyle();
  }
 private void addShapeToGraphicsContext(
     GraphicsContext graphics,
     Object parentGroup,
     String identifier,
     Coordinate coordinate,
     ShapeStyle style) {
   int vertexSize = getStyleService().getPointSymbolizerShapeAndSize().getSize();
   int halfVertexSize = vertexSize / 2;
   switch (styleService.getPointSymbolizerShapeAndSize().getShape()) {
     case SQUARE:
       Bbox rectangle =
           new Bbox(
               coordinate.getX() - halfVertexSize,
               coordinate.getY() - halfVertexSize,
               vertexSize,
               vertexSize);
       graphics.drawRectangle(parentGroup, identifier, rectangle, style);
       break;
     case CIRCLE:
       graphics.drawCircle(parentGroup, identifier, coordinate, vertexSize, style);
       break;
   }
 }
  // TODO make use of the findGeometryStyle method.
  private void updateGeometry(Geometry geometry, GeometryIndex index, boolean bringToFront)
      throws GeometryIndexNotFoundException {
    // Some initialization:
    String identifier = baseName + "." + editingService.getIndexService().format(index);
    boolean marked = editingService.getIndexStateService().isMarkedForDeletion(index);

    // Find current and previous parent groups:
    Composite parentGroup =
        groups.get(identifier.substring(0, identifier.lastIndexOf('.')) + ".geometries");

    // Find the correct style:
    ShapeStyle style = new ShapeStyle();
    if (marked) {
      style = styleService.getBackgroundMarkedForDeletionStyle();
    }

    // Draw the inner ring:
    org.geomajas.gwt.client.spatial.geometry.Geometry transformed =
        mapWidget
            .getMapModel()
            .getMapView()
            .getWorldViewTransformer()
            .worldToPan(
                GeometryConverter.toGwt(
                    editingService.getIndexService().getGeometry(geometry, index)));
    if (transformed instanceof LinearRing) {
      Polygon polygon =
          mapWidget
              .getMapModel()
              .getGeometryFactory()
              .createPolygon((LinearRing) transformed, null);
      mapWidget
          .getVectorContext()
          .drawPolygon(parentGroup, identifier + ".background", polygon, style);
    }
  }
  public void onTentativeMove(GeometryEditTentativeMoveEvent event) {
    try {
      Coordinate[] vertices =
          editingService
              .getIndexService()
              .getSiblingVertices(editingService.getGeometry(), editingService.getInsertIndex());
      String geometryType =
          editingService
              .getIndexService()
              .getGeometryType(editingService.getGeometry(), editingService.getInsertIndex());

      if (vertices != null && Geometry.LINE_STRING.equals(geometryType)) {
        String identifier =
            baseName
                + "."
                + editingService.getIndexService().format(editingService.getInsertIndex());
        Object parentGroup =
            groups.get(identifier.substring(0, identifier.lastIndexOf('.')) + ".edges");

        Coordinate temp1 = event.getOrigin();
        Coordinate temp2 = event.getCurrentPosition();
        Coordinate c1 =
            mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp1);
        Coordinate c2 =
            mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp2);

        LineString edge =
            mapWidget
                .getMapModel()
                .getGeometryFactory()
                .createLineString(new Coordinate[] {c1, c2});
        mapWidget
            .getVectorContext()
            .drawLine(
                parentGroup, insertMoveEdgeId1, edge, styleService.getEdgeTentativeMoveStyle());
      } else if (vertices != null && Geometry.LINEAR_RING.equals(geometryType)) {
        String identifier =
            baseName
                + "."
                + editingService.getIndexService().format(editingService.getInsertIndex());
        Object parentGroup =
            groups.get(identifier.substring(0, identifier.lastIndexOf('.')) + ".edges");

        // Line 1
        Coordinate temp1 = event.getOrigin();
        Coordinate temp2 = event.getCurrentPosition();
        Coordinate c1 =
            mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp1);
        Coordinate c2 =
            mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp2);
        LineString edge =
            mapWidget
                .getMapModel()
                .getGeometryFactory()
                .createLineString(new Coordinate[] {c1, c2});
        mapWidget
            .getVectorContext()
            .drawLine(
                parentGroup, insertMoveEdgeId1, edge, styleService.getEdgeTentativeMoveStyle());

        // Line 2
        if (styleService.isCloseRingWhileInserting()) {
          temp1 = vertices[vertices.length - 1];
          c1 = mapWidget.getMapModel().getMapView().getWorldViewTransformer().worldToPan(temp1);
          edge =
              mapWidget
                  .getMapModel()
                  .getGeometryFactory()
                  .createLineString(new Coordinate[] {c1, c2});
          mapWidget
              .getVectorContext()
              .drawLine(
                  parentGroup, insertMoveEdgeId2, edge, styleService.getEdgeTentativeMoveStyle());
        }
      }
    } catch (GeometryIndexNotFoundException e) {
      throw new IllegalStateException(e);
    }
  }
  public void onGeometryEditMove(GeometryEditMoveEvent event) {

    // Find the elements that need updating:
    Map<GeometryIndex, Boolean> indicesToUpdate = new HashMap<GeometryIndex, Boolean>();
    for (GeometryIndex index : event.getIndices()) {
      if (!indicesToUpdate.containsKey(index)) {
        indicesToUpdate.put(index, false);
        if (!Geometry.POINT.equals(editingService.getGeometry().getGeometryType())
            && !Geometry.MULTI_POINT.equals(editingService.getGeometry().getGeometryType())) {
          try {
            List<GeometryIndex> neighbors = null;
            switch (editingService.getIndexService().getType(index)) {
              case TYPE_VERTEX:
                // Move current vertex to the back. This helps the delete operation.
                indicesToUpdate.put(index, true);
                neighbors =
                    editingService.getIndexService().getAdjacentEdges(event.getGeometry(), index);
                if (neighbors != null) {
                  for (GeometryIndex neighborIndex : neighbors) {
                    if (!indicesToUpdate.containsKey(neighborIndex)) {
                      indicesToUpdate.put(neighborIndex, false);
                    }
                  }
                }

                neighbors =
                    editingService
                        .getIndexService()
                        .getAdjacentVertices(event.getGeometry(), index);
                if (neighbors != null) {
                  for (GeometryIndex neighborIndex : neighbors) {
                    if (!indicesToUpdate.containsKey(neighborIndex)) {
                      indicesToUpdate.put(neighborIndex, false);
                    }
                  }
                }
                break;
              case TYPE_EDGE:
                neighbors =
                    editingService
                        .getIndexService()
                        .getAdjacentVertices(event.getGeometry(), index);
                if (neighbors != null) {
                  for (GeometryIndex neighborIndex : neighbors) {
                    if (!indicesToUpdate.containsKey(neighborIndex)) {
                      indicesToUpdate.put(neighborIndex, false);
                    }
                  }
                }
                break;
              default:
            }
          } catch (GeometryIndexNotFoundException e) {
            throw new IllegalStateException(e);
          }
        }
      }
    }

    // Check if we need to draw the background (nice, but slows down):
    if (styleService.getBackgroundStyle() != null
        && styleService.getBackgroundStyle().getFillOpacity() > 0) {
      if (event.getGeometry().getGeometryType().equals(Geometry.POLYGON)) {
        org.geomajas.gwt.client.spatial.geometry.Geometry transformed =
            mapWidget
                .getMapModel()
                .getMapView()
                .getWorldViewTransformer()
                .worldToPan(GeometryConverter.toGwt(event.getGeometry()));
        mapWidget
            .getVectorContext()
            .drawPolygon(
                groups.get(baseName + ".background"),
                "background",
                (Polygon) transformed,
                styleService.getBackgroundStyle());
      } else if (event.getGeometry().getGeometryType().equals(Geometry.MULTI_POLYGON)
          && event.getGeometry().getGeometries() != null) {
        for (int i = 0; i < event.getGeometry().getGeometries().length; i++) {
          Geometry polygon = event.getGeometry().getGeometries()[i];

          org.geomajas.gwt.client.spatial.geometry.Geometry transformed =
              mapWidget
                  .getMapModel()
                  .getMapView()
                  .getWorldViewTransformer()
                  .worldToPan(GeometryConverter.toGwt(polygon));
          mapWidget
              .getVectorContext()
              .drawPolygon(
                  groups.get(baseName + ".geometry" + i + ".background"),
                  "background",
                  (Polygon) transformed,
                  styleService.getBackgroundStyle());
        }
      }
    }

    // Next, redraw the list:
    for (GeometryIndex index : indicesToUpdate.keySet()) {
      update(event.getGeometry(), index, indicesToUpdate.get(index));
    }
  }