KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > AntTargetContentProvider


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.launchConfigurations;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.ant.core.TargetInfo;
20 import org.eclipse.jface.viewers.IStructuredContentProvider;
21 import org.eclipse.jface.viewers.TableViewer;
22 import org.eclipse.jface.viewers.Viewer;
23
24 public class AntTargetContentProvider implements IStructuredContentProvider {
25
26     protected List JavaDoc elements = new ArrayList JavaDoc();
27     protected TableViewer viewer;
28     private boolean fFilterInternalTargets= false;
29     private int fNumFilteredTargets= 0;
30     private int fNumTotalTargets= 0;
31
32     public void add(Object JavaDoc o) {
33         elements.add(o);
34         viewer.add(o);
35     }
36     
37     public void addAll(List JavaDoc list) {
38         elements.addAll(list);
39         viewer.add(list.toArray());
40     }
41
42     public void dispose() {
43     }
44
45     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
46         fNumTotalTargets= elements.size();
47         fNumFilteredTargets= 0;
48         if (fNumTotalTargets == 0) {
49             return new Object JavaDoc[0];
50         }
51         if (!fFilterInternalTargets) {
52             return elements.toArray(new Object JavaDoc[fNumTotalTargets]);
53         }
54         Iterator JavaDoc iter= elements.iterator();
55         while (iter.hasNext()) {
56             if (isInternal((TargetInfo) iter.next())) {
57                 fNumFilteredTargets++;
58             }
59         }
60         Object JavaDoc[] targets= new Object JavaDoc[getNumTargets()];
61         iter= elements.iterator();
62         int i= 0;
63         while (iter.hasNext()) {
64             TargetInfo target= (TargetInfo) iter.next();
65             if (!isInternal(target)) {
66                 targets[i++]= target;
67             }
68         }
69         return targets;
70     }
71     
72     /**
73      * Returns whether the given target is an internal target. Internal
74      * targets are targets which has no description. The default target
75      * is never considered internal.
76      * @param target the target to examine
77      * @return whether the given target is an internal target
78      */

79     public boolean isInternal(TargetInfo target) {
80         return !target.isDefault() && target.getDescription() == null;
81     }
82
83     public void inputChanged(Viewer newViewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
84         this.viewer = (TableViewer) newViewer;
85         elements.clear();
86         if (newInput != null && ((Object JavaDoc[]) newInput).length != 0) {
87             elements.addAll(Arrays.asList((Object JavaDoc[]) newInput));
88         }
89     }
90     
91     /**
92      * Removes the given target from the list of targets. Has no effect if the
93      * given target does not exist in the list.
94      *
95      * @param target the target to remove
96      */

97     public void removeTarget(Object JavaDoc target) {
98         elements.remove(target);
99         viewer.remove(target);
100     }
101
102     /**
103      * Moves the given target up in the list of active targets. Has no effect if
104      * the given target is already the first target in the list or the given
105      * index is invalid.
106      *
107      * @param index the index of the target to move up
108      */

109     public void moveUpTarget(int index) {
110         Object JavaDoc target = elements.get(index);
111         if (index == 0 || target == null) {
112             return;
113         }
114         elements.set(index, elements.get(index - 1));
115         elements.set(index - 1, target);
116     }
117
118     /**
119      * Moves the given target down in the list of active targets. Has no effect
120      * if the given target is already the last target in the list or the given
121      * index is invalid.
122      *
123      * @param index the index of the target to move down
124      */

125     public void moveDownTarget(int index) {
126         Object JavaDoc target = elements.get(index);
127         if (index == elements.size() - 1 || target == null) {
128             return;
129         }
130         elements.set(index, elements.get(index + 1));
131         elements.set(index + 1, target);
132     }
133     
134     /**
135      * Returns the number of targets filtered out of the list
136      * of targets last returned by this content provider
137      * @return the number of targets filtered out of the last request
138      * for targets
139      */

140     public int getNumFiltered() {
141         return fNumFilteredTargets;
142     }
143     
144     /**
145      * Returns the number of targets returned the last time this content
146      * provider was queried. If targets were filtered out, those targets are
147      * not included in this count.
148      * @return the number of targets returned the last time targets
149      * were requested
150      */

151     public int getNumTargets() {
152         return fNumTotalTargets - fNumFilteredTargets;
153     }
154     
155     /**
156      * Sets whether this content provider should filter out internal targets.
157      * Internal targets are targets which have no description. If set <code>true</code>,
158      * targets with no description will not be returned when getElements() is called.
159      * @param filter sets whether internal targets should be filtered out
160      */

161     public void setFilterInternalTargets(boolean filter) {
162         fFilterInternalTargets= filter;
163     }
164 }
165
Popular Tags