package _VisualDVM.ProjectData.SapforData.Arrays.UI;
import Common.Visual.UI_;
import _VisualDVM.Current;
import Common.Visual.ControlForm;
import Common.Visual.Tables.Grid.GridAnchestor;
import Common.Visual.Tables.StyledTable;
import _VisualDVM.Global;
import _VisualDVM.Visual.UI;
import _VisualDVM.ProjectData.SapforData.Arrays.ProjectArray;
import _VisualDVM.ProjectData.SapforData.Arrays.Templates.TemplateDimension;

import java.awt.*;
import java.util.Vector;

import static Common.Visual.Tables.TableEditors.EditorDimension;
import static Common.Visual.Tables.TableRenderers.RendererDimension;
public class DimensionsTableForm extends ControlForm<StyledTable> {
    public DimensionsTableForm() {
        super(StyledTable.class);
    }
    @Override
    public void Show() {
        super.Show();
        content.add(scroll, BorderLayout.CENTER);
        content.updateUI();
    }
    @Override
    public void Clear() {
        super.Clear();
        UI_.Clear(content);
    }
    @Override
    public void CreateControl() {
        Vector<String> columns = new Vector<>();
        columns.add("шаблон");
        for (int i = 0; i < Global.mainModule.getProject().maxdim; ++i)
            columns.add(ProjectArray.alignNames[i]);
        Vector<Object> dimensions = new Vector<>();
        Global.mainModule.getProject().templates.forEach(t -> dimensions.add(t.CreateTemplateCells()));
        control = new StyledTable(new GridAnchestor(columns, dimensions) {
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return ((Object[]) data.get(rowIndex))[columnIndex];
            }
            @Override
            public boolean isCellEditable(int row, int col) {
                return (col > 0) && !((TemplateDimension) getValueAt(row, col)).isBlocked();
            }
        }) {
            @Override
            public void Init() {
                setDefaultRenderer(TemplateDimension.class, UI.TableRenderers.get(RendererDimension));
                setDefaultEditor(TemplateDimension.class, UI.TableEditors.get(EditorDimension));
            }
        };
    }
}
