1 11 package org.eclipse.update.internal.ui.model; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 import java.util.Vector ; 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 children; 31 private ICategory category; 32 private String 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 getAnnotation() { 43 return Messages.SiteCategory_other_description; 44 } 45 public URL getURL() { 46 return null; 47 } 48 public Object getAdapter(Class clazz) { 49 return null; 50 } 51 public int getType() { 52 return IURLEntry.UPDATE_SITE; 53 } 54 }; 55 } 56 public String getName() { 57 return SiteCategory.this.getName(); 58 } 59 public String getLabel() { 60 return SiteCategory.this.getName(); 61 } 62 public IURLEntry getDescription() { 63 return entry; 64 } 65 public Object getAdapter(Class clazz) { 66 return null; 67 } 68 } 69 70 public SiteCategory(SiteBookmark bookmark, String 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 (); 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 [] getChildren() { 91 return canceled ? new Object [0] : children.toArray(); 92 } 93 94 public int getChildCount() { 95 return canceled ? 0 : children.size(); 96 } 97 98 public String getName() { 99 return name; 100 } 101 public String getFullName() { 102 return category.getName(); 103 } 104 105 public String toString() { 106 return category.getLabel(); 107 } 108 109 public ICategory getCategory() { 110 return category; 111 } 112 113 void add(Object 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 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 e) { 150 canceled = true; 151 } catch (InvocationTargetException e) { 152 } 153 } 154 155 public void addFeaturesTo(Vector flatList) { 156 for (int i = 0; i < children.size(); i++) { 157 Object child = children.get(i); 158 if (child instanceof FeatureReferenceAdapter) { 159 FeatureReferenceAdapter cfeature = 160 (FeatureReferenceAdapter) child; 161 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 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 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 |