private CellContainerAdapter createCellContainerListener()

in cell/src/main/java/jetbrains/jetpad/cell/toView/CellContainerToViewMapper.java [328:421]


  private CellContainerAdapter createCellContainerListener() {
    return new CellContainerAdapter() {
      @Override
      public void onCellPropertyChanged(Cell cell, CellPropertySpec<?> prop, PropertyChangeEvent<?> event) {
        BaseCellMapper<?, ?> target = (BaseCellMapper<?, ?>) rootMapper().getDescendantMapper(cell);
        if (target == null) return;

        if (Cell.isPopupProp(prop)) {
          PropertyChangeEvent<Cell> changeEvent = (PropertyChangeEvent<Cell>) event;
          if (target.isAutoPopupManagement()) {
            target.onEvent(changeEvent);
          }
          if (changeEvent.getOldValue() != null) {
            updateCachesOnRemove(changeEvent.getOldValue());
          }
          if (changeEvent.getNewValue() != null) {
            updateCachesOnAdd(changeEvent.getNewValue());
          }
        } else {
          target.refreshProperties();
          if (cell.isPopup() && target.isAutoPopupManagement()) {
            target.onPopupPropertyChanged(prop, event);
          }
        }

        if (prop == TextCell.CARET_VISIBLE) {
          TextCell text = (TextCell) cell;
          if (text.caretVisible().get()) {
            myWithCaret.add(text);
          } else {
            myWithCaret.remove(text);
          }
        }

        if (prop == Cell.FOCUS_HIGHLIGHTED) {
          if (cell.focusHighlighted().get()) {
            myHighlighted.add(cell);
          } else {
            myHighlighted.remove(cell);
          }
        }
      }

      @Override
      public void onChildAdded(Cell parent, CollectionItemEvent<? extends Cell> change) {
        BaseCellMapper<?, ?> parentMapper = (BaseCellMapper<?, ?>) rootMapper().getDescendantMapper(parent);
        if (parentMapper == null) return;

        parentMapper.childAdded(change.getIndex(), change.getNewItem());

        updateCachesOnAdd(change.getNewItem());
      }

      @Override
      public void onChildRemoved(Cell parent, CollectionItemEvent<? extends Cell> change) {
        BaseCellMapper<?, ?> parentMapper = (BaseCellMapper<?, ?>) rootMapper().getDescendantMapper(parent);
        if (parentMapper == null) return;

        parentMapper.childRemoved(change.getIndex(), change.getOldItem());

        updateCachesOnRemove(change.getOldItem());
      }

      private void updateCachesOnAdd(Cell cell) {
        if (cell instanceof TextCell) {
          TextCell text = (TextCell) cell;
          if (text.caretVisible().get()) {
            myWithCaret.add(text);
          }
        }

        if (cell.focusHighlighted().get()) {
          myHighlighted.add(cell);
        }

        for (Cell child : cell.children()) {
          updateCachesOnAdd(child);
        }
      }

      private void updateCachesOnRemove(Cell cell) {
        for (Cell child : cell.children()) {
          updateCachesOnRemove(child);
        }

        if (cell instanceof TextCell) {
          TextCell text = (TextCell) cell;
          myWithCaret.remove(text);
        }

        myHighlighted.remove(cell);
      }
    };
  }