/* * 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 java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.ui.IStorageComparable; import jetbrains.teamcity.ui.StorageCompareInput; import org.eclipse.compare.CompareEditorInput; import org.eclipse.compare.CompareUI; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.IActionDelegate; import org.jetbrains.annotations.NotNull; public class CompareContentAction extends Action implements IActionDelegate, ISelectionChangedListener { private final ArrayList myComparables = new ArrayList(); CompareContentAction() { super(Messages.getString("CompareContentAction.compare_patch_content_with_workspace_resource_action_name"), SharedImages.getImageDescriptorByPath(SharedImages.COMPARE)); //$NON-NLS-1$ } public CompareContentAction(final @NotNull IStorageComparable... comparables) { this(); myComparables.addAll(Arrays.asList(comparables)); } public CompareContentAction(final @NotNull Image image, final @NotNull String contentId, final @NotNull String leftContentId, final @NotNull String rightContentId, final @NotNull IStorage left, final @NotNull IStorage right) { this(); myComparables.add(new IStorageComparable() { public String getTitle() { return contentId; } public String getRightStorageTitle() { return rightContentId; } public IStorage getRightStorage() { return right; } public String getLeftStorageTitle() { return leftContentId; } public IStorage getLeftStorage() { return left; } public Image getImage() { return image; } }); } @Override public void run() { for (IStorageComparable comparable : myComparables) { final CompareEditorInput input = StorageCompareInput.createInput(comparable.getImage(), comparable.getTitle(), comparable.getLeftStorageTitle(), comparable.getRightStorageTitle(), comparable.getLeftStorage(), comparable.getRightStorage()); CompareUI.openCompareEditorOnPage(input, null); } } public void run(IAction action) { run(); } public void selectionChanged(IAction action, ISelection selection) { final HashSet comparables = new HashSet(); for (Object seleceted : ((IStructuredSelection) selection).toArray()) { if (seleceted instanceof IStorageComparable) { comparables.add((IStorageComparable) seleceted); } else if (seleceted instanceof IAdaptable) { final IStorageComparable adapter = (IStorageComparable) ((IAdaptable) seleceted).getAdapter(IStorageComparable.class); if (adapter != null) { comparables.add(adapter); } } else { final IStorageComparable adapter = (IStorageComparable) Platform.getAdapterManager().getAdapter(seleceted, IStorageComparable.class); if (adapter != null) { comparables.add(adapter); } } } myComparables.clear(); myComparables.addAll(comparables); checkEnablement(); } private void checkEnablement() { setEnabled(!myComparables.isEmpty()); } public void selectionChanged(SelectionChangedEvent event) { selectionChanged(this, event.getSelection()); } }