1 11 package org.eclipse.update.internal.operations; 12 13 import java.util.ArrayList ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.update.configuration.*; 18 import org.eclipse.update.core.*; 19 import org.eclipse.update.internal.core.*; 20 21 41 public class FeatureHierarchyElement { 42 43 private Object root; 44 private ArrayList children; 45 private IFeatureReference oldFeatureRef; 46 private IFeatureReference newFeatureRef; 47 private boolean checked; 48 private boolean optionalChildren; 49 private boolean nativeUpgrade = false; 50 51 public FeatureHierarchyElement( 52 IFeatureReference oldRef, 53 IFeatureReference newRef) { 54 oldFeatureRef = oldRef; 55 newFeatureRef = newRef; 56 } 57 58 public void setRoot(Object root) { 59 this.root = root; 60 } 61 62 public Object getRoot() { 63 return root; 64 } 65 66 69 public boolean isEditable() { 70 if (isOptional() == false) 72 return false; 73 if (oldFeatureRef != null) 76 return false; 77 return true; 78 } 79 80 89 90 public boolean isFalseUpdate() { 91 if (oldFeatureRef != null && newFeatureRef != null) { 92 try { 93 return oldFeatureRef.getVersionedIdentifier().equals( 94 newFeatureRef.getVersionedIdentifier()); 95 } catch (CoreException e) { 96 } 97 } 98 return false; 99 } 100 103 public boolean isOptional() { 104 return newFeatureRef instanceof IIncludedFeatureReference 105 && ((IIncludedFeatureReference) newFeatureRef).isOptional(); 106 } 107 112 public boolean isChecked() { 113 return checked; 114 } 115 116 void setNativeUpgrade(boolean nativeUpgrade) { 117 this.nativeUpgrade = nativeUpgrade; 118 } 119 120 132 public boolean isEnabled(IInstallConfiguration config) { 133 if (nativeUpgrade) 134 return true; 135 if (isOptional() && oldFeatureRef != null) { 136 try { 137 IFeature oldFeature = oldFeatureRef.getFeature(null); 138 IConfiguredSite csite = 139 UpdateUtils.getConfigSite(oldFeature, config); 140 return csite.isConfigured(oldFeature); 141 } catch (CoreException e) { 142 } 143 } 144 return true; 145 } 146 147 public IFeature getFeature() { 148 try { 149 IFeature feature = newFeatureRef.getFeature(null); 150 return feature; 151 } catch (CoreException e) { 152 return null; 153 } 154 } 155 156 159 public void setChecked(boolean checked) { 160 this.checked = checked; 161 } 162 165 public String getLabel() { 166 try { 167 return getFeatureLabel(newFeatureRef); 168 } catch (CoreException e) { 169 if (newFeatureRef instanceof IIncludedFeatureReference) { 170 String iname = 171 ((IIncludedFeatureReference) newFeatureRef).getName(); 172 if (iname != null) 173 return iname; 174 } 175 try { 176 VersionedIdentifier vid = 177 newFeatureRef.getVersionedIdentifier(); 178 return vid.toString(); 179 } catch (CoreException e2) { 180 } 181 } 182 return null; 183 } 184 187 private String getFeatureLabel(IFeatureReference featureRef) 188 throws CoreException { 189 IFeature feature = featureRef.getFeature(null); 190 return feature.getLabel() 191 + " " + feature.getVersionedIdentifier().getVersion().toString(); 193 } 194 198 public FeatureHierarchyElement[] getChildren( 199 boolean update, 200 boolean patch, 201 IInstallConfiguration config) { 202 computeChildren(update, patch, config); 203 FeatureHierarchyElement[] array = 204 new FeatureHierarchyElement[children.size()]; 205 children.toArray(array); 206 return array; 207 } 208 209 public FeatureHierarchyElement[] getChildren() { 210 if (children != null) { 211 FeatureHierarchyElement[] array = 212 new FeatureHierarchyElement[children.size()]; 213 children.toArray(array); 214 return array; 215 } 216 217 return new FeatureHierarchyElement[0]; 218 } 219 222 public void computeChildren( 223 boolean update, 224 boolean patch, 225 IInstallConfiguration config) { 226 if (children == null) { 227 children = new ArrayList (); 228 try { 229 IFeature oldFeature = null; 230 IFeature newFeature = null; 231 newFeature = newFeatureRef.getFeature(null); 232 if (oldFeatureRef != null) 233 oldFeature = oldFeatureRef.getFeature(null); 234 optionalChildren = 235 computeElements( 236 oldFeature, 237 newFeature, 238 update, 239 patch, 240 config, 241 children); 242 for (int i = 0; i < children.size(); i++) { 243 FeatureHierarchyElement element = 244 (FeatureHierarchyElement) children.get(i); 245 element.setRoot(getRoot()); 246 } 247 } catch (CoreException e) { 248 } 249 } 250 } 251 254 public boolean hasOptionalChildren() { 255 return optionalChildren; 256 } 257 260 public void addCheckedOptionalFeatures( 261 boolean update, 262 boolean patch, 263 IInstallConfiguration config, 264 Set set) { 265 if (isOptional() && isChecked()) { 266 if (!update || !isFalseUpdate()) 271 set.add(newFeatureRef); 272 } 273 FeatureHierarchyElement[] elements = getChildren(update, patch, config); 274 for (int i = 0; i < elements.length; i++) { 275 elements[i].addCheckedOptionalFeatures(update, patch, config, set); 276 } 277 } 278 279 285 public static boolean computeElements( 286 IFeature oldFeature, 287 IFeature newFeature, 288 boolean update, 289 boolean patch, 290 IInstallConfiguration config, 291 ArrayList list) { 292 Object [] oldChildren = null; 293 Object [] newChildren = getIncludedFeatures(newFeature); 294 boolean optionalChildren = false; 295 296 try { 297 if (oldFeature != null) { 298 oldChildren = getIncludedFeatures(oldFeature); 299 } 300 for (int i = 0; i < newChildren.length; i++) { 301 IFeatureReference oldRef = null; 302 IFeatureReference newRef = (IFeatureReference) newChildren[i]; 303 if (oldChildren != null) { 304 String newId = 305 newRef.getVersionedIdentifier().getIdentifier(); 306 307 for (int j = 0; j < oldChildren.length; j++) { 308 IFeatureReference cref = 309 (IFeatureReference) oldChildren[j]; 310 try { 311 if (cref 312 .getVersionedIdentifier() 313 .getIdentifier() 314 .equals(newId)) { 315 oldRef = cref; 316 break; 317 } 318 } catch (CoreException ex) { 319 } 320 } 321 } else if (patch) { 322 if (!UpdateUtils.isPatch(newFeature)) { 325 oldRef = findPatchedReference(newRef, config); 326 } 327 } 328 if (oldRef != null 330 && ((oldRef instanceof IIncludedFeatureReference 331 && ((IIncludedFeatureReference) oldRef).isOptional()) 332 || patch)) { 333 try { 334 IFeature f = oldRef.getFeature(null); 335 if (f == null) 336 oldRef = null; 337 } catch (CoreException e) { 338 oldRef = null; 340 } 341 } 342 FeatureHierarchyElement element = 343 new FeatureHierarchyElement(oldRef, newRef); 344 if (element.isOptional() && (update || patch)) { 348 element.setChecked(oldRef != null); 349 if (oldRef == null) { 350 if (hasOlderVersion(newRef)) { 357 element.setNativeUpgrade(true); 358 element.setChecked(true); 359 } 360 } 361 } else 362 element.setChecked(true); 363 list.add(element); 364 element.computeChildren(update, patch, config); 365 if (element.isOptional() || element.hasOptionalChildren()) 366 optionalChildren = true; 367 } 368 } catch (CoreException e) { 369 } 370 return optionalChildren; 371 } 372 public static boolean hasOlderVersion(IFeatureReference newRef) { 373 try { 374 VersionedIdentifier vid = newRef.getVersionedIdentifier(); 375 PluginVersionIdentifier version = vid.getVersion(); 376 String mode = getUpdateVersionsMode(); 377 378 IFeature[] allInstalled = 379 UpdateUtils.getInstalledFeatures(vid, false); 380 for (int i = 0; i < allInstalled.length; i++) { 381 IFeature candidate = allInstalled[i]; 382 PluginVersionIdentifier cversion = 383 candidate.getVersionedIdentifier().getVersion(); 384 if (mode.equals(UpdateCore.EQUIVALENT_VALUE)) { 387 if (version.isEquivalentTo(cversion)) 388 return true; 389 } else if (mode.equals(UpdateCore.COMPATIBLE_VALUE)) { 390 if (version.isCompatibleWith(cversion)) 391 return true; 392 } 393 } 394 } catch (CoreException e) { 395 } 396 return false; 397 } 398 399 private static IFeatureReference findPatchedReference( 400 IFeatureReference newRef, 401 IInstallConfiguration config) 402 throws CoreException { 403 VersionedIdentifier vid = newRef.getVersionedIdentifier(); 404 IConfiguredSite[] csites = config.getConfiguredSites(); 405 for (int i = 0; i < csites.length; i++) { 406 IConfiguredSite csite = csites[i]; 407 IFeatureReference[] refs = csite.getConfiguredFeatures(); 408 for (int j = 0; j < refs.length; j++) { 409 IFeatureReference ref = refs[j]; 410 VersionedIdentifier refVid = ref.getVersionedIdentifier(); 411 if (vid.getIdentifier().equals(refVid.getIdentifier())) 412 return ref; 413 } 414 } 415 return null; 416 } 417 418 421 public static Object [] getIncludedFeatures(IFeatureReference ref) { 422 try { 423 IFeature feature = ref.getFeature(null); 424 return getIncludedFeatures(feature); 425 } catch (CoreException e) { 426 } 427 return new Object [0]; 428 } 429 430 433 434 public static Object [] getIncludedFeatures(IFeature feature) { 435 try { 436 return feature.getIncludedFeatureReferences(); 437 } catch (CoreException e) { 438 } 439 return new Object [0]; 440 } 441 442 private static String getUpdateVersionsMode() { 443 Preferences store = UpdateCore.getPlugin().getPluginPreferences(); 444 return store.getString(UpdateCore.P_UPDATE_VERSIONS); 445 } 446 } 447 | Popular Tags |