KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > AntProjectContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.views;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.ant.internal.ui.views.elements.AntNode;
18 import org.eclipse.ant.internal.ui.views.elements.ProjectNode;
19 import org.eclipse.ant.internal.ui.views.elements.RootNode;
20 import org.eclipse.ant.internal.ui.views.elements.TargetNode;
21 import org.eclipse.jface.viewers.ITreeContentProvider;
22 import org.eclipse.jface.viewers.Viewer;
23
24 /**
25  * Content provider that provides a tree of ant projects.
26  */

27 public class AntProjectContentProvider implements ITreeContentProvider {
28
29     /**
30      * The root node of the project tree
31      */

32     private RootNode rootNode;
33     private boolean fIsFilteringInternalTargets= false;
34
35     /**
36      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
37      */

38     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
39         return getChildren(inputElement);
40     }
41
42     /**
43      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
44      */

45     public void dispose() {
46     }
47
48     /**
49      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
50      */

51     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
52         if (newInput instanceof RootNode) {
53             rootNode= (RootNode) newInput;
54         }
55     }
56     
57     /**
58      * Adds the project to the tree. Has no effect if a project with the same buildfile name
59      * is already present in the tree.
60      *
61      * @param project The project to add
62      */

63     public void addProject(ProjectNode project) {
64         ProjectNode[] projects= getRootNode().getProjects();
65         for (int i = 0; i < projects.length; i++) {
66             ProjectNode node = projects[i];
67             if (node.getBuildFileName().equals(project.getBuildFileName())) {
68                 return;
69             }
70         }
71         getRootNode().addProject(project);
72     }
73     
74     /**
75      * Returns the root node of the tree or <code>null</code> if no root has
76      * been set as the input
77      *
78      * @return RootNode the root node of the tree
79      */

80     public RootNode getRootNode() {
81         return rootNode;
82     }
83     
84     /**
85      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(Object)
86      */

87     public Object JavaDoc[] getChildren(Object JavaDoc element) {
88         if (element instanceof RootNode) {
89             return ((RootNode) element).getProjects();
90         } else if (element instanceof ProjectNode) {
91             if (!fIsFilteringInternalTargets) {
92                 return ((ProjectNode) element).getTargets();
93             }
94             TargetNode[] targets= ((ProjectNode) element).getTargets();
95             List JavaDoc filteredTargets= new ArrayList JavaDoc();
96             for (int i = 0; i < targets.length; i++) {
97                 TargetNode node = targets[i];
98                 if (!isInternal(node)) {
99                     filteredTargets.add(node);
100                 }
101             }
102             return filteredTargets.toArray();
103         }
104         return null;
105     }
106
107     /**
108      * Returns whether the given target is an internal target. Internal
109      * targets are targets which has no description. The default target
110      * is never considered internal.
111      * @param target the target to examine
112      * @return whether the given target is an internal target
113      */

114     private boolean isInternal(TargetNode target) {
115         return target != target.getProject().getDefaultTarget() && target.getDescription() == null;
116     }
117     
118     /**
119      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(Object)
120      */

121     public Object JavaDoc getParent(Object JavaDoc element) {
122         if (element instanceof AntNode) {
123             return ((AntNode) element).getParent();
124         }
125         return null;
126     }
127     
128     /**
129      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(Object)
130      */

131     public boolean hasChildren(Object JavaDoc element) {
132         if (element instanceof TargetNode) {
133             return false;
134         }
135         return true;
136     }
137     
138     public boolean isFilterInternalTargets() {
139         return fIsFilteringInternalTargets;
140     }
141
142     /**
143      * @param filter
144      */

145     public void setFilterInternalTargets(boolean filter) {
146         fIsFilteringInternalTargets= filter;
147     }
148
149 }
150
Popular Tags