ISelection::ConstPointer PagePartSelectionTracker::GetSelection()
{
    IWorkbenchPart::Pointer part = this->GetPart();
    if (part.IsNotNull())
    {
        ISelectionProvider::Pointer sp = part->GetSite()->GetSelectionProvider();
        if (sp.IsNotNull())
        {
            return sp->GetSelection();
        }
    }
    return ISelection::Pointer(nullptr);
}
void PagePartSelectionTracker::SetPart(IWorkbenchPart::Pointer part,
                                       bool notify)
{
    if (fPart.IsNotNull())
    {
        // remove myself as a listener from the existing part
        ISelectionProvider::Pointer sp = fPart->GetSite()->GetSelectionProvider();
        if (sp.IsNotNull())
        {
            sp->RemoveSelectionChangedListener(selChangedListener.data());
            if (sp.Cast<IPostSelectionProvider> ().IsNotNull())
            {
                sp.Cast<IPostSelectionProvider> () ->RemovePostSelectionChangedListener(
                    postSelectionListener.data());
            }
            else
            {
                sp->RemoveSelectionChangedListener(postSelectionListener.data());
            }
        }
    }
    fPart = part;
    ISelection::ConstPointer sel;
    if (part.IsNotNull())
    {
        ISelectionProvider::Pointer sp = part->GetSite()->GetSelectionProvider();
        if (sp.IsNotNull())
        {
            sp->AddSelectionChangedListener(selChangedListener.data());
            if (sp.Cast<IPostSelectionProvider> ().IsNotNull())
            {
                sp.Cast<IPostSelectionProvider> () ->AddPostSelectionChangedListener(
                    postSelectionListener.data());
            }
            else
            {
                sp->AddSelectionChangedListener(postSelectionListener.data());
            }
            if (notify)
            {
                // get the selection to send below
                sel = sp->GetSelection();
            }
        }
    }
    if (notify)
    {
        this->FireSelection(part, sel);
        this->FirePostSelection(part, sel);
    }
}
SelectionChangedEvent::SelectionChangedEvent(
    ISelectionProvider::Pointer source, ISelection::ConstPointer selection)
{
  poco_assert(source.IsNotNull());
  poco_assert(selection.IsNotNull());

  this->source = source;
  this->selection = selection;
}
void AbstractSelectionService::SetActivePart(IWorkbenchPart::Pointer newPart)
{
  // Optimize.
  if (newPart == activePart)
  {
    return;
  }

  ISelectionProvider::Pointer selectionProvider;

  if (newPart.IsNotNull())
  {
    selectionProvider = newPart->GetSite()->GetSelectionProvider();

    if (selectionProvider.IsNull())
    {
      newPart = nullptr;
    }
  }

  if (newPart == activePart)
  {
    return;
  }

  if (activePart.IsNotNull())
  {
    if (activeProvider.IsNotNull())
    {
      activeProvider->RemoveSelectionChangedListener(selListener.data());
      if (activeProvider.Cast<IPostSelectionProvider>().IsNotNull())
      {
        activeProvider.Cast<IPostSelectionProvider>()
        ->RemovePostSelectionChangedListener(postSelListener.data());
      }
      else
      {
        activeProvider
        ->RemoveSelectionChangedListener(postSelListener.data());
      }
      activeProvider = nullptr;
    }
    activePart = nullptr;
  }

  activePart = newPart;

  if (newPart.IsNotNull())
  {
    activeProvider = selectionProvider;
    // Fire an event if there's an active provider
    activeProvider->AddSelectionChangedListener(selListener.data());
    ISelection::ConstPointer sel = activeProvider->GetSelection();
    this->FireSelection(newPart, sel);
    if (activeProvider.Cast<IPostSelectionProvider>().IsNotNull())
    {
      activeProvider.Cast<IPostSelectionProvider>()
      ->AddPostSelectionChangedListener(postSelListener.data());
    }
    else
    {
      activeProvider->AddSelectionChangedListener(postSelListener.data());
    }
    this->FirePostSelection(newPart, sel);
  }
  else
  {
    this->FireSelection(IWorkbenchPart::Pointer(nullptr), ISelection::ConstPointer(nullptr));
    this->FirePostSelection(IWorkbenchPart::Pointer(nullptr), ISelection::ConstPointer(nullptr));
  }
}