diff --git a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/util/Progress.java b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/util/Progress.java index 4e21bf0c002..09dfb3404ba 100644 --- a/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/util/Progress.java +++ b/org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/util/Progress.java @@ -14,20 +14,33 @@ package org.eclipse.jdt.internal.ui.util; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; -@SuppressWarnings("deprecation") public class Progress { private Progress() { // static Utility } public static IProgressMonitor subMonitor(IProgressMonitor monitor, int ticks) { - return new org.eclipse.core.runtime.SubProgressMonitor(monitor, ticks); + if (monitor instanceof SubMonitor) { + return ((SubMonitor) monitor).split(ticks); + } + return SubMonitor.convert(monitor, ticks); } + public static IProgressMonitor subMonitorSupressed(IProgressMonitor monitor, int ticks) { - return new org.eclipse.core.runtime.SubProgressMonitor(monitor, ticks, org.eclipse.core.runtime.SubProgressMonitor.SUPPRESS_SUBTASK_LABEL); + if (monitor instanceof SubMonitor) { + return ((SubMonitor) monitor).split(ticks, SubMonitor.SUPPRESS_SUBTASK); + } + return SubMonitor.convert(monitor, ticks).split(ticks, SubMonitor.SUPPRESS_SUBTASK); } + public static IProgressMonitor subMonitorPrepend(IProgressMonitor monitor, int ticks) { - return new org.eclipse.core.runtime.SubProgressMonitor(monitor, ticks, org.eclipse.core.runtime.SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK); + // PREPEND_MAIN_LABEL_TO_SUBTASK has no direct replacement in SubMonitor. + // Simply use split() as clients should use fully-formatted task labels instead. + if (monitor instanceof SubMonitor) { + return ((SubMonitor) monitor).split(ticks); + } + return SubMonitor.convert(monitor, ticks); } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitDocumentProvider.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitDocumentProvider.java index 36c5acbd317..458e5f55444 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitDocumentProvider.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/CompilationUnitDocumentProvider.java @@ -46,6 +46,7 @@ import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.jobs.ISchedulingRule; import org.eclipse.core.resources.IEncodedStorage; @@ -127,7 +128,6 @@ import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor; import org.eclipse.jdt.internal.ui.text.java.IProblemRequestorExtension; import org.eclipse.jdt.internal.ui.text.spelling.JavaSpellingReconcileStrategy; -import org.eclipse.jdt.internal.ui.util.Progress; public class CompilationUnitDocumentProvider extends TextFileDocumentProvider implements ICompilationUnitDocumentProvider, IAnnotationModelFactory { @@ -1312,123 +1312,103 @@ public void disconnect(Object element) { } - /** - * Creates and returns a new sub-progress monitor for the - * given parent monitor. - * - * @param monitor the parent progress monitor - * @param ticks the number of work ticks allocated from the parent monitor - * @return the new sub-progress monitor - */ - private IProgressMonitor getSubProgressMonitor(IProgressMonitor monitor, int ticks) { - if (monitor != null) - return Progress.subMonitorPrepend(monitor, ticks); - - return new NullProgressMonitor(); - } protected void commitWorkingCopy(IProgressMonitor monitor, Object element, final CompilationUnitInfo info, boolean overwrite) throws CoreException { if (monitor == null) monitor= new NullProgressMonitor(); - monitor.beginTask("", 100); //$NON-NLS-1$ + SubMonitor subMonitor = SubMonitor.convert(monitor, "", 100); //$NON-NLS-1$ - try { - IDocument document= info.fTextFileBuffer.getDocument(); - IResource resource= info.fCopy.getResource(); + IDocument document= info.fTextFileBuffer.getDocument(); + IResource resource= info.fCopy.getResource(); - Assert.isTrue(resource instanceof IFile); + Assert.isTrue(resource instanceof IFile); - boolean isSynchronized= resource.isSynchronized(IResource.DEPTH_ZERO); + boolean isSynchronized= resource.isSynchronized(IResource.DEPTH_ZERO); - /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=98327 - * Make sure file gets save in commit() if the underlying file has been deleted */ - if (!isSynchronized && isDeleted(element)) - info.fTextFileBuffer.setDirty(true); + /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=98327 + * Make sure file gets save in commit() if the underlying file has been deleted */ + if (!isSynchronized && isDeleted(element)) + info.fTextFileBuffer.setDirty(true); - if (!resource.exists()) { - // underlying resource has been deleted, just recreate file, ignore the rest - createFileFromDocument(monitor, (IFile) resource, document); - return; - } + if (!resource.exists()) { + // underlying resource has been deleted, just recreate file, ignore the rest + createFileFromDocument(subMonitor, (IFile) resource, document); + return; + } - if (fSavePolicy != null) - fSavePolicy.preSave(info.fCopy); + if (fSavePolicy != null) + fSavePolicy.preSave(info.fCopy); + + IProgressMonitor workMonitor= null; + try { + fIsAboutToSave= true; - IProgressMonitor subMonitor= null; - try { - fIsAboutToSave= true; + IPostSaveListener[] listeners= JavaPlugin.getDefault().getSaveParticipantRegistry().getEnabledPostSaveListeners(info.fCopy.getJavaProject().getProject()); - IPostSaveListener[] listeners= JavaPlugin.getDefault().getSaveParticipantRegistry().getEnabledPostSaveListeners(info.fCopy.getJavaProject().getProject()); + CoreException changedRegionException= null; + boolean needsChangedRegions= false; + try { + if (listeners.length > 0) + needsChangedRegions= SaveParticipantRegistry.isChangedRegionsRequired(info.fCopy); + } catch (CoreException ex) { + changedRegionException= ex; + } - CoreException changedRegionException= null; - boolean needsChangedRegions= false; + IRegion[] changedRegions= null; + if (needsChangedRegions) { try { - if (listeners.length > 0) - needsChangedRegions= SaveParticipantRegistry.isChangedRegionsRequired(info.fCopy); + changedRegions= EditorUtility.calculateChangedLineRegions(info.fTextFileBuffer, subMonitor.split(20)); } catch (CoreException ex) { changedRegionException= ex; + } finally { + workMonitor= subMonitor.split(50); } + } else + workMonitor= subMonitor.split(listeners.length > 0 ? 70 : 100); - IRegion[] changedRegions= null; - if (needsChangedRegions) { - try { - changedRegions= EditorUtility.calculateChangedLineRegions(info.fTextFileBuffer, getSubProgressMonitor(monitor, 20)); - } catch (CoreException ex) { - changedRegionException= ex; - } finally { - subMonitor= getSubProgressMonitor(monitor, 50); - } - } else - subMonitor= getSubProgressMonitor(monitor, listeners.length > 0 ? 70 : 100); - - info.fCopy.commitWorkingCopy(overwrite || isSynchronized, subMonitor); - if (listeners.length > 0) - notifyPostSaveListeners(info, changedRegions, listeners, getSubProgressMonitor(monitor, 30)); + info.fCopy.commitWorkingCopy(overwrite || isSynchronized, workMonitor); + if (listeners.length > 0) + notifyPostSaveListeners(info, changedRegions, listeners, subMonitor.split(30)); - if (changedRegionException != null) { - throw changedRegionException; - } - } catch (JavaModelException x) { - // inform about the failure - fireElementStateChangeFailed(element); - if (IJavaModelStatusConstants.UPDATE_CONFLICT == x.getStatus().getCode()) - // convert JavaModelException to CoreException - throw new CoreException(new Status(IStatus.WARNING, JavaUI.ID_PLUGIN, IResourceStatus.OUT_OF_SYNC_LOCAL, JavaEditorMessages.CompilationUnitDocumentProvider_error_outOfSync, null)); - throw x; - } catch (CoreException | RuntimeException x) { - // inform about the failure - fireElementStateChangeFailed(element); - throw x; - } finally { - fIsAboutToSave= false; - if (subMonitor != null) - subMonitor.done(); + if (changedRegionException != null) { + throw changedRegionException; } + } catch (JavaModelException x) { + // inform about the failure + fireElementStateChangeFailed(element); + if (IJavaModelStatusConstants.UPDATE_CONFLICT == x.getStatus().getCode()) + // convert JavaModelException to CoreException + throw new CoreException(new Status(IStatus.WARNING, JavaUI.ID_PLUGIN, IResourceStatus.OUT_OF_SYNC_LOCAL, JavaEditorMessages.CompilationUnitDocumentProvider_error_outOfSync, null)); + throw x; + } catch (CoreException | RuntimeException x) { + // inform about the failure + fireElementStateChangeFailed(element); + throw x; + } finally { + fIsAboutToSave= false; + } - // If here, the dirty state of the editor will change to "not dirty". - // Thus, the state changing flag will be reset. - if (info.fModel instanceof AbstractMarkerAnnotationModel) { - AbstractMarkerAnnotationModel model= (AbstractMarkerAnnotationModel) info.fModel; - model.updateMarkers(document); - } + // If here, the dirty state of the editor will change to "not dirty". + // Thus, the state changing flag will be reset. + if (info.fModel instanceof AbstractMarkerAnnotationModel) { + AbstractMarkerAnnotationModel model= (AbstractMarkerAnnotationModel) info.fModel; + model.updateMarkers(document); + } - if (fSavePolicy != null) { - ICompilationUnit unit= fSavePolicy.postSave(info.fCopy); - if (unit != null && info.fModel instanceof AbstractMarkerAnnotationModel) { - IResource r= unit.getResource(); - IMarker[] markers= r.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO); - if (markers != null && markers.length > 0) { - AbstractMarkerAnnotationModel model= (AbstractMarkerAnnotationModel) info.fModel; - for (IMarker marker : markers) { - model.updateMarker(document, marker, null); - } + if (fSavePolicy != null) { + ICompilationUnit unit= fSavePolicy.postSave(info.fCopy); + if (unit != null && info.fModel instanceof AbstractMarkerAnnotationModel) { + IResource r= unit.getResource(); + IMarker[] markers= r.findMarkers(IMarker.MARKER, true, IResource.DEPTH_ZERO); + if (markers != null && markers.length > 0) { + AbstractMarkerAnnotationModel model= (AbstractMarkerAnnotationModel) info.fModel; + for (IMarker marker : markers) { + model.updateMarker(document, marker, null); } } } - } finally { - monitor.done(); } } @@ -1597,7 +1577,7 @@ protected void notifyPostSaveListeners(final CompilationUnitInfo info, final IRe String message= JavaEditorMessages.CompilationUnitDocumentProvider_error_saveParticipantProblem; final MultiStatus errorStatus= new MultiStatus(JavaUI.ID_PLUGIN, IJavaStatusConstants.EDITOR_POST_SAVE_NOTIFICATION, message, null); - monitor.beginTask(JavaEditorMessages.CompilationUnitDocumentProvider_progressNotifyingSaveParticipants, listeners.length * 5); + final SubMonitor subMonitor = SubMonitor.convert(monitor, JavaEditorMessages.CompilationUnitDocumentProvider_progressNotifyingSaveParticipants, listeners.length * 5); try { for (IPostSaveListener listener : listeners) { final String participantName= listener.getName(); @@ -1607,7 +1587,7 @@ public void run() { try { long stamp= unit.getResource().getModificationStamp(); - listener.saved(unit, changedRegions, getSubProgressMonitor(monitor, 4)); + listener.saved(unit, changedRegions, subMonitor.split(4)); if (stamp != unit.getResource().getModificationStamp()) { String msg= Messages.format(JavaEditorMessages.CompilationUnitDocumentProvider_error_saveParticipantSavedFile, participantName); @@ -1615,15 +1595,13 @@ public void run() { } if (buffer.hasUnsavedChanges()) - buffer.save(getSubProgressMonitor(monitor, 1), true); + buffer.save(subMonitor.split(1), true); if (stamp != unit.getResource().getModificationStamp()) { unit.updateTimeStamp(); } } catch (CoreException ex) { handleException(ex); - } finally { - monitor.worked(1); } } @@ -1638,7 +1616,7 @@ public void handleException(Throwable ex) { // Revert the changes if (buffer.hasUnsavedChanges()) { try { - info.fTextFileBuffer.revert(getSubProgressMonitor(monitor, 1)); + info.fTextFileBuffer.revert(subMonitor.split(1)); } catch (CoreException e) { msg= Messages.format("Error on revert after failure of save participant ''{0}''.", participantName); //$NON-NLS-1$ IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.EDITOR_POST_SAVE_NOTIFICATION, msg, ex); @@ -1654,7 +1632,7 @@ public void handleException(Throwable ex) { // XXX: Work in progress 'Save As' case // else if (buffer.hasUnsavedChanges()) { // try { -// buffer.save(getSubProgressMonitor(monitor, 1), true); +// buffer.save(subMonitor.split(1), true); // } catch (JavaModelException e) { // message= Messages.format("Error reverting changes after failure of save participant ''{0}''.", participantName); //$NON-NLS-1$ // IStatus status= new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.OK, message, ex); @@ -1664,10 +1642,10 @@ public void handleException(Throwable ex) { } }); } - } finally { - monitor.done(); if (!errorStatus.isOK()) throw new CoreException(errorStatus); + } finally { + subMonitor.done(); } } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java index 4edeebb83ea..d28e8567f99 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/EditorUtility.java @@ -31,10 +31,10 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.ISafeRunnable; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.content.IContentDescription; import org.eclipse.core.runtime.content.IContentType; @@ -105,7 +105,6 @@ import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.compare.JavaCompareUtilities; import org.eclipse.jdt.internal.ui.text.LineComparator; -import org.eclipse.jdt.internal.ui.util.Progress; /** @@ -801,19 +800,18 @@ public void handleException(Throwable exception) { */ @Override public void run() throws Exception { - monitor.beginTask(JavaEditorMessages.CompilationUnitDocumentProvider_calculatingChangedRegions_message, 20); + SubMonitor subMonitor = SubMonitor.convert(monitor, JavaEditorMessages.CompilationUnitDocumentProvider_calculatingChangedRegions_message, 20); IPath location= buffer.getLocation(); ITextFileBufferManager fileBufferManager= FileBuffers.createTextFileBufferManager(); - fileBufferManager.connect(location, LocationKind.NORMALIZE, getSubProgressMonitor(monitor, 15)); + fileBufferManager.connect(location, LocationKind.NORMALIZE, subMonitor.split(15)); try { IDocument currentDocument= buffer.getDocument(); IDocument oldDocument= fileBufferManager.getTextFileBuffer(location, LocationKind.NORMALIZE).getDocument(); result[0]= getChangedLineRegions(oldDocument, currentDocument); } finally { - fileBufferManager.disconnect(location, LocationKind.NORMALIZE, getSubProgressMonitor(monitor, 5)); - monitor.done(); + fileBufferManager.disconnect(location, LocationKind.NORMALIZE, subMonitor.split(5)); } } @@ -873,21 +871,6 @@ private IRegion[] getChangedLineRegions(IDocument oldDocument, IDocument current return result[0]; } - /** - * Creates and returns a new sub-progress monitor for the - * given parent monitor. - * - * @param monitor the parent progress monitor - * @param ticks the number of work ticks allocated from the parent monitor - * @return the new sub-progress monitor - * @since 3.4 - */ - private static IProgressMonitor getSubProgressMonitor(IProgressMonitor monitor, int ticks) { - if (monitor != null) - return Progress.subMonitorPrepend(monitor, ticks); - - return new NullProgressMonitor(); - } private EditorUtility() { } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java index 210234e25dc..2fea3a7fbfc 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/wizards/NewTypeWizardPage.java @@ -158,7 +158,6 @@ import org.eclipse.jdt.internal.ui.refactoring.contentassist.ControlContentAssistHelper; import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaPackageCompletionProcessor; import org.eclipse.jdt.internal.ui.refactoring.contentassist.JavaTypeCompletionProcessor; -import org.eclipse.jdt.internal.ui.util.Progress; import org.eclipse.jdt.internal.ui.util.SWTUtil; import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages; import org.eclipse.jdt.internal.ui.wizards.SuperInterfaceSelectionDialog; @@ -2739,7 +2738,7 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt monitor= new NullProgressMonitor(); } - monitor.beginTask(NewWizardMessages.NewTypeWizardPage_operationdesc, 8); + SubMonitor subMonitor= SubMonitor.convert(monitor, NewWizardMessages.NewTypeWizardPage_operationdesc, 8); IPackageFragmentRoot root= getPackageFragmentRoot(); IPackageFragment pack= getPackageFragment(); @@ -2749,9 +2748,9 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt if (!pack.exists()) { String packName= pack.getElementName(); - pack= root.createPackageFragment(packName, true, Progress.subMonitor(monitor, 1)); + pack= root.createPackageFragment(packName, true, subMonitor.split(1)); } else { - monitor.worked(1); + subMonitor.split(1); } boolean needsSave; @@ -2773,11 +2772,11 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt lineDelimiter= StubUtility.getLineDelimiterUsed(pack.getJavaProject()); String cuName= getCompilationUnitName(typeName); - ICompilationUnit parentCU= pack.createCompilationUnit(cuName, "", false, Progress.subMonitor(monitor, 2)); //$NON-NLS-1$ + ICompilationUnit parentCU= pack.createCompilationUnit(cuName, "", false, subMonitor.split(2)); //$NON-NLS-1$ // create a working copy with a new owner needsSave= true; - parentCU.becomeWorkingCopy(Progress.subMonitor(monitor, 1)); // cu is now a (primary) working copy + parentCU.becomeWorkingCopy(subMonitor.split(1)); // cu is now a (primary) working copy connectedCU= parentCU; IBuffer buffer= parentCU.getBuffer(); @@ -2812,7 +2811,7 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt ICompilationUnit parentCU= enclosingType.getCompilationUnit(); needsSave= !parentCU.isWorkingCopy(); - parentCU.becomeWorkingCopy(Progress.subMonitor(monitor, 1)); // cu is now for sure (primary) a working copy + parentCU.becomeWorkingCopy(subMonitor.split(1)); // cu is now for sure (primary) a working copy connectedCU= parentCU; CompilationUnit astRoot= createASTForImports(parentCU); @@ -2851,7 +2850,7 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt sibling = elems.length > 0 ? elems[0] : null; } - createdType= enclosingType.createType(content.toString(), sibling, false, Progress.subMonitor(monitor, 2)); + createdType= enclosingType.createType(content.toString(), sibling, false, subMonitor.split(2)); indent= StubUtility.getIndentUsed(enclosingType) + 1; } @@ -2863,7 +2862,7 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt ICompilationUnit cu= createdType.getCompilationUnit(); - imports.create(false, Progress.subMonitor(monitor, 1)); + imports.create(false, subMonitor.split(1)); JavaModelUtil.reconcile(cu); @@ -2875,10 +2874,10 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt CompilationUnit astRoot= createASTForImports(imports.getCompilationUnit()); imports= new ImportsManager(astRoot); - createTypeMembers(createdType, imports, Progress.subMonitor(monitor, 1)); + createTypeMembers(createdType, imports, subMonitor.split(1)); // add imports - imports.create(false, Progress.subMonitor(monitor, 1)); + imports.create(false, subMonitor.split(1)); removeUnusedImports(cu, existingImports, false); @@ -2902,9 +2901,9 @@ public void createType(IProgressMonitor monitor) throws CoreException, Interrupt fCreatedType= createdType; if (needsSave) { - cu.commitWorkingCopy(true, Progress.subMonitor(monitor, 1)); + cu.commitWorkingCopy(true, subMonitor.split(1)); } else { - monitor.worked(1); + subMonitor.split(1); } updateSealedSuperTypes();