package tool.action;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;
import org.eclipse.ui.navigator.CommonNavigator;
import org.eclipse.ui.part.FileEditorInput;
import tool.ToolPlugin;
import tool.editors.ToolEditor;
import tool.editors.cdf.ClassEditor;
import tool.model.ToolClass;
import tool.model.ToolComponent;
/**
* This action is to locate and reveal the Class definition related to the
* CEX file being edited
* @author Peter
*
*/
public class OpenClassDelegate extends ActionDelegate implements IEditorActionDelegate {
private static final String SUPER_CDF_ID = "tool.editor.findSuperCDFAction";
private static final String CDF_ID = "tool.editor.findCDFAction";
protected IEditorPart currentEditorPart;
protected ISelectionService selectionService;
@Override
public void setActiveEditor(IAction action, IEditorPart editorPart) {
if (editorPart != null && editorPart instanceof ToolEditor){
this.currentEditorPart = (ToolEditor)editorPart;
this.selectionService = editorPart.getSite().getWorkbenchWindow().getSelectionService();
}
}
/**
* find the cdf for this cex and select it in the common navigator
*/
@Override
public void run(IAction action) {
IEditorInput input = this.currentEditorPart.getEditorInput();
if (input instanceof FileEditorInput){
FileEditorInput fei = (FileEditorInput)input;
IFile cex = fei.getFile();
//make the cdf file name, the is a looming issue here if the resource has cex in it
String cdfName = cex.getName().replace("cex", "cdf");
//find the containing folder
IFolder folder = (IFolder)cex.getParent();
//find the dcdf file
IFile cdf = (IFile)folder.findMember(cdfName);
ToolClass toolClass = ToolClass.fetch(cdf);
if (action.getId().equalsIgnoreCase(SUPER_CDF_ID)){
toolClass = toolClass.getToolSuperClass();
}
showInEditor(toolClass);
}
}
public static void showInEditor(ToolComponent cls){
try{
//get an handle to the Tool navigator
CommonNavigator toolNavigator = (CommonNavigator)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(ToolPlugin.TOOL_NAVIGATOR_ID);
//open it in the active page
IWorkbenchWindow window=PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = window.getActivePage();
//get the IFile for the class
IFile cdf = cls.getFile();
StructuredSelection target = null;
if (cdf == null){
// we have a library class
target = new StructuredSelection(cls);
} else {
target = new StructuredSelection(cdf);
}
//select and reveal the cdf in the navigator
// CommonViewer viewer = toolNavigator.getCommonViewer();
// viewer.expandToLevel(target, 1);
toolNavigator.selectReveal(target);
if (cdf!=null){
//now open it in the cdf editor
IEditorInput editorInput = new FileEditorInput(cdf);
//invoke the editor
page.openEditor(editorInput, ClassEditor.ID);
}
} catch (PartInitException e) {
ToolPlugin.showError("Error editing Tool class", e);
}
}
}