/*
 * Copyright 2000-2017 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.vcs.log.ui.table;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.ScrollingUtil;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.UIUtil;
import com.intellij.vcs.log.impl.VcsLogUiProperties;
import com.intellij.vcs.log.ui.VcsLogColorManager;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

import static com.intellij.vcs.log.impl.CommonUiProperties.SHOW_ROOT_NAMES;

class RootCellRenderer extends JBLabel implements TableCellRenderer {
  @NotNull private final VcsLogUiProperties myProperties;
  @NotNull private final VcsLogColorManager myColorManager;
  @NotNull private Color myColor = UIUtil.getTableBackground();
  @NotNull private Color myBorderColor = UIUtil.getTableBackground();
  private boolean isNarrow = true;
  private int myRowHeight;

  RootCellRenderer(@NotNull VcsLogUiProperties properties, @NotNull VcsLogColorManager colorManager) {
    super("", CENTER);
    myProperties = properties;
    myColorManager = colorManager;
  }

  @Override
  protected void paintComponent(Graphics g) {
    g.setColor(myColor);

    int width = getWidth();

    if (isNarrow) {
      g.fillRect(0, 0, width - JBUI.scale(VcsLogGraphTable.ROOT_INDICATOR_WHITE_WIDTH), myRowHeight);
      g.setColor(myBorderColor);
      g.fillRect(width - JBUI.scale(VcsLogGraphTable.ROOT_INDICATOR_WHITE_WIDTH), 0,
                 JBUI.scale(VcsLogGraphTable.ROOT_INDICATOR_WHITE_WIDTH),
                 myRowHeight);
    }
    else {
      g.fillRect(0, 0, width, myRowHeight);
    }

    super.paintComponent(g);
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    String text;
    Color color;

    if (value instanceof VirtualFile) {
      VirtualFile root = (VirtualFile)value;
      int readableRow = ScrollingUtil.getReadableRow(table, Math.round(table.getRowHeight() * 0.5f));
      if (row < readableRow) {
        text = "";
      }
      else if (row == 0 || !value.equals(table.getModel().getValueAt(row - 1, column)) || readableRow == row) {
        text = root.getName();
      }
      else {
        text = "";
      }
      color = VcsLogGraphTable.getRootBackgroundColor(root, myColorManager);
    }
    else {
      text = null;
      color = UIUtil.getTableBackground(isSelected);
    }

    myColor = color;
    Color background = ((VcsLogGraphTable)table).getStyle(row, column, hasFocus, isSelected).getBackground();
    assert background != null;
    myBorderColor = background;
    setForeground(UIUtil.getTableForeground(false));

    if (myProperties.exists(SHOW_ROOT_NAMES) && myProperties.get(SHOW_ROOT_NAMES)) {
      setText(text);
      isNarrow = false;
    }
    else {
      setText("");
      isNarrow = true;
    }

    myRowHeight = table.getRowHeight();

    return this;
  }

  @Override
  public void setBackground(Color bg) {
    myBorderColor = bg;
  }
}

@@ -744,26 +744,29 @@
 .ui.
-components.JBLabel
+SimpleColoredRenderer
 ;%0Aim
@@ -1178,15 +1178,29 @@
 nds 
-JBLabel
+SimpleColoredRenderer
  imp
@@ -1517,35 +1517,8 @@
 rue;
-%0A  private int myRowHeight;
 %0A%0A  
@@ -1622,31 +1622,8 @@
 ) %7B%0A
-    super(%22%22, CENTER);%0A
     
@@ -1690,65 +1690,139 @@
 ;%0A  
-%7D%0A%0A  @Override%0A  protected void paintComponent(Graphics g
+  setTextAlign(SwingConstants.CENTER);%0A  %7D%0A%0A  @Override%0A  protected void paintBackground(Graphics2D g, int x, int width, int height
 ) %7B%0A
@@ -1851,37 +1851,8 @@
 );%0A%0A
-    int width = getWidth();%0A%0A
     
@@ -1876,33 +1876,33 @@
      g.fillRect(
-0
+x
 , 0, width - JBU
@@ -1955,22 +1955,17 @@
 WIDTH), 
-myRowH
+h
 eight);%0A
@@ -2014,16 +2014,20 @@
 illRect(
+x + 
 width - 
@@ -2173,30 +2173,25 @@
             
-myRowH
+h
 eight);%0A    
@@ -2220,17 +2220,17 @@
 illRect(
-0
+x
 , 0, wid
@@ -2233,22 +2233,17 @@
  width, 
-myRowH
+h
 eight);%0A
@@ -2251,38 +2251,8 @@
    %7D
-%0A%0A    super.paintComponent(g);
 %0A  %7D
@@ -2981,20 +2981,18 @@
  text = 
-null
+%22%22
 ;%0A      
@@ -3041,24 +3041,37 @@
 ed);%0A    %7D%0A%0A
+    clear();%0A
     myColor 
@@ -3397,23 +3397,22 @@
 %7B%0A      
-setText
+append
 (text);%0A
@@ -3458,23 +3458,22 @@
 %7B%0A      
-setText
+append
 (%22%22);%0A  
@@ -3504,49 +3504,8 @@
  %7D%0A%0A
-    myRowHeight = table.getRowHeight();%0A%0A
     

migrate RootCellRenderer to SimpleColoredRenderer from JBLabel