|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 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.pde.api.tools.ui.internal.actions; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Date; |
| 15 |
import java.util.HashMap; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Map; |
| 18 |
|
| 19 |
import org.eclipse.core.runtime.CoreException; |
| 20 |
import org.eclipse.core.runtime.IAdaptable; |
| 21 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 22 |
import org.eclipse.core.runtime.IStatus; |
| 23 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 24 |
import org.eclipse.core.runtime.Status; |
| 25 |
import org.eclipse.core.runtime.SubMonitor; |
| 26 |
import org.eclipse.core.runtime.jobs.Job; |
| 27 |
import org.eclipse.jdt.core.Flags; |
| 28 |
import org.eclipse.jdt.core.IClassFile; |
| 29 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 30 |
import org.eclipse.jdt.core.IJavaElement; |
| 31 |
import org.eclipse.jdt.core.IJavaProject; |
| 32 |
import org.eclipse.jdt.core.IPackageFragment; |
| 33 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 34 |
import org.eclipse.jdt.core.IType; |
| 35 |
import org.eclipse.jdt.core.JavaModelException; |
| 36 |
import org.eclipse.jface.action.IAction; |
| 37 |
import org.eclipse.jface.viewers.ISelection; |
| 38 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 39 |
import org.eclipse.jface.window.Window; |
| 40 |
import org.eclipse.pde.api.tools.internal.ApiBaselineManager; |
| 41 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 42 |
import org.eclipse.pde.api.tools.internal.provisional.Factory; |
| 43 |
import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
| 44 |
import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; |
| 45 |
import org.eclipse.pde.api.tools.internal.provisional.ISession; |
| 46 |
import org.eclipse.pde.api.tools.internal.provisional.ITreeModel; |
| 47 |
import org.eclipse.pde.api.tools.internal.provisional.ITreeNode; |
| 48 |
import org.eclipse.pde.api.tools.internal.provisional.RestrictionModifiers; |
| 49 |
import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
| 50 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiComparator; |
| 51 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiScope; |
| 52 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaVisitor; |
| 53 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta; |
| 54 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 55 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 56 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiScope; |
| 57 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; |
| 58 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
| 59 |
import org.eclipse.pde.api.tools.internal.util.Util; |
| 60 |
import org.eclipse.ui.IObjectActionDelegate; |
| 61 |
import org.eclipse.ui.IWorkbenchPart; |
| 62 |
import org.eclipse.ui.IWorkbenchPartSite; |
| 63 |
import org.eclipse.ui.views.properties.IPropertyDescriptor; |
| 64 |
import org.eclipse.ui.views.properties.IPropertySheetEntry; |
| 65 |
import org.eclipse.ui.views.properties.IPropertySource; |
| 66 |
import org.eclipse.ui.views.properties.PropertyDescriptor; |
| 67 |
import org.eclipse.ui.views.properties.TextPropertyDescriptor; |
| 68 |
|
| 69 |
import com.ibm.icu.text.DateFormat; |
| 70 |
|
| 71 |
public class CompareWithAction implements IObjectActionDelegate { |
| 72 |
|
| 73 |
public static class DeltaSession implements ISession { |
| 74 |
static Object[] NO_CHILDREN = new Object[0]; |
| 75 |
|
| 76 |
static class TreeNode implements ITreeNode, IPropertySource, IAdaptable { |
| 77 |
private static final IPropertyDescriptor[] NO_PROPERTY_DESCRIPTORS = new IPropertyDescriptor[0]; |
| 78 |
// Property id keys |
| 79 |
public static final String ID_MESSAGE = "IDelta.Message"; //$NON-NLS-1$ |
| 80 |
public static final String ID_COMPONENT = "IDelta.Component"; //$NON-NLS-1$ |
| 81 |
public static final String ID_ELEMENT_TYPE = "IDelta.ElementType"; //$NON-NLS-1$ |
| 82 |
public static final String ID_FLAGS = "IDelta.Flags"; //$NON-NLS-1$ |
| 83 |
private static final Object ID_KEY = "IDelta.Key"; //$NON-NLS-1$ |
| 84 |
private static final Object ID_KIND = "IDelta.Kind"; //$NON-NLS-1$ |
| 85 |
private static final Object ID_NEW_MODIFIERS = "IDelta.NewModifiers"; //$NON-NLS-1$ |
| 86 |
private static final Object ID_OLD_MODIFIERS = "IDelta.OldModifiers"; //$NON-NLS-1$ |
| 87 |
private static final Object ID_RESTRICTIONS = "IDelta.Restrictions"; //$NON-NLS-1$ |
| 88 |
private static final Object ID_TYPENAME = "IDelta.TypeName"; //$NON-NLS-1$ |
| 89 |
|
| 90 |
// categories |
| 91 |
public static final String P_MESSAGE_CATEGORY = ActionMessages.MessageCategory; |
| 92 |
public static final String P_INFO_CATEGORY = ActionMessages.InfoCategory; |
| 93 |
|
| 94 |
public static final String P_MESSAGE = ActionMessages.PropertyMessageKey; |
| 95 |
public static final String P_COMPONENT = ActionMessages.PropertyComponentKey; |
| 96 |
public static final String P_ELEMENT_TYPE = ActionMessages.PropertyElementTypeKey; |
| 97 |
public static final String P_FLAGS = ActionMessages.PropertyFlagsKey; |
| 98 |
public static final String P_KEY = ActionMessages.PropertyKeyKey; |
| 99 |
public static final String P_KIND = ActionMessages.PropertyKindKey; |
| 100 |
public static final String P_NEW_MODIFIERS = ActionMessages.PropertyNewModifiersKey; |
| 101 |
public static final String P_OLD_MODIFIERS = ActionMessages.PropertyOldModifiersKey; |
| 102 |
public static final String P_RESTRICTIONS = ActionMessages.PropertyRestrictionsKey; |
| 103 |
public static final String P_TYPENAME = ActionMessages.PropertyTypeNameKey; |
| 104 |
|
| 105 |
private static List Descriptors; |
| 106 |
static { |
| 107 |
Descriptors = new ArrayList(); |
| 108 |
|
| 109 |
PropertyDescriptor propertyDescriptor = new TextPropertyDescriptor(ID_MESSAGE, P_MESSAGE); |
| 110 |
propertyDescriptor.setCategory(P_MESSAGE_CATEGORY); |
| 111 |
Descriptors.add(propertyDescriptor); |
| 112 |
|
| 113 |
propertyDescriptor = new TextPropertyDescriptor(ID_COMPONENT, P_COMPONENT); |
| 114 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 115 |
Descriptors.add(propertyDescriptor); |
| 116 |
|
| 117 |
propertyDescriptor = new TextPropertyDescriptor(ID_KEY, P_KEY); |
| 118 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 119 |
propertyDescriptor.setFilterFlags(new String[] { IPropertySheetEntry.FILTER_ID_EXPERT }); |
| 120 |
Descriptors.add(propertyDescriptor); |
| 121 |
|
| 122 |
propertyDescriptor = new TextPropertyDescriptor(ID_KIND, P_KIND); |
| 123 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 124 |
Descriptors.add(propertyDescriptor); |
| 125 |
|
| 126 |
propertyDescriptor = new TextPropertyDescriptor(ID_NEW_MODIFIERS, P_NEW_MODIFIERS); |
| 127 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 128 |
Descriptors.add(propertyDescriptor); |
| 129 |
|
| 130 |
propertyDescriptor = new TextPropertyDescriptor(ID_OLD_MODIFIERS, P_OLD_MODIFIERS); |
| 131 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 132 |
Descriptors.add(propertyDescriptor); |
| 133 |
|
| 134 |
propertyDescriptor = new TextPropertyDescriptor(ID_RESTRICTIONS, P_RESTRICTIONS); |
| 135 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 136 |
Descriptors.add(propertyDescriptor); |
| 137 |
|
| 138 |
propertyDescriptor = new TextPropertyDescriptor(ID_TYPENAME, P_TYPENAME); |
| 139 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 140 |
Descriptors.add(propertyDescriptor); |
| 141 |
|
| 142 |
propertyDescriptor = new TextPropertyDescriptor(ID_FLAGS, P_FLAGS); |
| 143 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 144 |
Descriptors.add(propertyDescriptor); |
| 145 |
|
| 146 |
propertyDescriptor = new TextPropertyDescriptor(ID_ELEMENT_TYPE, P_ELEMENT_TYPE); |
| 147 |
propertyDescriptor.setCategory(P_INFO_CATEGORY); |
| 148 |
Descriptors.add(propertyDescriptor); |
| 149 |
} |
| 150 |
/** |
| 151 |
* Returns the descriptors |
| 152 |
*/ |
| 153 |
static List getDescriptors() { |
| 154 |
return Descriptors; |
| 155 |
} |
| 156 |
Map children; |
| 157 |
String name; |
| 158 |
TreeNode parent; |
| 159 |
Object data; |
| 160 |
int id; |
| 161 |
|
| 162 |
public TreeNode(int id, String name, Object data) { |
| 163 |
this.name = name; |
| 164 |
this.id = id; |
| 165 |
this.data = data; |
| 166 |
} |
| 167 |
public Object[] getChildren() { |
| 168 |
if (this.children == null) { |
| 169 |
return NO_CHILDREN; |
| 170 |
} |
| 171 |
return this.children.values().toArray(new Object[this.children.size()]); |
| 172 |
} |
| 173 |
public TreeNode getNode(String name) { |
| 174 |
if (this.children == null) { |
| 175 |
return null; |
| 176 |
} |
| 177 |
return (TreeNode) this.children.get(name); |
| 178 |
} |
| 179 |
public int getId() { |
| 180 |
return this.id; |
| 181 |
} |
| 182 |
public void add(TreeNode node) { |
| 183 |
if (this.children == null) { |
| 184 |
this.children = new HashMap(); |
| 185 |
} |
| 186 |
this.children.put(node.name, node); |
| 187 |
} |
| 188 |
public boolean hasChildren() { |
| 189 |
return this.children != null && !this.children.isEmpty(); |
| 190 |
} |
| 191 |
public String toString() { |
| 192 |
return String.valueOf(this.name); |
| 193 |
} |
| 194 |
public Object getData() { |
| 195 |
return this.data; |
| 196 |
} |
| 197 |
public Object getEditableValue() { |
| 198 |
return null; |
| 199 |
} |
| 200 |
public IPropertyDescriptor[] getPropertyDescriptors() { |
| 201 |
if (this.data != null) { |
| 202 |
return (IPropertyDescriptor[]) getDescriptors().toArray( |
| 203 |
new IPropertyDescriptor[getDescriptors().size()]); |
| 204 |
} |
| 205 |
return NO_PROPERTY_DESCRIPTORS; |
| 206 |
} |
| 207 |
public Object getPropertyValue(Object propKey) { |
| 208 |
if (this.data == null) return null; |
| 209 |
IDelta delta = (IDelta) this.data; |
| 210 |
if (ID_MESSAGE.equals(propKey)) { |
| 211 |
return delta.getMessage(); |
| 212 |
} |
| 213 |
if (ID_COMPONENT.equals(propKey)) { |
| 214 |
return delta.getComponentVersionId(); |
| 215 |
} |
| 216 |
if (ID_ELEMENT_TYPE.equals(propKey)) { |
| 217 |
int elementType = delta.getElementType(); |
| 218 |
StringBuffer buffer = new StringBuffer(Util.getDeltaElementType(elementType)); |
| 219 |
buffer.append(" (").append(elementType).append(')'); //$NON-NLS-1$ |
| 220 |
return String.valueOf(buffer); |
| 221 |
} |
| 222 |
if (ID_FLAGS.equals(propKey)) { |
| 223 |
int flags = delta.getFlags(); |
| 224 |
StringBuffer buffer = new StringBuffer(Util.getDeltaFlagsName(flags)); |
| 225 |
buffer.append(" (").append(flags).append(')'); //$NON-NLS-1$ |
| 226 |
return String.valueOf(buffer); |
| 227 |
} |
| 228 |
if (ID_KEY.equals(propKey)) { |
| 229 |
return delta.getKey(); |
| 230 |
} |
| 231 |
if (ID_KIND.equals(propKey)) { |
| 232 |
int kind = delta.getKind(); |
| 233 |
StringBuffer buffer = new StringBuffer(Util.getDeltaKindName(kind)); |
| 234 |
buffer.append(" (").append(kind).append(')'); //$NON-NLS-1$ |
| 235 |
return String.valueOf(buffer); |
| 236 |
} |
| 237 |
if (ID_NEW_MODIFIERS.equals(propKey)) { |
| 238 |
return getDisplayedModifiers(delta.getNewModifiers()); |
| 239 |
} |
| 240 |
if (ID_OLD_MODIFIERS.equals(propKey)) { |
| 241 |
return getDisplayedModifiers(delta.getOldModifiers()); |
| 242 |
} |
| 243 |
if (ID_RESTRICTIONS.equals(propKey)) { |
| 244 |
int restrictions = delta.getRestrictions(); |
| 245 |
StringBuffer buffer = new StringBuffer(RestrictionModifiers.getRestrictionText(restrictions)); |
| 246 |
buffer.append(" (0x").append(Integer.toHexString(restrictions)).append(')'); //$NON-NLS-1$ |
| 247 |
return String.valueOf(buffer); |
| 248 |
} |
| 249 |
if (ID_TYPENAME.equals(propKey)) { |
| 250 |
return delta.getTypeName(); |
| 251 |
} |
| 252 |
return null; |
| 253 |
} |
| 254 |
public boolean isPropertySet(Object id) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
public void resetPropertyValue(Object id) { |
| 258 |
// nothing to do |
| 259 |
} |
| 260 |
public void setPropertyValue(Object id, Object value) { |
| 261 |
// nothing to do |
| 262 |
} |
| 263 |
/* (non-Javadoc) |
| 264 |
* Method declared on IAdaptable |
| 265 |
*/ |
| 266 |
public Object getAdapter(Class adapter) { |
| 267 |
if (adapter == IPropertySource.class) { |
| 268 |
return this; |
| 269 |
} |
| 270 |
return null; |
| 271 |
} |
| 272 |
private static String getDisplayedModifiers(int newModifiers) { |
| 273 |
StringBuffer buffer = new StringBuffer(); |
| 274 |
if(newModifiers == 0) { |
| 275 |
buffer.append(ActionMessages.PropertyPackageVisibility); |
| 276 |
} else { |
| 277 |
if (Flags.isAbstract(newModifiers)) { |
| 278 |
buffer.append("abstract"); //$NON-NLS-1$ |
| 279 |
} |
| 280 |
String separator = " | "; //$NON-NLS-1$ |
| 281 |
if(Flags.isFinal(newModifiers)) { |
| 282 |
if(buffer.length() > 0) { |
| 283 |
buffer.append(separator); |
| 284 |
} |
| 285 |
buffer.append("final"); //$NON-NLS-1$ |
| 286 |
} |
| 287 |
if(Flags.isNative(newModifiers)) { |
| 288 |
if(buffer.length() > 0) { |
| 289 |
buffer.append(separator); |
| 290 |
} |
| 291 |
buffer.append("native"); //$NON-NLS-1$ |
| 292 |
} |
| 293 |
if(Flags.isPrivate(newModifiers)) { |
| 294 |
if(buffer.length() > 0) { |
| 295 |
buffer.append(separator); |
| 296 |
} |
| 297 |
buffer.append("private"); //$NON-NLS-1$ |
| 298 |
} |
| 299 |
if(Flags.isProtected(newModifiers)) { |
| 300 |
if(buffer.length() > 0) { |
| 301 |
buffer.append(separator); |
| 302 |
} |
| 303 |
buffer.append("protected"); //$NON-NLS-1$ |
| 304 |
} |
| 305 |
if(Flags.isPublic(newModifiers)) { |
| 306 |
if(buffer.length() > 0) { |
| 307 |
buffer.append(separator); |
| 308 |
} |
| 309 |
buffer.append("public"); //$NON-NLS-1$ |
| 310 |
} |
| 311 |
if(Flags.isStatic(newModifiers)) { |
| 312 |
if(buffer.length() > 0) { |
| 313 |
buffer.append(separator); |
| 314 |
} |
| 315 |
buffer.append("static"); //$NON-NLS-1$ |
| 316 |
} |
| 317 |
if(Flags.isStrictfp(newModifiers)) { |
| 318 |
if(buffer.length() > 0) { |
| 319 |
buffer.append(separator); |
| 320 |
} |
| 321 |
buffer.append("strictfp"); //$NON-NLS-1$ |
| 322 |
} |
| 323 |
if(Flags.isSynchronized(newModifiers)) { |
| 324 |
if(buffer.length() > 0) { |
| 325 |
buffer.append(separator); |
| 326 |
} |
| 327 |
buffer.append("synchronized"); //$NON-NLS-1$ |
| 328 |
} |
| 329 |
if(Flags.isTransient(newModifiers)) { |
| 330 |
if(buffer.length() > 0) { |
| 331 |
buffer.append(separator); |
| 332 |
} |
| 333 |
buffer.append("transient"); //$NON-NLS-1$ |
| 334 |
} |
| 335 |
if(Flags.isVolatile(newModifiers)) { |
| 336 |
if(buffer.length() > 0) { |
| 337 |
buffer.append(separator); |
| 338 |
} |
| 339 |
buffer.append("volatile"); //$NON-NLS-1$ |
| 340 |
} |
| 341 |
if(Flags.isVarargs(newModifiers)) { |
| 342 |
if(buffer.length() > 0) { |
| 343 |
buffer.append(separator); |
| 344 |
} |
| 345 |
buffer.append("vargars"); //$NON-NLS-1$ |
| 346 |
} |
| 347 |
} |
| 348 |
buffer.append(" (0x").append(Integer.toHexString(newModifiers)).append(')'); //$NON-NLS-1$ |
| 349 |
return buffer.toString(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static class TreeModel implements ITreeModel, IAdaptable { |
| 354 |
TreeNode root; |
| 355 |
|
| 356 |
TreeModel(TreeNode root) { |
| 357 |
this.root = root; |
| 358 |
} |
| 359 |
public ITreeNode getRoot() { |
| 360 |
return this.root; |
| 361 |
} |
| 362 |
public Object getAdapter(Class adapter) { |
| 363 |
// TODO Auto-generated method stub |
| 364 |
return null; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
IDelta delta; |
| 369 |
String baselineName; |
| 370 |
String timestamp; |
| 371 |
|
| 372 |
public DeltaSession(IDelta delta, String baselineName) { |
| 373 |
this.delta = delta; |
| 374 |
this.baselineName = baselineName; |
| 375 |
this.timestamp = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date(System.currentTimeMillis())); |
| 376 |
} |
| 377 |
public ITreeModel getModel() { |
| 378 |
TreeNode root = new TreeNode(0, null, this.delta); |
| 379 |
TreeModel model = new TreeModel(root); |
| 380 |
class TreeBuilder extends DeltaVisitor { |
| 381 |
TreeNode node; |
| 382 |
|
| 383 |
public TreeBuilder(TreeNode node) { |
| 384 |
this.node = node; |
| 385 |
} |
| 386 |
public void endVisit(IDelta delta) { |
| 387 |
if (delta.getChildren().length == 0) { |
| 388 |
String typeName = delta.getTypeName(); |
| 389 |
if (typeName == null) { |
| 390 |
this.node.add(new TreeNode(0, delta.getKey(), delta)); |
| 391 |
} else { |
| 392 |
// split the type name (package - type) |
| 393 |
int index = typeName.lastIndexOf('.'); |
| 394 |
String packageName = "<default package>"; //$NON-NLS-1$ |
| 395 |
String actualTypeName = null; |
| 396 |
if (index != -1) { |
| 397 |
packageName = typeName.substring(0, index); |
| 398 |
actualTypeName = typeName.substring(index + 1); |
| 399 |
} else { |
| 400 |
actualTypeName = typeName; |
| 401 |
} |
| 402 |
TreeNode node2 = this.node.getNode(packageName); |
| 403 |
if (node2 == null) { |
| 404 |
node2 = new TreeNode(ITreeNode.PACKAGE, packageName, null); |
| 405 |
this.node.add(node2); |
| 406 |
} |
| 407 |
TreeNode node3 = node2.getNode(actualTypeName); |
| 408 |
if (node3 == null) { |
| 409 |
int id = 0; |
| 410 |
switch(delta.getElementType()) { |
| 411 |
case IDelta.ANNOTATION_ELEMENT_TYPE : |
| 412 |
id = ITreeNode.ANNOTATION; |
| 413 |
break; |
| 414 |
case IDelta.INTERFACE_ELEMENT_TYPE : |
| 415 |
id = ITreeNode.INTERFACE; |
| 416 |
break; |
| 417 |
case IDelta.CLASS_ELEMENT_TYPE : |
| 418 |
id = ITreeNode.CLASS; |
| 419 |
break; |
| 420 |
case IDelta.ENUM_ELEMENT_TYPE : |
| 421 |
id = ITreeNode.ENUM; |
| 422 |
default : |
| 423 |
// we need to retrieve the type kind |
| 424 |
try { |
| 425 |
String componentVersionId = delta.getComponentVersionId(); |
| 426 |
if (componentVersionId != null) { |
| 427 |
int indexOfOpen = componentVersionId.lastIndexOf('('); |
| 428 |
String componentID = componentVersionId.substring(0, indexOfOpen); |
| 429 |
String version = componentVersionId.substring(indexOfOpen + 1, componentVersionId.length() - 1); |
| 430 |
IApiBaseline baseline = ApiBaselineManager.getManager().getApiBaseline(DeltaSession.this.baselineName); |
| 431 |
int modifiers = 0; |
| 432 |
if (baseline != null) { |
| 433 |
IApiComponent apiComponent = baseline.getApiComponent(componentID); |
| 434 |
if (apiComponent != null && version.equals(apiComponent.getVersion())) { |
| 435 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(typeName); |
| 436 |
if (typeRoot != null) { |
| 437 |
IApiType structure = typeRoot.getStructure(); |
| 438 |
modifiers = structure.getModifiers(); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
if (modifiers == 0) { |
| 443 |
// try the workspace baseline |
| 444 |
baseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); |
| 445 |
if (baseline != null) { |
| 446 |
IApiComponent apiComponent = baseline.getApiComponent(componentID); |
| 447 |
if (apiComponent != null && version.equals(apiComponent.getVersion())) { |
| 448 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(typeName); |
| 449 |
if (typeRoot != null) { |
| 450 |
IApiType structure = typeRoot.getStructure(); |
| 451 |
modifiers = structure.getModifiers(); |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
if (Flags.isEnum(modifiers)) { |
| 457 |
id = ITreeNode.ENUM; |
| 458 |
} else if (Flags.isAnnotation(modifiers)) { |
| 459 |
id = ITreeNode.ANNOTATION; |
| 460 |
} else if (Flags.isInterface(modifiers)) { |
| 461 |
id = ITreeNode.INTERFACE; |
| 462 |
} else { |
| 463 |
id = ITreeNode.CLASS; |
| 464 |
} |
| 465 |
} |
| 466 |
} catch (CoreException e) { |
| 467 |
// ignore |
| 468 |
} |
| 469 |
} |
| 470 |
node3 = new TreeNode(id, actualTypeName, null); |
| 471 |
node2.add(node3); |
| 472 |
} |
| 473 |
node3.add(new TreeNode(0, delta.getMessage(), delta)); |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
if (this.delta == ApiComparator.NO_DELTA) { |
| 479 |
root.add(new TreeNode(0, ActionMessages.CompareTaskNoChanges, null)); |
| 480 |
} else { |
| 481 |
this.delta.accept(new TreeBuilder(root)); |
| 482 |
} |
| 483 |
return model; |
| 484 |
} |
| 485 |
|
| 486 |
public String getTimestamp() { |
| 487 |
return this.timestamp; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
private IWorkbenchPartSite workbenchPartSite; |
| 492 |
private ISelection selection = null; |
| 493 |
|
| 494 |
/** |
| 495 |
* Constructor for Action1. |
| 496 |
*/ |
| 497 |
public CompareWithAction() { |
| 498 |
super(); |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) |
| 503 |
*/ |
| 504 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 505 |
workbenchPartSite = targetPart.getSite(); |
| 506 |
} |
| 507 |
/** |
| 508 |
* @see IActionDelegate#run(IAction) |
| 509 |
*/ |
| 510 |
public void run(IAction action) { |
| 511 |
if (this.selection instanceof IStructuredSelection) { |
| 512 |
final IStructuredSelection structuredSelection=(IStructuredSelection) this.selection; |
| 513 |
CompareDialog dialog = new CompareDialog(workbenchPartSite, ActionMessages.CompareDialogTitle); |
| 514 |
int returnCode = dialog.open(); |
| 515 |
if (returnCode == Window.CANCEL) return; |
| 516 |
final String baselineName = dialog.baseline; |
| 517 |
if (baselineName == null) return; |
| 518 |
final IApiBaseline baseline = ApiBaselineManager.getManager().getApiBaseline(baselineName); |
| 519 |
if (baseline == null) { |
| 520 |
return; |
| 521 |
} |
| 522 |
Job job = new Job(ActionMessages.CompareWithAction_comparing_apis){ |
| 523 |
protected IStatus run(IProgressMonitor monitor) { |
| 524 |
SubMonitor progress = SubMonitor.convert(monitor, 100); |
| 525 |
progress.subTask(ActionMessages.CompareDialogCollectingElementTaskName); |
| 526 |
SubMonitor loopProgress = progress.newChild(10).setWorkRemaining(structuredSelection.size()); |
| 527 |
final IApiScope scope = walkStructureSelection(structuredSelection, loopProgress); |
| 528 |
try { |
| 529 |
progress.subTask(ActionMessages.CompareDialogComputeDeltasTaskName); |
| 530 |
SubMonitor compareProgress = progress.newChild(98).setWorkRemaining(scope.getApiElements().length); |
| 531 |
try { |
| 532 |
IDelta delta = ApiComparator.compare(scope, baseline, VisibilityModifiers.API, false, compareProgress); |
| 533 |
ApiPlugin.getDefault().getSessionManager().addSession(new DeltaSession(delta, baselineName), true); |
| 534 |
progress.worked(1); |
| 535 |
return Status.OK_STATUS; |
| 536 |
} catch (CoreException e) { |
| 537 |
ApiPlugin.log(e); |
| 538 |
} catch(OperationCanceledException e) { |
| 539 |
// ignore |
| 540 |
} |
| 541 |
} finally { |
| 542 |
monitor.done(); |
| 543 |
} |
| 544 |
return Status.CANCEL_STATUS; |
| 545 |
} |
| 546 |
}; |
| 547 |
job.setSystem(false); |
| 548 |
job.setPriority(Job.LONG); |
| 549 |
job.schedule(); |
| 550 |
return; |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
public static ApiScope walkStructureSelection( |
| 555 |
IStructuredSelection structuredSelection, |
| 556 |
IProgressMonitor monitor) { |
| 557 |
Object[] selected=structuredSelection.toArray(); |
| 558 |
ApiScope scope = new ApiScope(); |
| 559 |
IApiBaseline workspaceBaseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); |
| 560 |
if (workspaceBaseline == null) { |
| 561 |
return scope; |
| 562 |
} |
| 563 |
for (int i=0, max = selected.length; i < max; i++) { |
| 564 |
Object currentSelection = selected[i]; |
| 565 |
if (currentSelection instanceof IJavaElement) { |
| 566 |
monitor.worked(1); |
| 567 |
IJavaElement element =(IJavaElement) currentSelection; |
| 568 |
IJavaProject javaProject = element.getJavaProject(); |
| 569 |
try { |
| 570 |
switch (element.getElementType()) { |
| 571 |
case IJavaElement.COMPILATION_UNIT: { |
| 572 |
ICompilationUnit compilationUnit = (ICompilationUnit) element; |
| 573 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 574 |
if (apiComponent != null) { |
| 575 |
addElementFor(compilationUnit, apiComponent, scope); |
| 576 |
} |
| 577 |
break; |
| 578 |
} |
| 579 |
case IJavaElement.PACKAGE_FRAGMENT: { |
| 580 |
IPackageFragment fragment = (IPackageFragment) element; |
| 581 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 582 |
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); |
| 583 |
boolean isArchive = false; |
| 584 |
if (packageFragmentRoot != null) { |
| 585 |
isArchive = packageFragmentRoot.isArchive(); |
| 586 |
} |
| 587 |
if (apiComponent != null) { |
| 588 |
addElementFor(fragment, isArchive, apiComponent, scope); |
| 589 |
} |
| 590 |
break; |
| 591 |
} |
| 592 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: { |
| 593 |
IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) element; |
| 594 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 595 |
if (apiComponent != null) { |
| 596 |
addElementFor(fragmentRoot, apiComponent, scope); |
| 597 |
} |
| 598 |
break; |
| 599 |
} |
| 600 |
case IJavaElement.JAVA_PROJECT: |
| 601 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 602 |
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); |
| 603 |
for (int j = 0, max2 = roots.length; j < max2; j++) { |
| 604 |
addElementFor(roots[j], apiComponent, scope); |
| 605 |
} |
| 606 |
break; |
| 607 |
} |
| 608 |
} catch (JavaModelException e) { |
| 609 |
ApiPlugin.log(e); |
| 610 |
} catch (CoreException e) { |
| 611 |
ApiPlugin.log(e); |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
return scope; |
| 616 |
} |
| 617 |
|
| 618 |
private static void addElementFor( |
| 619 |
IPackageFragmentRoot fragmentRoot, IApiComponent apiComponent, |
| 620 |
ApiScope scope) throws JavaModelException, CoreException { |
| 621 |
boolean isArchive = fragmentRoot.isArchive(); |
| 622 |
IJavaElement[] packageFragments = fragmentRoot.getChildren(); |
| 623 |
for (int j = 0, max2 = packageFragments.length; j < max2; j++) { |
| 624 |
IPackageFragment packageFragment = (IPackageFragment) packageFragments[j]; |
| 625 |
addElementFor(packageFragment, isArchive, apiComponent, scope); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
private static void addElementFor( |
| 630 |
IPackageFragment packageFragment, |
| 631 |
boolean isArchive, |
| 632 |
IApiComponent apiComponent, |
| 633 |
ApiScope scope) |
| 634 |
throws JavaModelException, CoreException { |
| 635 |
|
| 636 |
// add package fragment elements only if this is an API package |
| 637 |
IApiDescription apiDescription = apiComponent.getApiDescription(); |
| 638 |
IApiAnnotations annotations = apiDescription.resolveAnnotations(Factory.packageDescriptor(packageFragment.getElementName())); |
| 639 |
if (annotations == null || !VisibilityModifiers.isAPI(annotations.getVisibility())) { |
| 640 |
return; |
| 641 |
} |
| 642 |
if (isArchive) { |
| 643 |
IClassFile[] classFiles = packageFragment.getClassFiles(); |
| 644 |
for (int i = 0, max= classFiles.length; i < max; i++) { |
| 645 |
addElementFor(classFiles[i], apiComponent, scope); |
| 646 |
} |
| 647 |
} else { |
| 648 |
ICompilationUnit[] units = packageFragment.getCompilationUnits(); |
| 649 |
for (int i = 0, max= units.length; i < max; i++) { |
| 650 |
addElementFor(units[i], apiComponent, scope); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
private static void addElementFor(IClassFile classFile, |
| 656 |
IApiComponent apiComponent, ApiScope scope) { |
| 657 |
try { |
| 658 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(classFile.getType().getFullyQualifiedName()); |
| 659 |
if (typeRoot != null) { |
| 660 |
scope.add(typeRoot); |
| 661 |
} |
| 662 |
} catch (CoreException e) { |
| 663 |
ApiPlugin.log(e); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
private static void addElementFor(ICompilationUnit compilationUnit, IApiComponent component, ApiScope scope) throws JavaModelException { |
| 668 |
IType[] types = compilationUnit.getTypes(); |
| 669 |
for (int i = 0, max = types.length; i < max; i++) { |
| 670 |
try { |
| 671 |
IApiTypeRoot typeRoot = component.findTypeRoot(types[i].getFullyQualifiedName()); |
| 672 |
if (typeRoot != null) { |
| 673 |
scope.add(typeRoot); |
| 674 |
} |
| 675 |
} catch (CoreException e) { |
| 676 |
ApiPlugin.log(e); |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* @see IActionDelegate#selectionChanged(IAction, ISelection) |
| 683 |
*/ |
| 684 |
public void selectionChanged(IAction action, ISelection selection) { |
| 685 |
this.selection = selection; |
| 686 |
} |
| 687 |
} |