Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 157430 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/wst/dtd/ui/views/contentoutline/DTDTreeContentProvider.java (-145 / +150 lines)
Lines 149-202 Link Here
149
	}
149
	}
150
150
151
	public Object getParent(Object element) {
151
	public Object getParent(Object element) {
152
		List parents = getParents(element);
152
		Object parent = null;
153
		Object parent = null;
153
		if (element instanceof DTDNode) {
154
		if(parents.size() > 0) {
154
			DTDNode node = (DTDNode) element;
155
			parent = parents.get(0);
155
			if (element instanceof Attribute) {
156
				// then we must say that the element with the same name
157
				// as our attribute's parent attributelist is our parent
158
				parent = node.getParentNode();
159
				if (parent != null && parent instanceof AttributeList) {
160
					return getParent(parent);
161
				}
162
			}
163
			if (element instanceof AttributeList) {
164
				// then we must say that the element with the same name
165
				// as our attributelist is our parent
166
				String attListName = ((AttributeList) element).getName();
167
				Iterator iter = node.getDTDFile().getElementsAndParameterEntityReferences().getNodes().iterator();
168
				while (iter.hasNext() && parent == null) {
169
					DTDNode currentNode = (DTDNode) iter.next();
170
					if (currentNode instanceof Element && currentNode.getName().equals(attListName)) {
171
						parent = currentNode;
172
					}
173
				}
174
			}
175
176
			if (parent == null) {
177
				parent = ((DTDNode) element).getParentNode();
178
			}
179
180
			// if showing in the logical order, return the IndexedNodeList
181
			// acting as the parent in the tree
182
			if (parent == null) {
183
				if (isShowLogicalOrder()) {
184
					Object[] indexedNodeLists = getChildren(((DTDModelImpl) fInputObject).getDTDFile());
185
					for (int i = 0; i < indexedNodeLists.length && parent == null; i++) {
186
						if (indexedNodeLists[i] instanceof NodeList) {
187
							if (((NodeList) indexedNodeLists[i]).getNodes().contains(element))
188
								parent = indexedNodeLists[i];
189
						}
190
					}
191
				}
192
				else {
193
					parent = ((DTDModelImpl) fInputObject).getDTDFile();
194
				}
195
			}
196
		}
197
		else if (element instanceof NodeList && fInputObject instanceof DTDModelImpl) {
198
			parent = ((DTDModelImpl) fInputObject).getDTDFile();
199
		}
156
		}
157
		
200
		return parent;
158
		return parent;
201
	}
159
	}
202
160
Lines 242-307 Link Here
242
		return true;// !nodeList.getListType().equals(DTDRegionTypes.ATTLIST_TAG);
200
		return true;// !nodeList.getListType().equals(DTDRegionTypes.ATTLIST_TAG);
243
	}
201
	}
244
202
203
	/**
204
	 * <p>If a node changed then refresh the tree for that node</p>
205
	 * 
206
	 * @see org.eclipse.wst.dtd.core.internal.event.IDTDFileListener#nodeChanged(org.eclipse.wst.dtd.core.internal.DTDNode)
207
	 */
245
	public void nodeChanged(final DTDNode node) {
208
	public void nodeChanged(final DTDNode node) {
246
		if (fViewer instanceof StructuredViewer) {
209
		refreshTree(node);
247
			// System.out.println("node changed notified");
248
			// System.out.println("selection before = " +
249
			// ((StructuredViewer)view).getSelection());
250
			if (node instanceof AttributeList && isShowLogicalOrder()) {
251
				// in this case, we are showing attributes under the element.
252
				// refresh the element object instead
253
				Iterator iter = node.getDTDFile().getNodes().iterator();
254
				while (iter.hasNext()) {
255
					DTDNode currentNode = (DTDNode) iter.next();
256
					if (currentNode.getName().equals(node.getName()) && currentNode instanceof Element) {
257
						((StructuredViewer) fViewer).refresh(currentNode, true);
258
					}
259
				} // end of while ()
260
			}
261
			else {
262
				// do standard stuff
263
				fViewer.getControl().getDisplay().asyncExec(new Runnable() {
264
					public void run() {
265
						if (fViewer.getControl().isDisposed())
266
							return;
267
						if (node.getParentNode() != null) {
268
							((StructuredViewer) fViewer).refresh(node.getParentNode(), true);
269
						}
270
						((StructuredViewer) fViewer).refresh(node, true);
271
					}
272
				});
273
			}
274
			
275
			if (node instanceof Attribute) {
276
				fViewer.getControl().getDisplay().asyncExec(new Runnable() {
277
					public void run() {
278
						if (fViewer.getControl().isDisposed())
279
							return;
280
						/*
281
						 * go from the attribute to its list and then owning element
282
						 * so we refresh the tree item there as well
283
						 */
284
						Object attrList = node.getParentNode();
285
						if (attrList != null && attrList instanceof AttributeList) {
286
							String attListName = ((AttributeList) attrList).getName();
287
							Iterator iter = node.getDTDFile().getElementsAndParameterEntityReferences().getNodes().iterator();
288
							Object parent = null;
289
							while (iter.hasNext() && parent == null) {
290
								DTDNode currentNode = (DTDNode) iter.next();
291
								if (currentNode instanceof Element && currentNode.getName().equals(attListName)) {
292
									parent = currentNode;
293
								}
294
							}
295
							if (parent != null) {
296
								((StructuredViewer) fViewer).refresh(parent, true);
297
							}
298
						}
299
					}
300
				});
301
			}
302
			// System.out.println("selection after = " +
303
			// ((StructuredViewer)view).getSelection());
304
		}
305
	}
210
	}
306
211
307
	public void nodesAdded(NodesEvent event) {
212
	public void nodesAdded(NodesEvent event) {
Lines 315-359 Link Here
315
				oldSelectedNode = (DTDNode) firstObj;
220
				oldSelectedNode = (DTDNode) firstObj;
316
			}
221
			}
317
222
318
			final AbstractTreeViewer abstractTreeViewer = (AbstractTreeViewer) fViewer;
223
			//update the tree
319
			for (Iterator it = event.getNodes().iterator(); it.hasNext();) {
224
			this.refreshTree(event);
320
				Object node = it.next();
321
				final Object parent = getParent(node);
322
				// Bug 111100 - If it is a top level node (ie. parent is a
323
				// DTDFile),
324
				// insert the node directly to improve performance
325
				if (parent instanceof DTDFile) {
326
					Object[] objs = getChildren(parent);
327
					for (int i = 0; i < objs.length; i++) {
328
						if (objs[i] == node) {
329
							abstractTreeViewer.insert(parent, node, i);
330
							break;
331
						}
332
					}
333
				}
334
				// If the parent node is not a DTDFile, just refresh the
335
				// parent for now
336
				else if (parent != null) {
337
					fViewer.getControl().getDisplay().asyncExec(new Runnable() {
338
						public void run() {
339
							if (fViewer.getControl().isDisposed())
340
								return;
341
							abstractTreeViewer.refresh(parent, true);
342
						}
343
					});
344
				}
345
				// You should never reach this block, if you do, just refresh
346
				// the whole tree
347
				else {
348
					fViewer.getControl().getDisplay().asyncExec(new Runnable() {
349
						public void run() {
350
							if (fViewer.getControl().isDisposed())
351
								return;
352
							abstractTreeViewer.refresh(true);
353
						}
354
					});
355
				}
356
			}
357
225
358
			Iterator iter = event.getNodes().iterator();
226
			Iterator iter = event.getNodes().iterator();
359
			List newSelection = new ArrayList();
227
			List newSelection = new ArrayList();
Lines 379-385 Link Here
379
				abstractTreeViewer.remove(iter.next());
247
				abstractTreeViewer.remove(iter.next());
380
			}
248
			}
381
		}
249
		}
382
250
		
251
		//update the tree
252
		this.refreshTree(event);
383
	}
253
	}
384
254
385
	/**
255
	/**
Lines 395-398 Link Here
395
			logicalNodeLists = null;
265
			logicalNodeLists = null;
396
		}
266
		}
397
	}
267
	}
268
	
269
	/**
270
	 * <p>Used to update the tree after a node event such as a node added or removed.</p>
271
	 * @param event the {@link NodesEvent} that caused the tree to need updating
272
	 */
273
	private void refreshTree(NodesEvent event) {
274
		for (Iterator it = event.getNodes().iterator(); it.hasNext();) {
275
			Object node = it.next();
276
			this.refreshTree(node);
277
			
278
		}
279
	}
280
	
281
	/**
282
	 * <p>Refreshes the tree from the parents of the given node.</p>
283
	 * @param node refresh the tree from the parents of this node
284
	 */
285
	private void refreshTree(Object node) {
286
		List parents = getParents(node);
287
		if(parents.size() > 0) {
288
			for(int p = 0; p < parents.size(); ++p) {
289
				final Object parent = parents.get(p);
290
			
291
				// Bug 111100 - If it is a top level node (ie. parent is a
292
				// DTDFile),
293
				// insert the node directly to improve performance
294
				if (parent instanceof DTDFile) {
295
					Object[] objs = getChildren(parent);
296
					for (int i = 0; i < objs.length; i++) {
297
						if (objs[i] == node) {
298
							((AbstractTreeViewer) fViewer).insert(parent, node, i);
299
							break;
300
						}
301
					}
302
				}
303
				
304
				this.refreshTreeNode(parent, true);
305
			}
306
		}
307
	}
308
	
309
	/**
310
	 * @param element get the tree parents of this element
311
	 * @return {@link List} of parents of the given element
312
	 */
313
	private List getParents(Object element) {
314
		List parents = new ArrayList();
315
		
316
		Object parent = null;
317
		if (element instanceof DTDNode) {
318
			DTDNode node = (DTDNode) element;
319
			if (element instanceof Attribute) {
320
				parent = node.getParentNode();
321
				if (parent != null && parent instanceof AttributeList) {
322
					parents.addAll(getElementParentsOfAttributeList((AttributeList)parent));
323
				}
324
			} else if(element instanceof AttributeList) {
325
				parents.addAll(getElementParentsOfAttributeList((AttributeList)element));
326
			}
327
328
			// if showing in the logical order, return the IndexedNodeList
329
			// acting as a parent in the tree
330
			if (isShowLogicalOrder()) {
331
				Object[] indexedNodeLists = getChildren(((DTDModelImpl) fInputObject).getDTDFile());
332
				for (int i = 0; i < indexedNodeLists.length && parent == null; i++) {
333
					if (indexedNodeLists[i] instanceof NodeList) {
334
						if (((NodeList) indexedNodeLists[i]).getNodes().contains(element)) {
335
							parents.add(indexedNodeLists[i]);
336
						}
337
					}
338
				}
339
			}
340
			
341
			//try and get the simple parent
342
			parent = ((DTDNode) element).getParentNode();
343
			if(parent != null) {
344
				parents.add(parent);
345
			}
346
			
347
			//if no parents found must be new nodes so refresh from root
348
			if(parents.size() == 0) {
349
				parents.add(((DTDModelImpl) fInputObject).getDTDFile());
350
			}
351
		}else if (element instanceof NodeList && fInputObject instanceof DTDModelImpl) {
352
			parents.add(((DTDModelImpl) fInputObject).getDTDFile());
353
		}
354
		
355
		return parents;
356
	}
357
	
358
	/**
359
	 * @param attList get the element parents of the given {@link AttributeList}
360
	 * @return the element parents of the given {@link AttributeList}, if no parents
361
	 * can be found the list contains the DTD file element
362
	 */
363
	private List getElementParentsOfAttributeList(AttributeList attList) {
364
		List parents = new ArrayList();
365
		Iterator iterAttList = attList.getDTDFile().getNodes().iterator();
366
		while (iterAttList.hasNext()) {
367
			DTDNode currentNode = (DTDNode) iterAttList.next();
368
			if (currentNode instanceof Element &&
369
					currentNode.getName().equals(attList.getName())) {
370
371
				parents.add(currentNode);
372
			}
373
		}
374
		
375
		if(parents.size() == 0) {
376
			parents.add(((DTDModelImpl) fInputObject).getDTDFile());
377
		}
378
		
379
		return parents;
380
	}
381
	
382
	/**
383
	 * <p>Executes a refresh on the {@link AbstractTreeViewer} for the given
384
	 * node</p>
385
	 * 
386
	 * @param node refresh the tree for this node
387
	 * @param updateLabels <code>true</code> to update the labels on the tree,
388
	 * <code>false</code> otherwise.
389
	 */
390
	private void refreshTreeNode(final Object node, final boolean updateLabels) {
391
		fViewer.getControl().getDisplay().asyncExec(new Runnable() {
392
			public void run() {
393
				if (!fViewer.getControl().isDisposed()) {
394
					if(node != null) {
395
						((AbstractTreeViewer) fViewer).refresh(node, updateLabels);
396
					} else {
397
						((AbstractTreeViewer) fViewer).refresh(updateLabels);
398
					}
399
				}
400
			}
401
		});
402
	}
398
}
403
}

Return to bug 157430