void LMSImportChannelMapDialog::LoadMapping(wxCommandEvent& event)
{
    wxFileDialog dlg(this);
    if (dlg.ShowModal() == wxID_OK) {
        for (int x = 0; x <  modelNames.size(); x++) {
            ModelsChoice->Append(modelNames[x]);
        }
        wxFileInputStream input(dlg.GetPath());
        wxTextInputStream text(input, "\t");
        MapByStrand->SetValue("true" == text.ReadLine());
        int count = wxAtoi(text.ReadLine());
        modelNames.clear();
        for (int x = 0; x < count; x++) {
            std::string mn = text.ReadLine().ToStdString();
            int idx = ModelsChoice->FindString(mn);
            if (idx == wxNOT_FOUND) {
                wxMessageBox("Model " + mn + " not part of sequence.  Not mapping channels to this model.", "", wxICON_WARNING | wxOK , this);
            } else {
                ModelsChoice->Delete(idx);
                modelNames.push_back(mn);
            }
        }
        if (MapByStrand->GetValue()) {
            SetupByStrand();
        } else {
            SetupByNode();
        }
        wxString line = text.ReadLine();
        int r = 0;
        while (line != "") {

            wxString model = FindTab(line);
            wxString strand = FindTab(line);
            wxString node = FindTab(line);
            wxString mapping = FindTab(line);
            xlColor color(FindTab(line));

            if (std::find(modelNames.begin(), modelNames.end(), model.ToStdString()) != modelNames.end()) {
                while (model != ChannelMapGrid->GetCellValue(r, 0)
                       && r < ChannelMapGrid->GetNumberRows()) {
                    r++;
                }

                if (model != ChannelMapGrid->GetCellValue(r, 0)
                    || strand != ChannelMapGrid->GetCellValue(r, 1)
                    || node !=  ChannelMapGrid->GetCellValue(r, 2)) {
                    wxMessageBox(model + "/"+strand+"/"+node+ " not found.  Has the models changed?", "", wxICON_WARNING | wxOK , this);
                } else {
                    ChannelMapGrid->SetCellValue(r, 3, mapping);
                    ChannelMapGrid->SetCellBackgroundColour(r, 4, color.asWxColor());
                }
                r++;
            }
            line = text.ReadLine();
        }
    }
}
void LMSImportChannelMapDialog::OnMapByStrandClick(wxCommandEvent& event)
{
    ChannelMapGrid->BeginBatch();
    wxGridCellChoiceEditor *editor = new wxGridCellChoiceEditor(Convert(MapByStrand->GetValue() ? ccrNames : channelNames));
    ChannelMapGrid->SetDefaultEditor(editor);
    if (MapByStrand->GetValue()) {
        SetupByStrand();
    } else {
        SetupByNode();
    }
    ChannelMapGrid->EndBatch();
}
void LMSImportChannelMapDialog::LoadMapping(wxCommandEvent& event)
{
    bool strandwarning = false;
    bool modelwarning = false;
    if (_dirty)
    {
        if (wxMessageBox("Are you sure you dont want to save your changes for future imports?", "Are you sure?", wxYES_NO | wxCENTER, this) == wxNO)
        {
            return;
        }
    }

    wxFileDialog dlg(this);
    if (dlg.ShowModal() == wxID_OK) {
        for (size_t x = 0; x <  modelNames.size(); x++) {
            ModelsChoice->Append(modelNames[x]);
        }
        ChannelMapGrid->BeginBatch();
        wxFileInputStream input(dlg.GetPath());
        wxTextInputStream text(input, "\t");
        MapByStrand->SetValue("true" == text.ReadLine());
        int count = wxAtoi(text.ReadLine());
        modelNames.clear();
        for (int x = 0; x < count; x++) {
            std::string mn = text.ReadLine().ToStdString();
            int idx = ModelsChoice->FindString(mn);
            if (idx == wxNOT_FOUND) {
                //wxMessageBox("Model " + mn + " not part of sequence.  Not mapping channels to this model.", "", wxICON_WARNING | wxOK , this);
                if (!modelwarning)
                {
                    if (wxMessageBox("Model " + mn + " not part of sequence.  Not mapping channels to this model. Do you want to see future occurences of this error during this import?", "", wxICON_WARNING | wxYES_NO, this) == wxNO)
                    {
                        modelwarning = true;
                    }
                }
            } else {
                ModelsChoice->Delete(idx);
                modelNames.push_back(mn);
            }
        }
        if (MapByStrand->GetValue()) {
            SetupByStrand();
        } else {
            SetupByNode();
        }
        wxString line = text.ReadLine();
        int r = 0;
        while (line != "") {

            wxString model = FindTab(line);
            wxString strand = FindTab(line);
            wxString node = FindTab(line);
            wxString mapping = FindTab(line);
            xlColor color(FindTab(line));

            Element *modelEl = mSequenceElements->GetElement(model.ToStdString());
            if (modelEl == nullptr && allowAddModels) {
                modelEl = mSequenceElements->AddElement(model.ToStdString(), "model", false, false, false, false);
                modelEl->AddEffectLayer();
            }
            if (modelEl != nullptr) {
                while (model != ChannelMapGrid->GetCellValue(r, 0)
                       && r < ChannelMapGrid->GetNumberRows()) {
                    r++;
                }

                if (model != ChannelMapGrid->GetCellValue(r, 0)
                    || strand != ChannelMapGrid->GetCellValue(r, 1)
                    || node !=  ChannelMapGrid->GetCellValue(r, 2)) {
                    if (!strandwarning)
                    {
                        if (wxMessageBox(model + "/" + strand + "/" + node + " not found.  Has the models changed? Do you want to see future occurences of this error during this import?", "", wxICON_WARNING | wxYES_NO, this) == wxNO)
                        {
                            strandwarning = true;
                        }
                    }
                } else {
                    ChannelMapGrid->SetCellValue(r, 3, mapping);
                    ChannelMapGrid->SetCellBackgroundColour(r, 4, color.asWxColor());
                }
                r++;
            }
            line = text.ReadLine();
        }
        ChannelMapGrid->EndBatch();
        _dirty = false;
    }
}