4484e9470f75446fada25308f127b0041d94d3f5
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / outline / XMLOutlineContentProvider.java
1 /*
2  * Copyright (c) 2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: XMLOutlineContentProvider.java,v 1.3 2006-10-21 23:14:14 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.outline;
15
16 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
17 import net.sourceforge.phpeclipse.xml.core.model.IXMLElement;
18
19 import org.eclipse.jface.viewers.ITreeContentProvider;
20 import org.eclipse.jface.viewers.Viewer;
21
22 /**
23  * Content provider for the XML outline page.
24  */
25 public class XMLOutlineContentProvider implements ITreeContentProvider {
26
27         // Instance Variables ------------------------------------------------------
28
29         /**
30          * The parsed XML document.
31          */
32         private IXMLDocument document;
33
34         // ITreeContentProvider Implementation -------------------------------------
35
36         /*
37          * ITreeContentProvider#getChildren(Object)
38          */
39         public Object[] getChildren(Object parentElement) {
40                 if (parentElement instanceof IXMLElement) {
41                         return ((IXMLElement) parentElement).getChildren();
42                 }
43                 return new Object[0];
44         }
45
46         /*
47          * @see ITreeContentProvider#getParent(Object)
48          */
49         public Object getParent(Object element) {
50                 if (element instanceof IXMLElement) {
51                         return ((IXMLElement) element).getParent();
52                 }
53                 return null;
54         }
55
56         /*
57          * @see ITreeContentProvider#hasChildren(Object)
58          */
59         public boolean hasChildren(Object element) {
60                 return getChildren(element).length > 0;
61         }
62
63         /*
64          * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
65          */
66         public Object[] getElements(Object inputElement) {
67                 if ((document != null) && (document.getRoot() != null)) {
68                         return new Object[] { document.getRoot() };
69                 }
70                 return new Object[0];
71         }
72
73         /*
74          * @see org.eclipse.jface.viewers.IContentProvider#dispose()
75          */
76         public void dispose() {
77                 document = null;
78         }
79
80         /*
81          * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer,
82          *      Object, Object)
83          */
84         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
85                 if (oldInput != newInput) {
86                         if (oldInput instanceof IXMLDocument) {
87                                 document = null;
88                         }
89                         if (newInput instanceof IXMLDocument) {
90                                 document = (IXMLDocument) newInput;
91                         }
92                 }
93         }
94
95 }