KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > SourceOutlinePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.viewers.IBaseLabelProvider;
18 import org.eclipse.jface.viewers.IContentProvider;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.TreeViewer;
21 import org.eclipse.jface.viewers.ViewerComparator;
22 import org.eclipse.pde.internal.core.text.IEditingModel;
23 import org.eclipse.pde.internal.core.text.IReconcilingParticipant;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.ui.part.IPageSite;
27
28 /**
29  * Content outline page for the XML editor.
30  */

31 public class SourceOutlinePage extends PDEOutlinePage implements IReconcilingParticipant, ISortableContentOutlinePage{
32     
33     private IEditingModel fModel;
34     private IBaseLabelProvider fLabelProvider;
35     private IContentProvider fContentProvider;
36     private ViewerComparator fDefaultComparator;
37     private ViewerComparator fViewerComparator;
38     private boolean sorted;
39     TreeViewer viewer;
40     
41     /**
42      * This list is redundant; but, required because we can't access
43      * org.eclipse.ui.views.contentoutline.ContentOutlinePage.selectionChangedListeners
44      * from our parent
45      */

46     private ArrayList JavaDoc fListenerList;
47     
48     public SourceOutlinePage(IEditingModel model, IBaseLabelProvider lProvider,
49             IContentProvider cProvider, ViewerComparator defaultComparator,
50             ViewerComparator comparator) {
51         super();
52         fModel = model;
53         fLabelProvider = lProvider;
54         fContentProvider = cProvider;
55         fDefaultComparator = defaultComparator;
56         fViewerComparator = comparator;
57         fListenerList = new ArrayList JavaDoc();
58     }
59         
60     /**
61      * Creates the control for this outline page.
62      */

63     public void createControl(Composite parent) {
64         super.createControl(parent);
65         viewer= getTreeViewer();
66         viewer.setContentProvider(fContentProvider);
67         viewer.setLabelProvider(fLabelProvider);
68         if(sorted)
69             viewer.setComparator(fViewerComparator);
70         else
71             viewer.setComparator(fDefaultComparator);
72         viewer.setInput(fModel);
73         viewer.expandAll();
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#init(org.eclipse.ui.part.IPageSite)
78      */

79     public void init(IPageSite pageSite) {
80         super.init(pageSite);
81     }
82     
83
84     /* (non-Javadoc)
85      * @see org.eclipse.pde.internal.ui.editor.text.IReconcilingParticipant#reconciled(org.eclipse.jface.text.IDocument)
86      */

87     public void reconciled(IDocument document) {
88         final Control control = getControl();
89         if (control == null || control.isDisposed())
90             return;
91         control.getDisplay().asyncExec(new Runnable JavaDoc() {
92             public void run() {
93                 if (control.isDisposed())
94                     return;
95                 control.setRedraw(false);
96                 // Temporarily remove all selection listeners from the tree
97
// viewer. This is required because the refresh fires a
98
// selection event back to the source page (observered in
99
// the bundle source page) when typing
100
removeAllSelectionChangedListeners();
101                 getTreeViewer().refresh();
102                 addAllSelectionChangedListeners();
103                 getTreeViewer().expandAll();
104                 control.setRedraw(true);
105             }
106         });
107     }
108     
109     /* (non-Javadoc)
110      * @see org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage#sort(boolean)
111      */

112     public void sort (boolean sorting) {
113         sorted = sorting;
114         if (isViewerDefined()) {
115             if (sorting) {
116                 viewer.setComparator(fViewerComparator);
117             } else {
118                 viewer.setComparator(fDefaultComparator);
119             }
120         }
121     }
122     
123     /**
124      * Used for restoral after temporary removal. Uses listeners cached.
125      */

126     public void addAllSelectionChangedListeners() {
127         // Re-add the tree listener added by our parent for our parent:
128
// org.eclipse.ui.views.contentoutline.ContentOutlinePage
129
if (isViewerDefined()) {
130             viewer.addSelectionChangedListener(this);
131         }
132         // Add all current listeners
133
for (int i = 0; i < fListenerList.size(); i++) {
134             super.addSelectionChangedListener(
135                     (ISelectionChangedListener)fListenerList.get(i));
136         }
137     }
138     
139     /**
140      * @return
141      */

142     private boolean isViewerDefined() {
143         if (viewer == null) {
144             return false;
145         } else if (viewer.getTree().isDisposed()) {
146             return false;
147         }
148         return true;
149     }
150     
151     /**
152      * Used for temporary removal. Listeners cached.
153      */

154     public void removeAllSelectionChangedListeners() {
155         // Remove the tree listener added by our parent for our parent:
156
// org.eclipse.ui.views.contentoutline.ContentOutlinePage
157
if (isViewerDefined()) {
158             viewer.removeSelectionChangedListener(this);
159         }
160         // Remove all current listeners
161
for (int i = 0; i < fListenerList.size(); i++) {
162             super.removeSelectionChangedListener(
163                     (ISelectionChangedListener)fListenerList.get(i));
164         }
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
169      */

170     public void addSelectionChangedListener(ISelectionChangedListener listener) {
171         // Add the listener to our private list
172
fListenerList.add(listener);
173         super.addSelectionChangedListener(listener);
174     }
175     
176     /* (non-Javadoc)
177      * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
178      */

179     public void removeSelectionChangedListener(
180             ISelectionChangedListener listener) {
181         // Remove the listener from our private list
182
fListenerList.remove(listener);
183         super.removeSelectionChangedListener(listener);
184     }
185 }
186
Popular Tags