/* * 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 jetbrains.teamcity.Activator; import jetbrains.teamcity.core.JdtUtil; import jetbrains.teamcity.ui.UIUtil; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.ui.JavaUI; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IAction; import org.eclipse.ui.PartInitException; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collection; public class OpenJavaTypeAction extends Action { private static final String GO_TO_FILE = Messages.getString("OpenTestAction.name"); //$NON-NLS-1$ private final String myClassName; private final boolean myIsActivate; public OpenJavaTypeAction(String className, boolean activate) { super(GO_TO_FILE); myClassName = className; myIsActivate = activate; } private void openJavaElement() { try { final Collection elements = JdtUtil.findClass(myClassName); if (elements.isEmpty()) { OpenTestAction.showNotFound(myClassName); } else if (elements.size() == 1) { openInEditor(elements.iterator().next()); } else { // found multiple final ArrayList actions = new ArrayList(); for (final IJavaElement element : elements) { Action action = new Action(JdtUtil.getElementName(element)) { @Override public void run() { try { openInEditor(element); } catch (CoreException e) { Activator.getDefault().getLog().log(e.getStatus()); } } }; actions.add(action); } UIUtil.showChoice(actions); } } catch (CoreException e) { Activator.getDefault().getLog().log(e.getStatus()); } } private void openInEditor(@NotNull final IJavaElement element) throws JavaModelException, PartInitException { JavaUI.openInEditor(element, myIsActivate, true); } @Override public void run() { openJavaElement(); } }