/*==========================================================================*\
| $Id$
|*-------------------------------------------------------------------------*|
| Copyright (C) 2006-2009 Virginia Tech
|
| This file is part of Web-CAT Eclipse Plugins.
|
| Web-CAT is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2 of the License, or
| (at your option) any later version.
|
| Web-CAT is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Web-CAT; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/
package net.sf.webcat.eclipse.cxxtest.ui;
import net.sf.webcat.eclipse.cxxtest.i18n.Messages;
import net.sf.webcat.eclipse.cxxtest.model.ICxxTestAssertion;
import net.sf.webcat.eclipse.cxxtest.model.ICxxTestStackFrame;
import net.sf.webcat.eclipse.cxxtest.model.ICxxTestSuite;
import net.sf.webcat.eclipse.cxxtest.model.ICxxTestSuiteChild;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* A JFace action that opens the text editor and positions the cursor at the
* requested CxxTest result.
*
* @author Tony Allevato (Virginia Tech Computer Science)
* @author latest changes by: $Author$
* @version $Revision$ $Date$
*/
@SuppressWarnings("restriction")
public class OpenTestAction extends Action
{
private Object test;
private TestRunnerViewPart testRunnerView;
public OpenTestAction(TestRunnerViewPart testRunnerView, Object test)
{
this.testRunnerView = testRunnerView;
this.test = test;
setText(Messages.OpenTestAction_OpenInEditorLabel);
setToolTipText(Messages.OpenTestAction_OpenInEditorTooltip);
}
public void run()
{
String filename = ""; //$NON-NLS-1$
int lineNumber = 1;
if(test instanceof ICxxTestSuite)
{
filename = ((ICxxTestSuite)test).getFile();
lineNumber = ((ICxxTestSuite)test).getLineNumber();
}
else if(test instanceof ICxxTestSuiteChild)
{
filename = ((ICxxTestSuite)
((ICxxTestSuiteChild)test).getParent()).getFile();
lineNumber = ((ICxxTestSuiteChild)test).getLineNumber();
}
else if(test instanceof ICxxTestAssertion)
{
ICxxTestSuiteChild child =
(ICxxTestSuiteChild)((ICxxTestAssertion)test).getParent();
ICxxTestSuite suite = (ICxxTestSuite)child.getParent();
filename = suite.getFile();
lineNumber = ((ICxxTestAssertion)test).getLineNumber();
}
else if(test instanceof ICxxTestStackFrame)
{
ICxxTestStackFrame sf = (ICxxTestStackFrame)test;
filename = sf.getFile();
lineNumber = sf.getLineNumber();
}
if(filename == null || filename.length() == 0)
return;
if(filename.startsWith("..")) //$NON-NLS-1$
filename = filename.substring(3);
ICProject project = testRunnerView.getLaunchedProject();
IFile file = project.getProject().getFile(filename);
try
{
ITextEditor editor =
(ITextEditor)EditorUtility.openInEditor(file, true);
try
{
IDocument document= editor.getDocumentProvider().
getDocument(editor.getEditorInput());
editor.selectAndReveal(
document.getLineOffset(lineNumber - 1),
document.getLineLength(lineNumber - 1));
}
catch(BadLocationException x) { }
}
catch (PartInitException e) { }
catch (CModelException e) { }
}
}