Example #1
0
  private void paintSelection(Graphics2D g) {
    int selectionStartOffset = editor.getSelectionModel().getSelectionStart();
    int selectionEndOffset = editor.getSelectionModel().getSelectionEnd();
    int firstSelectedLine = coords.offsetToScreenSpace(selectionStartOffset);
    int firstSelectedCharacter = coords.offsetToCharacterInLine(selectionStartOffset);
    int lastSelectedLine = coords.offsetToScreenSpace(selectionEndOffset);
    int lastSelectedCharacter = coords.offsetToCharacterInLine(selectionEndOffset);

    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.90f));
    g.setColor(
        editor
            .getColorsScheme()
            .getColor(ColorKey.createColorKey("SELECTION_BACKGROUND", JBColor.BLUE)));

    if (firstSelectedLine == lastSelectedLine) {
      // Single line is easy
      g.fillRect(
          firstSelectedCharacter,
          firstSelectedLine,
          lastSelectedCharacter - firstSelectedCharacter,
          config.pixelsPerLine);
    } else {
      // Draw the line leading in
      g.fillRect(
          firstSelectedCharacter,
          firstSelectedLine,
          getWidth() - firstSelectedCharacter,
          config.pixelsPerLine);

      // Then the line at the end
      g.fillRect(0, lastSelectedLine, lastSelectedCharacter, config.pixelsPerLine);

      if (firstSelectedLine + 1 != lastSelectedLine) {
        // And if there is anything in between, fill it in
        g.fillRect(
            0,
            firstSelectedLine + config.pixelsPerLine,
            getWidth(),
            lastSelectedLine - firstSelectedLine - config.pixelsPerLine);
      }
    }
  }