KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.ui.model.AdaptableList;
18
19 /**
20  * Provider used by the NewWizardNewPage.
21  *
22  * @since 3.0
23  */

24 public class WizardContentProvider implements ITreeContentProvider {
25
26     private AdaptableList input;
27
28     /*
29      * (non-Javadoc)
30      *
31      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
32      */

33     public void dispose() {
34         input = null;
35     }
36
37     /*
38      * (non-Javadoc)
39      *
40      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
41      */

42     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
43         if (parentElement instanceof WizardCollectionElement) {
44             ArrayList JavaDoc list = new ArrayList JavaDoc();
45             WizardCollectionElement element = (WizardCollectionElement) parentElement;
46
47             Object JavaDoc[] childCollections = element.getChildren();
48             for (int i = 0; i < childCollections.length; i++) {
49                 handleChild(childCollections[i], list);
50             }
51
52             Object JavaDoc[] childWizards = element.getWizards();
53             for (int i = 0; i < childWizards.length; i++) {
54                 handleChild(childWizards[i], list);
55             }
56
57             // flatten lists with only one category
58
if (list.size() == 1
59                     && list.get(0) instanceof WizardCollectionElement) {
60                 return getChildren(list.get(0));
61             }
62
63             return list.toArray();
64         } else if (parentElement instanceof AdaptableList) {
65             AdaptableList aList = (AdaptableList) parentElement;
66             Object JavaDoc[] children = aList.getChildren();
67             ArrayList JavaDoc list = new ArrayList JavaDoc(children.length);
68             for (int i = 0; i < children.length; i++) {
69                 handleChild(children[i], list);
70             }
71             // if there is only one category, return it's children directly (flatten list)
72
if (list.size() == 1
73                     && list.get(0) instanceof WizardCollectionElement) {
74                 return getChildren(list.get(0));
75             }
76                 
77             return list.toArray();
78         } else {
79             return new Object JavaDoc[0];
80         }
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
87      */

88     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
89         return getChildren(inputElement);
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
96      */

97     public Object JavaDoc getParent(Object JavaDoc element) {
98         if (element instanceof WizardCollectionElement) {
99             Object JavaDoc[] children = input.getChildren();
100             for (int i = 0; i < children.length; i++) {
101                 if (children[i].equals(element)) {
102                     return input;
103                 }
104             }
105             return ((WizardCollectionElement) element).getParent(element);
106         }
107         return null;
108     }
109
110     /**
111      * Adds the item to the list, unless it's a collection element without any children.
112      *
113      * @param element the element to test and add
114      * @param list the <code>Collection</code> to add to.
115      * @since 3.0
116      */

117     private void handleChild(Object JavaDoc element, ArrayList JavaDoc list) {
118         if (element instanceof WizardCollectionElement) {
119             if (hasChildren(element)) {
120                 list.add(element);
121             }
122         } else {
123             list.add(element);
124         }
125     }
126
127     /*
128      * (non-Javadoc)
129      *
130      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
131      */

132     public boolean hasChildren(Object JavaDoc element) {
133         if (element instanceof WizardCollectionElement) {
134             if (getChildren(element).length > 0) {
135                 return true;
136             }
137         }
138         return false;
139     }
140
141     /*
142      * (non-Javadoc)
143      *
144      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
145      * java.lang.Object, java.lang.Object)
146      */

147     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
148         input = (AdaptableList) newInput;
149     }
150 }
151
Popular Tags