blob: e65fa06034166a467dea2696ed723ac4733c4eb5 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2012 SAP AG.
* 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:
* Georgi Konstantinov - initial API and implementation.
*******************************************************************************/
package org.eclipse.wst.sse.sieditor.search.ui.actions;
import java.io.File;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.wst.sse.sieditor.model.api.IModelObject;
import org.eclipse.wst.sse.sieditor.model.wsdl.api.IDescription;
import org.eclipse.wst.sse.sieditor.search.i18n.Messages;
import org.eclipse.wst.sse.sieditor.search.ui.tree.FileNode;
import org.eclipse.wst.sse.sieditor.ui.DataTypesEditor;
import org.eclipse.wst.sse.sieditor.ui.ServiceInterfaceEditor;
import org.eclipse.wst.sse.sieditor.ui.v2.nodes.ITreeNode;
public class OpenInEditorAction extends AbstractActiveOnSingleSelectionAction {
public OpenInEditorAction(ISelection selection) {
super(selection);
}
@Override
public void run() {
final Object firstElementFromSelection = getStructuredSelectionFirstElement();
if (!(firstElementFromSelection instanceof ITreeNode)) {
return;
}
ITreeNode selectedTreeNode = (ITreeNode) firstElementFromSelection;
IFile theFile = getFileFromTreeNode(selectedTreeNode);
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
FileEditorInput eInput = new FileEditorInput(theFile);
String editorID = getEditorId(selectedTreeNode);
IWorkbenchPage workbenchActivePage = window.getActivePage();
try {
workbenchActivePage.openEditor(eInput, editorID);
} catch (PartInitException e) {
throw new IllegalStateException("Cannot create input for editor", e); //$NON-NLS-1$
}
}
protected String getEditorId(ITreeNode selectedTreeNode) {
boolean isWsdlFile = false;
if (selectedTreeNode instanceof FileNode) {
FileNode fileNode = (FileNode) selectedTreeNode;
isWsdlFile = !fileNode.isXsdFile();
} else {
IModelObject root = selectedTreeNode.getModelObject().getRoot();
isWsdlFile = root instanceof IDescription;
}
return isWsdlFile ? ServiceInterfaceEditor.EDITOR_ID : DataTypesEditor.EDITOR_ID;
}
protected IFile getFileFromTreeNode(ITreeNode selectedTreeNode) {
if (selectedTreeNode instanceof FileNode) {
FileNode fileNode = (FileNode) selectedTreeNode;
return fileNode.getFile();
}
IModelObject modelObject = selectedTreeNode.getModelObject();
String fileString = modelObject.getComponent().eResource().getURI().toFileString();
File file = new File(fileString);
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IFile[] resources = workspaceRoot.findFilesForLocationURI(file.getAbsoluteFile().toURI());
if (null == resources || resources.length == 0) {
throw new IllegalStateException("Cannot find file for the selected object"); //$NON-NLS-1$
}
return resources[0];
}
@Override
public String getText() {
return Messages.OpenInEditorAction_Label;
}
}