package org.yamcs.ui.yamcsmonitor;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import org.yamcs.protobuf.YamcsManagement.LinkInfo;
import org.yamcs.ui.UiColors;

public class LinkTable extends JTable {
    private static final long serialVersionUID = 1L;
    private LinkTableModel linkTableModel;
    private LinkCellRenderer linkInfoRenderer;

    public LinkTable(LinkTableModel linkTableModel) {
        super(linkTableModel);
        this.linkTableModel = linkTableModel;
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setAutoCreateRowSorter(true);
        linkInfoRenderer = new LinkCellRenderer(this);
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        int row = rowAtPoint(e.getPoint());
        if (row == -1)
            return "";
        row = convertRowIndexToModel(row);
        return linkTableModel.getLinkInfo(row).getDetailedStatus();
    }

    @Override
    public TableCellRenderer getCellRenderer(int row, int column) {
        return linkInfoRenderer;
    }
    
    private static class LinkCellRenderer extends DefaultTableCellRenderer {
        private static final long serialVersionUID = 1L;
        final Border statusBorder;
        final Border statusBorderSelected;
        final Font statusFont;
        final LinkTable linkTable;
        LinkCellRenderer(LinkTable linkTable) {
            this.linkTable = linkTable;
            statusBorder = BorderFactory.createMatteBorder(1, 1, 1, 1, linkTable.getBackground());
            statusBorderSelected = BorderFactory.createMatteBorder(1, 1, 1, 1, linkTable.getSelectionBackground());
            statusFont = linkTable.getFont().deriveFont(getFont().getSize() * .8f);
        }
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, false /* Prevent blue focus border */, row, column);
            final int modelRow = linkTable.convertRowIndexToModel(row);
            final int modelColumn = linkTable.convertColumnIndexToModel(column);
            final LinkInfo info = linkTable.linkTableModel.getLinkInfo(modelRow);
            setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
            if (info != null && modelColumn==4) {
                setHorizontalAlignment(SwingConstants.CENTER);
                setFont(statusFont);
                setBorder(isSelected ? statusBorderSelected : statusBorder);

                if(info.getDisabled()) {
                    setBackground(UiColors.DISABLED_FAINT_BG);
                    setForeground(UiColors.DISABLED_FAINT_FG);
                } else if ("OK".equals(info.getStatus())) {
                    if (linkTable.linkTableModel.isDataCountIncreasing(modelRow)) {
                        setBackground(UiColors.GOOD_BRIGHT_BG);
                        setForeground(UiColors.GOOD_BRIGHT_FG);
                    } else {
                        setBackground(UiColors.GOOD_FAINT_BG);
                        setForeground(UiColors.GOOD_FAINT_FG);
                    }
                } else {
                    setBackground(UiColors.ERROR_FAINT_BG);
                    setForeground(UiColors.ERROR_FAINT_FG);
                }
            } else if(modelColumn == 5) { // Data count
                setHorizontalAlignment(SwingConstants.RIGHT);
            } else {
                setHorizontalAlignment(SwingConstants.LEFT);
            }
            return this;
        }
    }
}

@@ -49,39 +49,9 @@
 awt.
-Component;%0Aimport java.awt.Font
+*
 ;%0Aim
@@ -82,17 +82,16 @@
 eEvent;%0A
-%0A
 import j
@@ -97,96 +97,53 @@
 java
-x.swing.BorderFactory;%0Aimport javax.swing.JTable;%0Aimport javax.swing.ListSelectionModel;
+.text.NumberFormat;%0Aimport java.util.Locale;%0A
 %0Aimp
@@ -158,30 +158,17 @@
 x.swing.
-SwingConstants
+*
 ;%0Aimport
@@ -556,16 +556,69 @@
 enderer;
+%0A    private DataCountCellRenderer dataCountRenderer;
 %0A%0A    pu
@@ -898,16 +898,77 @@
 (this);%0A
+        dataCountRenderer = new DataCountCellRenderer(this);%0A
     %7D%0A%0A 
@@ -1311,32 +1311,190 @@
 , int column) %7B%0A
+        final int modelColumn = convertColumnIndexToModel(column);%0A        if (modelColumn == 5) %7B%0A            return dataCountRenderer;%0A        %7D else %7B%0A    
         return l
@@ -1510,26 +1510,32 @@
 nderer;%0A
+        %7D%0A
     %7D%0A
-    
 %0A    pri
@@ -4179,11 +4179,770 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Adds some extra capabilities for the data count column to render big numbers in groups of three%0A     */%0A    private static final class DataCountCellRenderer extends LinkCellRenderer %7B%0A        private static final long serialVersionUID = 1L;%0A        private final NumberFormat formatter = NumberFormat.getInstance(Locale.US);%0A%0A        DataCountCellRenderer(LinkTable linkTable) %7B%0A            super(linkTable);%0A        %7D%0A%0A        @Override%0A        protected void setValue(Object value) %7B%0A            Object result = value;%0A            if (value != null) %7B%0A                Number numberValue = (Number) value;%0A                result = formatter.format(numberValue.doubleValue());%0A            %7D%0A            super.setValue(result);%0A        %7D%0A    %7D
 %0A%7D%0A

add decimal grouping for data count