9096ce612e1dec451621efc2fd78a1134d5dd242
[phpeclipse.git] /
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.1 2004-09-02 18:28:05 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui.internal.outline;
15
16 import org.eclipse.jface.viewers.ITreeContentProvider;
17 import org.eclipse.jface.viewers.Viewer;
18
19 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
20 import net.sourceforge.phpeclipse.xml.core.model.IXMLElement;
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, Object, Object)
82          */
83         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
84                 if (oldInput != newInput) {
85                         if (oldInput instanceof IXMLDocument) {
86                                 document = null;
87                         }
88                         if (newInput instanceof IXMLDocument) {
89                                 document = (IXMLDocument) newInput;
90                         }          
91                 }
92         }
93
94 }