protected void onAttach()

in view/src/main/java/jetbrains/jetpad/projectional/view/toGwt/ViewContainerToElementMapper.java [304:405]


  protected void onAttach(MappingContext ctx) {
    super.onAttach(ctx);
    update();

    myRootMapper.set(myCtx.getFactory().createMapper(getSource().root()));
    myRootDiv.appendChild(myRootMapper.get().getTarget());

    TextMetrics metrics = TextMetricsCalculator.calculate(TextView.DEFAULT_FONT);
    final int baseLine = metrics.baseLine();
    final double fontWidth = metrics.dimension().x;
    final double fontHeight = metrics.dimension().y;

    getSource().setPeer(new ViewContainerPeer() {
      private Registration myReg;

      @Override
      public void attach(final ViewContainer container) {
        myReg = container.root().valid().addHandler(new EventHandler<PropertyChangeEvent<Boolean>>() {
          @Override
          public void onEvent(PropertyChangeEvent<Boolean> event) {
            Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
              @Override
              public void execute() {
                container.root().validate();
              }
            });
          }
        });

        container.root().validate();
      }

      @Override
      public void detach() {
        myReg.remove();
      }

      @Override
      public void repaint(View view) {
      }

      @Override
      public Rectangle visibleRect() {
        getSource().root().validate();
        Rectangle visiblePart = DomUtil.visiblePart(myRootDiv);
        return new Rectangle(visiblePart.origin.sub(new Vector(myRootDiv.getAbsoluteLeft(), myRootDiv.getAbsoluteTop())), visiblePart.dimension);
      }

      @Override
      public void scrollTo(Rectangle rect, View view) {
        Scrolling.scrollTo(rect, (Element) myRootMapper.get().getDescendantMapper(view).getTarget());
      }

      @Override
      public void boundsChanged(View view, PropertyChangeEvent<Rectangle> change) {
      }

      @Override
      public int textHeight(Font font) {
        if (font.equals(TextView.DEFAULT_FONT)) {
          return (int) fontHeight;
        } else {
          return (int) TextMetricsCalculator.calculateApprox(font).dimension().y;
        }
      }

      @Override
      public int textBaseLine(Font font) {
        if (font.equals(TextView.DEFAULT_FONT)) {
          return baseLine;
        } else {
          return TextMetricsCalculator.calculateApprox(font).baseLine();
        }
      }

      @Override
      public int textWidth(Font font, String text) {
        if (TextView.DEFAULT_FONT.getFamily() == FontFamily.MONOSPACED && font.equals(TextView.DEFAULT_FONT)) {
          return (int) (text.length() * fontWidth);
        } else {
          return (int) TextMetricsCalculator.calculateApprox(font, text).dimension().x;
        }
      }

      @Override
      public void requestFocus() {
        myRootDiv.focus();
      }

      @Override
      public Object getMappedTo(View view) {
        Mapper<? super View, ?> mapper = myRootMapper.get().getDescendantMapper(view);
        if (mapper == null) return null;
        return mapper.getTarget();
      }

      @Override
      public EventDispatchThread getEdt() {
        return JsEventDispatchThread.INSTANCE;
      }
    });
  }