001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.viewers.AbstractTreeViewer;
015: import org.eclipse.jface.viewers.ILabelProvider;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.jface.viewers.ITreeContentProvider;
019: import org.eclipse.jface.viewers.LabelProvider;
020: import org.eclipse.jface.viewers.SelectionChangedEvent;
021: import org.eclipse.jface.viewers.StructuredSelection;
022: import org.eclipse.jface.viewers.TreeViewer;
023: import org.eclipse.jface.viewers.ViewerComparator;
024: import org.eclipse.pde.core.IBaseModel;
025: import org.eclipse.pde.core.IModelChangeProvider;
026: import org.eclipse.pde.core.IModelChangedEvent;
027: import org.eclipse.pde.core.IModelChangedListener;
028: import org.eclipse.pde.internal.core.plugin.ImportObject;
029: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
030: import org.eclipse.pde.internal.ui.PDEPlugin;
031: import org.eclipse.pde.internal.ui.PDEPluginImages;
032: import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
033: import org.eclipse.swt.SWT;
034: import org.eclipse.swt.graphics.Image;
035: import org.eclipse.swt.widgets.Composite;
036: import org.eclipse.swt.widgets.Control;
037: import org.eclipse.swt.widgets.Tree;
038: import org.eclipse.ui.forms.editor.IFormPage;
039:
040: public class FormOutlinePage extends PDEOutlinePage implements
041: IModelChangedListener, ISortableContentOutlinePage {
042:
043: private boolean fStale;
044: private ViewerComparator fViewerComparator;
045: private boolean fSorted;
046:
047: public class BasicContentProvider extends DefaultContentProvider
048: implements ITreeContentProvider {
049: public Object[] getElements(Object obj) {
050: return getPages();
051: }
052:
053: public Object[] getChildren(Object obj) {
054: return FormOutlinePage.this .getChildren(obj);
055: }
056:
057: public boolean hasChildren(Object obj) {
058: return getChildren(obj).length > 0;
059: }
060:
061: public Object getParent(Object obj) {
062: return null;
063: }
064: }
065:
066: public class BasicLabelProvider extends LabelProvider {
067: public String getText(Object obj) {
068: if (obj instanceof IFormPage)
069: return ((IFormPage) obj).getTitle();
070: return PDEPlugin.getDefault().getLabelProvider().getText(
071: obj);
072: }
073:
074: public Image getImage(Object obj) {
075: if (obj instanceof IFormPage)
076: return PDEPlugin.getDefault().getLabelProvider().get(
077: PDEPluginImages.DESC_PAGE_OBJ);
078: return PDEPlugin.getDefault().getLabelProvider().getImage(
079: obj);
080: }
081: }
082:
083: public class BasicComparator extends ViewerComparator {
084: /* (non-Javadoc)
085: * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
086: */
087: public int category(Object element) {
088: Object[] pages = getPages();
089: for (int i = 0; i < pages.length; i++) {
090: if (pages[i] == element) {
091: return i;
092: }
093: }
094: return Integer.MAX_VALUE;
095: }
096: }
097:
098: protected TreeViewer fTreeViewer;
099: protected boolean fEditorSelection = false;
100: protected boolean fOutlineSelection = false;
101:
102: public FormOutlinePage(PDEFormEditor editor) {
103: super (editor);
104: }
105:
106: /**
107: * @return
108: */
109: public ITreeContentProvider createContentProvider() {
110: return new BasicContentProvider();
111: }
112:
113: /**
114: * @return
115: */
116: public ViewerComparator createOutlineSorter() {
117: return new BasicComparator();
118: }
119:
120: public void createControl(Composite parent) {
121: Tree widget = new Tree(parent, SWT.MULTI | SWT.H_SCROLL
122: | SWT.V_SCROLL);
123: fTreeViewer = new TreeViewer(widget);
124: fTreeViewer.addSelectionChangedListener(this );
125: fTreeViewer.setContentProvider(createContentProvider());
126: fTreeViewer.setLabelProvider(createLabelProvider());
127: fViewerComparator = createOutlineSorter();
128: if (fSorted)
129: fTreeViewer.setComparator(fViewerComparator);
130: else
131: fTreeViewer.setComparator(null);
132: fTreeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
133: fTreeViewer.setUseHashlookup(true);
134: fTreeViewer.setInput(fEditor);
135: IBaseModel model = fEditor.getAggregateModel();
136: if (model instanceof IModelChangeProvider)
137: ((IModelChangeProvider) model)
138: .addModelChangedListener(this );
139: }
140:
141: /**
142: * @return
143: */
144: public ILabelProvider createLabelProvider() {
145: return new BasicLabelProvider();
146: }
147:
148: public void dispose() {
149: IBaseModel model = fEditor.getAggregateModel();
150: if (model instanceof IModelChangeProvider)
151: ((IModelChangeProvider) model)
152: .removeModelChangedListener(this );
153: super .dispose();
154: }
155:
156: public Control getControl() {
157: return fTreeViewer != null ? fTreeViewer.getControl() : null;
158: }
159:
160: private Object[] getPages() {
161: ArrayList formPages = new ArrayList();
162: IFormPage[] pages = fEditor.getPages();
163: for (int i = 0; i < pages.length; i++) {
164: if (pages[i].isEditor() == false)
165: formPages.add(pages[i]);
166: }
167: return formPages.toArray();
168: }
169:
170: public void modelChanged(IModelChangedEvent event) {
171: IFormPage page = fEditor.getActivePageInstance();
172: fStale = true;
173: if (page.isEditor() == false)
174: refresh();
175: }
176:
177: public void refresh() {
178: if (fStale) {
179: final Control control = getControl();
180: if (control == null || control.isDisposed())
181: return;
182: control.getDisplay().asyncExec(new Runnable() {
183: public void run() {
184: if (!fTreeViewer.getControl().isDisposed()) {
185: fTreeViewer.refresh();
186: fTreeViewer.expandAll();
187: fStale = false;
188: }
189: }
190: });
191: }
192: }
193:
194: protected String getParentPageId(Object item) {
195: if (item instanceof IFormPage)
196: return ((IFormPage) item).getId();
197: return null;
198: }
199:
200: protected Object[] getChildren(Object parent) {
201: return new Object[0];
202: }
203:
204: public void selectionChanged(Object item) {
205: IFormPage page = fEditor.getActivePageInstance();
206: String id = getParentPageId(item);
207: IFormPage newPage = null;
208: if (id != null && (page == null || !page.getId().equals(id)))
209: newPage = fEditor.setActivePage(id);
210: IFormPage revealPage = newPage != null ? newPage : page;
211: if (revealPage != null && !(item instanceof IFormPage))
212: revealPage.selectReveal(item);
213: }
214:
215: public void selectionChanged(SelectionChangedEvent event) {
216: if (fEditorSelection)
217: return;
218: fOutlineSelection = true;
219: try {
220: ISelection selection = event.getSelection();
221: if (selection.isEmpty() == false
222: && selection instanceof IStructuredSelection) {
223: IStructuredSelection ssel = (IStructuredSelection) selection;
224: Object item = ssel.getFirstElement();
225: selectionChanged(item);
226: }
227: fireSelectionChanged(selection);
228: } finally {
229: fOutlineSelection = false;
230: }
231: }
232:
233: public void setFocus() {
234: if (fTreeViewer != null)
235: fTreeViewer.getTree().setFocus();
236: }
237:
238: public ISelection getSelection() {
239: if (fTreeViewer == null)
240: return StructuredSelection.EMPTY;
241: return fTreeViewer.getSelection();
242: }
243:
244: public void sort(boolean sorting) {
245: fSorted = sorting;
246: if (fTreeViewer != null)
247: if (sorting)
248: fTreeViewer.setComparator(fViewerComparator);
249: else
250: fTreeViewer.setComparator(null);
251: }
252:
253: /*
254: * (non-Javadoc) Method declared on ISelectionProvider.
255: */
256: public void setSelection(ISelection selection) {
257: if (fOutlineSelection)
258: return;
259: fEditorSelection = true;
260: try {
261: if (fTreeViewer == null)
262: return;
263: if (selection != null && !selection.isEmpty()
264: && selection instanceof IStructuredSelection) {
265: Object item = ((IStructuredSelection) selection)
266: .getFirstElement();
267: if (item instanceof ImportObject) {
268: selection = new StructuredSelection(
269: ((ImportObject) item).getImport());
270: }
271: if (item instanceof IDocumentElementNode) {
272: while (null == fTreeViewer.testFindItem(item)) {
273: item = ((IDocumentElementNode) item)
274: .getParentNode();
275: if (item == null) {
276: break;
277: }
278: selection = new StructuredSelection(item);
279: }
280: }
281: }
282: fTreeViewer.setSelection(selection);
283: } finally {
284: fEditorSelection = false;
285: }
286: }
287:
288: protected TreeViewer getTreeViewer() {
289: return fTreeViewer;
290: }
291: }
|