private CellContainerPeer createContainerPeer()

in cell/src/main/java/jetbrains/jetpad/cell/toView/CellContainerToViewMapper.java [134:229]


  private CellContainerPeer createContainerPeer() {
    final RootCellMapper rootMapper = rootMapper();
    return new CellContainerPeer() {
      @Override
      public int getCaretAt(TextCell tv, int x) {
        TextView textView = (TextView) rootMapper.getDescendantMapper(tv).getTarget();
        return textView.getCaretAt(x);
      }

      @Override
      public int getCaretOffset(TextCell tv, int caret) {
        Mapper<? super TextCell, ?> mapper = rootMapper.getDescendantMapper(tv);
        if (mapper == null) {
          throw new IllegalStateException("Can't find a mapper for " + tv);
        }
        return ((TextView) mapper.getTarget()).getCaretOffset(caret);
      }

      @Override
      public Rectangle getBounds(Cell cell) {
        Rectangle bounds = calculateBounds(cell);
        return bounds == null ? new Rectangle(Vector.ZERO, Vector.ZERO) : bounds;
      }

      private Rectangle calculateBounds(Cell cell) {
        getTarget().container().root().validate();
        BaseCellMapper<?, ? extends View> descendantMapper = (BaseCellMapper<?, ? extends View>) rootMapper.getDescendantMapper(cell);

        if (descendantMapper == null) {
          if (cell instanceof NewLineCell) {
            return null;
          } else if (cell instanceof IndentCell) {
            IndentCell indentCell = (IndentCell) cell;
            return Cells.indentBounds(indentCell);
          } else {
            throw new IllegalStateException("Can't find a mapper for " + cell);
          }
        }
        return descendantMapper.getTarget().bounds().get();
      }

      @Override
      public void scrollTo(Rectangle rect, Cell cell) {
        BaseCellMapper<?, ? extends View> mapper = (BaseCellMapper<?, ? extends View>) rootMapper.getDescendantMapper(cell);
        if (mapper == null) return;
        mapper.getTarget().scrollTo(rect);
      }

      @Override
      public Cell findCell(Cell root, Vector loc) {
        final View rootView = getViewFor(root);
        if (rootView == null) return null;
        View view = rootView.viewAt(loc);
        if (view == null) return null;
        return findCellFor(view);
      }

      private Cell findCellFor(View v) {
        Mapper<? extends Cell, ?> result = myContext.findMapper(v);
        if (result != null) return result.getSource();
        View parent = v.getParent();
        if (parent == null) return null;
        return findCellFor(parent);
      }

      @Override
      public Rectangle visibleRect() {
        Rectangle result = myTargetView.container().visibleRect().intersect(myTargetView.getBounds());
        return result != null ? result : new Rectangle(0, 0, 0, 0);
      }

      @Override
      public void requestFocus() {
        getTarget().container().requestFocus();
      }

      @Override
      public ReadableProperty<Boolean> focused() {
        return getTarget().focused();
      }

      @Override
      public EventDispatchThread getEdt() {
        if (myTargetView.container() == null) {
          throw new IllegalStateException("Target view isn't attached " + myTargetView);
        }
        return myTargetView.container().getEdt();
      }

      private View getViewFor(Cell cell) {
        Mapper<? super Cell, ?> mapper = rootMapper.getDescendantMapper(cell);
        if (mapper == null) return null;
        return (View) mapper.getTarget();
      }
    };
  }