//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicPasteWellLogCurveFeature::onActionTriggered(bool isChecked)
{
    caf::PdmObjectHandle* destinationObject = dynamic_cast<caf::PdmObjectHandle*>(caf::SelectionManager::instance()->selectedItem());

    RimWellLogTrack* wellLogTrack = nullptr;
    destinationObject->firstAncestorOrThisOfType(wellLogTrack);
    if (!wellLogTrack)
    {
        return;
    }

    std::vector<caf::PdmPointer<RimWellLogCurve> > sourceObjects = RicPasteWellLogCurveFeature::curves();

    for (size_t i = 0; i < sourceObjects.size(); i++)
    {
        RimWellLogFileCurve* fileCurve = dynamic_cast<RimWellLogFileCurve*>(sourceObjects[i].p());
        if (fileCurve)
        {
            RimWellLogFileCurve* newObject = dynamic_cast<RimWellLogFileCurve*>(sourceObjects[i]->xmlCapability()->copyByXmlSerialization(caf::PdmDefaultObjectFactory::instance()));
            CVF_ASSERT(newObject);

            wellLogTrack->addCurve(newObject);

            // Resolve references after object has been inserted into the project data model
            newObject->resolveReferencesRecursively();
            newObject->initAfterReadRecursively();

            newObject->loadDataAndUpdate();

            wellLogTrack->updateConnectedEditors();
        }

        RimWellLogExtractionCurve* extractionCurve = dynamic_cast<RimWellLogExtractionCurve*>(sourceObjects[i].p());
        if (extractionCurve)
        {
            RimWellLogExtractionCurve* newObject = dynamic_cast<RimWellLogExtractionCurve*>(sourceObjects[i]->xmlCapability()->copyByXmlSerialization(caf::PdmDefaultObjectFactory::instance()));
            CVF_ASSERT(newObject);

            wellLogTrack->addCurve(newObject);

            // Resolve references after object has been inserted into the project data model
            newObject->resolveReferencesRecursively();
            newObject->initAfterReadRecursively();

            newObject->loadDataAndUpdate();

            wellLogTrack->updateConnectedEditors();
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicNewWellLogFileCurveFeature::addWellLogChannelsToPlotTrack(RimWellLogTrack* plotTrack, const std::vector<RimWellLogFileChannel*>& wellLogFileChannels)
{
    for (size_t cIdx = 0; cIdx < wellLogFileChannels.size(); cIdx++)
    {
        RimWellLogFileCurve* plotCurve = addCurve(plotTrack);
    
        RimWellPath* wellPath;
        wellLogFileChannels[cIdx]->firstAncestorOrThisOfType(wellPath);
        if (wellPath)
        {
            plotCurve->setWellPath(wellPath);
            plotCurve->setWellLogChannelName(wellLogFileChannels[cIdx]->name());
            plotCurve->loadDataAndUpdate();
            plotCurve->updateConnectedEditors();
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
RimWellLogFileCurve* RicNewWellLogFileCurveFeature::addCurve(RimWellLogTrack* plotTrack)
{
    CVF_ASSERT(plotTrack);

    RimWellLogFileCurve* curve = new RimWellLogFileCurve();
    plotTrack->addCurve(curve);

    cvf::Color3f curveColor = RicWellLogPlotCurveFeatureImpl::curveColorFromTable();
    curve->setColor(curveColor);

    plotTrack->updateConnectedEditors();

    RiuMainPlotWindow* plotwindow = RiaApplication::instance()->getOrCreateAndShowMainPlotWindow();
    plotwindow->selectAsCurrentItem(curve);

    return curve;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicNewWellLogFileCurveFeature::onActionTriggered(bool isChecked)
{
    RimWellLogTrack* wellLogPlotTrack = selectedWellLogPlotTrack();
    if (wellLogPlotTrack)
    {
        addCurve(wellLogPlotTrack);
    }
    else
    {
        RimWellPath* wellPath = selectedWellPathWithLogFile();
        if (wellPath)
        {
            RimWellLogTrack* wellLogPlotTrack = RicNewWellLogPlotFeatureImpl::createWellLogPlotTrack();
            RimWellLogFileCurve* plotCurve = addCurve(wellLogPlotTrack);
            plotCurve->setWellPath(wellPath);
            plotCurve->updateConnectedEditors();
        }
    }
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RicNewWellLogFileCurveFeature::onActionTriggered(bool isChecked)
{
    RimWellLogTrack* wellLogPlotTrack = caf::SelectionManager::instance()->selectedItemAncestorOfType<RimWellLogTrack>();
    if (wellLogPlotTrack)
    {
        RicWellLogTools::addFileCurve(wellLogPlotTrack);
    }
    else
    {
        RimWellPath* wellPath = RicWellLogTools::selectedWellPathWithLogFile();
        if (wellPath)
        {
            RimWellLogTrack* newWellLogPlotTrack = RicNewWellLogPlotFeatureImpl::createWellLogPlotTrack();
            RimWellLogFileCurve* plotCurve = RicWellLogTools::addFileCurve(newWellLogPlotTrack);
            plotCurve->setWellPath(wellPath);

            if (wellPath->wellLogFiles().size() == 1)
            {
                plotCurve->setWellLogFile(wellPath->wellLogFiles().front());
            }
            plotCurve->updateConnectedEditors();
        }
    }
}