// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.execution.services;

import com.intellij.execution.services.ServiceModel.ServiceViewItem;
import com.intellij.execution.services.ServiceViewDragHelper.ServiceViewDragBean;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

import static com.intellij.execution.services.ServiceViewActionProvider.getSelectedView;

public class SplitToNewTabsAction extends DumbAwareAction {
  @Override
  public void update(@NotNull AnActionEvent e) {
    ServiceView serviceView = getSelectedView(e);
    List<ServiceViewItem> items = serviceView == null ? Collections.emptyList() : serviceView.getSelectedItems();
    boolean enabled = !items.isEmpty() && items.stream().allMatch(item -> item instanceof ServiceModel.ServiceNode);
    e.getPresentation().setEnabled(enabled);
    e.getPresentation().setVisible(enabled || !ActionPlaces.isPopupPlace(e.getPlace()));
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    Project project = e.getProject();
    if (project == null) return;

    ServiceView serviceView = getSelectedView(e);
    if (serviceView == null) return;

    ServiceViewManagerImpl manager = (ServiceViewManagerImpl)ServiceViewManager.getInstance(project);
    for (ServiceViewItem item : serviceView.getSelectedItems()) {
      manager.extract(new ServiceViewDragBean(serviceView, Collections.singletonList(item)));
    }
  }
}

@@ -612,31 +612,8 @@
 ons;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -879,35 +879,23 @@
     
-List%3CServiceViewItem%3E items
+boolean enabled
  = s
@@ -909,44 +909,20 @@
 iew 
-=
+!
 = null 
-? Collections.emptyList() : 
+&& !
 serv
@@ -951,124 +951,17 @@
 ms()
-;%0A    boolean enabled = !items.isEmpty() && items.stream().allMatch(item -%3E item instanceof ServiceModel.ServiceNode
+.isEmpty(
 );%0A 

allow to split groups to new tabs