public static void paintComponentDecoration(
      final GuiEditor editor, final RadComponent component, final Graphics g) {
    // Collect selected components and paint decoration for non selected components
    final ArrayList<RadComponent> selection = new ArrayList<RadComponent>();
    final Rectangle layeredPaneRect = editor.getLayeredPane().getVisibleRect();
    FormEditingUtil.iterate(
        component,
        new FormEditingUtil.ComponentVisitor<RadComponent>() {
          public boolean visit(final RadComponent component) {
            if (!component.getDelegee().isShowing()) { // Skip invisible components
              return true;
            }
            final Shape oldClip = g.getClip();
            final RadContainer parent = component.getParent();
            if (parent != null) {
              final Point p =
                  SwingUtilities.convertPoint(
                      component.getDelegee(), 0, 0, editor.getLayeredPane());
              final Rectangle visibleRect =
                  layeredPaneRect.intersection(
                      new Rectangle(p.x, p.y, parent.getWidth(), parent.getHeight()));
              g.setClip(visibleRect);
            }
            if (component.isSelected()) { // we will paint selection later
              selection.add(component);
            } else {
              paintComponentBoundsImpl(editor, component, g);
            }
            paintGridOutline(editor, component, g);
            if (parent != null) {
              g.setClip(oldClip);
            }
            return true;
          }
        });

    // Let's paint decoration for selected components
    for (int i = selection.size() - 1; i >= 0; i--) {
      final Shape oldClip = g.getClip();
      final RadComponent c = selection.get(i);
      final RadContainer parent = c.getParent();
      if (parent != null) {
        final Point p = SwingUtilities.convertPoint(c.getDelegee(), 0, 0, editor.getLayeredPane());
        final Rectangle visibleRect =
            layeredPaneRect.intersection(
                new Rectangle(p.x, p.y, parent.getWidth(), parent.getHeight()));
        g.setClip(visibleRect);
      }
      paintComponentBoundsImpl(editor, c, g);
      if (parent != null) {
        g.setClip(oldClip);
      }
    }
  }
  /** This method paints grid bounds for "grid" containers */
  public static void paintGridOutline(
      final GuiEditor editor, @NotNull final RadComponent component, final Graphics g) {
    if (!editor.isShowGrid()) {
      return;
    }
    if (!(component instanceof RadContainer)) {
      return;
    }
    final RadContainer container = (RadContainer) component;
    if (!container.getLayoutManager().isGrid()) {
      return;
    }

    // performance: don't paint grid outline in drag layer
    Container parent = component.getDelegee().getParent();
    while (parent != null) {
      if (parent == editor.getDragLayer()) {
        return;
      }
      parent = parent.getParent();
    }

    final Point point =
        SwingUtilities.convertPoint(
            component.getDelegee(), 0, 0, editor.getRootContainer().getDelegee());
    g.translate(point.x, point.y);
    try {
      // Paint grid
      if (container.getWidth() > 0 && container.getHeight() > 0) {
        Image gridImage = CachedGridImage.getGridImage(container);
        g.drawImage(gridImage, 0, 0, null);
      }
    } finally {
      g.translate(-point.x, -point.y);
    }
  }