001: /*******************************************************************************
002: * Copyright (c) 2003, 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:
014: import org.eclipse.jface.viewers.ITreeContentProvider;
015: import org.eclipse.jface.viewers.Viewer;
016: import org.eclipse.ui.model.AdaptableList;
017:
018: /**
019: * Provider used by the NewWizardNewPage.
020: *
021: * @since 3.0
022: */
023: public class WizardContentProvider implements ITreeContentProvider {
024:
025: private AdaptableList input;
026:
027: /*
028: * (non-Javadoc)
029: *
030: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
031: */
032: public void dispose() {
033: input = null;
034: }
035:
036: /*
037: * (non-Javadoc)
038: *
039: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
040: */
041: public Object[] getChildren(Object parentElement) {
042: if (parentElement instanceof WizardCollectionElement) {
043: ArrayList list = new ArrayList();
044: WizardCollectionElement element = (WizardCollectionElement) parentElement;
045:
046: Object[] childCollections = element.getChildren();
047: for (int i = 0; i < childCollections.length; i++) {
048: handleChild(childCollections[i], list);
049: }
050:
051: Object[] childWizards = element.getWizards();
052: for (int i = 0; i < childWizards.length; i++) {
053: handleChild(childWizards[i], list);
054: }
055:
056: // flatten lists with only one category
057: if (list.size() == 1
058: && list.get(0) instanceof WizardCollectionElement) {
059: return getChildren(list.get(0));
060: }
061:
062: return list.toArray();
063: } else if (parentElement instanceof AdaptableList) {
064: AdaptableList aList = (AdaptableList) parentElement;
065: Object[] children = aList.getChildren();
066: ArrayList list = new ArrayList(children.length);
067: for (int i = 0; i < children.length; i++) {
068: handleChild(children[i], list);
069: }
070: // if there is only one category, return it's children directly (flatten list)
071: if (list.size() == 1
072: && list.get(0) instanceof WizardCollectionElement) {
073: return getChildren(list.get(0));
074: }
075:
076: return list.toArray();
077: } else {
078: return new Object[0];
079: }
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
086: */
087: public Object[] getElements(Object inputElement) {
088: return getChildren(inputElement);
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
095: */
096: public Object getParent(Object element) {
097: if (element instanceof WizardCollectionElement) {
098: Object[] children = input.getChildren();
099: for (int i = 0; i < children.length; i++) {
100: if (children[i].equals(element)) {
101: return input;
102: }
103: }
104: return ((WizardCollectionElement) element)
105: .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 element, ArrayList 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 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 oldInput,
148: Object newInput) {
149: input = (AdaptableList) newInput;
150: }
151: }
|