package jetbrains.mps.workbench.dialogs.project.components.parts.diffrowtable;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import javax.swing.JTable;
import java.awt.Point;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import javax.swing.table.TableModel;
import java.awt.Rectangle;
import javax.swing.table.TableColumn;
import java.util.Enumeration;
import java.awt.Dimension;
import javax.swing.event.ListSelectionEvent;

@GeneratedClass(node = "r:2645cf7b-becf-4fd8-946c-a69d83379844(jetbrains.mps.workbench.dialogs.project.components.parts.diffrowtable)/1560298786499490568", model = "r:2645cf7b-becf-4fd8-946c-a69d83379844(jetbrains.mps.workbench.dialogs.project.components.parts.diffrowtable)")
public class DiffRowTable extends JTable {
  public DiffRowTable() {
    setUI(new BasicDiffRowTableUI());
  }
  @Override
  public int rowAtPoint(Point point) {
    int y = point.y;
    int rowSpacing = getIntercellSpacing().height;
    int rowCount = getRowCount();
    int rowHeight = 0;
    for (int row = 0; row < rowCount; row++) {
      rowHeight += getRowHeight(row) + rowSpacing;
      if (y < rowHeight) {
        return row;
      }
    }
    return -1;
  }
  public int getHeight(Object obj, int colNum) {
    TableCellRenderer tcr = getColumnModel().getColumn(colNum).getCellRenderer();
    if (tcr != null) {
      Component rendererComponent = tcr.getTableCellRendererComponent(this, obj, true, true, 0, 0);
      return (int) Math.round(rendererComponent.getPreferredSize().getHeight());
    }
    return getRowHeight();
  }
  @Override
  public int getRowHeight() {
    return getFontMetrics(getFont()).getHeight();
  }
  @Override
  public int getRowHeight(int row) {
    TableModel tm = getModel();
    int height = getFontMetrics(getFont()).getHeight();
    for (int i = 0; i < getColumnModel().getColumnCount(); i++) {
      try {
        height = Math.max(height, getHeight(tm.getValueAt(row, i), i));
      } catch (Exception e) {
      }
    }
    return height;
  }
  @Override
  public Rectangle getCellRect(int row, int column, boolean includeSpacing) {
    Rectangle cellFrame;
    TableColumn aColumn;
    cellFrame = new Rectangle();
    cellFrame.height = getRowHeight(row) + rowMargin;
    cellFrame.y = 0;
    for (int i = 0; i < row; i++) {
      cellFrame.y += getRowHeight(i) + rowMargin;
    }
    int index = 0;
    int columnMargin = getColumnModel().getColumnMargin();
    Enumeration enumeration = getColumnModel().getColumns();
    while (enumeration.hasMoreElements()) {
      aColumn = (TableColumn) enumeration.nextElement();
      cellFrame.width = aColumn.getWidth() + columnMargin;
      if (index == column) {
        break;
      }
      cellFrame.x += cellFrame.width;
      index++;
    }
    if (!(includeSpacing)) {
      Dimension spacing = getIntercellSpacing();
      cellFrame.setBounds(cellFrame.x + spacing.width / 2, cellFrame.y + spacing.height / 2, cellFrame.width - spacing.width, cellFrame.height - spacing.height);
    }
    return cellFrame;
  }
  @Override
  public void columnSelectionChanged(ListSelectionEvent e) {
    repaint();
  }
  @Override
  public void valueChanged(ListSelectionEvent e) {
    int firstIndex = e.getFirstIndex();
    int lastIndex = e.getLastIndex();
    if (firstIndex == -1 && lastIndex == -1) {
      repaint();
    }
    Rectangle dirtyRegion = getCellRect(firstIndex, 0, false);
    int numColumns = getColumnCount();
    int index = firstIndex;
    for (int i = 0; i < numColumns; i++) {
      dirtyRegion.add(getCellRect(index, i, false));
    }
    index = lastIndex;
    for (int i = 0; i < numColumns; i++) {
      dirtyRegion.add(getCellRect(index, i, false));
    }
    repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
  }
}
