/* * 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.text.MessageFormat; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.core.Util; import org.eclipse.core.resources.IWorkspaceRunnable; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jface.action.Action; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.team.core.TeamException; import org.eclipse.team.core.mapping.ISynchronizationContext; import org.eclipse.team.ui.synchronize.*; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.progress.IProgressService; import org.eclipse.ui.progress.IWorkbenchSiteProgressService; public class RefreshParticipantAction extends Action { private final ISynchronizePageConfiguration myConfiguration; public RefreshParticipantAction(final ISynchronizePageConfiguration configuration) { super(Messages.getString("RefreshParticipantAction.name"), SharedImages.getImageDescriptorByPath(SharedImages.SYNC_IMG)); //$NON-NLS-1$ myConfiguration = configuration; myConfiguration.getPage().getViewer().getControl().addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { if (event.keyCode == SWT.F5) { run(); } }}); setAccelerator(SWT.F5); setToolTipText(MessageFormat.format(Messages.getString("RefreshParticipantAction.tooltip.pattern"), Messages.getString("RefreshParticipantAction.name"))); //$NON-NLS-1$ //$NON-NLS-2$ } @Override public void run() { IProgressService service = (IProgressService) myConfiguration.getSite().getWorkbenchSite().getService(IWorkbenchSiteProgressService.class); if (service == null) { service = PlatformUI.getWorkbench().getProgressService(); } final ISynchronizeParticipant participant = myConfiguration.getParticipant(); if (participant != null) { final Job job = new Job(getText()) { @Override protected IStatus run(final IProgressMonitor monitor) { try { ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { public void run(IProgressMonitor monitor) throws CoreException { if(participant instanceof SubscriberParticipant){ final IStatus status = ((SubscriberParticipant)participant).refreshNow(null, Messages.getString("RefreshParticipantAction.task.name"), monitor); //$NON-NLS-1$; if(status.matches(Status.ERROR)){ throw new TeamException(status); } } else if (participant instanceof ModelSynchronizeParticipant){ final ISynchronizationContext synchronizationContext = ((ModelSynchronizeParticipant)participant).getContext(); synchronizationContext.refresh(synchronizationContext.getScope().getMappings(), monitor); } } }, monitor); } catch (CoreException e) { return Util.createStatus(e); } return Status.OK_STATUS; } @Override public boolean belongsTo(Object family) { return ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION.equals(family) || super.belongsTo(family); } }; service.showInDialog(null, job); job.schedule(); } } }