|
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.io.BufferedWriter; |
| 14 |
import java.io.File; |
| 15 |
import java.io.FileWriter; |
| 16 |
import java.io.IOException; |
| 17 |
import java.lang.reflect.InvocationTargetException; |
| 18 |
|
| 19 |
import org.eclipse.core.runtime.CoreException; |
| 20 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 21 |
import org.eclipse.core.runtime.OperationCanceledException; |
| 22 |
import org.eclipse.core.runtime.SubMonitor; |
| 23 |
import org.eclipse.jdt.core.IClassFile; |
| 24 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 25 |
import org.eclipse.jdt.core.IJavaElement; |
| 26 |
import org.eclipse.jdt.core.IJavaProject; |
| 27 |
import org.eclipse.jdt.core.IPackageFragment; |
| 28 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
| 29 |
import org.eclipse.jdt.core.IType; |
| 30 |
import org.eclipse.jdt.core.JavaModelException; |
| 31 |
import org.eclipse.jface.action.IAction; |
| 32 |
import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 33 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
| 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.comparator.DeltaXmlVisitor; |
| 39 |
import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
| 40 |
import org.eclipse.pde.api.tools.internal.provisional.Factory; |
| 41 |
import org.eclipse.pde.api.tools.internal.provisional.IApiAnnotations; |
| 42 |
import org.eclipse.pde.api.tools.internal.provisional.IApiDescription; |
| 43 |
import org.eclipse.pde.api.tools.internal.provisional.VisibilityModifiers; |
| 44 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiComparator; |
| 45 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.ApiScope; |
| 46 |
import org.eclipse.pde.api.tools.internal.provisional.comparator.IDelta; |
| 47 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiBaseline; |
| 48 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiComponent; |
| 49 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiScope; |
| 50 |
import org.eclipse.pde.api.tools.internal.provisional.model.IApiTypeRoot; |
| 51 |
import org.eclipse.pde.api.tools.internal.util.Util; |
| 52 |
import org.eclipse.pde.api.tools.ui.internal.ApiUIPlugin; |
| 53 |
import org.eclipse.ui.IObjectActionDelegate; |
| 54 |
import org.eclipse.ui.IWorkbenchPart; |
| 55 |
import org.eclipse.ui.IWorkbenchPartSite; |
| 56 |
|
| 57 |
public class CompareWithAction implements IObjectActionDelegate { |
| 58 |
|
| 59 |
private IWorkbenchPartSite workbenchPartSite; |
| 60 |
private ISelection selection = null; |
| 61 |
|
| 62 |
/** |
| 63 |
* Constructor for Action1. |
| 64 |
*/ |
| 65 |
public CompareWithAction() { |
| 66 |
super(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) |
| 71 |
*/ |
| 72 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
| 73 |
workbenchPartSite = targetPart.getSite(); |
| 74 |
} |
| 75 |
/** |
| 76 |
* @see IActionDelegate#run(IAction) |
| 77 |
*/ |
| 78 |
public void run(IAction action) { |
| 79 |
if (this.selection instanceof IStructuredSelection) { |
| 80 |
final IStructuredSelection structuredSelection=(IStructuredSelection) this.selection; |
| 81 |
CompareDialog dialog = new CompareDialog(workbenchPartSite, ActionMessages.CompareDialogTitle); |
| 82 |
int returnCode = dialog.open(); |
| 83 |
if (returnCode == Window.CANCEL) return; |
| 84 |
final String baselineName = dialog.baseline; |
| 85 |
final String reportFileName = dialog.reportFileName; |
| 86 |
final boolean convertToHtml = dialog.convertToHtml; |
| 87 |
if (baselineName == null || reportFileName == null) return; |
| 88 |
final IApiBaseline baseline = ApiBaselineManager.getManager().getApiBaseline(baselineName); |
| 89 |
if (baseline == null) return; |
| 90 |
final ProgressMonitorDialog progressDialog=new ProgressMonitorDialog(workbenchPartSite.getShell()); |
| 91 |
try { |
| 92 |
progressDialog.run(true, true, new IRunnableWithProgress() { |
| 93 |
public void run(IProgressMonitor monitor) |
| 94 |
throws InvocationTargetException, InterruptedException { |
| 95 |
// Convert the given monitor into a progress instance |
| 96 |
SubMonitor progress = SubMonitor.convert(monitor, 100); |
| 97 |
progress.subTask(ActionMessages.CompareDialogCollectingElementTaskName); |
| 98 |
SubMonitor loopProgress = progress.newChild(10).setWorkRemaining(structuredSelection.size()); |
| 99 |
final IApiScope scope = walkStructureSelection(structuredSelection, loopProgress); |
| 100 |
try { |
| 101 |
progress.subTask(ActionMessages.CompareDialogComputeDeltasTaskName); |
| 102 |
SubMonitor compareProgress = progress.newChild(98).setWorkRemaining(scope.getApiElement().length); |
| 103 |
IDelta delta = ApiComparator.compare(scope, baseline, VisibilityModifiers.API, compareProgress); |
| 104 |
progress.worked(1); |
| 105 |
progress.subTask(ActionMessages.CompareDialogCreateReportTaskName); |
| 106 |
Util.checkCanceled(progress); |
| 107 |
File outputFile = new File(reportFileName); |
| 108 |
BufferedWriter writer = null; |
| 109 |
try { |
| 110 |
if (outputFile.exists()) { |
| 111 |
outputFile.delete(); |
| 112 |
} |
| 113 |
writer = new BufferedWriter(new FileWriter(outputFile)); |
| 114 |
DeltaXmlVisitor visitor = new DeltaXmlVisitor(); |
| 115 |
delta.accept(visitor); |
| 116 |
writer.write(visitor.getXML()); |
| 117 |
writer.flush(); |
| 118 |
} catch (IOException e) { |
| 119 |
ApiPlugin.log(e); |
| 120 |
} catch (CoreException e) { |
| 121 |
ApiPlugin.log(e); |
| 122 |
} catch(OperationCanceledException e) { |
| 123 |
// ignore |
| 124 |
} finally { |
| 125 |
try { |
| 126 |
if (writer != null) { |
| 127 |
writer.close(); |
| 128 |
} |
| 129 |
} catch(IOException e) { |
| 130 |
// ignore |
| 131 |
} |
| 132 |
} |
| 133 |
progress.worked(1); |
| 134 |
} finally { |
| 135 |
monitor.done(); |
| 136 |
} |
| 137 |
if(convertToHtml) { |
| 138 |
// remaining part is to convert the xml file to html using xslt |
| 139 |
} |
| 140 |
} |
| 141 |
}); |
| 142 |
} catch (InvocationTargetException e) { |
| 143 |
ApiUIPlugin.log(e); |
| 144 |
} catch (InterruptedException e) { |
| 145 |
return; |
| 146 |
} |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public static ApiScope walkStructureSelection( |
| 152 |
IStructuredSelection structuredSelection, |
| 153 |
IProgressMonitor monitor) { |
| 154 |
Object[] selected=structuredSelection.toArray(); |
| 155 |
ApiScope scope = new ApiScope(); |
| 156 |
IApiBaseline workspaceBaseline = ApiBaselineManager.getManager().getWorkspaceBaseline(); |
| 157 |
if (workspaceBaseline == null) { |
| 158 |
return scope; |
| 159 |
} |
| 160 |
for (int i=0, max = selected.length; i < max; i++) { |
| 161 |
Object currentSelection = selected[i]; |
| 162 |
if (currentSelection instanceof IJavaElement) { |
| 163 |
monitor.worked(1); |
| 164 |
IJavaElement element =(IJavaElement) currentSelection; |
| 165 |
IJavaProject javaProject = element.getJavaProject(); |
| 166 |
try { |
| 167 |
switch (element.getElementType()) { |
| 168 |
case IJavaElement.COMPILATION_UNIT: { |
| 169 |
ICompilationUnit compilationUnit = (ICompilationUnit) element; |
| 170 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 171 |
if (apiComponent != null) { |
| 172 |
addElementFor(compilationUnit, apiComponent, scope); |
| 173 |
} |
| 174 |
break; |
| 175 |
} |
| 176 |
case IJavaElement.PACKAGE_FRAGMENT: { |
| 177 |
IPackageFragment fragment = (IPackageFragment) element; |
| 178 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 179 |
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); |
| 180 |
boolean isArchive = false; |
| 181 |
if (packageFragmentRoot != null) { |
| 182 |
isArchive = packageFragmentRoot.isArchive(); |
| 183 |
} |
| 184 |
if (apiComponent != null) { |
| 185 |
addElementFor(fragment, isArchive, apiComponent, scope); |
| 186 |
} |
| 187 |
break; |
| 188 |
} |
| 189 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: { |
| 190 |
IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) element; |
| 191 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 192 |
if (apiComponent != null) { |
| 193 |
addElementFor(fragmentRoot, apiComponent, scope); |
| 194 |
} |
| 195 |
break; |
| 196 |
} |
| 197 |
case IJavaElement.JAVA_PROJECT: |
| 198 |
IApiComponent apiComponent = workspaceBaseline.getApiComponent(javaProject.getElementName()); |
| 199 |
IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots(); |
| 200 |
for (int j = 0, max2 = roots.length; j < max2; j++) { |
| 201 |
addElementFor(roots[j], apiComponent, scope); |
| 202 |
} |
| 203 |
break; |
| 204 |
} |
| 205 |
} catch (JavaModelException e) { |
| 206 |
ApiPlugin.log(e); |
| 207 |
} catch (CoreException e) { |
| 208 |
ApiPlugin.log(e); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
return scope; |
| 213 |
} |
| 214 |
|
| 215 |
private static void addElementFor( |
| 216 |
IPackageFragmentRoot fragmentRoot, IApiComponent apiComponent, |
| 217 |
ApiScope scope) throws JavaModelException, CoreException { |
| 218 |
boolean isArchive = fragmentRoot.isArchive(); |
| 219 |
IJavaElement[] packageFragments = fragmentRoot.getChildren(); |
| 220 |
for (int j = 0, max2 = packageFragments.length; j < max2; j++) { |
| 221 |
IPackageFragment packageFragment = (IPackageFragment) packageFragments[j]; |
| 222 |
addElementFor(packageFragment, isArchive, apiComponent, scope); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private static void addElementFor( |
| 227 |
IPackageFragment packageFragment, |
| 228 |
boolean isArchive, |
| 229 |
IApiComponent apiComponent, |
| 230 |
ApiScope scope) |
| 231 |
throws JavaModelException, CoreException { |
| 232 |
|
| 233 |
// add package fragment elements only if this is an API package |
| 234 |
IApiDescription apiDescription = apiComponent.getApiDescription(); |
| 235 |
IApiAnnotations annotations = apiDescription.resolveAnnotations(Factory.packageDescriptor(packageFragment.getElementName())); |
| 236 |
if (annotations == null || !VisibilityModifiers.isAPI(annotations.getVisibility())) { |
| 237 |
return; |
| 238 |
} |
| 239 |
if (isArchive) { |
| 240 |
IClassFile[] classFiles = packageFragment.getClassFiles(); |
| 241 |
for (int i = 0, max= classFiles.length; i < max; i++) { |
| 242 |
addElementFor(classFiles[i], apiComponent, scope); |
| 243 |
} |
| 244 |
} else { |
| 245 |
ICompilationUnit[] units = packageFragment.getCompilationUnits(); |
| 246 |
for (int i = 0, max= units.length; i < max; i++) { |
| 247 |
addElementFor(units[i], apiComponent, scope); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
private static void addElementFor(IClassFile classFile, |
| 253 |
IApiComponent apiComponent, ApiScope scope) { |
| 254 |
try { |
| 255 |
IApiTypeRoot typeRoot = apiComponent.findTypeRoot(classFile.getType().getFullyQualifiedName()); |
| 256 |
if (typeRoot != null) { |
| 257 |
scope.add(typeRoot); |
| 258 |
} |
| 259 |
} catch (CoreException e) { |
| 260 |
ApiPlugin.log(e); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
private static void addElementFor(ICompilationUnit compilationUnit, IApiComponent component, ApiScope scope) throws JavaModelException { |
| 265 |
IType[] types = compilationUnit.getTypes(); |
| 266 |
for (int i = 0, max = types.length; i < max; i++) { |
| 267 |
try { |
| 268 |
IApiTypeRoot typeRoot = component.findTypeRoot(types[i].getFullyQualifiedName()); |
| 269 |
if (typeRoot != null) { |
| 270 |
scope.add(typeRoot); |
| 271 |
} |
| 272 |
} catch (CoreException e) { |
| 273 |
ApiPlugin.log(e); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* @see IActionDelegate#selectionChanged(IAction, ISelection) |
| 280 |
*/ |
| 281 |
public void selectionChanged(IAction action, ISelection selection) { |
| 282 |
this.selection = selection; |
| 283 |
} |
| 284 |
|
| 285 |
} |