void PerspectivesPreferencePage::RevertPerspective()
{
  PerspectiveDescriptor::Pointer desc = GetSelectedPerspective();
  if (desc.IsNotNull() && !perspToRevert.contains(desc))
  {
    perspToRevert.push_back(desc);
  }
  UpdateButtons();
}
void PerspectivesPreferencePage::MakeDefaultPerspective()
{
  PerspectiveDescriptor::Pointer desc = GetSelectedPerspective();
  if (desc.IsNotNull() && !perspToDelete.contains(desc))
  {
    int row = perspectives.indexOf(desc);
    defaultPerspectiveId = desc->GetId();
    UpdatePerspectivesTable();
    ui->perspectivesListWidget->item(row)->setSelected(true);
  }
  UpdateButtons();
}
void PerspectivesPreferencePage::DeletePerspective()
{
  PerspectiveDescriptor::Pointer desc = GetSelectedPerspective();
  if (desc.IsNotNull() && !perspToDelete.contains(desc))
  {
    if (!FindOpenInstance(desc))
    {
      perspToDelete.push_back(desc);
      perspToRevert.removeAll(desc);
      perspectives.removeAll(desc);
      UpdatePerspectivesTable();
    }
  }
  UpdateButtons();
}
void SavePerspectiveHandler::SaveNonSingleton(const IWorkbenchPage::Pointer& page,
                                              const PerspectiveDescriptor::Pointer& oldDesc) {
  // Get reg.
  PerspectiveRegistry* reg = static_cast<PerspectiveRegistry*>(
                               berry::WorkbenchPlugin::GetDefault()->GetPerspectiveRegistry());
  assert(reg != nullptr);

  // Get persp name.
  SavePerspectiveDialog dlg(*reg, page->GetWorkbenchWindow()->GetShell()->GetControl());
  // Look up the descriptor by id again to ensure it is still valid.
  IPerspectiveDescriptor::Pointer description = reg->FindPerspectiveWithId(oldDesc->GetId());
  dlg.SetInitialSelection(description);
  if (dlg.exec() != QDialog::Accepted)
  {
    return;
  }

  // Create descriptor.
  IPerspectiveDescriptor::Pointer newDesc = dlg.GetPersp();
  if (newDesc.IsNull())
  {
    QString name = dlg.GetPerspName();
    newDesc = reg->CreatePerspective(name, description);
    if (newDesc.IsNull())
    {
      QMessageBox::critical(&dlg,
                            "Cannot Save Perspective",
                            "Invalid Perspective Descriptor");
      return;
    }
  }

  // Save state.
  page->SavePerspectiveAs(newDesc);
}
void PerspectivesPreferencePage::UpdateButtons()
{
  PerspectiveDescriptor::Pointer desc = GetSelectedPerspective();
  if (desc)
  {
    ui->revertButton->setEnabled(desc->IsPredefined() && desc->HasCustomDefinition() && !perspToRevert.contains(desc));
    ui->deleteButton->setEnabled(!desc->IsPredefined());
    ui->makeDefaultButton->setEnabled(true);
  }
  else
  {
    ui->revertButton->setEnabled(false);
    ui->deleteButton->setEnabled(false);
    ui->makeDefaultButton->setEnabled(false);
  }
}
bool PerspectiveComparator(const PerspectiveDescriptor::Pointer& p1, const PerspectiveDescriptor::Pointer& p2)
{
  return p1->GetLabel() < p2->GetLabel();
}
bool PerspectivesPreferencePage::FindOpenInstance(const PerspectiveDescriptor::Pointer& desc)
{
  QList<IWorkbenchWindow::Pointer> windows = workbench->GetWorkbenchWindows();

  //find all active perspectives currently
  for (int i = 0; i < windows.size(); i++)
  {
    QList<IWorkbenchPage::Pointer> pages = windows[i]->GetPages();
    for (int j = 0; j < pages.size(); j++)
    {
      WorkbenchPage::Pointer page = pages[j].Cast<WorkbenchPage>();
      if (page->FindPerspective(desc).IsNotNull())
      {
        QMessageBox::StandardButton returnCode =
            QMessageBox::question(workbench->GetActiveWorkbenchWindow()->GetShell()->GetControl(),
                                  "Delete Perspective",
                                  QString("Are you sure you want to delete the \"%1\" perspective? It has open instances.").arg(desc->GetLabel()),
                                  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
        return (returnCode != QMessageBox::Yes);
      }
    }
  }

  return false;
}