public void handleCheck(Brick brick, boolean isChecked) {
    if (brick == null) {
      return;
    }
    if (isChecked) {
      if (selectMode == ListView.CHOICE_MODE_SINGLE) {
        clearCheckedItems();
      }
      if (brick.getCheckBox() != null && smartBrickSelection(brick, isChecked)) {
        return;
      }
      addElementToCheckedBricks(brick);
    } else {
      if (brick.getCheckBox() != null && smartBrickSelection(brick, isChecked)) {
        return;
      }
      checkedBricks.remove(brick);
    }
    notifyDataSetChanged();

    if (scriptFragment != null) {
      scriptFragment.onBrickChecked();
    }
  }
  private boolean smartBrickSelection(Brick brick, boolean checked) {

    if (brick instanceof ScriptBrick) {

      if (checked) {
        addElementToCheckedBricks(brick);
        animatedBricks.add(brick);
      } else {
        checkedBricks.remove(brick);
      }

      int brickPosition = brickList.indexOf(brick) + 1;
      while ((brickPosition < brickList.size())
          && !(brickList.get(brickPosition) instanceof ScriptBrick)) {
        Brick currentBrick = brickList.get(brickPosition);
        if (currentBrick == null) {
          break;
        }
        if (checked) {
          addElementToCheckedBricks(currentBrick);
          animatedBricks.add(currentBrick);
        } else {
          checkedBricks.remove(currentBrick);
        }
        if (currentBrick.getCheckBox() != null) {
          currentBrick.getCheckBox().setChecked(checked);
          currentBrick.setCheckedBoolean(checked);
        }
        handleBrickEnabledState(currentBrick, !checked);
        brickPosition++;
      }

      animateSelectedBricks();

      if (scriptFragment != null) {
        scriptFragment.onBrickChecked();
      }
      notifyDataSetChanged();
      return true;
    } else if (brick instanceof NestingBrick) {
      for (NestingBrick currentBrick : ((NestingBrick) brick).getAllNestingBrickParts(true)) {
        if (currentBrick == null) {
          break;
        }
        if (checked) {
          animatedBricks.add((Brick) currentBrick);
          addElementToCheckedBricks((Brick) currentBrick);
        } else {
          checkedBricks.remove(currentBrick);
        }

        if (((Brick) currentBrick).getCheckBox() != null) {
          ((Brick) currentBrick).getCheckBox().setChecked(checked);
        }
      }

      animateSelectedBricks();

      if (scriptFragment != null) {
        scriptFragment.onBrickChecked();
      }
      notifyDataSetChanged();
      return true;
    }
    return false;
  }