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