KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > ViewContentProvider


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.ui.internal.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.ui.activities.WorkbenchActivityHelper;
21 import org.eclipse.ui.internal.intro.IIntroConstants;
22 import org.eclipse.ui.views.IViewCategory;
23 import org.eclipse.ui.views.IViewDescriptor;
24 import org.eclipse.ui.views.IViewRegistry;
25
26 /**
27  * Provides content for viewers that wish to show Views.
28  */

29 public class ViewContentProvider implements ITreeContentProvider {
30
31     /**
32      * Child cache. Map from Object->Object[]. Our hasChildren() method is
33      * expensive so it's better to cache the results of getChildren().
34      */

35     private Map JavaDoc childMap = new HashMap JavaDoc();
36
37     /**
38      * Create a new instance of the ViewContentProvider.
39      */

40     public ViewContentProvider() {
41         //no-op
42
}
43
44     /*
45      * (non-Javadoc)
46      *
47      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
48      */

49     public void dispose() {
50         childMap.clear();
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
57      */

58     public Object JavaDoc[] getChildren(Object JavaDoc element) {
59         Object JavaDoc[] children = (Object JavaDoc[]) childMap.get(element);
60         if (children == null) {
61             children = createChildren(element);
62             childMap.put(element, children);
63         }
64         return children;
65     }
66
67     /**
68      * Does the actual work of getChildren.
69      *
70      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
71      */

72     private Object JavaDoc[] createChildren(Object JavaDoc element) {
73         if (element instanceof IViewRegistry) {
74             IViewRegistry reg = (IViewRegistry) element;
75             IViewCategory [] categories = reg.getCategories();
76
77             ArrayList JavaDoc filtered = new ArrayList JavaDoc();
78             for (int i = 0; i < categories.length; i++) {
79                 if (!hasChildren(categories[i])) {
80                     continue;
81                 }
82
83                 filtered.add(categories[i]);
84             }
85             categories = (IViewCategory[]) filtered.toArray(new IViewCategory[filtered
86                     .size()]);
87
88             // if there is only one category, return it's children directly
89
if (categories.length == 1) {
90                 return getChildren(categories[0]);
91             }
92             return categories;
93         } else if (element instanceof IViewCategory) {
94             IViewDescriptor [] views = ((IViewCategory) element).getViews();
95             if (views != null) {
96                 ArrayList JavaDoc filtered = new ArrayList JavaDoc();
97                 for (int i = 0; i < views.length; i++) {
98                     Object JavaDoc o = views[i];
99                     if (WorkbenchActivityHelper.filterItem(o)) {
100                         continue;
101                     }
102                     filtered.add(o);
103                 }
104                 return removeIntroView(filtered).toArray();
105             }
106         }
107
108         return new Object JavaDoc[0];
109     }
110
111     /**
112      * Removes the temporary intro view from the list so that it cannot be activated except through
113      * the introduction command.
114      *
115      * @param list the list of view descriptors
116      * @return the modified list.
117      * @since 3.0
118      */

119     private ArrayList JavaDoc removeIntroView(ArrayList JavaDoc list) {
120         for (Iterator JavaDoc i = list.iterator(); i.hasNext();) {
121             IViewDescriptor view = (IViewDescriptor) i.next();
122             if (view.getId().equals(IIntroConstants.INTRO_VIEW_ID)) {
123                 i.remove();
124             }
125         }
126         return list;
127     }
128
129     /*
130      * (non-Javadoc)
131      *
132      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
133      */

134     public Object JavaDoc[] getElements(Object JavaDoc element) {
135         return getChildren(element);
136     }
137
138     /*
139      * (non-Javadoc)
140      *
141      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
142      */

143     public Object JavaDoc getParent(Object JavaDoc element) {
144         return null;
145     }
146
147     /*
148      * (non-Javadoc)
149      *
150      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
151      */

152     public boolean hasChildren(java.lang.Object JavaDoc element) {
153         if (element instanceof IViewRegistry) {
154             return true;
155         } else if (element instanceof IViewCategory) {
156             if (getChildren(element).length > 0) {
157                 return true;
158             }
159         }
160         return false;
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
167      * java.lang.Object, java.lang.Object)
168      */

169     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
170         childMap.clear();
171     }
172 }
173
Popular Tags