001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jface.text.IDocument;
015: import org.eclipse.jface.viewers.IBaseLabelProvider;
016: import org.eclipse.jface.viewers.IContentProvider;
017: import org.eclipse.jface.viewers.ISelectionChangedListener;
018: import org.eclipse.jface.viewers.TreeViewer;
019: import org.eclipse.jface.viewers.ViewerComparator;
020: import org.eclipse.pde.internal.core.text.IEditingModel;
021: import org.eclipse.pde.internal.core.text.IReconcilingParticipant;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Control;
024: import org.eclipse.ui.part.IPageSite;
025:
026: /**
027: * Content outline page for the XML editor.
028: */
029: public class SourceOutlinePage extends PDEOutlinePage implements
030: IReconcilingParticipant, ISortableContentOutlinePage {
031:
032: private IEditingModel fModel;
033: private IBaseLabelProvider fLabelProvider;
034: private IContentProvider fContentProvider;
035: private ViewerComparator fDefaultComparator;
036: private ViewerComparator fViewerComparator;
037: private boolean sorted;
038: TreeViewer viewer;
039:
040: /**
041: * This list is redundant; but, required because we can't access
042: * org.eclipse.ui.views.contentoutline.ContentOutlinePage.selectionChangedListeners
043: * from our parent
044: */
045: private ArrayList fListenerList;
046:
047: public SourceOutlinePage(IEditingModel model,
048: IBaseLabelProvider lProvider, IContentProvider cProvider,
049: ViewerComparator defaultComparator,
050: ViewerComparator comparator) {
051: super ();
052: fModel = model;
053: fLabelProvider = lProvider;
054: fContentProvider = cProvider;
055: fDefaultComparator = defaultComparator;
056: fViewerComparator = comparator;
057: fListenerList = new ArrayList();
058: }
059:
060: /**
061: * Creates the control for this outline page.
062: */
063: public void createControl(Composite parent) {
064: super .createControl(parent);
065: viewer = getTreeViewer();
066: viewer.setContentProvider(fContentProvider);
067: viewer.setLabelProvider(fLabelProvider);
068: if (sorted)
069: viewer.setComparator(fViewerComparator);
070: else
071: viewer.setComparator(fDefaultComparator);
072: viewer.setInput(fModel);
073: viewer.expandAll();
074: }
075:
076: /* (non-Javadoc)
077: * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#init(org.eclipse.ui.part.IPageSite)
078: */
079: public void init(IPageSite pageSite) {
080: super .init(pageSite);
081: }
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.pde.internal.ui.editor.text.IReconcilingParticipant#reconciled(org.eclipse.jface.text.IDocument)
085: */
086: public void reconciled(IDocument document) {
087: final Control control = getControl();
088: if (control == null || control.isDisposed())
089: return;
090: control.getDisplay().asyncExec(new Runnable() {
091: public void run() {
092: if (control.isDisposed())
093: return;
094: control.setRedraw(false);
095: // Temporarily remove all selection listeners from the tree
096: // viewer. This is required because the refresh fires a
097: // selection event back to the source page (observered in
098: // the bundle source page) when typing
099: removeAllSelectionChangedListeners();
100: getTreeViewer().refresh();
101: addAllSelectionChangedListeners();
102: getTreeViewer().expandAll();
103: control.setRedraw(true);
104: }
105: });
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.pde.internal.ui.editor.ISortableContentOutlinePage#sort(boolean)
110: */
111: public void sort(boolean sorting) {
112: sorted = sorting;
113: if (isViewerDefined()) {
114: if (sorting) {
115: viewer.setComparator(fViewerComparator);
116: } else {
117: viewer.setComparator(fDefaultComparator);
118: }
119: }
120: }
121:
122: /**
123: * Used for restoral after temporary removal. Uses listeners cached.
124: */
125: public void addAllSelectionChangedListeners() {
126: // Re-add the tree listener added by our parent for our parent:
127: // org.eclipse.ui.views.contentoutline.ContentOutlinePage
128: if (isViewerDefined()) {
129: viewer.addSelectionChangedListener(this );
130: }
131: // Add all current listeners
132: for (int i = 0; i < fListenerList.size(); i++) {
133: super
134: .addSelectionChangedListener((ISelectionChangedListener) fListenerList
135: .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
163: .removeSelectionChangedListener((ISelectionChangedListener) fListenerList
164: .get(i));
165: }
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
170: */
171: public void addSelectionChangedListener(
172: ISelectionChangedListener listener) {
173: // Add the listener to our private list
174: fListenerList.add(listener);
175: super .addSelectionChangedListener(listener);
176: }
177:
178: /* (non-Javadoc)
179: * @see org.eclipse.ui.views.contentoutline.ContentOutlinePage#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
180: */
181: public void removeSelectionChangedListener(
182: ISelectionChangedListener listener) {
183: // Remove the listener from our private list
184: fListenerList.remove(listener);
185: super.removeSelectionChangedListener(listener);
186: }
187: }
|