blob: 1bbb4c4abe5c956e2b1388beec707151a94c6e92 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 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.eclipse.jst.jsp.ui.internal.contentassist;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.IDocument;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
/**
* <p>Helpful utility methods for JSP content assist</p>
*/
public class JSPContentAssistHelper {
/**
* Returns project request is in
*
* @param request
* @return {@link IResource} representing the project the given request was made in
*/
public static IResource getResource(ContentAssistRequest request) {
IResource resource = null;
String baselocation = null;
if (request != null) {
IStructuredDocumentRegion region = request.getDocumentRegion();
if (region != null) {
IDocument document = region.getParentDocument();
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
baselocation = model.getBaseLocation();
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
}
if (baselocation != null) {
// copied from JSPTranslationAdapter#getJavaProject
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath filePath = new Path(baselocation);
IFile file = null;
if (filePath.segmentCount() > 1) {
file = root.getFile(filePath);
}
if (file != null) {
resource = file.getProject();
}
}
return resource;
}
}