Skip to content

Commit 2174610

Browse files
committed
Avoid errors when running headless
This change adjusts several classes to handle headless state, i.e. when the workbench is not available. The respective operations now do nothing without a workbench, instead of throwing exceptions. Fixes: #2673
1 parent 3b9b910 commit 2174610

5 files changed

Lines changed: 47 additions & 24 deletions

File tree

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPlugin.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -601,18 +601,17 @@ public void doneSaving(ISaveContext saveContext) {
601601
}
602602
};
603603
PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(fThemeListener);
604+
// do the asynchronous exec last - see bug 209920
605+
getStandardDisplay().asyncExec(
606+
() -> {
607+
// initialize the selected resource `
608+
SelectedResourceManager.getDefault();
609+
// forces launch shortcuts to be initialized so their
610+
// key-bindings work
611+
getLaunchConfigurationManager().getLaunchShortcuts();
612+
});
604613
}
605614

606-
// do the asynchronous exec last - see bug 209920
607-
getStandardDisplay().asyncExec(
608-
() -> {
609-
// initialize the selected resource `
610-
SelectedResourceManager.getDefault();
611-
// forces launch shortcuts to be initialized so their
612-
// key-bindings work
613-
getLaunchConfigurationManager().getLaunchShortcuts();
614-
});
615-
616615
DebugUIPluginSaveParticipant saveParticipant = new DebugUIPluginSaveParticipant();
617616
ResourcesPlugin.getWorkspace().addSaveParticipant(getUniqueIdentifier(), saveParticipant);
618617
}

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/DebugUIPreferenceInitializer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,12 @@ private static void setDefault(IPreferenceStore store, String key, RGB newValue,
227227
}
228228

229229
public static void setThemeBasedPreferences(final IPreferenceStore store, final boolean fireEvent) {
230+
if (!PlatformUI.isWorkbenchRunning()) {
231+
return;
232+
}
230233
Display display= PlatformUI.getWorkbench().getDisplay();
231234
Runnable runnable = () -> {
232-
ColorRegistry registry = null;
233-
if (PlatformUI.isWorkbenchRunning()) {
234-
registry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
235-
}
235+
ColorRegistry registry = PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
236236
setDefault(store, IDebugPreferenceConstants.CONSOLE_BAKGROUND_COLOR, findRGB(registry, IInternalDebugUIConstants.THEME_CONSOLE_COLOR_BACKGROUND, new RGB(255, 255, 255)), fireEvent);
237237
setDefault(store, IDebugPreferenceConstants.CONSOLE_SYS_OUT_COLOR, findRGB(registry, IInternalDebugUIConstants.THEME_CONSOLE_COLOR_STD_OUT, new RGB(0, 0, 0)), fireEvent);
238238
setDefault(store, IDebugPreferenceConstants.CONSOLE_SYS_IN_COLOR, findRGB(registry, IInternalDebugUIConstants.THEME_CONSOLE_COLOR_STD_IN, new RGB(0, 200, 125)), fireEvent);

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/contextlaunching/LaunchingResourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ private void addAllToList(List<ILaunchConfiguration> list, ILaunchConfiguration[
554554
* Starts up the manager
555555
*/
556556
public void startup() {
557-
IWorkbench workbench = PlatformUI.getWorkbench();
557+
IWorkbench workbench = PlatformUI.isWorkbenchRunning() ? PlatformUI.getWorkbench() : null;
558558
if(workbench != null) {
559559
workbench.addWindowListener(this);
560560
// initialize for already open windows

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/breakpoints/BreakpointSetOrganizer.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@
5454
*/
5555
public class BreakpointSetOrganizer extends AbstractBreakpointOrganizerDelegate implements IBreakpointOrganizerDelegateExtension, IPropertyChangeListener, IBreakpointsListener {
5656

57-
private IWorkingSetManager fWorkingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
57+
private IWorkingSetManager fWorkingSetManager = PlatformUI.isWorkbenchRunning()
58+
? PlatformUI.getWorkbench().getWorkingSetManager()
59+
: null;
5860

5961
/**
6062
* A cache for mapping markers to the working set they belong to
@@ -71,7 +73,9 @@ public class BreakpointSetOrganizer extends AbstractBreakpointOrganizerDelegate
7173
* working sets and fires property change notification.
7274
*/
7375
public BreakpointSetOrganizer() {
74-
fWorkingSetManager.addPropertyChangeListener(this);
76+
if (fWorkingSetManager != null) {
77+
fWorkingSetManager.addPropertyChangeListener(this);
78+
}
7579
fCache = new BreakpointWorkingSetCache();
7680
DebugUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
7781
DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
@@ -80,6 +84,9 @@ public BreakpointSetOrganizer() {
8084

8185
@Override
8286
public IAdaptable[] getCategories(IBreakpoint breakpoint) {
87+
if (fWorkingSetManager == null) {
88+
return new IAdaptable[0];
89+
}
8390
List<IAdaptable> result = new ArrayList<>();
8491
IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets();
8592
for (IWorkingSet set : workingSets) {
@@ -98,8 +105,10 @@ public IAdaptable[] getCategories(IBreakpoint breakpoint) {
98105

99106
@Override
100107
public void dispose() {
101-
fWorkingSetManager.removePropertyChangeListener(this);
102-
fWorkingSetManager = null;
108+
if (fWorkingSetManager != null) {
109+
fWorkingSetManager.removePropertyChangeListener(this);
110+
fWorkingSetManager = null;
111+
}
103112
DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this);
104113
DebugUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this);
105114
super.dispose();
@@ -220,6 +229,9 @@ private void addBreakpointsToSet(IBreakpoint[] breakpoints, IWorkingSet set) {
220229
@Override
221230
public void breakpointsRemoved(IBreakpoint[] breakpoints,
222231
IMarkerDelta[] deltas) {
232+
if (fWorkingSetManager == null) {
233+
return;
234+
}
223235
IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets();
224236
IWorkingSet set = null;
225237
for (IWorkingSet workingSet : workingSets) {
@@ -273,7 +285,7 @@ public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas)
273285
public static IWorkingSet getDefaultWorkingSet() {
274286
IPreferenceStore preferenceStore = DebugUIPlugin.getDefault().getPreferenceStore();
275287
String name = preferenceStore.getString(IInternalDebugUIConstants.MEMENTO_BREAKPOINT_WORKING_SET_NAME);
276-
if (name != null) {
288+
if (name != null && PlatformUI.isWorkbenchRunning()) {
277289
return PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name);
278290
}
279291
return null;
@@ -366,6 +378,9 @@ public void removeBreakpoint(IBreakpoint breakpoint, IAdaptable category) {
366378

367379
@Override
368380
public IAdaptable[] getCategories() {
381+
if (fWorkingSetManager == null) {
382+
return new IAdaptable[0];
383+
}
369384
IWorkingSet[] workingSets = fWorkingSetManager.getWorkingSets();
370385
List<IAdaptable> all = new ArrayList<>();
371386
for (IWorkingSet set : workingSets) {

debug/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/views/breakpoints/WorkingSetBreakpointOrganizer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@
3434
*/
3535
public class WorkingSetBreakpointOrganizer extends AbstractBreakpointOrganizerDelegate implements IPropertyChangeListener {
3636

37-
IWorkingSetManager fWorkingSetManager = PlatformUI.getWorkbench().getWorkingSetManager();
37+
IWorkingSetManager fWorkingSetManager = PlatformUI.isWorkbenchRunning()
38+
? PlatformUI.getWorkbench().getWorkingSetManager()
39+
: null;
3840

3941
/**
4042
* Constructs a working set breakpoint organizer. Listens for changes in
4143
* working sets and fires property change notification.
4244
*/
4345
public WorkingSetBreakpointOrganizer() {
44-
fWorkingSetManager.addPropertyChangeListener(this);
46+
if (fWorkingSetManager != null) {
47+
fWorkingSetManager.addPropertyChangeListener(this);
48+
}
4549
}
4650

4751
@Override
@@ -56,6 +60,9 @@ public IAdaptable[] getCategories(IBreakpoint breakpoint) {
5660
parents.add(res);
5761
}
5862
}
63+
if (fWorkingSetManager == null) {
64+
return result.toArray(new IAdaptable[result.size()]);
65+
}
5966
for (IWorkingSet workingSet : fWorkingSetManager.getWorkingSets()) {
6067
if (!IDebugUIConstants.BREAKPOINT_WORKINGSET_ID.equals(workingSet.getId())) {
6168
for (IAdaptable element : workingSet.getElements()) {
@@ -74,8 +81,10 @@ public IAdaptable[] getCategories(IBreakpoint breakpoint) {
7481

7582
@Override
7683
public void dispose() {
77-
fWorkingSetManager.removePropertyChangeListener(this);
78-
fWorkingSetManager = null;
84+
if (fWorkingSetManager != null) {
85+
fWorkingSetManager.removePropertyChangeListener(this);
86+
fWorkingSetManager = null;
87+
}
7988
super.dispose();
8089
}
8190

0 commit comments

Comments
 (0)