diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/BundleManifestChange.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/BundleManifestChange.java index 62e6806501..2f96cd8a4c 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/BundleManifestChange.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/BundleManifestChange.java @@ -17,6 +17,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; import org.eclipse.core.filebuffers.FileBuffers; import org.eclipse.core.filebuffers.ITextFileBufferManager; @@ -53,22 +55,9 @@ public class BundleManifestChange { public static Change createMoveToPackageChange(IFile file, MoveFromChange change, IProgressMonitor monitor) throws CoreException { - try { - Bundle bundle = getBundle(file, monitor); - if (bundle == null) { - return null; - } - - BundleModel model = (BundleModel) bundle.getModel(); - BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument()); - bundle.getModel().addModelChangedListener(listener); + return recordBundleChanges(file, monitor, f -> new TextFileChange("", f), bundle -> { //$NON-NLS-1$ addPackage(bundle, change); - return createChange(listener, file); - } catch (CoreException | MalformedTreeException e) { - } finally { - FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor); - } - return null; + }); } public static Change createDeletePackagesChange(IFile file, Collection packages, @@ -77,87 +66,39 @@ public static Change createDeletePackagesChange(IFile file, Collection new TextFileChange("", f), bundle -> { //$NON-NLS-1$ BasePackageHeader header = (BasePackageHeader) bundle.getManifestHeader(Constants.EXPORT_PACKAGE); if (header != null) { packageNames.stream().filter(header::hasPackage).forEach(name -> removePackage(header, name)); } - TextEdit[] operations = listener.getTextOperations(); - if (operations.length > 0) { - TextFileChange change = new TextFileChange("", file); //$NON-NLS-1$ - MultiTextEdit multi = new MultiTextEdit(); - multi.addChildren(operations); - change.setEdit(multi); - PDEModelUtility.setChangeTextType(change, file); - return change; - } - } catch (CoreException | MalformedTreeException e) { - } finally { - FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor); - } - return null; + }); } public static MoveFromChange createMovePackageChange(IFile file, Object[] elements, IProgressMonitor monitor) throws CoreException { - try { - Bundle bundle = getBundle(file, monitor); - if (bundle == null) { - return null; - } + List list = new ArrayList<>(); + MoveFromChange change = recordBundleChanges(file, monitor, f -> new MoveFromChange("", f), bundle -> { //$NON-NLS-1$ - BundleModel model = (BundleModel) bundle.getModel(); - BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument()); - bundle.getModel().addModelChangedListener(listener); - - ArrayList list = new ArrayList<>(); for (Object element : elements) { if (element instanceof IJavaElement) { String packageName = ((IJavaElement) element).getElementName(); - PDEManifestElement export = removePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE), packageName); + PDEManifestElement export = removePackage(bundle.getManifestHeader(Constants.EXPORT_PACKAGE), + packageName); if (export != null) { list.add(export); } } } - - TextEdit[] operations = listener.getTextOperations(); - if (operations.length > 0) { - MoveFromChange change = new MoveFromChange("", file); //$NON-NLS-1$ - MultiTextEdit edit = new MultiTextEdit(); - edit.addChildren(operations); - change.setEdit(edit); - PDEModelUtility.setChangeTextType(change, file); - if (!list.isEmpty()) { - change.setMovedElements(list.toArray(new PDEManifestElement[list.size()])); - } - return change; - } - } catch (CoreException | MalformedTreeException e) { - } finally { - FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor); + }); + if (change != null && !list.isEmpty()) { + change.setMovedElements(list.toArray(PDEManifestElement[]::new)); } - return null; + return change; } public static Change createRenameChange(IFile file, Object[] elements, String[] newTexts, IProgressMonitor monitor) throws CoreException { - try { - Bundle bundle = getBundle(file, monitor); - if (bundle == null) { - return null; - } + return recordBundleChanges(file, monitor, f -> new TextFileChange("", f), bundle -> { //$NON-NLS-1$ - BundleModel model = (BundleModel) bundle.getModel(); - BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument()); - bundle.getModel().addModelChangedListener(listener); boolean localizationRenamed = false; for (int i = 0; i < elements.length; i++) { Object element = elements[i]; @@ -185,7 +126,32 @@ public static Change createRenameChange(IFile file, Object[] elements, String[] renamePackage(bundle.getManifestHeader(Constants.IMPORT_PACKAGE), oldText, newText); } } - return createChange(listener, file); + }); + } + + private static C recordBundleChanges(IFile file, IProgressMonitor monitor, + Function changeFactory, Consumer bundleChanger) throws CoreException { + try { + Bundle bundle = getBundle(file, monitor); + if (bundle == null) { + return null; + } + BundleModel model = (BundleModel) bundle.getModel(); + BundleTextChangeListener listener = new BundleTextChangeListener(model.getDocument()); + bundle.getModel().addModelChangedListener(listener); + + bundleChanger.accept(bundle); + + TextEdit[] operations = listener.getTextOperations(); + if (operations.length > 0) { + C change = changeFactory.apply(file); + MultiTextEdit edit = new MultiTextEdit(); + edit.addChildren(operations); + change.setEdit(edit); + PDEModelUtility.setChangeTextType(change, file); + return change; + } + return null; } catch (CoreException | MalformedTreeException e) { } finally { FileBuffers.getTextFileBufferManager().disconnect(file.getFullPath(), LocationKind.NORMALIZE, monitor); @@ -193,19 +159,6 @@ public static Change createRenameChange(IFile file, Object[] elements, String[] return null; } - private static Change createChange(BundleTextChangeListener listener, IFile file) { - TextEdit[] operations = listener.getTextOperations(); - if (operations.length > 0) { - TextFileChange change = new TextFileChange("", file); //$NON-NLS-1$ - MultiTextEdit edit = new MultiTextEdit(); - edit.addChildren(operations); - change.setEdit(edit); - PDEModelUtility.setChangeTextType(change, file); - return change; - } - return null; - } - private static void renameLocalization(Bundle bundle, String oldText, String newText) { if (newText.endsWith(".properties")) { //$NON-NLS-1$ bundle.setHeader(Constants.BUNDLE_LOCALIZATION, LocaleUtil.trimLocalization(newText)); diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestPackageDeleteParticipant.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestPackageDeleteParticipant.java index 9d4e1aa349..d6c3f04a2f 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestPackageDeleteParticipant.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestPackageDeleteParticipant.java @@ -37,7 +37,7 @@ public class ManifestPackageDeleteParticipant extends PDEDeleteParticipant { private IProject fProject; - private Set fPackages = new HashSet<>(); + private final Set fPackages = new HashSet<>(); @Override protected boolean initialize(Object element) { diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestTypeDeleteParticipant.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestTypeDeleteParticipant.java index ada50e5754..f2dc9e6725 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestTypeDeleteParticipant.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/refactoring/ManifestTypeDeleteParticipant.java @@ -13,9 +13,9 @@ *******************************************************************************/ package org.eclipse.pde.internal.ui.refactoring; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -42,7 +42,7 @@ public class ManifestTypeDeleteParticipant extends PDEDeleteParticipant { private IProject fProject; - private Set fTypes = new LinkedHashSet<>(); + private final Set fTypes = new HashSet<>(); @Override protected boolean initialize(Object element) { @@ -99,7 +99,6 @@ protected void addChanges(CompositeChange result, IProgressMonitor pm) throws Co if (change != null) { result.add(change); } - } /** @@ -118,14 +117,7 @@ private boolean willPackageBeEmpty(IPackageFragment pkg, Set d } // Check if any compilation unit in the package is NOT being deleted ICompilationUnit[] compilationUnits = pkg.getCompilationUnits(); - for (ICompilationUnit cu : compilationUnits) { - if (!deletedCUs.contains(cu)) { - // This compilation unit is not being deleted, so package is not - // empty - return false; - } - } - return true; + return deletedCUs.containsAll(Arrays.asList(compilationUnits)); } }