/* * Copyright 2000-2020 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.teamcity.ui.actions; import jetbrains.teamcity.core.shelve.IShelvedChangeSet; import jetbrains.teamcity.core.shelve.IShelvedContainer; import jetbrains.teamcity.core.shelve.IShelvedResource; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.IActionDelegate; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; public abstract class ShelveAbstractAction extends Action implements IActionDelegate { private IStructuredSelection mySelection; public ShelveAbstractAction() { super(); } protected IStructuredSelection getSelection() { return mySelection; } public void run(IAction action) { run(); } @NotNull protected Map> createActionScope() { final HashMap> map = new HashMap>(); for (Object obj : mySelection.toArray()) { if (obj instanceof IShelvedResource) { final IShelvedResource resource = (IShelvedResource) obj; switch (resource.getType()) { case FILE: { getContainer(map, resource.getChangeSet()).add(resource); break; } case FOLDER: case PROJECT: { getContainer(map, resource.getChangeSet()).addAll(((IShelvedContainer) resource).members()); break; } } } else if (obj instanceof IShelvedChangeSet) { getContainer(map, (IShelvedChangeSet) obj).addAll(((IShelvedContainer) obj).members()); } } return map; } @NotNull private Collection getContainer(Map> map, IShelvedChangeSet changeSet) { Collection container = map.get(changeSet); if (container == null) { container = new HashSet(); map.put(changeSet, container); } return container; } public void setSelection(IStructuredSelection selection) { this.mySelection = selection; } public void selectionChanged(IAction action, ISelection selection) { mySelection = (IStructuredSelection) selection; } }