Skip to content

Commit c490b9c

Browse files
committed
Extract common base class for RulerToggle*Breakpoint* delegates
See #660
1 parent d8fb6ab commit c490b9c

File tree

3 files changed

+134
-181
lines changed

3 files changed

+134
-181
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2022 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.internal.debug.ui.actions;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.debug.internal.ui.DebugUIPlugin;
18+
import org.eclipse.jface.action.Action;
19+
import org.eclipse.jface.action.IAction;
20+
import org.eclipse.jface.text.BadLocationException;
21+
import org.eclipse.jface.text.IDocument;
22+
import org.eclipse.jface.text.IRegion;
23+
import org.eclipse.jface.text.ITextSelection;
24+
import org.eclipse.jface.text.TextSelection;
25+
import org.eclipse.jface.text.source.IVerticalRulerInfo;
26+
import org.eclipse.jface.viewers.ISelection;
27+
import org.eclipse.jface.viewers.ISelectionProvider;
28+
import org.eclipse.swt.widgets.Event;
29+
import org.eclipse.ui.IActionDelegate2;
30+
import org.eclipse.ui.IEditorPart;
31+
import org.eclipse.ui.texteditor.AbstractRulerActionDelegate;
32+
import org.eclipse.ui.texteditor.IDocumentProvider;
33+
import org.eclipse.ui.texteditor.ITextEditor;
34+
35+
public abstract class AbstractRulerToggleBreakpointActionDelegate extends AbstractRulerActionDelegate implements IActionDelegate2 {
36+
37+
protected ITextEditor currentEditor;
38+
private IAction dummyAction;
39+
40+
@Override
41+
protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) {
42+
dummyAction = new Action() {
43+
// empty implementation to make compiler happy
44+
};
45+
return dummyAction;
46+
}
47+
48+
@Override
49+
public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
50+
if (targetEditor instanceof ITextEditor textEditor) {
51+
currentEditor = textEditor;
52+
} else {
53+
currentEditor = null;
54+
}
55+
}
56+
57+
@Override
58+
public void init(IAction action) {
59+
}
60+
61+
@Override
62+
public void dispose() {
63+
currentEditor = null;
64+
dummyAction = null;
65+
super.dispose();
66+
}
67+
68+
@Override
69+
public void runWithEvent(IAction action, Event event) {
70+
if (currentEditor == null) {
71+
return;
72+
}
73+
IVerticalRulerInfo rulerInfo = currentEditor.getAdapter(IVerticalRulerInfo.class);
74+
if (rulerInfo == null) {
75+
return;
76+
}
77+
int lineOfLastMouseButtonActivity = rulerInfo.getLineOfLastMouseButtonActivity();
78+
if (lineOfLastMouseButtonActivity < 0) {
79+
return;
80+
}
81+
IDocument document = getDocument(currentEditor);
82+
if (document == null) {
83+
return;
84+
}
85+
ToggleBreakpointAdapter toggle = new ToggleBreakpointAdapter();
86+
try {
87+
ITextSelection selection = getTextSelection(currentEditor, document, lineOfLastMouseButtonActivity);
88+
if (toggle.canToggleLineBreakpoints(currentEditor, selection)) {
89+
boolean success = doWork(currentEditor, selection);
90+
if (success) {
91+
toggle.toggleBreakpoints(currentEditor, selection);
92+
}
93+
}
94+
} catch (BadLocationException | CoreException e) {
95+
DebugUIPlugin.log(e);
96+
}
97+
}
98+
99+
protected static IDocument getDocument(ITextEditor editor) {
100+
IDocumentProvider provider = editor.getDocumentProvider();
101+
if (provider != null) {
102+
return provider.getDocument(editor.getEditorInput());
103+
}
104+
IDocument doc = editor.getAdapter(IDocument.class);
105+
if (doc != null) {
106+
return doc;
107+
}
108+
return null;
109+
}
110+
111+
protected ITextSelection getTextSelection(ITextEditor editor, IDocument document, int line) throws BadLocationException {
112+
IRegion region = document.getLineInformation(line);
113+
ITextSelection textSelection = new TextSelection(document, region.getOffset(), 0);
114+
ISelectionProvider provider = editor.getSite().getSelectionProvider();
115+
if (provider != null) {
116+
ISelection selection = provider.getSelection();
117+
if (selection instanceof ITextSelection && ((ITextSelection) selection).getStartLine() <= line
118+
&& ((ITextSelection) selection).getEndLine() >= line) {
119+
textSelection = (ITextSelection) selection;
120+
}
121+
}
122+
return textSelection;
123+
}
124+
125+
abstract protected boolean doWork(ITextEditor editor, ITextSelection selection);
126+
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RulerToggleLambdaEntryBreakpointActionDelegate.java

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -13,106 +13,14 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.debug.ui.actions;
1515

16-
import org.eclipse.core.runtime.CoreException;
17-
import org.eclipse.debug.internal.ui.DebugUIPlugin;
18-
import org.eclipse.jface.action.Action;
19-
import org.eclipse.jface.action.IAction;
20-
import org.eclipse.jface.text.BadLocationException;
21-
import org.eclipse.jface.text.IDocument;
22-
import org.eclipse.jface.text.IRegion;
2316
import org.eclipse.jface.text.ITextSelection;
24-
import org.eclipse.jface.text.TextSelection;
25-
import org.eclipse.jface.text.source.IVerticalRulerInfo;
26-
import org.eclipse.jface.viewers.ISelection;
27-
import org.eclipse.jface.viewers.ISelectionProvider;
28-
import org.eclipse.swt.widgets.Event;
29-
import org.eclipse.ui.IActionDelegate2;
30-
import org.eclipse.ui.IEditorPart;
31-
import org.eclipse.ui.texteditor.AbstractRulerActionDelegate;
32-
import org.eclipse.ui.texteditor.IDocumentProvider;
3317
import org.eclipse.ui.texteditor.ITextEditor;
3418

35-
public class RulerToggleLambdaEntryBreakpointActionDelegate extends AbstractRulerActionDelegate implements IActionDelegate2 {
36-
37-
private IEditorPart currentEditor;
38-
private IAction dummyAction;
19+
public class RulerToggleLambdaEntryBreakpointActionDelegate extends AbstractRulerToggleBreakpointActionDelegate {
3920

4021
@Override
41-
protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) {
42-
dummyAction = new Action() {
43-
// empty implementation to make compiler happy
44-
};
45-
return dummyAction;
46-
}
47-
48-
@Override
49-
public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
50-
currentEditor = targetEditor;
51-
}
52-
53-
@Override
54-
public void init(IAction action) {
55-
}
56-
57-
@Override
58-
public void dispose() {
59-
currentEditor = null;
60-
dummyAction = null;
61-
super.dispose();
62-
}
63-
64-
@Override
65-
public void runWithEvent(IAction action, Event event) {
66-
if (!(currentEditor instanceof ITextEditor)) {
67-
return;
68-
}
69-
IVerticalRulerInfo rulerInfo = currentEditor.getAdapter(IVerticalRulerInfo.class);
70-
if (rulerInfo == null) {
71-
return;
72-
}
73-
int lineOfLastMouseButtonActivity = rulerInfo.getLineOfLastMouseButtonActivity();
74-
if (lineOfLastMouseButtonActivity < 0) {
75-
return;
76-
}
77-
IDocument document = getDocument((ITextEditor) currentEditor);
78-
if (document == null) {
79-
return;
80-
}
81-
ToggleBreakpointAdapter toggle = new ToggleBreakpointAdapter();
82-
try {
83-
ITextSelection selection = getTextSelection(currentEditor, document, lineOfLastMouseButtonActivity);
84-
if (toggle.canToggleLineBreakpoints(currentEditor, selection)) {
85-
BreakpointToggleUtils.setUnsetLambdaEntryBreakpoint(true);
86-
toggle.toggleBreakpoints(currentEditor, selection);
87-
}
88-
} catch (BadLocationException | CoreException e) {
89-
DebugUIPlugin.log(e);
90-
}
91-
}
92-
93-
private static IDocument getDocument(ITextEditor editor) {
94-
IDocumentProvider provider = editor.getDocumentProvider();
95-
if (provider != null) {
96-
return provider.getDocument(editor.getEditorInput());
97-
}
98-
IDocument doc = editor.getAdapter(IDocument.class);
99-
if (doc != null) {
100-
return doc;
101-
}
102-
return null;
103-
}
104-
105-
private static ITextSelection getTextSelection(IEditorPart editor, IDocument document, int line) throws BadLocationException {
106-
IRegion region = document.getLineInformation(line);
107-
ITextSelection textSelection = new TextSelection(document, region.getOffset(), 0);
108-
ISelectionProvider provider = editor.getSite().getSelectionProvider();
109-
if (provider != null) {
110-
ISelection selection = provider.getSelection();
111-
if (selection instanceof ITextSelection && ((ITextSelection) selection).getStartLine() <= line
112-
&& ((ITextSelection) selection).getEndLine() >= line) {
113-
textSelection = (ITextSelection) selection;
114-
}
115-
}
116-
return textSelection;
22+
protected boolean doWork(ITextEditor editor, ITextSelection selection) {
23+
BreakpointToggleUtils.setUnsetLambdaEntryBreakpoint(true);
24+
return true;
11725
}
11826
}

org.eclipse.jdt.debug.ui/ui/org/eclipse/jdt/internal/debug/ui/actions/RulerToggleTracepointActionDelegate.java

Lines changed: 4 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,95 +13,14 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.debug.ui.actions;
1515

16-
import org.eclipse.core.runtime.CoreException;
17-
import org.eclipse.debug.internal.ui.DebugUIPlugin;
18-
import org.eclipse.jface.action.Action;
19-
import org.eclipse.jface.action.IAction;
20-
import org.eclipse.jface.text.BadLocationException;
21-
import org.eclipse.jface.text.IDocument;
22-
import org.eclipse.jface.text.IRegion;
2316
import org.eclipse.jface.text.ITextSelection;
24-
import org.eclipse.jface.text.TextSelection;
25-
import org.eclipse.jface.text.source.IVerticalRulerInfo;
26-
import org.eclipse.swt.widgets.Event;
27-
import org.eclipse.ui.IActionDelegate2;
28-
import org.eclipse.ui.IEditorPart;
29-
import org.eclipse.ui.texteditor.AbstractRulerActionDelegate;
30-
import org.eclipse.ui.texteditor.IDocumentProvider;
3117
import org.eclipse.ui.texteditor.ITextEditor;
3218

33-
public class RulerToggleTracepointActionDelegate extends AbstractRulerActionDelegate implements IActionDelegate2 {
34-
35-
private IEditorPart currentEditor;
36-
private IAction dummyAction;
19+
public class RulerToggleTracepointActionDelegate extends AbstractRulerToggleBreakpointActionDelegate {
3720

3821
@Override
39-
protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) {
40-
dummyAction = new Action() {
41-
// empty implementation to make compiler happy
42-
};
43-
return dummyAction;
44-
}
45-
46-
@Override
47-
public void setActiveEditor(IAction callerAction, IEditorPart targetEditor) {
48-
currentEditor = targetEditor;
49-
}
50-
51-
@Override
52-
public void init(IAction action) {
53-
}
54-
55-
@Override
56-
public void dispose() {
57-
currentEditor = null;
58-
dummyAction = null;
59-
super.dispose();
60-
}
61-
62-
@Override
63-
public void runWithEvent(IAction action, Event event) {
64-
if (!(currentEditor instanceof ITextEditor)) {
65-
return;
66-
}
67-
IVerticalRulerInfo rulerInfo = currentEditor.getAdapter(IVerticalRulerInfo.class);
68-
if (rulerInfo == null) {
69-
return;
70-
}
71-
int lineOfLastMouseButtonActivity = rulerInfo.getLineOfLastMouseButtonActivity();
72-
if (lineOfLastMouseButtonActivity < 0) {
73-
return;
74-
}
75-
IDocument document = getDocument((ITextEditor) currentEditor);
76-
if (document == null) {
77-
return;
78-
}
79-
ToggleBreakpointAdapter toggle = new ToggleBreakpointAdapter();
80-
try {
81-
ITextSelection selection = getTextSelection(document, lineOfLastMouseButtonActivity);
82-
if (toggle.canToggleLineBreakpoints(currentEditor, selection)) {
83-
BreakpointToggleUtils.setUnsetTracepoints(true);
84-
toggle.toggleBreakpoints(currentEditor, selection);
85-
}
86-
} catch (BadLocationException | CoreException e) {
87-
DebugUIPlugin.log(e);
88-
}
89-
}
90-
91-
private static IDocument getDocument(ITextEditor editor) {
92-
IDocumentProvider provider = editor.getDocumentProvider();
93-
if (provider != null) {
94-
return provider.getDocument(editor.getEditorInput());
95-
}
96-
IDocument doc = editor.getAdapter(IDocument.class);
97-
if (doc != null) {
98-
return doc;
99-
}
100-
return null;
101-
}
102-
103-
private static ITextSelection getTextSelection(IDocument document, int line) throws BadLocationException {
104-
IRegion region = document.getLineInformation(line);
105-
return new TextSelection(document, region.getOffset(), 0);
22+
protected boolean doWork(ITextEditor editor, ITextSelection selection) {
23+
BreakpointToggleUtils.setUnsetTracepoints(true);
24+
return true;
10625
}
10726
}

0 commit comments

Comments
 (0)