Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -1607,23 +1587,21 @@ 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);
errorStatus.add(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IJavaStatusConstants.EDITOR_POST_SAVE_NOTIFICATION, msg, null));
}

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);
}
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -1664,10 +1642,10 @@ public void handleException(Throwable ex) {
}
});
}
} finally {
monitor.done();
if (!errorStatus.isOK())
throw new CoreException(errorStatus);
} finally {
subMonitor.done();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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() {
}
Expand Down
Loading
Loading