/* * 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.lang.reflect.InvocationTargetException; import java.text.MessageFormat; import jetbrains.teamcity.Activator; import jetbrains.teamcity.SharedImages; import jetbrains.teamcity.core.Util; import jetbrains.teamcity.core.test.IStackTraceElement; import jetbrains.teamcity.ui.InformationalDialog; import jetbrains.teamcity.ui.UIUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.action.Action; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.IDocument; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.texteditor.ITextEditor; /** * Abstract Action for opening a Java editor. */ public class OpenTestAction extends Action { private static final String GO_TO_FILE = Messages.getString("OpenTestAction.name"); //$NON-NLS-1$ protected String myClassName; private final boolean myIsActivate; private final Shell myShell; private final int myLineNumber; private String myTestName; private final IStackTraceElement myStackTraceElement; public OpenTestAction(Shell shell, String testName, boolean activate) { super(GO_TO_FILE); setImageDescriptor(SharedImages.getImageDescriptorByPath(SharedImages.OPEN_LOG)); myShell = shell; myTestName = testName; myIsActivate = activate; myLineNumber = -1; myStackTraceElement = null; } public OpenTestAction(final Shell shell, final IStackTraceElement element, boolean activate) { super(GO_TO_FILE); setImageDescriptor(SharedImages.getImageDescriptorByPath(SharedImages.OPEN_LOG)); myShell = shell; myClassName = element.getClassName(); myIsActivate = activate; myLineNumber = element.getLineNumber(); myStackTraceElement = element; } /* * @see IAction#run() */ @Override public void run() { if (myStackTraceElement != null && IStackTraceElement.Type.JUNIT != myStackTraceElement.getType()) { openElement(myStackTraceElement); } else { try { final jetbrains.teamcity.ui.actions.OpenJavaTestAction javaDelegate; if (myTestName != null) { javaDelegate = new jetbrains.teamcity.ui.actions.OpenJavaTestAction(myShell, myTestName, myIsActivate); } else { javaDelegate = new jetbrains.teamcity.ui.actions.OpenJavaTestAction(myShell, myStackTraceElement, myIsActivate); } javaDelegate.run(); } catch (Throwable e) { // JDT cannot be found } } } private void openElement(final IStackTraceElement stackTraceElement) { if (stackTraceElement != null) { try { PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { final IFile[] files = Util.findFiles(new String[] { stackTraceElement.getFileName() }, monitor); if (monitor.isCanceled()) { // break action return; } if (files.length > 0) { UIUtil.openFiles(files, stackTraceElement.getLineNumber(), monitor); } else { showNotFound(stackTraceElement.getFileName()); } } catch (CoreException e) { UIUtil.execUI(new Runnable() { public void run() { InformationalDialog.openError(UIUtil.getWorkbenchShell(), Messages.getString("OpenTestAction.open_file_error.dialog.title"), null, Util .createStatus(IStatus.ERROR, MessageFormat.format(Messages.getString("OpenTestAction.open_file_error.dialog.message"), stackTraceElement.getFileName())), null); } }, true); } } }); } catch (Exception e) { Activator.getDefault().getLog().log(Util.createStatus(e)); } } } static void showNotFound(final String elementName) { Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog .openError( null, Messages.getString("OpenTestAction.could_not_find.dialog.title"), MessageFormat.format(Messages.getString("OpenTestAction.could_not_find.dialog.message"), elementName)); //$NON-NLS-1$ //$NON-NLS-2$ } }); } protected Shell getShell() { return myShell; } protected String getClassName() { return myClassName; } protected void reveal(ITextEditor textEditor) { if (textEditor != null) { if (myLineNumber >= 0) { try { IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); textEditor.selectAndReveal(document.getLineOffset(myLineNumber - 1), document.getLineLength(myLineNumber - 1)); } catch (BadLocationException x) { // marker refers to invalid text position -> do nothing } } } } }