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.ui.internal.dialogs;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.Map;
016:
017: import org.eclipse.jface.viewers.ITreeContentProvider;
018: import org.eclipse.jface.viewers.Viewer;
019: import org.eclipse.ui.activities.WorkbenchActivityHelper;
020: import org.eclipse.ui.internal.intro.IIntroConstants;
021: import org.eclipse.ui.views.IViewCategory;
022: import org.eclipse.ui.views.IViewDescriptor;
023: import org.eclipse.ui.views.IViewRegistry;
024:
025: /**
026: * Provides content for viewers that wish to show Views.
027: */
028: public class ViewContentProvider implements ITreeContentProvider {
029:
030: /**
031: * Child cache. Map from Object->Object[]. Our hasChildren() method is
032: * expensive so it's better to cache the results of getChildren().
033: */
034: private Map childMap = new HashMap();
035:
036: /**
037: * Create a new instance of the ViewContentProvider.
038: */
039: public ViewContentProvider() {
040: //no-op
041: }
042:
043: /*
044: * (non-Javadoc)
045: *
046: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
047: */
048: public void dispose() {
049: childMap.clear();
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
056: */
057: public Object[] getChildren(Object element) {
058: Object[] children = (Object[]) childMap.get(element);
059: if (children == null) {
060: children = createChildren(element);
061: childMap.put(element, children);
062: }
063: return children;
064: }
065:
066: /**
067: * Does the actual work of getChildren.
068: *
069: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
070: */
071: private Object[] createChildren(Object element) {
072: if (element instanceof IViewRegistry) {
073: IViewRegistry reg = (IViewRegistry) element;
074: IViewCategory[] categories = reg.getCategories();
075:
076: ArrayList filtered = new ArrayList();
077: for (int i = 0; i < categories.length; i++) {
078: if (!hasChildren(categories[i])) {
079: continue;
080: }
081:
082: filtered.add(categories[i]);
083: }
084: categories = (IViewCategory[]) filtered
085: .toArray(new IViewCategory[filtered.size()]);
086:
087: // if there is only one category, return it's children directly
088: if (categories.length == 1) {
089: return getChildren(categories[0]);
090: }
091: return categories;
092: } else if (element instanceof IViewCategory) {
093: IViewDescriptor[] views = ((IViewCategory) element)
094: .getViews();
095: if (views != null) {
096: ArrayList filtered = new ArrayList();
097: for (int i = 0; i < views.length; i++) {
098: Object o = views[i];
099: if (WorkbenchActivityHelper.filterItem(o)) {
100: continue;
101: }
102: filtered.add(o);
103: }
104: return removeIntroView(filtered).toArray();
105: }
106: }
107:
108: return new Object[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 removeIntroView(ArrayList list) {
120: for (Iterator 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[] getElements(Object 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 getParent(Object 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 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 oldInput,
170: Object newInput) {
171: childMap.clear();
172: }
173: }
|