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.activities.ws;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Set;
016:
017: import org.eclipse.jface.viewers.ITreeContentProvider;
018: import org.eclipse.jface.viewers.Viewer;
019: import org.eclipse.ui.activities.IActivity;
020: import org.eclipse.ui.activities.IActivityManager;
021: import org.eclipse.ui.activities.IActivityRequirementBinding;
022: import org.eclipse.ui.activities.ICategory;
023: import org.eclipse.ui.activities.ICategoryActivityBinding;
024:
025: /**
026: * Tree provider that provides <code>ICategory</code> objects in an
027: * <code>IActivityManager</code> at the top level, with <code>IActivity</code>
028: * objects as second level children under the <code>ICategory</code>.
029: * <p>
030: * Note that the <code>IActivity</code> objects are not instances of
031: * <code>org.eclipse.ui.internal.activities.Activity</code>, but rather proxies
032: * that also have a pointer to the <code>ICategory</code> for which the
033: * <code>IActivity</code> should be represented under.
034: *
035: * @since 3.0
036: */
037: public class ActivityCategoryContentProvider implements
038: ITreeContentProvider {
039:
040: /**
041: * The manager to extract content from.
042: */
043: private IActivityManager manager;
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
047: */
048: public void dispose() {
049: manager = null;
050: }
051:
052: /**
053: * @param category the category to fetch.
054: * @return all activities in the category.
055: */
056: private IActivity[] getCategoryActivities(ICategory category) {
057: Set activityBindings = category.getCategoryActivityBindings();
058: List categoryActivities = new ArrayList(activityBindings.size());
059: for (Iterator j = activityBindings.iterator(); j.hasNext();) {
060: ICategoryActivityBinding binding = (ICategoryActivityBinding) j
061: .next();
062: String activityId = binding.getActivityId();
063: IActivity activity = manager.getActivity(activityId);
064: if (activity.isDefined()) {
065: categoryActivities.add(new CategorizedActivity(
066: category, activity));
067: }
068: }
069: return (IActivity[]) categoryActivities
070: .toArray(new IActivity[categoryActivities.size()]);
071: }
072:
073: /**
074: * Get the duplicate activities found in the other defined categories.
075: *
076: * @param categorizedActivity
077: * The categorized activity.
078: * @return the list of duplicate categorized activities.
079: */
080: public Object[] getDuplicateCategoryActivities(
081: CategorizedActivity categorizedActivity) {
082: ArrayList duplicateCategorizedactivities = new ArrayList();
083: Set categoryIds = manager.getDefinedCategoryIds();
084: ICategory currentCategory = null;
085: String currentActivityId = null;
086: IActivity[] categoryActivities = null;
087: String currentCategoryId = null;
088: // find the duplicate activities in the defined categories
089: for (Iterator i = categoryIds.iterator(); i.hasNext();) {
090: currentCategoryId = (String) i.next();
091: if (!currentCategoryId.equals(categorizedActivity
092: .getCategory().getId())) {
093: currentCategory = manager
094: .getCategory(currentCategoryId);
095: categoryActivities = getCategoryActivities(currentCategory);
096: // traverse the category's activities to find a duplicate
097: for (int index = 0; index < categoryActivities.length; index++) {
098: currentActivityId = categoryActivities[index]
099: .getId();
100: if (currentActivityId.equals(categorizedActivity
101: .getActivity().getId())) {
102: duplicateCategorizedactivities
103: .add(new CategorizedActivity(
104: currentCategory,
105: manager
106: .getActivity(currentActivityId)));
107: // Assuming only one unique activity per category -
108: // break
109: break;
110: }
111: }
112:
113: }
114:
115: }
116: return duplicateCategorizedactivities.toArray();
117: }
118:
119: /**
120: * Get the child required activities.
121: *
122: * @param activityId
123: * The parent activity id.
124: * @return the list of child required activities.
125: */
126: public Object[] getChildRequiredActivities(String activityId) {
127: ArrayList childRequiredActivities = new ArrayList();
128: IActivity activity = manager.getActivity(activityId);
129: Set actvitiyRequirementBindings = activity
130: .getActivityRequirementBindings();
131: String requiredActivityId = null;
132: IActivityRequirementBinding currentActivityRequirementBinding = null;
133: Object[] currentCategoryIds = null;
134: for (Iterator i = actvitiyRequirementBindings.iterator(); i
135: .hasNext();) {
136: currentActivityRequirementBinding = (IActivityRequirementBinding) i
137: .next();
138: requiredActivityId = currentActivityRequirementBinding
139: .getRequiredActivityId();
140: currentCategoryIds = getActivityCategories(requiredActivityId);
141: for (int index = 0; index < currentCategoryIds.length; index++) {
142: childRequiredActivities
143: .add(new CategorizedActivity(
144: manager
145: .getCategory((String) currentCategoryIds[index]),
146: manager.getActivity(requiredActivityId)));
147: }
148:
149: }
150:
151: return childRequiredActivities.toArray();
152: }
153:
154: /**
155: * Get the parent required activities.
156: *
157: * @param activityId
158: * The child activity id.
159: * @return the list of parent required activities.
160: */
161: public Object[] getParentRequiredActivities(String activityId) {
162: ArrayList parentRequiredActivities = new ArrayList();
163: Set definedActivities = manager.getDefinedActivityIds();
164: String currentActivityId = null;
165: Set activityRequirementBindings = null;
166: IActivityRequirementBinding currentActivityRequirementBinding = null;
167: Object[] currentCategoryIds = null;
168: for (Iterator i = definedActivities.iterator(); i.hasNext();) {
169: currentActivityId = (String) i.next();
170: activityRequirementBindings = manager.getActivity(
171: currentActivityId).getActivityRequirementBindings();
172: for (Iterator j = activityRequirementBindings.iterator(); j
173: .hasNext();) {
174: currentActivityRequirementBinding = (IActivityRequirementBinding) j
175: .next();
176: if (currentActivityRequirementBinding
177: .getRequiredActivityId().equals(activityId)) {
178: // We found one - add it to the list
179: currentCategoryIds = getActivityCategories(currentActivityId);
180: for (int index = 0; index < currentCategoryIds.length; index++) {
181: parentRequiredActivities
182: .add(new CategorizedActivity(
183: manager
184: .getCategory((String) currentCategoryIds[index]),
185: manager
186: .getActivity(currentActivityId)));
187: }
188: }
189: }
190:
191: }
192: return parentRequiredActivities.toArray();
193: }
194:
195: /**
196: * Get the activity's categories (there could be more than one).
197: *
198: * @param activityId
199: * The activity id.
200: * @return the activity's categories.
201: */
202: private Object[] getActivityCategories(String activityId) {
203: ArrayList activityCategories = new ArrayList();
204: Set categoryIds = manager.getDefinedCategoryIds();
205: String currentCategoryId = null;
206: IActivity[] categoryActivities = null;
207: for (Iterator i = categoryIds.iterator(); i.hasNext();) {
208: currentCategoryId = (String) i.next();
209: categoryActivities = getCategoryActivities(manager
210: .getCategory(currentCategoryId));
211: for (int index = 0; index < categoryActivities.length; index++) {
212: if (categoryActivities[index].getId()
213: .equals(activityId)) {
214: activityCategories.add(currentCategoryId);
215: break;
216: }
217: }
218: }
219: return activityCategories.toArray();
220: }
221:
222: /* (non-Javadoc)
223: * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
224: */
225: public Object[] getChildren(Object parentElement) {
226: if (parentElement instanceof IActivityManager) {
227: Set categoryIds = manager.getDefinedCategoryIds();
228: ArrayList categories = new ArrayList(categoryIds.size());
229: for (Iterator i = categoryIds.iterator(); i.hasNext();) {
230: String categoryId = (String) i.next();
231: ICategory category = manager.getCategory(categoryId);
232: if (getCategoryActivities(category).length > 0) {
233: categories.add(category);
234: }
235: }
236: return categories.toArray();
237: } else if (parentElement instanceof ICategory) {
238: return getCategoryActivities((ICategory) parentElement);
239: }
240: return new Object[0];
241: }
242:
243: /* (non-Javadoc)
244: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
245: */
246: public Object[] getElements(Object inputElement) {
247: return getChildren(inputElement);
248: }
249:
250: /* (non-Javadoc)
251: * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
252: */
253: public Object getParent(Object element) {
254: if (element instanceof CategorizedActivity) {
255: return ((CategorizedActivity) element).getCategory();
256: }
257: return null;
258: }
259:
260: /* (non-Javadoc)
261: * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
262: */
263: public boolean hasChildren(Object element) {
264: if (element instanceof IActivityManager
265: || element instanceof ICategory) {
266: return true;
267: }
268: return false;
269: }
270:
271: /* (non-Javadoc)
272: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
273: */
274: public void inputChanged(Viewer viewer, Object oldInput,
275: Object newInput) {
276: manager = (IActivityManager) newInput;
277: }
278: }
|