KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > model > SiteCategory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.update.internal.ui.model;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jface.operation.IRunnableContext;
21 import org.eclipse.jface.operation.IRunnableWithProgress;
22 import org.eclipse.update.core.ICategory;
23 import org.eclipse.update.core.IFeatureReference;
24 import org.eclipse.update.core.IURLEntry;
25 import org.eclipse.update.internal.core.Messages;
26 import org.eclipse.update.internal.ui.UpdateUIMessages;
27
28 public class SiteCategory extends UIModelObject {
29
30     Vector JavaDoc children;
31     private ICategory category;
32     private String JavaDoc name;
33     private boolean touched;
34     private int featureCount;
35     private boolean canceled;
36     private SiteBookmark bookmark;
37
38     class OtherCategory implements ICategory {
39         IURLEntry entry;
40         public OtherCategory() {
41             entry = new IURLEntry() {
42                 public String JavaDoc getAnnotation() {
43                     return Messages.SiteCategory_other_description;
44                 }
45                 public URL JavaDoc getURL() {
46                     return null;
47                 }
48                 public Object JavaDoc getAdapter(Class JavaDoc clazz) {
49                     return null;
50                 }
51                 public int getType() {
52                     return IURLEntry.UPDATE_SITE;
53                 }
54             };
55         }
56         public String JavaDoc getName() {
57             return SiteCategory.this.getName();
58         }
59         public String JavaDoc getLabel() {
60             return SiteCategory.this.getName();
61         }
62         public IURLEntry getDescription() {
63             return entry;
64         }
65         public Object JavaDoc getAdapter(Class JavaDoc clazz) {
66             return null;
67         }
68     }
69
70     public SiteCategory(SiteBookmark bookmark, String JavaDoc name, ICategory category) {
71         this.bookmark = bookmark;
72         if (category == null) {
73             this.name = Messages.SiteCategory_other_label;
74             this.category = new OtherCategory();
75         } else {
76             this.name = name;
77             this.category = category;
78         }
79         children = new Vector JavaDoc();
80     }
81     
82     public SiteBookmark getBookmark() {
83         return bookmark;
84     }
85
86     public boolean isOtherCategory() {
87         return category instanceof OtherCategory;
88     }
89
90     public Object JavaDoc[] getChildren() {
91         return canceled ? new Object JavaDoc[0] : children.toArray();
92     }
93
94     public int getChildCount() {
95         return canceled ? 0 : children.size();
96     }
97
98     public String JavaDoc getName() {
99         return name;
100     }
101     public String JavaDoc getFullName() {
102         return category.getName();
103     }
104
105     public String JavaDoc toString() {
106         return category.getLabel();
107     }
108
109     public ICategory getCategory() {
110         return category;
111     }
112
113     void add(Object JavaDoc child) {
114         if (child instanceof IFeatureAdapter)
115             featureCount++;
116         children.add(child);
117     }
118
119     public void touchFeatures(IRunnableContext context) {
120         if (children.size() == 0 || touched || featureCount == 0)
121             return;
122
123         IRunnableWithProgress op = new IRunnableWithProgress() {
124             public void run(IProgressMonitor monitor) {
125                 monitor.beginTask(
126                     UpdateUIMessages.SiteBookmark_downloading,
127                     featureCount);
128                 for (int i = 0; i < children.size(); i++) {
129                     Object JavaDoc child = children.get(i);
130                     if (monitor.isCanceled())
131                         break;
132                     if (child instanceof IFeatureAdapter) {
133                         IFeatureAdapter adapter = (IFeatureAdapter) child;
134                         monitor.subTask(adapter.getFastLabel());
135                         try {
136                             adapter.getFeature(
137                                 new SubProgressMonitor(monitor, 1));
138                         } catch (CoreException e) {
139                         }
140                     }
141                 }
142                 monitor.done();
143             }
144         };
145
146         try {
147             context.run(true, true, op);
148             touched = true;
149         } catch (InterruptedException JavaDoc e) {
150             canceled = true;
151         } catch (InvocationTargetException JavaDoc e) {
152         }
153     }
154
155     public void addFeaturesTo(Vector JavaDoc flatList) {
156         for (int i = 0; i < children.size(); i++) {
157             Object JavaDoc child = children.get(i);
158             if (child instanceof FeatureReferenceAdapter) {
159                 FeatureReferenceAdapter cfeature =
160                     (FeatureReferenceAdapter) child;
161                 // Don't add duplicates - there may be the same
162
// feature present in several categories
163
if (findFeature(flatList, cfeature.getFeatureReference())
164                     == null) {
165                     flatList.add(child);
166                 }
167             } else if (child instanceof SiteCategory) {
168                 ((SiteCategory) child).addFeaturesTo(flatList);
169             }
170         }
171     }
172
173     private FeatureReferenceAdapter findFeature(
174         Vector JavaDoc flatList,
175         IFeatureReference featureRef) {
176         for (int i = 0; i < flatList.size(); i++) {
177             FeatureReferenceAdapter cfeature =
178                 (FeatureReferenceAdapter) flatList.get(i);
179             if (cfeature.getFeatureReference().equals(featureRef))
180                 return cfeature;
181         }
182         return null;
183     }
184
185     public boolean equals(Object JavaDoc object) {
186
187         if ( (object == null) || !( object instanceof SiteCategory))
188             return false;
189         
190         if ( this == object)
191             return true;
192         
193         return getName().equals(((SiteCategory)object).getName()) && bookmark.equals(((SiteCategory)object).getBookmark());
194
195     }
196
197     public int hashCode() {
198
199         return getName().hashCode() * ( (bookmark == null)? 1: bookmark.hashCode());
200     }
201     
202     
203 }
204
Popular Tags