/* * 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.Collection; import java.util.HashSet; import java.util.Map; import jetbrains.buildServer.BuildType; import jetbrains.teamcity.Activator; import jetbrains.teamcity.Constants; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.core.Util; import jetbrains.teamcity.core.remote.RemoteRunCustomData; import jetbrains.teamcity.core.remote.RemoteRunPreferencesHelper; import jetbrains.teamcity.ui.views.model.ProjectTreeBuildTypeNode; import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.Platform; import org.eclipse.jface.action.Action; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.internal.ide.misc.OverlayIcon; public class CleanCustomizationAction extends Action implements ISelectionChangedListener { private final Map myHolder; private final HashSet myElements = new HashSet(); public CleanCustomizationAction(final Shell parentShell, final Map holder) { super(Messages.getString("CleanCustomizationAction.name"), //$NON-NLS-1$ new OverlayIcon(SharedImages.getImageDescriptorByPath(SharedImages.REMOTE_RUN_ACTION_CUSTOMIZE), new ImageDescriptor[][] { {}, { SharedImages.getImageDescriptorByPath(SharedImages.REMOTE_RUN_ACTION_CUSTOMIZE_CLEAN_OVERLAY) } }, new Point(16, 16)) // new OverlayImage( // SharedImages // .getImageDescriptorByPath(SharedImages.REMOTE_RUN_ACTION_CUSTOMIZE), // SharedImages // .getImageDescriptorByPath(SharedImages.REMOTE_RUN_ACTION_CUSTOMIZE_CLEAN_OVERLAY), // IDecoration.BOTTOM_RIGHT) ); Assert.isNotNull(holder); myHolder = holder; setEnabled(false); } @Override public void run() { for (final BuildType element : elements()) { myHolder.remove(element.getBuildTypeId()); } notifyChanged(); save(); } public void cleanAll() { myHolder.clear(); notifyChanged(); save(); } private void save() { try { new RemoteRunPreferencesHelper(Platform.getPreferencesService()).save(myHolder); } catch (Throwable e) { Activator.getDefault().getLog().log(Util.createStatus(e)); } } private void notifyChanged() { Activator.broadcastPropertyChange(new PropertyChangeEvent(this, Constants.Notification.REMOTE_RUN_CUSTOM_DATA_CHANGED, null, myHolder)); notifyResult(true); } public Collection elements() { return myElements; } public void selectionChanged(SelectionChangedEvent event) { setEnabled(false); myElements.clear(); final IStructuredSelection selection = ((IStructuredSelection) event.getSelection()); final HashSet buffer = new HashSet(); for (final Object selected : selection.toList()) { if ((selected instanceof ProjectTreeBuildTypeNode && myHolder.containsKey(((ProjectTreeBuildTypeNode) selected).getID()))) { buffer.add(((ProjectTreeBuildTypeNode) selected).getBuildType()); } } myElements.addAll(buffer); setEnabled(!myElements.isEmpty()); } }