|
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.Date; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
import org.eclipse.core.runtime.CoreException; |
| 18 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 19 |
import org.eclipse.core.runtime.IStatus; |
| 20 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 21 |
import org.eclipse.core.runtime.Status; |
| 22 |
import org.eclipse.core.runtime.SubMonitor; |
| 23 |
import org.eclipse.core.runtime.jobs.Job; |
| 24 |
import org.eclipse.jdt.core.Flags; |
| 25 |
import org.eclipse.jdt.core.IClassFile; |
| 26 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 27 |
import org.eclipse.jdt.core.IJavaElement; |
| 28 |
import org.eclipse.jdt.core.IJavaProject; |
| 29 |
import org.eclipse.jdt.core.IPackageFragment; |
| 30 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 31 |
import org.eclipse.jdt.core.IType; |
| 32 |
import org.eclipse.jdt.core.JavaModelException; |
| 33 |
import org.eclipse.jface.action.IAction; |
| 34 |
import org.eclipse.jface.viewers.ISelection; |
| 35 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 36 |
import org.eclipse.jface.window.Window; |
| 37 |
import org.eclipse.pde.api.tools.internal.ApiBaselineManager; |
| 38 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 39 |
import org.eclipse.pde.api.tools.internal.provisional.Factory; |
| 40 |
import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
| 41 |
import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; |
| 42 |
import org.eclipse.pde.api.tools.internal.provisional.ISession; |
| 43 |
import org.eclipse.pde.api.tools.internal.provisional.ITreeModel; |
| 44 |
import org.eclipse.pde.api.tools.internal.provisional.ITreeNode; |
| 45 |
import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
| 46 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiComparator; |
| 47 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiScope; |
| 48 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.DeltaVisitor; |
| 49 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta; |
| 50 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 51 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 52 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiScope; |
| 53 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiType; |
| 54 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
| 55 |
import org.eclipse.ui.IObjectActionDelegate; |
| 56 |
import org.eclipse.ui.IWorkbenchPart; |
| 57 |
import org.eclipse.ui.IWorkbenchPartSite; |
| 58 |
|
| 59 |
import com.ibm.icu.text.DateFormat; |
| 60 |
|
| 61 |
public class CompareWithAction implements IObjectActionDelegate { |
| 62 |
|
| 63 |
public static class DeltaSession implements ISession { |
| 64 |
static Object[] NO_CHILDREN = new Object[0]; |
| 65 |
|
| 66 |
static class TreeNode implements ITreeNode { |
| 67 |
Map children; |
| 68 |
String name; |
| 69 |
TreeNode parent; |
| 70 |
Object data; |
| 71 |
int id; |
| 72 |
|
| 73 |
public TreeNode(int id, String name, Object data) { |
| 74 |
this.name = name; |
| 75 |
this.id = id; |
| 76 |
this.data = data; |
| 77 |
} |
| 78 |
public Object[] getChildren() { |
| 79 |
if (this.children == null) { |
| 80 |
return NO_CHILDREN; |
| 81 |
} |
| 82 |
return this.children.values().toArray(new Object[this.children.size()]); |
| 83 |
} |
| 84 |
public TreeNode getNode(String name) { |
| 85 |
if (this.children == null) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
return (TreeNode) this.children.get(name); |
| 89 |
} |
| 90 |
public int getId() { |
| 91 |
return this.id; |
| 92 |
} |
| 93 |
public void add(TreeNode node) { |
| 94 |
if (this.children == null) { |
| 95 |
this.children = new HashMap(); |
| 96 |
} |
| 97 |
this.children.put(node.name, node); |
| 98 |
} |
| 99 |
public boolean hasChildren() { |
| 100 |
return this.children != null && !this.children.isEmpty(); |
| 101 |
} |
| 102 |
public String toString() { |
| 103 |
return String.valueOf(this.name); |
| 104 |
} |
| 105 |
public Object getData() { |
| 106 |
return this.data; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static class TreeModel implements ITreeModel { |
| 111 |
TreeNode root; |
| 112 |
|
| 113 |
TreeModel(TreeNode root) { |
| 114 |
this.root = root; |
| 115 |
} |
| 116 |
public ITreeNode getRoot() { |
| 117 |
return this.root; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
IDelta delta; |
| 122 |
String baselineName; |
| 123 |
String timestamp; |
| 124 |
|
| 125 |
public DeltaSession(IDelta delta, String baselineName) { |
| 126 |
this.delta = delta; |
| 127 |
this.baselineName = baselineName; |
| 128 |
this.timestamp = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(new Date(System.currentTimeMillis())); |
| 129 |
} |
| 130 |
public ITreeModel getModel() { |
| 131 |
TreeNode root = new TreeNode(0, null, this.delta); |
| 132 |
TreeModel model = new TreeModel(root); |
| 133 |
class TreeBuilder extends DeltaVisitor { |
| 134 |
TreeNode node; |
| 135 |
|
| 136 |
public TreeBuilder(TreeNode node) { |
| 137 |
this.node = node; |
| 138 |
} |
| 139 |
public void endVisit(IDelta delta) { |
| 140 |
if (delta.getChildren().length == 0) { |
| 141 |
String typeName = delta.getTypeName(); |
| 142 |
if (typeName == null) { |
| 143 |
this.node.add(new TreeNode(0, delta.getKey(), delta)); |
| 144 |
} else { |
| 145 |
// split the type name (package - type) |
| 146 |
int index = typeName.lastIndexOf('.'); |
| 147 |
String packageName = "<default package>"; //$NON-NLS-1$ |
| 148 |
String actualTypeName = null; |
| 149 |
if (index != -1) { |
| 150 |
packageName = typeName.substring(0, index); |
| 151 |
actualTypeName = typeName.substring(index + 1); |
| 152 |
} else { |
| 153 |
actualTypeName = typeName; |
| 154 |
} |
| 155 |
TreeNode node2 = this.node.getNode(packageName); |
| 156 |
if (node2 == null) { |
| 157 |
node2 = new TreeNode(ITreeNode.PACKAGE, packageName, null); |
| 158 |
this.node.add(node2); |
| 159 |
} |
| 160 |
TreeNode node3 = node2.getNode(actualTypeName); |
| 161 |
if (node3 == null) { |
| 162 |
int id = 0; |
| 163 |
switch(delta.getElementType()) { |
| 164 |
case IDelta.ANNOTATION_ELEMENT_TYPE : |
| 165 |
id = ITreeNode.ANNOTATION; |
| 166 |
break; |
| 167 |
case IDelta.INTERFACE_ELEMENT_TYPE : |
| 168 |
id = ITreeNode.INTERFACE; |
| 169 |
break; |
| 170 |
case IDelta.CLASS_ELEMENT_TYPE : |
| 171 |
id = ITreeNode.CLASS; |
| 172 |
break; |
| 173 |
case IDelta.ENUM_ELEMENT_TYPE : |
| 174 |
id = ITreeNode.ENUM; |
| 175 |
default : |
| 176 |
// we need to retrieve the type kind |
| 177 |
try { |
| 178 |
String componentVersionId = delta.getComponentVersionId(); |
| 179 |
if (componentVersionId != null) { |
| 180 |
int indexOfOpen = componentVersionId.lastIndexOf('('); |
| 181 |
String componentID = componentVersionId.substring(0, indexOfOpen); |
| 182 |
String version = componentVersionId.substring(indexOfOpen + 1, componentVersionId.length() - 1); |
| 183 |
IApiBaseline baseline = ApiBaselineManager.getManager().getApiBaseline(DeltaSession.this.baselineName); |
| 184 |
int modifiers = 0; |
| 185 |
if (baseline != null) { |
| 186 |
IApiComponent apiComponent = baseline.getApiComponent(componentID); |
| 187 |
if (apiComponent != null && version.equals(apiComponent.getVersion())) { |
| 188 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(typeName); |
| 189 |
if (typeRoot != null) { |
| 190 |
IApiType structure = typeRoot.getStructure(); |
| 191 |
modifiers = structure.getModifiers(); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
if (modifiers == 0) { |
| 196 |
// try the workspace baseline |
| 197 |
baseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); |
| 198 |
if (baseline != null) { |
| 199 |
IApiComponent apiComponent = baseline.getApiComponent(componentID); |
| 200 |
if (apiComponent != null && version.equals(apiComponent.getVersion())) { |
| 201 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(typeName); |
| 202 |
if (typeRoot != null) { |
| 203 |
IApiType structure = typeRoot.getStructure(); |
| 204 |
modifiers = structure.getModifiers(); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
if (Flags.isEnum(modifiers)) { |
| 210 |
id = ITreeNode.ENUM; |
| 211 |
} else if (Flags.isAnnotation(modifiers)) { |
| 212 |
id = ITreeNode.ANNOTATION; |
| 213 |
} else if (Flags.isInterface(modifiers)) { |
| 214 |
id = ITreeNode.INTERFACE; |
| 215 |
} else { |
| 216 |
id = ITreeNode.CLASS; |
| 217 |
} |
| 218 |
} |
| 219 |
} catch (CoreException e) { |
| 220 |
// ignore |
| 221 |
} |
| 222 |
} |
| 223 |
node3 = new TreeNode(id, actualTypeName, null); |
| 224 |
node2.add(node3); |
| 225 |
} |
| 226 |
node3.add(new TreeNode(0, delta.getMessage(), delta)); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
if (this.delta == ApiComparator.NO_DELTA) { |
| 232 |
root.add(new TreeNode(0, ActionMessages.CompareTaskNoChanges, null)); |
| 233 |
} else { |
| 234 |
this.delta.accept(new TreeBuilder(root)); |
| 235 |
} |
| 236 |
return model; |
| 237 |
} |
| 238 |
|
| 239 |
public String getTimestamp() { |
| 240 |
return this.timestamp; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private IWorkbenchPartSite workbenchPartSite; |
| 245 |
private ISelection selection = null; |
| 246 |
|
| 247 |
/** |
| 248 |
* Constructor for Action1. |
| 249 |
*/ |
| 250 |
public CompareWithAction() { |
| 251 |
super(); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) |
| 256 |
*/ |
| 257 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 258 |
workbenchPartSite = targetPart.getSite(); |
| 259 |
} |
| 260 |
/** |
| 261 |
* @see IActionDelegate#run(IAction) |
| 262 |
*/ |
| 263 |
public void run(IAction action) { |
| 264 |
if (this.selection instanceof IStructuredSelection) { |
| 265 |
final IStructuredSelection structuredSelection=(IStructuredSelection) this.selection; |
| 266 |
CompareDialog dialog = new CompareDialog(workbenchPartSite, ActionMessages.CompareDialogTitle); |
| 267 |
int returnCode = dialog.open(); |
| 268 |
if (returnCode == Window.CANCEL) return; |
| 269 |
final String baselineName = dialog.baseline; |
| 270 |
if (baselineName == null) return; |
| 271 |
final IApiBaseline baseline = ApiBaselineManager.getManager().getApiBaseline(baselineName); |
| 272 |
if (baseline == null) { |
| 273 |
return; |
| 274 |
} |
| 275 |
Job job = new Job(ActionMessages.CompareWithAction_comparing_apis){ |
| 276 |
protected IStatus run(IProgressMonitor monitor) { |
| 277 |
SubMonitor progress = SubMonitor.convert(monitor, 100); |
| 278 |
progress.subTask(ActionMessages.CompareDialogCollectingElementTaskName); |
| 279 |
SubMonitor loopProgress = progress.newChild(10).setWorkRemaining(structuredSelection.size()); |
| 280 |
final IApiScope scope = walkStructureSelection(structuredSelection, loopProgress); |
| 281 |
try { |
| 282 |
progress.subTask(ActionMessages.CompareDialogComputeDeltasTaskName); |
| 283 |
SubMonitor compareProgress = progress.newChild(98).setWorkRemaining(scope.getApiElements().length); |
| 284 |
try { |
| 285 |
IDelta delta = ApiComparator.compare(scope, baseline, VisibilityModifiers.API, false, compareProgress); |
| 286 |
ApiPlugin.getDefault().getSessionManager().addSession(new DeltaSession(delta, baselineName), true); |
| 287 |
progress.worked(1); |
| 288 |
return Status.OK_STATUS; |
| 289 |
} catch (CoreException e) { |
| 290 |
ApiPlugin.log(e); |
| 291 |
} catch(OperationCanceledException e) { |
| 292 |
// ignore |
| 293 |
} |
| 294 |
} finally { |
| 295 |
monitor.done(); |
| 296 |
} |
| 297 |
return Status.CANCEL_STATUS; |
| 298 |
} |
| 299 |
}; |
| 300 |
job.setSystem(false); |
| 301 |
job.setPriority(Job.LONG); |
| 302 |
job.schedule(); |
| 303 |
return; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
public static ApiScope walkStructureSelection( |
| 308 |
IStructuredSelection structuredSelection, |
| 309 |
IProgressMonitor monitor) { |
| 310 |
Object[] selected=structuredSelection.toArray(); |
| 311 |
ApiScope scope = new ApiScope(); |
| 312 |
IApiBaseline workspaceBaseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); |
| 313 |
if (workspaceBaseline == null) { |
| 314 |
return scope; |
| 315 |
} |
| 316 |
for (int i=0, max = selected.length; i < max; i++) { |
| 317 |
Object currentSelection = selected[i]; |
| 318 |
if (currentSelection instanceof IJavaElement) { |
| 319 |
monitor.worked(1); |
| 320 |
IJavaElement element =(IJavaElement) currentSelection; |
| 321 |
IJavaProject javaProject = element.getJavaProject(); |
| 322 |
try { |
| 323 |
switch (element.getElementType()) { |
| 324 |
case IJavaElement.COMPILATION_UNIT: { |
| 325 |
ICompilationUnit compilationUnit = (ICompilationUnit) element; |
| 326 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 327 |
if (apiComponent != null) { |
| 328 |
addElementFor(compilationUnit, apiComponent, scope); |
| 329 |
} |
| 330 |
break; |
| 331 |
} |
| 332 |
case IJavaElement.PACKAGE_FRAGMENT: { |
| 333 |
IPackageFragment fragment = (IPackageFragment) element; |
| 334 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 335 |
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); |
| 336 |
boolean isArchive = false; |
| 337 |
if (packageFragmentRoot != null) { |
| 338 |
isArchive = packageFragmentRoot.isArchive(); |
| 339 |
} |
| 340 |
if (apiComponent != null) { |
| 341 |
addElementFor(fragment, isArchive, apiComponent, scope); |
| 342 |
} |
| 343 |
break; |
| 344 |
} |
| 345 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: { |
| 346 |
IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) element; |
| 347 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 348 |
if (apiComponent != null) { |
| 349 |
addElementFor(fragmentRoot, apiComponent, scope); |
| 350 |
} |
| 351 |
break; |
| 352 |
} |
| 353 |
case IJavaElement.JAVA_PROJECT: |
| 354 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 355 |
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); |
| 356 |
for (int j = 0, max2 = roots.length; j < max2; j++) { |
| 357 |
addElementFor(roots[j], apiComponent, scope); |
| 358 |
} |
| 359 |
break; |
| 360 |
} |
| 361 |
} catch (JavaModelException e) { |
| 362 |
ApiPlugin.log(e); |
| 363 |
} catch (CoreException e) { |
| 364 |
ApiPlugin.log(e); |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
return scope; |
| 369 |
} |
| 370 |
|
| 371 |
private static void addElementFor( |
| 372 |
IPackageFragmentRoot fragmentRoot, IApiComponent apiComponent, |
| 373 |
ApiScope scope) throws JavaModelException, CoreException { |
| 374 |
boolean isArchive = fragmentRoot.isArchive(); |
| 375 |
IJavaElement[] packageFragments = fragmentRoot.getChildren(); |
| 376 |
for (int j = 0, max2 = packageFragments.length; j < max2; j++) { |
| 377 |
IPackageFragment packageFragment = (IPackageFragment) packageFragments[j]; |
| 378 |
addElementFor(packageFragment, isArchive, apiComponent, scope); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
private static void addElementFor( |
| 383 |
IPackageFragment packageFragment, |
| 384 |
boolean isArchive, |
| 385 |
IApiComponent apiComponent, |
| 386 |
ApiScope scope) |
| 387 |
throws JavaModelException, CoreException { |
| 388 |
|
| 389 |
// add package fragment elements only if this is an API package |
| 390 |
IApiDescription apiDescription = apiComponent.getApiDescription(); |
| 391 |
IApiAnnotations annotations = apiDescription.resolveAnnotations(Factory.packageDescriptor(packageFragment.getElementName())); |
| 392 |
if (annotations == null || !VisibilityModifiers.isAPI(annotations.getVisibility())) { |
| 393 |
return; |
| 394 |
} |
| 395 |
if (isArchive) { |
| 396 |
IClassFile[] classFiles = packageFragment.getClassFiles(); |
| 397 |
for (int i = 0, max= classFiles.length; i < max; i++) { |
| 398 |
addElementFor(classFiles[i], apiComponent, scope); |
| 399 |
} |
| 400 |
} else { |
| 401 |
ICompilationUnit[] units = packageFragment.getCompilationUnits(); |
| 402 |
for (int i = 0, max= units.length; i < max; i++) { |
| 403 |
addElementFor(units[i], apiComponent, scope); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
private static void addElementFor(IClassFile classFile, |
| 409 |
IApiComponent apiComponent, ApiScope scope) { |
| 410 |
try { |
| 411 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(classFile.getType().getFullyQualifiedName()); |
| 412 |
if (typeRoot != null) { |
| 413 |
scope.add(typeRoot); |
| 414 |
} |
| 415 |
} catch (CoreException e) { |
| 416 |
ApiPlugin.log(e); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
private static void addElementFor(ICompilationUnit compilationUnit, IApiComponent component, ApiScope scope) throws JavaModelException { |
| 421 |
IType[] types = compilationUnit.getTypes(); |
| 422 |
for (int i = 0, max = types.length; i < max; i++) { |
| 423 |
try { |
| 424 |
IApiTypeRoot typeRoot = component.findTypeRoot(types[i].getFullyQualifiedName()); |
| 425 |
if (typeRoot != null) { |
| 426 |
scope.add(typeRoot); |
| 427 |
} |
| 428 |
} catch (CoreException e) { |
| 429 |
ApiPlugin.log(e); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* @see IActionDelegate#selectionChanged(IAction, ISelection) |
| 436 |
*/ |
| 437 |
public void selectionChanged(IAction action, ISelection selection) { |
| 438 |
this.selection = selection; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
} |