/* * 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.WeakHashMap; import jetbrains.buildServer.TeamCityState; import jetbrains.teamcity.Activator; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.core.Util; import jetbrains.teamcity.core.jobs.RemoteRunPreparationJob; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.mapping.ResourceMapping; import org.eclipse.core.runtime.jobs.IJobChangeEvent; import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.custom.BusyIndicator; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IViewActionDelegate; import org.eclipse.ui.IViewPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.progress.IProgressService; import org.jetbrains.annotations.Nullable; public class RemoteRunActionDelegate extends Action implements IActionDelegate, IViewActionDelegate { private static final String ACTTION_ID = "jetbrains.teamcity.core.remoterun.acttion"; //$NON-NLS-1$ private static final WeakHashMap ourRegistry = new WeakHashMap(); private ISelection mySelection; private IAction myAction; private boolean mySkipSynchronization = false; public static final String REMOTE_RUN_ACTION_IMG = SharedImages.REMOTE_RUN_ACTION_IMG; public static final String REMOTE_RUN_ACTION_NAME = Messages.getString("RemoteRunAction.action.name"); //$NON-NLS-1$ public RemoteRunActionDelegate() { super(REMOTE_RUN_ACTION_NAME, SharedImages.getImageDescriptorByPath(REMOTE_RUN_ACTION_IMG)); register(this); } RemoteRunActionDelegate(/*final IStructuredSelection selection, */boolean skipSynchronization) { super(); // setSelection(selection); mySkipSynchronization = skipSynchronization; } private static synchronized void register(final RemoteRunActionDelegate delegate) { ourRegistry.put(delegate, delegate); } private void setEnableSuper(boolean enabled) { if (myAction != null) { myAction.setEnabled(enabled); } super.setEnabled(enabled); } @Override public synchronized void setEnabled(boolean enabled) { for (final RemoteRunActionDelegate instance : ourRegistry.keySet()) { instance.setEnableSuper(enabled); } } public RemoteRunActionDelegate(String text, ImageDescriptor image) { this(); setText(text); setImageDescriptor(image); } public synchronized void setSelection(@Nullable final ISelection selection) { mySelection = selection; } public void selectionChanged(final IAction action, final ISelection selection) { setSelection(selection); } public void run(final IAction action) { myAction = action; run(); } @Override public void runWithEvent(Event event) { run(); } @Override public String getId() { return ACTTION_ID; } @Override public void run() { RemoteRunActionDelegate.this.setEnabled(false); PlatformUI.getWorkbench().saveAllEditors(true); //create an authorization wrapper to enforce login if required final Action innerAction = new Action() { @Override public void run() { BusyIndicator.showWhile(Display.getDefault(), new Runnable() { public void run() { final ResourceMapping[] mappings = Util.collectMappings((IStructuredSelection) mySelection, IResource.DEPTH_INFINITE); final RemoteRunPreparationJob launcher = new RemoteRunPreparationJob(mappings, mySkipSynchronization); final IProgressService progressService = PlatformUI.getWorkbench().getProgressService(); progressService.showInDialog(null, launcher); launcher.addJobChangeListener(new JobChangeAdapter() { @Override public void done(IJobChangeEvent event) { RemoteRunActionDelegate.this.setEnabled(true); } }); launcher.schedule(); } }); } }; final TeamCityState state = Activator.getDefault().getSnapshot().getState(); if (TeamCityState.LOGGING_IN != state && TeamCityState.LOGGED_IN != state) { new AuthorizationActionWarpper(innerAction).run(); } else { innerAction.run(); } // final AuthorizationActionWarpper wrapper = new AuthorizationActionWarpper(innerAction) { // // @Override // // protected void loginCancelled() { // // //do not forget enable action even login canceled // // RemoteRunActionDelegate.this.setEnabled(true); // // } // // }; // wrapper.run(); } public void init(IViewPart view) { } }