<commit_before>/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ui;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.impl.DelegateColorScheme;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.ex.*;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.editor.impl.LineSet;
import com.intellij.openapi.editor.impl.RangeMarkerTree;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.LineTokenizer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.Processor;
import com.intellij.util.text.CharSequenceSubSequence;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.List;

/**
 * @author gregsh
 */
public abstract class EditorTextFieldCellRenderer implements TableCellRenderer, Disposable {

  private static final Key<MyPanel> MY_PANEL_PROPERTY = Key.create("EditorTextFieldCellRenderer.MyEditorPanel");

  public EditorTextFieldCellRenderer(Disposable parent) {
    Disposer.register(parent, this);
  }

  protected abstract EditorColorsScheme getColorScheme();

  protected abstract String getText(FontMetrics fontMetrics, JTable table, Object value, int row, int column);

  @Nullable
  protected abstract TextAttributes getTextAttributes(Object value, boolean selected, int row, int col);

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    MyPanel panel = getEditorPanel(table);
    EditorEx editor = panel.myEditor;
    editor.getColorsScheme().setEditorFontSize(table.getFont().getSize());
    String text = getText(((EditorImpl)editor).getFontMetrics(Font.PLAIN), table, value, row, column);
    TextAttributes textAttributes = getTextAttributes(value, isSelected, row, column);
    panel.setText(text, textAttributes);

    ((EditorImpl)editor).setPaintSelection(isSelected);
    editor.getSelectionModel().setSelection(0, isSelected ? editor.getDocument().getTextLength() : 0);
    editor.getColorsScheme().setColor(EditorColors.SELECTION_BACKGROUND_COLOR, table.getSelectionBackground());
    editor.getColorsScheme().setColor(EditorColors.SELECTION_FOREGROUND_COLOR, table.getSelectionForeground());
    editor.setBackgroundColor(getCellBackgroundColor(getColorScheme(), table, isSelected, row));
    panel.setOpaque(!Comparing.equal(editor.getBackgroundColor(), table.getBackground()));

    panel.setBorder(null); // prevents double border painting when ExtendedItemRendererComponentWrapper is used

    return panel;
  }

  public static Color getCellBackgroundColor(EditorColorsScheme colorsScheme, JTable table, boolean isSelected, int row) {
    return isSelected ? table.getSelectionBackground() :
           table.getSelectionModel().getLeadSelectionIndex() == row ? colorsScheme.getColor(EditorColors.CARET_ROW_COLOR) :
           table.getBackground();
  }

  @NotNull
  private MyPanel getEditorPanel(JTable table) {
    MyPanel panel = UIUtil.getClientProperty(table, MY_PANEL_PROPERTY);
    if (panel != null) {
      DelegateColorScheme scheme = (DelegateColorScheme)panel.myEditor.getColorsScheme();
      scheme.setDelegate(getColorScheme());
      return panel;
    }

    // reuse EditorTextField initialization logic
    EditorTextField field = new EditorTextField(new MyDocument(), null, FileTypes.PLAIN_TEXT, false, false);
    field.setSupplementary(true);
    field.addNotify(); // creates editor

    EditorEx editor = (EditorEx)ObjectUtils.assertNotNull(field.getEditor());
    editor.setRendererMode(true);

    editor.setColorsScheme(editor.createBoundColorSchemeDelegate(null));
    editor.getColorsScheme().setColor(EditorColors.CARET_ROW_COLOR, null);

    editor.getScrollPane().setBorder(null);

    panel = new MyPanel(editor);
    Disposer.register(this, panel);

    table.putClientProperty(MY_PANEL_PROPERTY, panel);
    return panel;
  }
<commit_msg>EditorTextFieldRenderer: do not inherit fonts from LaF<commit_after>/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ui;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.colors.impl.DelegateColorScheme;
import com.intellij.openapi.editor.event.DocumentListener;
import com.intellij.openapi.editor.ex.*;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.editor.impl.LineSet;
import com.intellij.openapi.editor.impl.RangeMarkerTree;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.util.*;
import com.intellij.openapi.util.text.LineTokenizer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ObjectUtils;
import com.intellij.util.Processor;
import com.intellij.util.text.CharSequenceSubSequence;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.util.Collections;
import java.util.List;

/**
 * @author gregsh
 */
public abstract class EditorTextFieldCellRenderer implements TableCellRenderer, Disposable {

  private static final Key<MyPanel> MY_PANEL_PROPERTY = Key.create("EditorTextFieldCellRenderer.MyEditorPanel");

  public EditorTextFieldCellRenderer(Disposable parent) {
    Disposer.register(parent, this);
  }

  protected abstract EditorColorsScheme getColorScheme();

  protected abstract String getText(FontMetrics fontMetrics, JTable table, Object value, int row, int column);

  @Nullable
  protected abstract TextAttributes getTextAttributes(Object value, boolean selected, int row, int col);

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    MyPanel panel = getEditorPanel(table);
    EditorEx editor = panel.myEditor;
    editor.getColorsScheme().setEditorFontSize(table.getFont().getSize());
    String text = getText(((EditorImpl)editor).getFontMetrics(Font.PLAIN), table, value, row, column);
    TextAttributes textAttributes = getTextAttributes(value, isSelected, row, column);
    panel.setText(text, textAttributes);

    ((EditorImpl)editor).setPaintSelection(isSelected);
    editor.getSelectionModel().setSelection(0, isSelected ? editor.getDocument().getTextLength() : 0);
    editor.getColorsScheme().setColor(EditorColors.SELECTION_BACKGROUND_COLOR, table.getSelectionBackground());
    editor.getColorsScheme().setColor(EditorColors.SELECTION_FOREGROUND_COLOR, table.getSelectionForeground());
    editor.setBackgroundColor(getCellBackgroundColor(getColorScheme(), table, isSelected, row));
    panel.setOpaque(!Comparing.equal(editor.getBackgroundColor(), table.getBackground()));

    panel.setBorder(null); // prevents double border painting when ExtendedItemRendererComponentWrapper is used

    return panel;
  }

  public static Color getCellBackgroundColor(EditorColorsScheme colorsScheme, JTable table, boolean isSelected, int row) {
    return isSelected ? table.getSelectionBackground() :
           table.getSelectionModel().getLeadSelectionIndex() == row ? colorsScheme.getColor(EditorColors.CARET_ROW_COLOR) :
           table.getBackground();
  }

  @NotNull
  private MyPanel getEditorPanel(JTable table) {
    MyPanel panel = UIUtil.getClientProperty(table, MY_PANEL_PROPERTY);
    if (panel != null) {
      DelegateColorScheme scheme = (DelegateColorScheme)panel.myEditor.getColorsScheme();
      scheme.setDelegate(getColorScheme());
      return panel;
    }

    // reuse EditorTextField initialization logic
    EditorTextField field = new EditorTextField(new MyDocument(), null, FileTypes.PLAIN_TEXT, false, false);
    field.setSupplementary(true);
    field.setFontInheritedFromLAF(false);
    field.addNotify(); // creates editor

    EditorEx editor = (EditorEx)ObjectUtils.assertNotNull(field.getEditor());
    editor.setRendererMode(true);

    editor.setColorsScheme(editor.createBoundColorSchemeDelegate(null));
    editor.getColorsScheme().setColor(EditorColors.CARET_ROW_COLOR, null);

    editor.getScrollPane().setBorder(null);

    panel = new MyPanel(editor);
    Disposer.register(this, panel);

    table.putClientProperty(MY_PANEL_PROPERTY, panel);
    return panel;
  }

{'fraction_non_alphanumeric': 0.09941520467836257, 'fraction_numerical': 0.002599090318388564, 'mean_word_length': 5.502112251056126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5219739', 'n_tokens_mistral': 3065, 'n_tokens_neox': 3017, 'n_words': 669}