Skip to main content
summaryrefslogtreecommitdiffstats
blob: fba5ee7d10daf4a7c5ea44a4efb8ea8de22ae4a9 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*******************************************************************************
 * Copyright (c) 2005, 2008 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.wst.jsdt.core;

import java.util.ArrayList;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;


/**
 *  
 * Provisional API: This class/interface is part of an interim API that is still under development and expected to 
 * change significantly before reaching stability. It is being made available at this early stage to solicit feedback 
 * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken 
 * (repeatedly) as the API evolves.
 *
 */
public class LibrarySuperType {
	IPath cpEntry;
	String superTypeName;
	String libraryName;
	IJavaScriptProject javaProject;

	public static final String SUPER_TYPE_CONTAINER= "org.eclipse.wst.jsdt.ui.superType.container"; //$NON-NLS-1$
	public static final String SUPER_TYPE_NAME= "org.eclipse.wst.jsdt.ui.superType.name"; //$NON-NLS-1$

	/* Only one superTypeName per instance so enforce that */
	public LibrarySuperType(IPath classPathEntry, IJavaScriptProject project, String superTypeName) {
		this.cpEntry = classPathEntry;
		this.superTypeName = superTypeName;
		this.javaProject =  project;
		this.libraryName = initLibraryName();

	}

	public LibrarySuperType(String classPathEntry, IJavaScriptProject project, String superTypeName) {
		this(new Path(classPathEntry),project,superTypeName);
	}
	/* Construct parent */
	public LibrarySuperType(IPath classPathEntry,  IJavaScriptProject project) {
		this(classPathEntry,project, null);
	}

	public IPath getRawContainerPath() {
		return cpEntry;
	}

	public boolean hasChildren() {
		/* defined super type meeans I'm a child */
		if(superTypeName!=null) return false;
		JsGlobalScopeContainerInitializer init = getContainerInitializer();
		if (init == null) return false;
		String[] availableSuperTypes = init.containerSuperTypes();
		return availableSuperTypes!=null && availableSuperTypes.length>0;
	}

	public LibrarySuperType[] getChildren() {
		if(superTypeName!=null) return new LibrarySuperType[0];
		return getFlatLibrarySuperTypes(cpEntry,javaProject);
	}

	public LibrarySuperType getParent() {
		if(superTypeName==null) return null;
		return new LibrarySuperType(cpEntry,javaProject, null);
	}

	public boolean isParent() {
		return getParent()==null;
	}

	public JsGlobalScopeContainerInitializer getContainerInitializer() {
		return getContainerInitializer(cpEntry);
	}

	public IIncludePathEntry[] getClasspathEntries() {
		IJsGlobalScopeContainer container=null;
		try {
			container = JavaScriptCore.getJsGlobalScopeContainer(this.cpEntry, this.javaProject);
		} catch (JavaScriptModelException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		if(container!=null) return	container.getIncludepathEntries();

		return new IIncludePathEntry[0];
	}

	private static LibrarySuperType[] getFlatLibrarySuperTypes(IPath classPathEntry, IJavaScriptProject javaProject) {
		JsGlobalScopeContainerInitializer init = getContainerInitializer(classPathEntry);
		if (init == null) return new LibrarySuperType[0];
		String[] availableSuperTypes = init.containerSuperTypes();
		LibrarySuperType[] libSupers = new LibrarySuperType[availableSuperTypes.length];
		for (int i = 0; i < availableSuperTypes.length; i++) {
			libSupers[i] = new LibrarySuperType(classPathEntry, javaProject, availableSuperTypes[i]);
		}
		return libSupers;
	}

	public String getSuperTypeName() {
		return superTypeName;
	}

	public String getLibraryName() {
		return libraryName;
	}

	private String initLibraryName() {
		JsGlobalScopeContainerInitializer init = getContainerInitializer();

		/* parent node */
		if(superTypeName==null) {
			if(init==null) {
				return cpEntry.toString();
			}
			return  init.getDescription(cpEntry, javaProject);
		}
		Object parent = getParent();
		if(!(parent instanceof LibrarySuperType)) return null;
		return ((LibrarySuperType)parent).getLibraryName();
	}

	public String toString() {
		//JsGlobalScopeContainerInitializer init = getContainerInitializer();

		/* parent node */
		if(isParent()) {
			return getLibraryName();

		}
		
		return  Messages.getString("LibrarySuperType.0", new Object[]{superTypeName, getLibraryName()}); //$NON-NLS-1$
	}

	public boolean equals(Object o) {
		if(!(o instanceof LibrarySuperType)) return false;

		LibrarySuperType other = (LibrarySuperType)o;



		if(other.cpEntry!=null && !other.cpEntry.equals(cpEntry)) {
			return false;
		}

		if((other.superTypeName==superTypeName)) {
			return true;
		}

		if(other.superTypeName!=null && superTypeName!=null) {
			return other.superTypeName.equals(superTypeName);
		}

		return false;
	}

	public IPackageFragment[] getPackageFragments(){
		IIncludePathEntry[] entries = getClasspathEntries();
		ArrayList allFrags = new ArrayList();

		try {
			for(int i = 0;i<entries.length;i++) {
				IPath path = entries[i].getPath();
				IPackageFragmentRoot root = javaProject.findPackageFragmentRoot(path.makeAbsolute());

				IJavaScriptElement[] children = root.getChildren();
				for(int k = 0;k<children.length;k++) {
					if(children[k] instanceof IPackageFragment) {
						allFrags.add(children[k]);
					}
				}
			}
		} catch (JavaScriptModelException ex) {
			// TODO Auto-generated catch block
			ex.printStackTrace();
		}
		return (IPackageFragment[])allFrags.toArray(new IPackageFragment[allFrags.size()]);
	}

	public static JsGlobalScopeContainerInitializer getContainerInitializer(IPath classPathEntry) {
		if(classPathEntry==null ) return null;
		JsGlobalScopeContainerInitializer initializer= JavaScriptCore.getJsGlobalScopeContainerInitializer(classPathEntry.segment(0));
		return initializer ;
	}
}

Back to the top