From 4ec47dbc35b8b35e0d98e33a4bdae0889a54b8de Mon Sep 17 00:00:00 2001 From: Sergey Kovalchuk Date: Wed, 8 Jan 2020 10:39:41 +0300 Subject: [PATCH] Bug 558474 - CleanUp deprecated methods in org.eclipse.cdt.ui Reworked deprecated usages of SubProgressMonitor by SubMonitor (Part 5 Latest) Change-Id: Ia7a06893c5acda374f2045666447ec494b8fccdc Signed-off-by: Sergey Kovalchuk --- .../refactoring/ChangeExceptionHandler.java | 11 +- .../refactoring/rename/TextSearchWrapper.java | 12 +- .../TeamProjectIndexExportWizardPage.java | 6 +- .../cdt/ui/actions/FormatAllAction.java | 114 ++++++++---------- .../cdt/ui/dialogs/BinaryParserBlock.java | 20 ++- .../cdt/ui/dialogs/ReferenceBlock.java | 18 +-- .../cdt/ui/dialogs/TabFolderOptionBlock.java | 30 ++--- 7 files changed, 89 insertions(+), 122 deletions(-) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeExceptionHandler.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeExceptionHandler.java index 2391ddf4b4c..bb398fc0bbc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeExceptionHandler.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeExceptionHandler.java @@ -26,7 +26,7 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.ProgressMonitorWrapper; import org.eclipse.core.runtime.Status; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.ProgressMonitorDialog; @@ -144,14 +144,13 @@ public class ChangeExceptionHandler { IWorkspaceRunnable runnable = new IWorkspaceRunnable() { @Override public void run(IProgressMonitor monitor) throws CoreException { - monitor.beginTask("", 11); //$NON-NLS-1$ + SubMonitor subMonitor = SubMonitor.convert(monitor, 11); try { - undo.initializeValidationData(new NotCancelableProgressMonitor(new SubProgressMonitor(monitor, 1))); - if (undo.isValid(new SubProgressMonitor(monitor, 1)).hasFatalError()) { - monitor.done(); + undo.initializeValidationData(new NotCancelableProgressMonitor(subMonitor.split(1))); + if (undo.isValid(subMonitor.split(1)).hasFatalError()) { return; } - undo.perform(new SubProgressMonitor(monitor, 9)); + undo.perform(subMonitor.split(9)); } finally { undo.dispose(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java index 3c397a168e7..b490d065351 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/rename/TextSearchWrapper.java @@ -45,7 +45,7 @@ import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.search.core.text.TextSearchEngine; import org.eclipse.search.core.text.TextSearchMatchAccess; import org.eclipse.search.core.text.TextSearchRequestor; @@ -262,6 +262,7 @@ public class TextSearchWrapper { */ public IStatus searchWord(IFile[] filesToSearch, int scope, IFile scopeAnchor, String workingSet, String[] patterns, String word, IProgressMonitor monitor, final List target) { + SubMonitor subMonitor = SubMonitor.convert(monitor, 100); int startPos = target.size(); TextSearchEngine engine = TextSearchEngine.create(); StringBuilder searchPattern = new StringBuilder(word.length() + 8); @@ -285,14 +286,15 @@ public class TextSearchWrapper { return true; } }; - IStatus result = engine.search(searchscope, requestor, pattern, new SubProgressMonitor(monitor, 95)); - categorizeMatches(target.subList(startPos, target.size()), new SubProgressMonitor(monitor, 5)); + IStatus result = engine.search(searchscope, requestor, pattern, subMonitor.split(95)); + categorizeMatches(target.subList(startPos, target.size()), subMonitor.split(5)); return result; } public void categorizeMatches(List matches, IProgressMonitor monitor) { - monitor.beginTask(RenameMessages.TextSearch_monitor_categorizeMatches, matches.size()); + SubMonitor subMonitor = SubMonitor.convert(monitor, RenameMessages.TextSearch_monitor_categorizeMatches, + matches.size()); IFile file = null; ArrayList locations = null; for (Iterator iter = matches.iterator(); iter.hasNext();) { @@ -304,7 +306,7 @@ public class TextSearchWrapper { computeLocations(file, locations); } match.setLocation(findLocation(match, locations)); - monitor.worked(1); + subMonitor.worked(1); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/indexwizards/TeamProjectIndexExportWizardPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/indexwizards/TeamProjectIndexExportWizardPage.java index 9bf3abc2c9b..c4a0a509f48 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/indexwizards/TeamProjectIndexExportWizardPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/wizards/indexwizards/TeamProjectIndexExportWizardPage.java @@ -31,7 +31,7 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.MultiStatus; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; @@ -327,7 +327,7 @@ public class TeamProjectIndexExportWizardPage extends WizardPage implements List IRunnableWithProgress op = new IRunnableWithProgress() { @Override public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { - monitor.beginTask("", projects.length); //$NON-NLS-1$ + SubMonitor subMonitor = SubMonitor.convert(monitor, projects.length); for (ICProject project : projects) { TeamPDOMExportOperation op = new TeamPDOMExportOperation(project); op.setTargetLocation(dest); @@ -335,7 +335,7 @@ public class TeamProjectIndexExportWizardPage extends WizardPage implements List op.setOptions(TeamPDOMExportOperation.EXPORT_OPTION_RESOURCE_SNAPSHOT); } try { - op.run(new SubProgressMonitor(monitor, 1)); + op.run(subMonitor.split(1)); } catch (CoreException e) { status.merge(e.getStatus()); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/FormatAllAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/FormatAllAction.java index aae507d3b83..969ddd0383c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/FormatAllAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/actions/FormatAllAction.java @@ -51,9 +51,8 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.MultiStatus; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -338,70 +337,57 @@ public class FormatAllAction extends SelectionDispatchAction { private void doRunOnMultiple(ITranslationUnit[] tus, MultiStatus status, IProgressMonitor monitor) throws OperationCanceledException { - if (monitor == null) { - monitor = new NullProgressMonitor(); - } - monitor.setTaskName(ActionMessages.FormatAllAction_operation_description); + SubMonitor subMonitor = SubMonitor.convert(monitor, ActionMessages.FormatAllAction_operation_description, + tus.length * 4); + Map lastOptions = null; + ICProject lastProject = null; - monitor.beginTask("", tus.length * 4); //$NON-NLS-1$ - try { - Map lastOptions = null; - ICProject lastProject = null; - - for (int i = 0; i < tus.length; i++) { - ITranslationUnit tu = tus[i]; - IPath path = tu.getPath(); - if (lastProject == null || lastOptions == null || !lastProject.equals(tu.getCProject())) { - lastProject = tu.getCProject(); - lastOptions = getFomatterSettings(lastProject); - } - - ILanguage language = null; - try { - language = tu.getLanguage(); - } catch (CoreException exc) { - // use fallback CPP - language = GPPLanguage.getDefault(); - } - - // use working copy if available - ITranslationUnit wc = CDTUITools.getWorkingCopyManager().findSharedWorkingCopy(tu); - if (wc != null) { - tu = wc; - } - lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT, tu); - lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_LANGUAGE, language); - lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE, tu.getResource()); - - if (monitor.isCanceled()) { - throw new OperationCanceledException(); - } - - ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); - try { - try { - manager.connect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1)); - - monitor.subTask(path.makeRelative().toString()); - ITextFileBuffer fileBuffer = manager.getTextFileBuffer(path, LocationKind.IFILE); - boolean wasDirty = fileBuffer.isDirty(); - - formatTranslationUnit(fileBuffer, lastOptions); - - if (fileBuffer.isDirty() && !wasDirty) { - fileBuffer.commit(new SubProgressMonitor(monitor, 2), false); - } else { - monitor.worked(2); - } - } finally { - manager.disconnect(path, LocationKind.IFILE, new SubProgressMonitor(monitor, 1)); - } - } catch (CoreException e) { - status.add(e.getStatus()); - } + for (int i = 0; i < tus.length; i++) { + ITranslationUnit tu = tus[i]; + IPath path = tu.getPath(); + if (lastProject == null || lastOptions == null || !lastProject.equals(tu.getCProject())) { + lastProject = tu.getCProject(); + lastOptions = getFomatterSettings(lastProject); + } + + ILanguage language = null; + try { + language = tu.getLanguage(); + } catch (CoreException exc) { + // use fallback CPP + language = GPPLanguage.getDefault(); + } + + // use working copy if available + ITranslationUnit wc = CDTUITools.getWorkingCopyManager().findSharedWorkingCopy(tu); + if (wc != null) { + tu = wc; + } + lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_TRANSLATION_UNIT, tu); + lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_LANGUAGE, language); + lastOptions.put(DefaultCodeFormatterConstants.FORMATTER_CURRENT_FILE, tu.getResource()); + + ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager(); + try { + try { + manager.connect(path, LocationKind.IFILE, subMonitor.split(1)); + subMonitor.subTask(path.makeRelative().toString()); + ITextFileBuffer fileBuffer = manager.getTextFileBuffer(path, LocationKind.IFILE); + boolean wasDirty = fileBuffer.isDirty(); + + formatTranslationUnit(fileBuffer, lastOptions); + + if (fileBuffer.isDirty() && !wasDirty) { + fileBuffer.commit(subMonitor.split(2), false); + } else { + subMonitor.worked(2); + } + } finally { + manager.disconnect(path, LocationKind.IFILE, subMonitor.split(1)); + } + } catch (CoreException e) { + status.add(e.getStatus()); } - } finally { - monitor.done(); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/BinaryParserBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/BinaryParserBlock.java index 3df74d0573a..768c24923ec 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/BinaryParserBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/BinaryParserBlock.java @@ -39,10 +39,9 @@ import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Preferences; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.layout.PixelConverter; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.StructuredSelection; @@ -225,10 +224,7 @@ public class BinaryParserBlock extends AbstractBinaryParserPage { @Override public void performApply(IProgressMonitor monitor) throws CoreException { - if (monitor == null) { - monitor = new NullProgressMonitor(); - } - monitor.beginTask(CUIMessages.BinaryParserBlock_settingBinaryParser, 2); + SubMonitor subMonitor = SubMonitor.convert(monitor, CUIMessages.BinaryParserBlock_settingBinaryParser, 2); List parsers = binaryList.getElements(); final List selected = new ArrayList<>(); // must do this to get proper order. for (int i = 0; i < parsers.size(); i++) { @@ -241,13 +237,14 @@ public class BinaryParserBlock extends AbstractBinaryParserPage { @Override public void execute(ICDescriptor descriptor, IProgressMonitor monitor) throws CoreException { + SubMonitor subMonitor = SubMonitor.convert(monitor, 2); if (initialSelected == null || !selected.equals(initialSelected)) { descriptor.remove(CCorePlugin.BINARY_PARSER_UNIQ_ID); for (int i = 0; i < selected.size(); i++) { descriptor.create(CCorePlugin.BINARY_PARSER_UNIQ_ID, selected.get(i).getID()); } } - monitor.worked(1); + subMonitor.worked(1); // Give a chance to the contributions to save. // We have to do it last to make sure the parser id // is save @@ -255,13 +252,13 @@ public class BinaryParserBlock extends AbstractBinaryParserPage { for (int i = 0; i < selected.size(); i++) { ICOptionPage page = getBinaryParserPage(selected.get(i).getID()); if (page != null && page.getControl() != null) { - page.performApply(new SubProgressMonitor(monitor, 1)); + page.performApply(subMonitor.split(1)); } } } }; CCorePlugin.getDefault().getCDescriptorManager().runDescriptorOperation(getContainer().getProject(), op, - monitor); + subMonitor.split(2)); } else { if (initialSelected == null || !selected.equals(initialSelected)) { Preferences store = getContainer().getPreferences(); @@ -269,17 +266,16 @@ public class BinaryParserBlock extends AbstractBinaryParserPage { store.setValue(CCorePlugin.PREF_BINARY_PARSER, arrayToString(selected.toArray())); } } - monitor.worked(1); + subMonitor.worked(1); // Give a chance to the contributions to save. for (int i = 0; i < selected.size(); i++) { ICOptionPage page = getBinaryParserPage(selected.get(i).getID()); if (page != null && page.getControl() != null) { - page.performApply(new SubProgressMonitor(monitor, 1)); + page.performApply(subMonitor.split(1)); } } } initialSelected = selected; - monitor.done(); } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/ReferenceBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/ReferenceBlock.java index 08275cb8469..32d30c340df 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/ReferenceBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/ReferenceBlock.java @@ -24,8 +24,7 @@ import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.jface.viewers.CheckboxTableViewer; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.swt.SWT; @@ -168,18 +167,11 @@ public class ReferenceBlock extends AbstractCOptionPage { IProject[] refProjects = getReferencedProjects(); if (refProjects != null) { IProject project = getContainer().getProject(); - if (monitor == null) { - monitor = new NullProgressMonitor(); - } - monitor.beginTask(CUIMessages.ReferenceBlock_task_ReferenceProjects, 1); - try { - IProjectDescription description = project.getDescription(); - description.setReferencedProjects(refProjects); - project.setDescription(description, new SubProgressMonitor(monitor, 1)); - } catch (CoreException e) { - } + SubMonitor subMonitor = SubMonitor.convert(monitor, CUIMessages.ReferenceBlock_task_ReferenceProjects, 1); + IProjectDescription description = project.getDescription(); + description.setReferencedProjects(refProjects); + project.setDescription(description, subMonitor.split(1)); } - } @Override diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/TabFolderOptionBlock.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/TabFolderOptionBlock.java index 3f9d4e10a4d..d0c51fc63ac 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/TabFolderOptionBlock.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/dialogs/TabFolderOptionBlock.java @@ -22,8 +22,7 @@ import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.utils.ui.controls.TabFolderLayout; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.osgi.util.NLS; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; @@ -151,24 +150,17 @@ public abstract class TabFolderOptionBlock { public boolean performApply(IProgressMonitor monitor) { if (initializingTabs) return false; - if (monitor == null) { - monitor = new NullProgressMonitor(); - } - monitor.beginTask("", pages.size()); //$NON-NLS-1$ - try { - Iterator iter = pages.iterator(); - while (iter.hasNext()) { - ICOptionPage tab = iter.next(); - try { - tab.performApply(new SubProgressMonitor(monitor, 1)); - } catch (CoreException e) { - CUIPlugin.errorDialog(composite.getShell(), CUIMessages.TabFolderOptionBlock_error, - CUIMessages.TabFolderOptionBlock_error_settingOptions, e, true); - return false; - } + SubMonitor subMonitor = SubMonitor.convert(monitor, pages.size()); + Iterator iter = pages.iterator(); + while (iter.hasNext()) { + ICOptionPage tab = iter.next(); + try { + tab.performApply(subMonitor.split(1)); + } catch (CoreException e) { + CUIPlugin.errorDialog(composite.getShell(), CUIMessages.TabFolderOptionBlock_error, + CUIMessages.TabFolderOptionBlock_error_settingOptions, e, true); + return false; } - } finally { - monitor.done(); } return true; }