package _VisualDVM.ProjectData.Messages;
import Common.Database.Tables.DataSet;
import Common.Visual.DataSetControlForm;
import Common.Visual.Tables.ColumnInfo;
import Common.Visual.Tables.HeaderTextFilter;
import Common.Visual.Tables.RendererWrapText;
import _VisualDVM.Current;
import _VisualDVM.Global;
import _VisualDVM.ProjectData.SapforData.FileObjectWithMessages;

import javax.swing.*;
import java.util.Comparator;
public class MessagesControlForm extends DataSetControlForm<Message> {
    public MessagesControlForm(DataSet dataSource_in, JPanel mountPanel_in) {
        super(dataSource_in, mountPanel_in);
    }
    @Override
    protected boolean hasCheckBox() {
        return false;
    }
    @Override
    protected boolean isPKVisible() {
        return false;
    }
    @Override
    protected void createColumns() {
        AddColumns(
                new ColumnInfo<Message>("группа") {
                    @Override
                    public Object getFieldAt(Message object) {
                        return object.getGroup_s();
                    }
                },
                new ColumnInfo<Message>("строка") {
                    @Override
                    public Object getFieldAt(Message object) {
                        return object.line;
                    }
                },
                new ColumnInfo<Message>("текст") {
                    @Override
                    public Object getFieldAt(Message object) {
                        return object.value;
                    }
                    @Override
                    public Class getRendererClass() {
                        return RendererWrapText.class;
                    }
                    @Override
                    public int getMinWidth() {
                        return 700;
                    }
                }
        );
    }
    @Override
    protected void ShowCurrentObject() throws Exception {
        super.ShowCurrentObject();
        Global.mainModule.getFile().form.getEditor().gotoLine(getCurrent().line);
    }
    @Override
    protected void MouseAction2() throws Exception {
        ShowCurrentObject();
    }
    @Override
    protected void createFilters() {
        AddFilters(new HeaderTextFilter(dataSource, 3, 72));
    }
    @Override
    protected Comparator<Message> getDefaultComparator() {
        return Comparator.comparingInt(o -> o.line);
    }
    @Override
    protected boolean hasMenuBar() {
        return false;
    }
    @Override
    public boolean isObjectVisible(Message object) {
        if (super.isObjectVisible(object)) {
            if (Global.mainModule.HasFile() && Global.mainModule.getFile().name.equals(object.file)) {
                Object o = Global.mainModule.get(Current.FileGraphElement);
                return !(o instanceof FileObjectWithMessages) || ((FileObjectWithMessages) o).HasMessage(object);
            } else return false;
        }
        return false;
    }
}
