private Registration registerListeners()

in cell/src/main/java/jetbrains/jetpad/cell/toDom/CellContainerToDomMapper.java [439:641]


  private Registration registerListeners() {
    final CompositeRegistration reg = new CompositeRegistration();

    final Element focusTarget = getFocusTarget();
    final Element target = getTarget();
    final ClipboardSupport clipboardSupport = new ClipboardSupport(focusTarget);

    final Value<Boolean> pressed = new Value<>(false);
    final Value<Boolean> pressedOutside = new Value<>(false);

    reg.add(eventRegistration(Event.ONCLICK, target, new Function() {
      @Override
      public boolean f(Event e) {
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        getSource().mouseClicked(event);
        return !event.isConsumed();
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEDOWN, target, new Function() {
      @Override
      public boolean f(Event e) {
        pressed.set(true);
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        getSource().mousePressed(event);
        if (!myCellToDomContext.focused.get()) {
          $(focusTarget).focus();
        }
        return !event.isConsumed();
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEDOWN, Document.get(), new Function() {
      @Override
      public boolean f(Event e) {
        pressed.set(true);
        MouseEvent evt = toMouseEvent(e);
        if (!isContainerEvent(evt)) {
          pressedOutside.set(true);
          return true;
        }
        return true;
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEUP, target, new Function() {
      @Override
      public boolean f(Event e) {
        pressed.set(false);
        pressedOutside.set(false);
        return true;
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEUP, target, new Function() {
      @Override
      public boolean f(Event e) {
        pressed.set(false);
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        getSource().mouseReleased(event);
        return true;
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEMOVE, Document.get(), new Function() {
      @Override
      public boolean f(Event e) {
        MouseEvent evt = toMouseEvent(e);
        if (pressed.get() && !pressedOutside.get()) {
          getSource().mouseDragged(evt);
        }
        return true;
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEMOVE, target, new Function() {
      public boolean f(Event e) {
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        if (pressed.get() && !pressedOutside.get()) {
          getSource().mouseDragged(event);
        } else {
          getSource().mouseMoved(event);
        }
        return !event.isConsumed();
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEOVER, target, new Function() {
      public boolean f(Event e) {
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        getSource().mouseEntered(event);
        return false;
      }
    }));

    reg.add(eventRegistration(Event.ONMOUSEOUT, target, new Function() {
      public boolean f(Event e) {
        MouseEvent event = toMouseEvent(e);
        if (isDomCellEvent(event)) return true;
        getSource().mouseLeft(event);
        return false;
      }
    }));

    reg.add(eventRegistration(Event.ONKEYDOWN, focusTarget, new Function() {
      @Override
      public boolean f(Event e) {
        return EventTranslator.dispatchKeyPress(e, new Handler<KeyEvent>() {
          @Override
          public void handle(final KeyEvent e) {
            if (e.is(Key.SPACE)) {
              getSource().keyPressed(e);
              getSource().keyTyped(new KeyEvent(Key.SPACE, ' ', Collections.<ModifierKey>emptySet()));
              return;
            }

            if (e.is(KeyStrokeSpecs.PASTE)) {
              clipboardSupport.pasteContent(new Handler<String>() {
                @Override
                public void handle(String text) {
                  if (Strings.isNullOrEmpty(text)) {
                    getSource().keyPressed(e.copy());
                  } else {
                    getSource().paste(text);
                  }
                }
              });
              return;
            }

            if (e.is(KeyStrokeSpecs.CUT) || e.is(KeyStrokeSpecs.COPY)) {
              CopyCutEvent copyEvent;
              if (e.is(KeyStrokeSpecs.CUT)) {
                getSource().cut(copyEvent = new CopyCutEvent(true));
              } else {
                getSource().copy(copyEvent = new CopyCutEvent(false));
              }
              ClipboardContent content = copyEvent.getResult();
              if (content != null) {
                clipboardSupport.copyContent(content);
              }
              return;
            }

            getSource().keyPressed(e);
          }
        });
      }
    }));
    reg.add(eventRegistration(Event.ONKEYUP, focusTarget, new Function() {
      @Override
      public boolean f(Event e) {
        return EventTranslator.dispatchKeyRelease(e, new Handler<KeyEvent>() {
          @Override
          public void handle(KeyEvent e) {
            getSource().keyReleased(e);
          }
        });
      }
    }));
    reg.add(eventRegistration(Event.ONKEYPRESS, focusTarget, new Function() {
      @Override
      public boolean f(Event e) {
        return EventTranslator.dispatchKeyType(e, new Handler<KeyEvent>() {
          @Override
          public void handle(KeyEvent e) {
            //Space is a special key in Chrome. We emulate its typing in keydown
            if (e.getKeyChar() == ' ') return;
            getSource().keyTyped(e);
          }
        });
      }
    }));

    reg.add(eventRegistration(Event.ONFOCUS, focusTarget, new Function() {
      @Override
      public boolean f(Event e) {
        myCellToDomContext.focused.set(true);
        return false;
      }
    }));
    reg.add(eventRegistration(Event.ONBLUR, focusTarget, new Function() {
      @Override
      public boolean f(Event e) {
        myCellToDomContext.focused.set(false);
        return false;
      }
    }));

    reg.add(getSource().focusedCell.addHandler(new EventHandler<PropertyChangeEvent<Cell>>() {
      @Override
      public void onEvent(PropertyChangeEvent<Cell> event) {
        invalidateLineHighlight();
      }
    }));

    return reg;
  }