/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ package org.rubypeople.rdt.internal.testunit.ui; import org.eclipse.core.runtime.IStatus; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.ui.texteditor.ITextEditor; import org.rubypeople.rdt.core.IMethod; import org.rubypeople.rdt.core.IRubyElement; import org.rubypeople.rdt.core.IRubyProject; import org.rubypeople.rdt.core.ISourceRange; import org.rubypeople.rdt.core.IType; import org.rubypeople.rdt.core.ITypeHierarchy; import org.rubypeople.rdt.core.RubyConventions; import org.rubypeople.rdt.core.RubyModelException; import org.rubypeople.rdt.internal.core.util.Messages; /** * Open a class on a Test method. */ public class OpenTestAction extends OpenEditorAction { private String fMethodName; private ISourceRange fRange; public OpenTestAction(TestUnitView testRunner, String className, String method) { this(testRunner, className, method, true); } public OpenTestAction(TestUnitView testRunner, String className) { this(testRunner, className, null); } public OpenTestAction(TestUnitView testRunner, String className, String method, boolean activate) { super(testRunner, className, activate); // PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ITestUnitHelpContextIds.OPENTEST_ACTION); fMethodName= method; } protected IRubyElement findElement(IRubyProject project, String className) throws RubyModelException { IType type= findType(project, className); if (type == null) return null; if (fMethodName == null) return type; IMethod method= findMethod(type); if (method == null) { ITypeHierarchy typeHierarchy= type.newSupertypeHierarchy(null); IType[] types= typeHierarchy.getAllSuperclasses(type); for (int i= 0; i < types.length; i++) { method= findMethod(types[i]); if (method != null) break; } } if (method == null) { String title= TestUnitMessages.OpenTestAction_error_title; String message= Messages.format(TestUnitMessages.OpenTestAction_error_methodNoFound, fMethodName); MessageDialog.openInformation(getShell(), title, message); return type; } fRange= method.getNameRange(); return method; } IMethod findMethod(IType type) { IStatus status= RubyConventions.validateMethodName(fMethodName); if (! status.isOK()) return null; IMethod method= type.getMethod(fMethodName, new String[0]); if (method != null && method.exists()) return method; return null; } protected void reveal(ITextEditor textEditor) { if (fRange != null) textEditor.selectAndReveal(fRange.getOffset(), fRange.getLength()); } public boolean isEnabled() { try { return findType(getLaunchedProject(), getClassName()) != null; } catch (RubyModelException e) { } return false; } }