Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb6363065765744338f189bba5948f46c582dcf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * 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:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.context.resolver.structureddocument.internal.impl;

import org.eclipse.core.resources.IProject;
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.jst.jsf.context.IModelContext;
import org.eclipse.jst.jsf.context.resolver.structureddocument.IWorkspaceContextResolver;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;

/**
 * 
 * @author cbateman
 *
 */
/*package*/ class WorkspaceContextResolver implements IWorkspaceContextResolver 
{
	private final IStructuredDocumentContext		_context;
	
	/*package*/WorkspaceContextResolver(IStructuredDocumentContext context)
	{
		_context = context;
	}
	
	/**
	 * @see org.eclipse.jst.jsf.context.resolver.structureddocument.IWorkspaceContextResolver#getProject()
	 */
	public IProject getProject() 
	{
		// copied from ModelManagerImpl (with some rework by C.B.)
		final String path = getPath();
		
		if (path == null)
		{
			return null;
		}
		
		// TOODO needs rework for linked resources
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
		IPath iPath = new Path(path);
		if (iPath.isAbsolute())
		{
			String  projectName = iPath.segment(0);
			
			IProject projects[] =  root.getProjects();
			
			for (int i = 0; i < projects.length; i++)
			{
				IProject project = projects[i];
				
				if (project.isOpen()
						&& projectName.equals(project.getFullPath().segment(0)))
				{
					return project;  //
				}
			}
		}

		return null;
	}

	public IResource getResource() 
	{
		IProject project = getProject();
		
		if (project != null)
		{
			final String path = getPath();
			
			if (path != null)
			{
				final IPath iPath = new Path(path);
				if (iPath.isAbsolute())
				{
					return project.getFile(iPath.removeFirstSegments(1));
				}
			}
		}
		return null;
	}
	
	private String getPath()
	{
		IStructuredModel model = null;
		
		try
		{
			model = StructuredModelManager.getModelManager().getExistingModelForRead(_context.getStructuredDocument());
			
			if (model == null)
				return null;
			String path = model.getBaseLocation();
			if (path == null || path.length() == 0) 
			{
				Object id = model.getId();
				if (id == null)
					return null;
				path = id.toString();
			}
			
			return path;
		}
		finally
		{
			if (model != null)
			{
				model.releaseFromRead();
			}
		}
	}

	/**
	 * @see org.eclipse.jst.jsf.context.resolver.IContextResolver#canResolveContext(org.eclipse.jst.jsf.context.IModelContext)
	 */
	public boolean canResolveContext(IModelContext modelContext) 
	{
		return (modelContext.getAdapter(IStructuredDocumentContext.class) != null);
	}
}

Back to the top