Skip to content

Commit

Permalink
Make JavaPlugin.getActivePage() aware of not running platform and inline
Browse files Browse the repository at this point in the history
Currently JavaPlugin.getActivePage() fails if no workbench is running,
while the method itself can return null if there is no active window
anyways. Beside that is unnecessary access the default java plugin even
though all code called there is static anyways.

This now changes the following parts:

1) guard getActiveWorkbenchWindow against not running workbench
2) used getActiveWorkbenchWindow instead of similar code in
getActivePage like in getActiveWorkbenchShell
3) delete no longer needed private method
4) Use JavaPlugin.getActiveWorkbenchWindow() instead of PlatformUI
5) Use JavaPlugin.getActiveWorkbenchShell() instead of PlatformUI
  • Loading branch information
laeubi authored and akurtakov committed Jan 21, 2025
1 parent 9a4ff2a commit 07237c8
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import org.eclipse.ltk.core.refactoring.Change;
Expand Down Expand Up @@ -474,7 +473,7 @@ private int showStatus(RefactoringStatus status) {
if (!status.hasError())
return Window.OK;

Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();

Dialog dialog= RefactoringUI.createRefactoringStatusDialog(status, shell, "", false); //$NON-NLS-1$
return dialog.open();
Expand Down Expand Up @@ -674,7 +673,7 @@ private void showSlowCleanUpsWarning(HashSet<ICleanUp> slowCleanUps) {

private void showSlowCleanUpDialog(final StringBuilder cleanUpNames) {
if (OptionalMessageDialog.isDialogEnabled(SlowCleanUpWarningDialog.ID)) {
Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();
new SlowCleanUpWarningDialog(shell, FixMessages.CleanUpPostSaveListener_SlowCleanUpDialog_title, cleanUpNames.toString()).open();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

import org.eclipse.jface.text.ITextSelection;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
Expand Down Expand Up @@ -109,6 +107,7 @@
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
import org.eclipse.jdt.ui.refactoring.RenameSupport;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.actions.ActionMessages;
import org.eclipse.jdt.internal.ui.fix.CleanUpRefactoringWizard;
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
Expand Down Expand Up @@ -249,7 +248,7 @@ public static void startCleanupRefactoring(ICompilationUnit[] cus, ICleanUp[] cl
if (refactoring.getCleanUpTargetsSize() > 1) {
context= new ProgressMonitorDialog(shell);
} else {
context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
context= JavaPlugin.getActiveWorkbenchWindow();
}

RefactoringExecutionHelper helper= new RefactoringExecutionHelper(refactoring, IStatus.INFO, IRefactoringSaveModes.SAVE_REFACTORING, shell, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,18 @@ public static IWorkspace getWorkspace() {
}

public static IWorkbenchPage getActivePage() {
return getDefault().internalGetActivePage();
IWorkbenchWindow window= getActiveWorkbenchWindow();
if (window != null) {
return window.getActivePage();
}
return null;
}

public static IWorkbenchWindow getActiveWorkbenchWindow() {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (PlatformUI.isWorkbenchRunning()) {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
return null;
}

public static Shell getActiveWorkbenchShell() {
Expand Down Expand Up @@ -544,13 +551,6 @@ public void stop(BundleContext context) throws Exception {
}
}

private IWorkbenchPage internalGetActivePage() {
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null)
return null;
return window.getActivePage();
}

/**
* Private deprecated method to avoid deprecation warnings
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.core.IType;

Expand All @@ -33,6 +32,7 @@
import org.eclipse.jdt.ui.actions.AddGetterSetterAction;
import org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void apply(IDocument document) {
Display.getDefault().syncExec(() -> {
try {
IStructuredSelection selection= new StructuredSelection(fType);
IWorkbenchSite site= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
IWorkbenchSite site= JavaPlugin.getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
new AddGetterSetterAction(site).run(selection);
} catch (NullPointerException e) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import org.eclipse.jdt.core.IJavaProject;
Expand Down Expand Up @@ -106,7 +105,7 @@ public void run(IAction action) {
map.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED);
fJavaProject.setOptions(map);

Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();
Map<String, Object> data= new HashMap<>();
data.put(CompliancePreferencePage.DATA_SELECT_OPTION_KEY, JavaCore.COMPILER_PB_REPORT_PREVIEW_FEATURES);
data.put(CompliancePreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.core.IType;

Expand All @@ -33,6 +32,7 @@
import org.eclipse.jdt.ui.actions.GenerateHashCodeEqualsAction;
import org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void apply(IDocument document) {
Display.getDefault().syncExec(() -> {
try {
IStructuredSelection selection= new StructuredSelection(fType);
IWorkbenchSite site= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
IWorkbenchSite site= JavaPlugin.getActivePage().getActiveEditor().getSite();
new GenerateHashCodeEqualsAction(site).run(selection);
} catch (NullPointerException e) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -95,14 +94,11 @@ public ConfigureTemplatesAction() {

@Override
public void run() {
PreferenceDialog preferenceDialog= PreferencesUtil.createPreferenceDialogOn(getShell(), JAVA_TEMPLATE_PREFERENCE_PAGE_ID, new String[] { JAVA_TEMPLATE_PREFERENCE_PAGE_ID, CODE_TEMPLATE_PREFERENCE_PAGE_ID }, null);
PreferenceDialog preferenceDialog= PreferencesUtil.createPreferenceDialogOn(JavaPlugin.getActiveWorkbenchShell(), JAVA_TEMPLATE_PREFERENCE_PAGE_ID, new String[] { JAVA_TEMPLATE_PREFERENCE_PAGE_ID, CODE_TEMPLATE_PREFERENCE_PAGE_ID }, null);
preferenceDialog.getTreeViewer().expandAll();
preferenceDialog.open();
}

private Shell getShell() {
return JavaPlugin.getActiveWorkbenchWindow().getShell();
}
}

private static Action NONE_APPLICABLE_ACTION= new Action(ActionMessages.SurroundWithTemplateMenuAction_NoneApplicable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.IWorkbenchSite;
import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.core.IType;

Expand All @@ -33,6 +32,7 @@
import org.eclipse.jdt.ui.actions.GenerateToStringAction;
import org.eclipse.jdt.ui.text.java.correction.ChangeCorrectionProposal;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void apply(IDocument document) {
Display.getDefault().syncExec(() -> {
try {
IStructuredSelection selection= new StructuredSelection(fType);
IWorkbenchSite site= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor().getSite();
IWorkbenchSite site= JavaPlugin.getActivePage().getActiveEditor().getSite();
new GenerateToStringAction(site).run(selection);
} catch (NullPointerException e) {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;

import org.eclipse.ui.PlatformUI;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareUI;

import org.eclipse.jdt.core.IMember;

import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;


/**
Expand Down Expand Up @@ -67,7 +66,7 @@ public void run(ISelection selection) {
ci.setHelpContextId(IJavaHelpContextIds.COMPARE_ELEMENT_WITH_HISTORY_DIALOG);
CompareUI.openCompareDialog(ci);
} else {
TeamUI.showHistoryFor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), input, pageSource);
TeamUI.showHistoryFor(JavaPlugin.getActivePage(), input, pageSource);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ protected Object computeInput(Object input) {
return null;

IWorkbenchPart part= null;
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page= window.getActivePage();
if (page != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;

import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;

Expand Down Expand Up @@ -97,7 +95,7 @@ private IStatus export(JarPackageData[] jarPackages) {
Shell shell= getShell();
IJarExportRunnable op= jarPackages[0].createJarExportRunnable(jarPackages, shell);
try {
PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(false, true, op);
JavaPlugin.getActiveWorkbenchWindow().run(false, true, op);
//PlatformUI.getWorkbench().getProgressService().run(false, true, op); // see bug 118152
} catch (InvocationTargetException ex) {
if (ex.getTargetException() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.eclipse.jface.text.hyperlink.IHyperlink;

import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
Expand Down Expand Up @@ -290,7 +289,7 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK,
Messages.format(JavaEditorMessages.JavaElementImplementationHyperlink_error_status_message, javaElement.getElementName()), e.getCause());
JavaPlugin.log(status);
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
ErrorDialog.openError(JavaPlugin.getActiveWorkbenchShell(),
JavaEditorMessages.JavaElementImplementationHyperlink_hyperlinkText,
JavaEditorMessages.JavaElementImplementationHyperlink_error_no_implementations_found_message, status);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jface.text.IDocument;

import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextFileChange;
Expand Down Expand Up @@ -105,7 +104,7 @@ public void apply(IDocument document) {
refactoring.setConsiderVisibility(false);//private field references are just searched in local file
});
if (fNoDialog) {
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
final RefactoringExecutionHelper helper= new RefactoringExecutionHelper(compositeRefactoring, RefactoringStatus.ERROR, IRefactoringSaveModes.SAVE_REFACTORING, JavaPlugin.getActiveWorkbenchShell(), window);
if (Display.getCurrent() != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static void getOpenCompliancePageToEnablePreviewFeaturesProposal(IInvocat
@Override
public void apply(IDocument document) {

Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();
boolean usePropertyPage;

if (!hasProjectSpecificOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;

import org.eclipse.ui.PlatformUI;

import org.eclipse.ltk.core.refactoring.RefactoringCore;

import org.eclipse.jdt.core.dom.CompilationUnit;
Expand Down Expand Up @@ -108,7 +106,7 @@ public void resolve(MultiFixTarget[] targets, final IProgressMonitor monitor) th

IRunnableContext context= (fork, cancelable, runnable) -> runnable.run(monitor == null ? new NullProgressMonitor() : monitor);

Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();
RefactoringExecutionHelper helper= new RefactoringExecutionHelper(refactoring, IStatus.INFO, IRefactoringSaveModes.SAVE_REFACTORING, shell, context);
try {
helper.perform(true, true);
Expand Down Expand Up @@ -208,7 +206,7 @@ public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {

int stopSeverity= RefactoringCore.getConditionCheckingFailedSeverity();
Shell shell= JavaPlugin.getActiveWorkbenchShell();
IRunnableContext context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow();
RefactoringExecutionHelper executer= new RefactoringExecutionHelper(refactoring, stopSeverity, IRefactoringSaveModes.SAVE_NOTHING, shell, context);
try {
executer.perform(true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import org.eclipse.ui.texteditor.ITextEditor;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.javaeditor.SpecificContentAssistExecutor;

Expand Down Expand Up @@ -56,7 +56,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
}

private ITextEditor getActiveEditor() {
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchWindow window= JavaPlugin.getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page= window.getActivePage();
if (page != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@

import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import org.eclipse.ui.texteditor.AnnotationPreference;
Expand Down Expand Up @@ -656,7 +655,7 @@ public ConfigureAnnotationsAction(Annotation annotation, IInformationControl inf
*/
@Override
public void run() {
Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();

Object data= null;
AnnotationPreference preference= getAnnotationPreference(fAnnotation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.eclipse.jface.text.IInformationControl;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.PreferencesUtil;

import org.eclipse.jdt.core.IJavaProject;
Expand All @@ -34,6 +33,7 @@

import org.eclipse.jdt.ui.JavaElementLabels;

import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
import org.eclipse.jdt.internal.ui.preferences.ComplianceConfigurationBlock;
Expand Down Expand Up @@ -85,7 +85,7 @@ public ConfigureProblemSeverityAction(IJavaProject project, String optionId, Str
public void run() {
boolean showPropertyPage;

Shell shell= PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Shell shell= JavaPlugin.getActiveWorkbenchShell();

if (!hasProjectSpecificOptions()) {
String message= Messages.format(
Expand Down
Loading

0 comments on commit 07237c8

Please sign in to comment.