public SelectionSupport()

in projectional/src/main/java/jetbrains/jetpad/projectional/selection/SelectionSupport.java [49:113]


  public SelectionSupport(
      List<ItemT> source,
      Cell target,
      List<Cell> targetList) {
    mySource = source;
    myTarget = target;
    myTargetList = targetList;

    myTarget.addTrait(new CellTrait() {
      @Override
      public void onFocusGained(Cell cell, FocusEvent event) {
        super.onFocusGained(cell, event);
        handleFocusGain(event);
      }

      @Override
      public void onFocusLost(Cell cell, FocusEvent event) {
        super.onFocusLost(cell, event);
        handleFocusLost(event);
      }

      @Override
      public void onKeyPressed(Cell cell, KeyEvent event) {
        handleTargetKeyPress(event);
        if (event.isConsumed()) return;

        super.onKeyPressed(cell, event);
      }

      @Override
      public Object get(Cell cell, CellTraitPropertySpec<?> spec) {
        if (spec == SELECTION_SUPPORT) return SelectionSupport.this;
        return super.get(cell, spec);
      }
    });

    SelectionController preinstalled = myTarget.get(SelectionController.PROPERTY);
    if (preinstalled != null) {
      setSelectionController(preinstalled);
    }

    mySelectedItems.addHandler(new EventHandler<CollectionItemEvent<? extends ItemT>>() {
      @Override
      public void onEvent(CollectionItemEvent<? extends ItemT> event) {
        if (mySelectionController != null && !isLowerPrioritySelection()) {
          if (!mySelectedItems.isEmpty()) {
            int startIndex = mySource.indexOf(mySelectedItems.get(0));
            int endIndex = mySource.indexOf(mySelectedItems.get(mySelectedItems.size() - 1));
            /*
             * Because list clear may be implemented with iterate-remove, intermediate states
             * are possible when selection (mySelectedItems) contains items already removed
             * from the model (mySource). We bypass such cases.
             */
            if (startIndex != -1 && endIndex != -1) {
              mySelectionController.updateLegacySelection(myTargetList.get(startIndex), myTargetList.get(endIndex));
            } else if (!myChangingSelection) {
              throw new IllegalStateException("Intermediate state outside changing selection encountered. Selected items: " + mySelectedItems);
            }
          } else {
            mySelectionController.closeLegacySelection();
          }
        }
      }
    });
  }