KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.pde.internal.ui.editor;
12 import java.util.ArrayList JavaDoc;
13
14 import org.eclipse.jface.viewers.AbstractTreeViewer;
15 import org.eclipse.jface.viewers.ILabelProvider;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.LabelProvider;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TreeViewer;
23 import org.eclipse.jface.viewers.ViewerComparator;
24 import org.eclipse.pde.core.IBaseModel;
25 import org.eclipse.pde.core.IModelChangeProvider;
26 import org.eclipse.pde.core.IModelChangedEvent;
27 import org.eclipse.pde.core.IModelChangedListener;
28 import org.eclipse.pde.internal.core.plugin.ImportObject;
29 import org.eclipse.pde.internal.core.text.IDocumentNode;
30 import org.eclipse.pde.internal.ui.PDEPlugin;
31 import org.eclipse.pde.internal.ui.PDEPluginImages;
32 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Tree;
38 import org.eclipse.ui.forms.editor.IFormPage;
39
40 public class FormOutlinePage extends PDEOutlinePage
41         implements IModelChangedListener, ISortableContentOutlinePage {
42     
43     private boolean fStale;
44     private ViewerComparator fViewerComparator;
45     private boolean fSorted;
46     
47     public class BasicContentProvider extends DefaultContentProvider
48             implements
49                 ITreeContentProvider {
50         public Object JavaDoc[] getElements(Object JavaDoc obj) {
51             return getPages();
52         }
53         public Object JavaDoc[] getChildren(Object JavaDoc obj) {
54             return FormOutlinePage.this.getChildren(obj);
55         }
56         public boolean hasChildren(Object JavaDoc obj) {
57             return getChildren(obj).length > 0;
58         }
59         public Object JavaDoc getParent(Object JavaDoc obj) {
60             return null;
61         }
62     }
63     public class BasicLabelProvider extends LabelProvider {
64         public String JavaDoc getText(Object JavaDoc obj) {
65             if (obj instanceof IFormPage)
66                 return ((IFormPage)obj).getTitle();
67             return PDEPlugin.getDefault().getLabelProvider().getText(obj);
68         }
69         public Image getImage(Object JavaDoc obj) {
70             if (obj instanceof IFormPage)
71                 return PDEPlugin.getDefault().getLabelProvider().get(PDEPluginImages.DESC_PAGE_OBJ);
72             return PDEPlugin.getDefault().getLabelProvider().getImage(obj);
73         }
74     }
75     public class BasicComparator extends ViewerComparator {
76         /* (non-Javadoc)
77          * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
78          */

79         public int category(Object JavaDoc element) {
80             Object JavaDoc[] pages = getPages();
81             for(int i=0; i<pages.length; i++){
82                 if(pages[i]==element){
83                     return i;
84                 }
85             }
86             return Integer.MAX_VALUE;
87         }
88     }
89     protected TreeViewer fTreeViewer;
90     protected boolean fEditorSelection = false;
91     protected boolean fOutlineSelection = false;
92     public FormOutlinePage(PDEFormEditor editor) {
93         super(editor);
94     }
95     
96     /**
97      * @return
98      */

99     public ITreeContentProvider createContentProvider() {
100         return new BasicContentProvider();
101     }
102     
103     /**
104      * @return
105      */

106     public ViewerComparator createOutlineSorter(){
107         return new BasicComparator();
108     }
109     
110     public void createControl(Composite parent) {
111         Tree widget = new Tree(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
112         fTreeViewer = new TreeViewer(widget);
113         fTreeViewer.addSelectionChangedListener(this);
114         fTreeViewer.setContentProvider(createContentProvider());
115         fTreeViewer.setLabelProvider(createLabelProvider());
116         fViewerComparator = createOutlineSorter();
117         if(fSorted)
118             fTreeViewer.setComparator(fViewerComparator);
119         else
120             fTreeViewer.setComparator(null);
121         fTreeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS);
122         fTreeViewer.setUseHashlookup(true);
123         fTreeViewer.setInput(fEditor);
124         IBaseModel model = fEditor.getAggregateModel();
125         if (model instanceof IModelChangeProvider)
126             ((IModelChangeProvider)model).addModelChangedListener(this);
127     }
128     
129     /**
130      * @return
131      */

132     public ILabelProvider createLabelProvider() {
133         return new BasicLabelProvider();
134     }
135     
136     public void dispose() {
137         IBaseModel model = fEditor.getAggregateModel();
138         if (model instanceof IModelChangeProvider)
139             ((IModelChangeProvider)model).removeModelChangedListener(this);
140         super.dispose();
141     }
142     
143     public Control getControl() {
144         return fTreeViewer != null ? fTreeViewer.getControl() : null;
145     }
146     private Object JavaDoc[] getPages() {
147         ArrayList JavaDoc formPages = new ArrayList JavaDoc();
148         IFormPage [] pages = fEditor.getPages();
149         for (int i=0; i<pages.length; i++) {
150             if (pages[i].isEditor()==false)
151                 formPages.add(pages[i]);
152         }
153         return formPages.toArray();
154     }
155
156     public void modelChanged(IModelChangedEvent event) {
157         IFormPage page = fEditor.getActivePageInstance();
158         fStale=true;
159         if (page.isEditor()==false)
160             refresh();
161     }
162     
163     public void refresh() {
164         if (fStale) {
165             final Control control = getControl();
166             if (control == null || control.isDisposed())
167                 return;
168             control.getDisplay().asyncExec(new Runnable JavaDoc() {
169                 public void run() {
170                     if (!fTreeViewer.getControl().isDisposed()) {
171                         fTreeViewer.refresh();
172                         fTreeViewer.expandAll();
173                         fStale=false;
174                     }
175                 }
176             });
177         }
178     }
179     
180     protected String JavaDoc getParentPageId(Object JavaDoc item) {
181         if (item instanceof IFormPage)
182             return ((IFormPage)item).getId();
183         return null;
184     }
185     
186     protected Object JavaDoc[] getChildren(Object JavaDoc parent) {
187         return new Object JavaDoc[0];
188     }
189
190     public void selectionChanged(Object JavaDoc item) {
191         IFormPage page = fEditor.getActivePageInstance();
192         String JavaDoc id = getParentPageId(item);
193         IFormPage newPage=null;
194         if (id!=null && (page==null || !page.getId().equals(id)))
195             newPage = fEditor.setActivePage(id);
196         IFormPage revealPage = newPage!=null?newPage:page;
197         if (revealPage!=null && !(item instanceof IFormPage))
198             revealPage.selectReveal(item);
199     }
200     
201     public void selectionChanged(SelectionChangedEvent event) {
202         if (fEditorSelection)
203             return;
204         fOutlineSelection = true;
205         try {
206             ISelection selection = event.getSelection();
207             if (selection.isEmpty() == false
208                     && selection instanceof IStructuredSelection) {
209                 IStructuredSelection ssel = (IStructuredSelection) selection;
210                 Object JavaDoc item = ssel.getFirstElement();
211                 selectionChanged(item);
212             }
213             fireSelectionChanged(selection);
214         } finally {
215             fOutlineSelection = false;
216         }
217     }
218     public void setFocus() {
219         if (fTreeViewer != null)
220             fTreeViewer.getTree().setFocus();
221     }
222     public ISelection getSelection() {
223         if (fTreeViewer == null)
224             return StructuredSelection.EMPTY;
225         return fTreeViewer.getSelection();
226     }
227     public void sort (boolean sorting){
228         fSorted = sorting;
229         if(fTreeViewer!=null)
230             if(sorting)
231                 fTreeViewer.setComparator(fViewerComparator);
232             else
233                 fTreeViewer.setComparator(null);
234     }
235     /*
236      * (non-Javadoc) Method declared on ISelectionProvider.
237      */

238     public void setSelection(ISelection selection) {
239         if (fOutlineSelection)
240             return;
241         fEditorSelection = true;
242         try {
243             if (fTreeViewer == null)
244                 return;
245             if (selection != null && !selection.isEmpty()
246                     && selection instanceof IStructuredSelection) {
247                 Object JavaDoc item = ((IStructuredSelection) selection)
248                         .getFirstElement();
249                 if (item instanceof ImportObject) {
250                     selection = new StructuredSelection(((ImportObject) item)
251                             .getImport());
252                 }
253                 if (item instanceof IDocumentNode) {
254                     while (null == fTreeViewer.testFindItem(item)) {
255                         item = ((IDocumentNode) item).getParentNode();
256                         if (item == null) {
257                             break;
258                         }
259                         selection = new StructuredSelection(item);
260                     }
261                 }
262             }
263             fTreeViewer.setSelection(selection);
264         } finally {
265             fEditorSelection = false;
266         }
267     }
268     
269     protected TreeViewer getTreeViewer() {
270         return fTreeViewer;
271     }
272 }
273
Popular Tags