package com.connor.erp.materialquery.ViewStockOfERP;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.TableCellRenderer;

public class CTUI extends BasicTableUI {
  public void paint(Graphics g, JComponent c) {
    Rectangle r = g.getClipBounds();
    int firstCol = this.table.columnAtPoint(new Point(r.x, 0));
    int lastCol = this.table.columnAtPoint(new Point(r.x + r.width, 0));
    if (lastCol < 0)
      lastCol = this.table.getColumnCount() - 1; 
    for (int i = firstCol; i <= lastCol; i++)
      paintCol(i, g); 
  }
  
  private void paintCol(int col, Graphics g) {
    Rectangle r = g.getClipBounds();
    for (int i = 0; i < this.table.getRowCount(); i++) {
      Rectangle r1 = this.table.getCellRect(i, col, true);
      if (r1.intersects(r)) {
        int sk = ((CTable)this.table).map.visibleCell(this.table, i, col);
        paintCell(sk, col, g, r1);
        i += ((CTable)this.table).map.span(this.table, sk, col) - 1;
      } 
    } 
  }
  
  private void paintCell(int row, int column, Graphics g, Rectangle area) {
    int verticalMargin = this.table.getRowMargin();
    int horizontalMargin = this.table.getColumnModel().getColumnMargin();
    Color c = g.getColor();
    g.setColor(this.table.getGridColor());
    g.drawRect(area.x, area.y, area.width - 1, area.height - 1);
    g.setColor(c);
    area.setBounds(area.x + horizontalMargin / 2, area.y + verticalMargin / 2, area.width - horizontalMargin, area.height - verticalMargin);
    if (this.table.isEditing() && this.table.getEditingRow() == row && this.table.getEditingColumn() == column) {
      Component component = this.table.getEditorComponent();
      component.setBounds(area);
      component.validate();
    } else {
      TableCellRenderer renderer = this.table.getCellRenderer(row, column);
      Component component = this.table.prepareRenderer(renderer, row, column);
      if (component.getParent() == null)
        this.rendererPane.add(component); 
      this.rendererPane.paintComponent(g, component, this.table, area.x, area.y, area.width, area.height, true);
    } 
  }
}
