//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicImportInputEclipseCaseOpmFeature::onActionTriggered(bool isChecked)
{
    RiaApplication* app = RiaApplication::instance();
    QString defaultDir = app->lastUsedDialogDirectory("INPUT_FILES");
    QString fileName = QFileDialog::getOpenFileName(RiuMainWindow::instance(), "Import Eclipse Input file", defaultDir, "Eclipse Input Files (*.GRDECL);;All Files (*.*)");

    if (fileName.isEmpty()) return;

    // Remember the path to next time
    app->setLastUsedDialogDirectory("INPUT_FILES", QFileInfo(fileName).absolutePath());

    RimProject* proj = app->project();
    RimEclipseCaseCollection* analysisModels = proj->activeOilField() ? proj->activeOilField()->analysisModels() : NULL;
    if (analysisModels)
    {
        // This code originates from RiaApplication::openInputEclipseCaseFromFileNames

        RimEclipseInputCaseOpm* rimInputReservoir = new RimEclipseInputCaseOpm();
        proj->assignCaseIdToCase(rimInputReservoir);

        rimInputReservoir->importNewEclipseGridAndProperties(fileName);

        analysisModels->cases.push_back(rimInputReservoir);

        RimEclipseView* riv = rimInputReservoir->createAndAddReservoirView();

        riv->cellResult()->setResultType(RimDefines::INPUT_PROPERTY);
        riv->hasUserRequestedAnimation = true;

        riv->loadDataAndUpdate();

        if (!riv->cellResult()->hasResult())
        {
            riv->cellResult()->setResultVariable(RimDefines::undefinedResultName());
        }

        analysisModels->updateConnectedEditors();

        RiuMainWindow::instance()->selectAsCurrentItem(riv->cellResult());
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicPasteEclipseCasesFeature::addCasesToGridCaseGroup(PdmObjectGroup& objectGroup, RimIdenticalGridCaseGroup* gridCaseGroup)
{
    RimProject* proj = RiaApplication::instance()->project();
    CVF_ASSERT(proj);

    std::vector<RimEclipseResultCase*> resultCases;

    for (size_t i = 0; i < objectGroup.objects.size(); i++)
    {
        RimEclipseResultCase* eclCase = dynamic_cast<RimEclipseResultCase*>(objectGroup.objects[i]);
        if (eclCase)
        {
            RimEclipseResultCase* eclCaseCopy = dynamic_cast<RimEclipseResultCase*>(eclCase->copyByXmlSerialization(PdmDefaultObjectFactory::instance()));
            resultCases.push_back(eclCaseCopy);
        }
    }

    if (resultCases.size() == 0)
    {
        return;
    }

    RimEclipseResultCase* mainResultCase = NULL;
    std::vector< std::vector<int> > mainCaseGridDimensions;

    // Read out main grid and main grid dimensions if present in case group
    if (gridCaseGroup->mainCase())
    {
        mainResultCase = dynamic_cast<RimEclipseResultCase*>(gridCaseGroup->mainCase());
        CVF_ASSERT(mainResultCase);

        mainResultCase->readGridDimensions(mainCaseGridDimensions);
    }

    std::vector<RimEclipseResultCase*> insertedCases;

    // Add cases to case group
    for (size_t i = 0; i < resultCases.size(); i++)
    {
        RimEclipseResultCase* rimResultReservoir = resultCases[i];

        proj->assignCaseIdToCase(rimResultReservoir);

        if (gridCaseGroup->contains(rimResultReservoir))
        {
            continue;
        }

        insertedCases.push_back(rimResultReservoir);
    }

    // Load stuff 
    for (size_t i = 0; i < insertedCases.size(); i++)
    {
        RimEclipseResultCase* rimResultReservoir = insertedCases[i];

        if (!mainResultCase)
        {
            rimResultReservoir->openEclipseGridFile();
            rimResultReservoir->readGridDimensions(mainCaseGridDimensions);

            mainResultCase = rimResultReservoir;
        }
        else
        {
            std::vector< std::vector<int> > caseGridDimensions;
            rimResultReservoir->readGridDimensions(caseGridDimensions);

            bool identicalGrid = RigGridManager::isGridDimensionsEqual(mainCaseGridDimensions, caseGridDimensions);
            if (!identicalGrid)
            {
                continue;
            }

            if (!rimResultReservoir->openAndReadActiveCellData(mainResultCase->reservoirData()))
            {
                CVF_ASSERT(false);
            }
        }

        RimOilField* activeOilField = proj ? proj->activeOilField() : NULL;
        RimEclipseCaseCollection* analysisModels = (activeOilField) ? activeOilField->analysisModels() : NULL;
        if (analysisModels) analysisModels->insertCaseInCaseGroup(gridCaseGroup, rimResultReservoir);

        caf::PdmDocument::updateUiIconStateRecursively(rimResultReservoir);

        gridCaseGroup->updateConnectedEditors();

        for (size_t rvIdx = 0; rvIdx < rimResultReservoir->reservoirViews.size(); rvIdx++)
        {
            RimEclipseView* riv = rimResultReservoir->reservoirViews()[rvIdx];
            riv->loadDataAndUpdate();
        }
    }
}