*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / internal / outline / XMLOutlinePage.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: XMLOutlinePage.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 java.util.List;
17
18 import org.eclipse.jface.viewers.DecoratingLabelProvider;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TreeViewer;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.ui.texteditor.IDocumentProvider;
26 import org.eclipse.ui.views.contentoutline.ContentOutlinePage;
27
28 import net.sourceforge.phpeclipse.core.model.ISourceReference;
29 import net.sourceforge.phpeclipse.ui.views.outline.ProblemsLabelDecorator;
30 import net.sourceforge.phpeclipse.xml.core.model.IXMLDocument;
31 import net.sourceforge.phpeclipse.xml.ui.internal.editor.XMLDocumentProvider;
32 import net.sourceforge.phpeclipse.xml.ui.internal.editor.XMLEditor;
33
34 /**
35  * Implements the outline page associated with the XML editor.
36  */
37 public class XMLOutlinePage extends ContentOutlinePage {
38
39         // Instance Variables ------------------------------------------------------
40
41         /**
42          * The associated editor.
43          */
44         private XMLEditor editor;
45
46         // Constructors ------------------------------------------------------------
47
48         /**
49          * Constructor.
50          * 
51          * @param editor The associated text editor
52          */
53         public XMLOutlinePage(XMLEditor editor) {
54                 this.editor = editor;
55         }
56
57         // ContentOutlinePage Implementation ---------------------------------------
58
59         /*
60          * @see org.eclipse.ui.part.IPage#createControl(Composite)
61          */
62         public void createControl(Composite parent) {
63                 super.createControl(parent);
64                 TreeViewer viewer = getTreeViewer();
65                 viewer.setContentProvider(new XMLOutlineContentProvider());
66                 viewer.setLabelProvider(new DecoratingLabelProvider(
67                                 new XMLOutlineLabelProvider(),
68                                 new ProblemsLabelDecorator(editor)));
69                 viewer.setInput(getDocument());
70         }
71
72         // Public Methods ----------------------------------------------------------
73
74         /**
75          * Selects a specific element in the outline page.
76          * 
77          * @param element the element to select
78          */
79         public void select(ISourceReference element) {
80                 TreeViewer viewer = getTreeViewer();
81                 if (viewer != null) {
82                         ISelection selection = viewer.getSelection();
83                         if (selection instanceof IStructuredSelection) {
84                                 IStructuredSelection structuredSelection =
85                                         (IStructuredSelection) selection;
86                                 List elements = structuredSelection.toList();
87                                 if (!elements.contains(element)) {
88                                         if (element == null) {
89                                                 selection = StructuredSelection.EMPTY;
90                                         } else {
91                                                 selection = new StructuredSelection(element);
92                                         }
93                                         viewer.setSelection(selection, true);
94                                 }
95                         }
96                 }
97         }
98
99         /**
100          * Updates the outline page.
101          */
102         public void update() {
103                 IXMLDocument document = getDocument();
104                 if (document != null) {
105                         TreeViewer viewer = getTreeViewer();
106                         if (viewer != null) {
107                                 Control control = viewer.getControl();
108                                 if ((control != null) && !control.isDisposed()) {
109                                         control.setRedraw(false);
110                                         viewer.setInput(document);
111                                         viewer.expandAll();
112                                         control.setRedraw(true);
113                                 }
114                         }
115                 }
116         }
117
118         // Private Methods ---------------------------------------------------------
119
120         /**
121          * Returns the parsed model of the XML document that is loaded into the 
122          * associated editor.
123          * 
124          * @return the parsed XML document
125          */
126         private IXMLDocument getDocument() {
127                 IDocumentProvider provider = editor.getDocumentProvider();
128                 if (provider instanceof XMLDocumentProvider) {
129                         XMLDocumentProvider xmlProvider = (XMLDocumentProvider) provider;
130                         return xmlProvider.getModel(editor.getEditorInput());
131                 }
132                 return null;
133         }
134
135 }