Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 0f5b3410d1b2b1ce9c6b750165f62ec317644e84 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*******************************************************************************
 * Copyright (c) 2007, 2009 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.web.ui.views.contentoutline;

import java.util.Collection;

import org.eclipse.jface.text.Position;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wst.jsdt.core.IJavaScriptElement;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.internal.core.JavaElement;
import org.eclipse.wst.jsdt.ui.JavaScriptElementLabelProvider;
import org.eclipse.wst.jsdt.web.core.internal.Logger;
import org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation;
import org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.internal.model.FactoryRegistry;
import org.eclipse.wst.sse.core.internal.provisional.AbstractNotifier;
import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.document.ElementImpl;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.w3c.dom.Node;
/**
*

* 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 JsJfaceNode extends ElementImpl implements IndexedRegion, INodeNotifier, Node, IJavaWebNode {
	private class JsAdaptableNode extends AbstractNotifier {
		
		public FactoryRegistry getFactoryRegistry() {
			return adapterRegistry;
		}
	}
	private JsAdaptableNode adaptableDomNode = new JsAdaptableNode();
	private FactoryRegistry adapterRegistry;
	private Position fDocPosition;
//	private Node parent;
	private String typeName;
	//private IJavaScriptElement dirtyElement;
	private boolean hasChildren;
	//private String name;
	private Image me;
	
	public JsJfaceNode(Node parent, IJavaScriptElement originalElement, Position structureDocLocation) {
		this(parent, originalElement, structureDocLocation, null);
	}
	
	public JsJfaceNode(Node parent, IJavaScriptElement originalElement, Position structureDocLocation, String typeName) {
		//super();
		super(((ElementImpl)parent));
		// super(parentObject, parentObject.getElementName());
		fDocPosition = structureDocLocation;
		//this.parent = parent;
		this.typeName = typeName;
		try {
			hasChildren=((JavaElement)originalElement).hasChildren();
		} catch (JavaScriptModelException ex) {
			hasChildren=false;
		}
		removeAttributes();
		me = (new JavaScriptElementLabelProvider(JavaScriptElementLabelProvider.SHOW_DEFAULT|JavaScriptElementLabelProvider.SHOW_SMALL_ICONS)).getImage(originalElement);
	}
	
	public Image getImage() {
		return me;
	}

	public boolean hasChildren() {
//		try {
//			return ((JavaElement)this.dirtyElement).hasChildren();
//		} catch (JavaScriptModelException ex) {
//			// TODO Auto-generated catch block
//			ex.printStackTrace();
//		}
		return hasChildren;
	}
	

	public void setName(String name) {
		super.setTagName(name);
	}
		
	public void addAdapter(INodeAdapter adapter) {
		adaptableDomNode.addAdapter(adapter);
	}
	
	
	public boolean contains(int testPosition) {
		return false;
	}
	
	
	public boolean equals(Object o) {
		return (o != null && o instanceof JsJfaceNode && ((JsJfaceNode) o).fDocPosition == this.fDocPosition);
	}
	
// public static Object getInstance(Object parent, Position structureDocLocation
// ){
// //return new JsJfaceNode((JavaElement)parent.getParent(),
// parent.getElementName(),structureDocLocation);
// Vector interfaces = new Vector();
// interfaces.addAll(Arrays.asList(parent.getClass().getInterfaces()));
// JsJfaceNode me = new JsJfaceNode(parent,structureDocLocation);
// interfaces.addAll(Arrays.asList(me.getClass().getInterfaces()));
// Object proxy = null;
// try {
// proxy= Proxy.newProxyInstance(
// parent.getClass().getClassLoader(),
// (Class[])interfaces.toArray(new Class[]{}),me);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// System.out.println(e);
// }
// return proxy;
// }
//   
	
	public INodeAdapter getAdapterFor(Object type) {
		return adaptableDomNode.getAdapterFor(type);
	}
	
	
	public Collection getAdapters() {
		return adaptableDomNode.getAdapters();
	}
	
	
	public int getEndOffset() {
		return fDocPosition.getOffset() + fDocPosition.getLength();
	}
	
	
	public INodeAdapter getExistingAdapter(Object type) {
		// TODO Auto-generated method stub
		return adaptableDomNode.getExistingAdapter(type);
	}
	
	public synchronized IJavaScriptElement getJavaElement() {
		/*
		 * since this may become 'stale' we need to rediscover our element every
		 * time we're asked
		 */
		IJsTranslation tran = getTranslation();
		int startOffset = getStartOffset();
		int endOffset = getLength();
		if (typeName != null) {
			IJavaScriptElement myType = tran.getCompilationUnit().getType(typeName);
			return myType;
		}
		IJavaScriptElement elements[] = tran.getAllElementsInJsRange(startOffset, startOffset + endOffset);
		if (elements != null) {
			return elements[0];
		} else {
			System.out.println(Messages.getString("JsJfaceNode.1")); //$NON-NLS-1$
			return null;
		}
	}
	
	
	public int getLength() {
		return fDocPosition.getLength();
	}
	
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.xml.core.internal.document.NodeImpl#getOwnerDocument()
	 */
	

	
	

	
	
	public int getStartOffset() {
		return fDocPosition.getOffset();
	}
	
// private Method[] getMethods(){
// // returns the methods this class supports (as declared in interfaces)
// Class[] interfaces = getClass().getInterfaces();
// Vector vMethods = new Vector();
// for(int i = 0;i<interfaces.length;i++){
// Method methods[] = interfaces[i].getDeclaredMethods();
// vMethods.addAll(Arrays.asList(methods));
// }
//        
// return (Method[])vMethods.toArray();
// }
// public Object invoke(Object proxy, Method method, Object[] args) throws
// Throwable {
// Object result;
// Method[] myMethods = getMethods();
//        
// try {
// for(int i = 0;i<myMethods.length;i++){
// if(myMethods[i]==method){
// return method.invoke(this, args);
// }
// }
// result = method.invoke(parentType, args);
// } catch (InvocationTargetException e) {
// throw e.getTargetException();
// } catch (Exception e) {
// throw new RuntimeException("unexpected invocation exception: " +
// e.getMessage());
// }
//
// return result;
// }
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.xml.core.internal.document.NodeImpl#getStructuredDocument()
	 */
	

	
	public IJsTranslation getTranslation() {
		IStructuredModel model = null;
		IModelManager modelManager = StructuredModelManager.getModelManager();
		IDOMDocument xmlDoc = null;
		try {
			if (modelManager != null) {
				IStructuredDocument doc = getStructuredDocument();
				model = modelManager.getExistingModelForRead(doc);
				// model = modelManager.getModelForRead(doc);
			}
			IDOMModel domModel = (IDOMModel) model;
			xmlDoc = domModel.getDocument();
		} catch (Exception e) {
			Logger.logException(e);
		} finally {
			if (model != null) {
				// model.changedModel();
				model.releaseFromRead();
			}
		}
		if (xmlDoc == null) {
			return null;
		}
		JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
		return translationAdapter.getJsTranslation(true);
	}
	
	
	public void notify(int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
		adaptableDomNode.notify(eventType, changedFeature, oldValue, newValue, pos);
	}
	
	
	public void removeAdapter(INodeAdapter adapter) {
		adaptableDomNode.removeAdapter(adapter);
	}
	
	public void setAdapterRegistry(FactoryRegistry registry) {
		this.adapterRegistry = registry;
	}
}

Back to the top