KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ws > ActivityCategoryContentProvider


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.activities.ws;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.jface.viewers.ITreeContentProvider;
19 import org.eclipse.jface.viewers.Viewer;
20 import org.eclipse.ui.activities.IActivity;
21 import org.eclipse.ui.activities.IActivityManager;
22 import org.eclipse.ui.activities.IActivityRequirementBinding;
23 import org.eclipse.ui.activities.ICategory;
24 import org.eclipse.ui.activities.ICategoryActivityBinding;
25
26 /**
27  * Tree provider that provides <code>ICategory</code> objects in an
28  * <code>IActivityManager</code> at the top level, with <code>IActivity</code>
29  * objects as second level children under the <code>ICategory</code>.
30  * <p>
31  * Note that the <code>IActivity</code> objects are not instances of
32  * <code>org.eclipse.ui.internal.activities.Activity</code>, but rather proxies
33  * that also have a pointer to the <code>ICategory</code> for which the
34  * <code>IActivity</code> should be represented under.
35  *
36  * @since 3.0
37  */

38 public class ActivityCategoryContentProvider implements ITreeContentProvider {
39
40     /**
41      * The manager to extract content from.
42      */

43     private IActivityManager manager;
44
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
47      */

48     public void dispose() {
49         manager = null;
50     }
51
52     /**
53      * @param category the category to fetch.
54      * @return all activities in the category.
55      */

56     private IActivity[] getCategoryActivities(ICategory category) {
57         Set JavaDoc activityBindings = category.getCategoryActivityBindings();
58         List JavaDoc categoryActivities = new ArrayList JavaDoc(activityBindings.size());
59         for (Iterator JavaDoc j = activityBindings.iterator(); j.hasNext();) {
60             ICategoryActivityBinding binding = (ICategoryActivityBinding) j
61                     .next();
62             String JavaDoc activityId = binding.getActivityId();
63             IActivity activity = manager.getActivity(activityId);
64             if (activity.isDefined()) {
65                 categoryActivities.add(new CategorizedActivity(category,
66                         activity));
67             }
68         }
69         return (IActivity[]) categoryActivities
70                 .toArray(new IActivity[categoryActivities.size()]);
71     }
72
73     /**
74      * Get the duplicate activities found in the other defined categories.
75      *
76      * @param categorizedActivity
77      * The categorized activity.
78      * @return the list of duplicate categorized activities.
79      */

80     public Object JavaDoc[] getDuplicateCategoryActivities(
81             CategorizedActivity categorizedActivity) {
82         ArrayList JavaDoc duplicateCategorizedactivities = new ArrayList JavaDoc();
83         Set JavaDoc categoryIds = manager.getDefinedCategoryIds();
84         ICategory currentCategory = null;
85         String JavaDoc currentActivityId = null;
86         IActivity[] categoryActivities = null;
87         String JavaDoc currentCategoryId = null;
88         // find the duplicate activities in the defined categories
89
for (Iterator JavaDoc i = categoryIds.iterator(); i.hasNext();) {
90             currentCategoryId = (String JavaDoc) i.next();
91             if (!currentCategoryId.equals(categorizedActivity.getCategory()
92                     .getId())) {
93                 currentCategory = manager.getCategory(currentCategoryId);
94                 categoryActivities = getCategoryActivities(currentCategory);
95                 // traverse the category's activities to find a duplicate
96
for (int index = 0; index < categoryActivities.length; index++) {
97                     currentActivityId = categoryActivities[index].getId();
98                     if (currentActivityId.equals(categorizedActivity
99                             .getActivity().getId())) {
100                         duplicateCategorizedactivities
101                                 .add(new CategorizedActivity(currentCategory,
102                                         manager.getActivity(currentActivityId)));
103                         // Assuming only one unique activity per category -
104
// break
105
break;
106                     }
107                 }
108
109             }
110
111         }
112         return duplicateCategorizedactivities.toArray();
113     }
114
115     /**
116      * Get the child required activities.
117      *
118      * @param activityId
119      * The parent activity id.
120      * @return the list of child required activities.
121      */

122     public Object JavaDoc[] getChildRequiredActivities(String JavaDoc activityId) {
123         ArrayList JavaDoc childRequiredActivities = new ArrayList JavaDoc();
124         IActivity activity = manager.getActivity(activityId);
125         Set JavaDoc actvitiyRequirementBindings = activity
126                 .getActivityRequirementBindings();
127         String JavaDoc requiredActivityId = null;
128         IActivityRequirementBinding currentActivityRequirementBinding = null;
129         Object JavaDoc[] currentCategoryIds = null;
130         for (Iterator JavaDoc i = actvitiyRequirementBindings.iterator(); i.hasNext();) {
131             currentActivityRequirementBinding = (IActivityRequirementBinding) i
132                     .next();
133             requiredActivityId = currentActivityRequirementBinding
134                     .getRequiredActivityId();
135             currentCategoryIds = getActivityCategories(requiredActivityId);
136             for (int index = 0; index < currentCategoryIds.length; index++) {
137                 childRequiredActivities.add(new CategorizedActivity(manager
138                         .getCategory((String JavaDoc) currentCategoryIds[index]),
139                         manager.getActivity(requiredActivityId)));
140             }
141
142         }
143
144         return childRequiredActivities.toArray();
145     }
146
147     /**
148      * Get the parent required activities.
149      *
150      * @param activityId
151      * The child activity id.
152      * @return the list of parent required activities.
153      */

154     public Object JavaDoc[] getParentRequiredActivities(String JavaDoc activityId) {
155         ArrayList JavaDoc parentRequiredActivities = new ArrayList JavaDoc();
156         Set JavaDoc definedActivities = manager.getDefinedActivityIds();
157         String JavaDoc currentActivityId = null;
158         Set JavaDoc activityRequirementBindings = null;
159         IActivityRequirementBinding currentActivityRequirementBinding = null;
160         Object JavaDoc[] currentCategoryIds = null;
161         for (Iterator JavaDoc i = definedActivities.iterator(); i.hasNext();) {
162             currentActivityId = (String JavaDoc) i.next();
163             activityRequirementBindings = manager
164                     .getActivity(currentActivityId)
165                     .getActivityRequirementBindings();
166             for (Iterator JavaDoc j = activityRequirementBindings.iterator(); j
167                     .hasNext();) {
168                 currentActivityRequirementBinding = (IActivityRequirementBinding) j
169                         .next();
170                 if (currentActivityRequirementBinding.getRequiredActivityId()
171                         .equals(activityId)) {
172                     // We found one - add it to the list
173
currentCategoryIds = getActivityCategories(currentActivityId);
174                     for (int index = 0; index < currentCategoryIds.length; index++) {
175                         parentRequiredActivities
176                                 .add(new CategorizedActivity(
177                                         manager
178                                                 .getCategory((String JavaDoc) currentCategoryIds[index]),
179                                         manager.getActivity(currentActivityId)));
180                     }
181                 }
182             }
183
184         }
185         return parentRequiredActivities.toArray();
186     }
187
188     /**
189      * Get the activity's categories (there could be more than one).
190      *
191      * @param activityId
192      * The activity id.
193      * @return the activity's categories.
194      */

195     private Object JavaDoc[] getActivityCategories(String JavaDoc activityId) {
196         ArrayList JavaDoc activityCategories = new ArrayList JavaDoc();
197         Set JavaDoc categoryIds = manager.getDefinedCategoryIds();
198         String JavaDoc currentCategoryId = null;
199         IActivity[] categoryActivities = null;
200         for (Iterator JavaDoc i = categoryIds.iterator(); i.hasNext();) {
201             currentCategoryId = (String JavaDoc) i.next();
202             categoryActivities = getCategoryActivities(manager
203                     .getCategory(currentCategoryId));
204             for (int index = 0; index < categoryActivities.length; index++) {
205                 if (categoryActivities[index].getId().equals(activityId)) {
206                     activityCategories.add(currentCategoryId);
207                     break;
208                 }
209             }
210         }
211         return activityCategories.toArray();
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
216      */

217     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
218         if (parentElement instanceof IActivityManager) {
219             Set JavaDoc categoryIds = manager.getDefinedCategoryIds();
220             ArrayList JavaDoc categories = new ArrayList JavaDoc(categoryIds.size());
221             for (Iterator JavaDoc i = categoryIds.iterator(); i.hasNext();) {
222                 String JavaDoc categoryId = (String JavaDoc) i.next();
223                 ICategory category = manager.getCategory(categoryId);
224                 if (getCategoryActivities(category).length > 0) {
225                     categories.add(category);
226                 }
227             }
228             return categories.toArray();
229         } else if (parentElement instanceof ICategory) {
230             return getCategoryActivities((ICategory) parentElement);
231         }
232         return new Object JavaDoc[0];
233     }
234
235     /* (non-Javadoc)
236      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
237      */

238     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
239         return getChildren(inputElement);
240     }
241
242     /* (non-Javadoc)
243      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
244      */

245     public Object JavaDoc getParent(Object JavaDoc element) {
246         if (element instanceof CategorizedActivity) {
247             return ((CategorizedActivity) element).getCategory();
248         }
249         return null;
250     }
251
252     /* (non-Javadoc)
253      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
254      */

255     public boolean hasChildren(Object JavaDoc element) {
256         if (element instanceof IActivityManager || element instanceof ICategory) {
257             return true;
258         }
259         return false;
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
264      */

265     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
266         manager = (IActivityManager) newInput;
267     }
268 }
269
Popular Tags