17
17
import org .eclipse .debug .core .model .Breakpoint ;
18
18
import org .eclipse .debug .internal .ui .DebugUIPlugin ;
19
19
import org .eclipse .debug .internal .ui .actions .ActionMessages ;
20
+ import org .eclipse .debug .internal .ui .viewers .model .provisional .TreeModelViewer ;
21
+ import org .eclipse .debug .internal .ui .views .breakpoints .BreakpointsView ;
22
+ import org .eclipse .debug .ui .IDebugUIConstants ;
20
23
import org .eclipse .jface .action .IAction ;
21
- import org .eclipse .jface .dialogs .Dialog ;
22
- import org .eclipse .jface .dialogs .IDialogConstants ;
23
24
import org .eclipse .jface .viewers .ISelection ;
24
25
import org .eclipse .jface .viewers .IStructuredSelection ;
25
- import org .eclipse .jface .window . Window ;
26
+ import org .eclipse .jface .viewers . TreeSelection ;
26
27
import org .eclipse .swt .SWT ;
27
- import org .eclipse .swt .events .SelectionAdapter ;
28
- import org .eclipse .swt .events .SelectionEvent ;
29
- import org .eclipse .swt .layout . GridData ;
30
- import org .eclipse .swt .widgets . Button ;
31
- import org .eclipse .swt .widgets . Composite ;
32
- import org .eclipse .swt .widgets . Control ;
28
+ import org .eclipse .swt .events .KeyAdapter ;
29
+ import org .eclipse .swt .events .KeyEvent ;
30
+ import org .eclipse .swt .graphics . Font ;
31
+ import org .eclipse .swt .graphics . GC ;
32
+ import org .eclipse .swt .graphics . Point ;
33
+ import org .eclipse .swt .graphics . Rectangle ;
33
34
import org .eclipse .swt .widgets .Label ;
34
- import org .eclipse .swt .widgets .Shell ;
35
35
import org .eclipse .swt .widgets .Text ;
36
+ import org .eclipse .swt .widgets .TreeItem ;
37
+ import org .eclipse .swt .widgets .Widget ;
36
38
import org .eclipse .ui .IViewActionDelegate ;
37
39
import org .eclipse .ui .IViewPart ;
40
+ import org .eclipse .ui .IWorkbenchPage ;
41
+ import org .eclipse .ui .PlatformUI ;
38
42
39
43
public class BreakpointLabelAction implements IViewActionDelegate {
40
44
41
45
private IViewPart fView ;
42
-
43
46
protected IViewPart getView () {
44
47
return fView ;
45
48
}
@@ -50,26 +53,85 @@ protected void setView(IViewPart view) {
50
53
51
54
@ Override
52
55
public void run (IAction action ) {
56
+ String emptyString = "" ; //$NON-NLS-1$
53
57
IStructuredSelection selection = getSelection ();
54
- if (selection .getFirstElement () instanceof Breakpoint bp ) {
55
- String label = askForLabel (bp );
56
- if (label == null ) {
57
- return ;
58
- }
59
- if (!label .isEmpty ()) {
60
- try {
61
- bp .setBreakpointLabel (label );
62
- } catch (CoreException e ) {
63
- DebugUIPlugin .log (e );
64
- }
65
- } else {
66
- try {
67
- bp .setBreakpointLabel ("" ); //$NON-NLS-1$
68
- } catch (CoreException e ) {
69
- DebugUIPlugin .log (e );
58
+
59
+ if (selection instanceof TreeSelection treeSelect && selection .getFirstElement () instanceof Breakpoint breakpoint ) {
60
+ if (treeSelect .size () == 1 ) {
61
+ IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
62
+ IViewPart viewPart = page .findView (IDebugUIConstants .ID_BREAKPOINT_VIEW );
63
+ if (viewPart instanceof BreakpointsView breakpointView ) {
64
+ TreeModelViewer treeViewer = breakpointView .getTreeModelViewer ();
65
+ Widget item = treeViewer .findItem (treeSelect .getPaths ()[0 ]);
66
+ if (item instanceof TreeItem tree ) {
67
+ String current = tree .getText ();
68
+ Rectangle bounds ;
69
+ try {
70
+ bounds = tree .getBounds ();
71
+ } catch (ArrayIndexOutOfBoundsException e ) { // TreeItem having FontData [Breakpoints having
72
+ // custom label]
73
+ tree .setFont (null );
74
+ GC gc = new GC (tree .getParent ());
75
+ Font currentFont = gc .getFont ();
76
+ gc .setFont (currentFont );
77
+ Point textWidth = gc .textExtent (tree .getText ());
78
+ gc .dispose ();
79
+ bounds = tree .getBounds (0 );
80
+ bounds .x = bounds .x + 10 ;
81
+ bounds .width = textWidth .x + 20 ;
82
+
83
+ }
84
+ Label label = new Label (tree .getParent (), SWT .WRAP );
85
+ label .setText (ActionMessages .BreakpointLabelDialog );
86
+ label .setBounds (bounds .x , bounds .y - 20 , label .computeSize (SWT .DEFAULT , SWT .DEFAULT ).x ,
87
+ label .computeSize (SWT .DEFAULT , SWT .DEFAULT ).y );
88
+
89
+ Text inlineEditor = new Text (tree .getParent (), SWT .BORDER );
90
+ inlineEditor .setBounds (bounds .x , bounds .y , bounds .width , bounds .height );
91
+ inlineEditor .setText (current );
92
+ inlineEditor .setFocus ();
93
+
94
+ inlineEditor .addListener (SWT .FocusOut , event -> {
95
+ tree .setText (current );
96
+ label .dispose ();
97
+ inlineEditor .dispose ();
98
+
99
+ });
100
+ inlineEditor .addKeyListener (new KeyAdapter () {
101
+ @ Override
102
+ public void keyPressed (KeyEvent e ) {
103
+ if (e .keyCode == SWT .ESC ) {
104
+ tree .setText (current );
105
+ inlineEditor .dispose ();
106
+ label .dispose ();
107
+ } else if (e .keyCode == SWT .CR ) {
108
+ String newLabel = inlineEditor .getText ();
109
+ if (!newLabel .isEmpty () && !newLabel .equals (current )) {
110
+ try {
111
+ breakpoint .setBreakpointLabel (newLabel );
112
+ } catch (CoreException e1 ) {
113
+ DebugUIPlugin .log (e1 );
114
+ }
115
+ } else if (newLabel .isEmpty ()) {
116
+ try {
117
+ breakpoint .setBreakpointLabel (emptyString ); // Set to default
118
+ } catch (CoreException e2 ) {
119
+ DebugUIPlugin .log (e2 );
120
+ }
121
+ }
122
+ inlineEditor .dispose ();
123
+ label .dispose ();
124
+ }
125
+ }
126
+ });
127
+ tree .setText (emptyString );
128
+
129
+ }
130
+
70
131
}
71
132
}
72
133
}
134
+
73
135
}
74
136
75
137
protected IStructuredSelection getSelection () {
@@ -85,78 +147,4 @@ public void init(IViewPart view) {
85
147
setView (view );
86
148
}
87
149
88
- private String askForLabel (Breakpoint breakpoint ) {
89
- String current = breakpoint .getBreakpointLabel ();
90
-
91
- class BreakpointLabelBox extends Dialog {
92
- private Text textInput ;
93
- private String value ;
94
- private Button clearButton ;
95
- private String initialLabel ;
96
-
97
- protected BreakpointLabelBox (Shell parentShell , String intialValue ) {
98
- super (parentShell );
99
- initialLabel = intialValue ;
100
- }
101
- @ Override
102
- protected Control createDialogArea (Composite parent ) {
103
- Composite composite = (Composite ) super .createDialogArea (parent );
104
- Label label = new Label (composite , SWT .WRAP );
105
- label .setText (ActionMessages .BreakpointLabelDialog );
106
- GridData data = new GridData (GridData .GRAB_HORIZONTAL | GridData .GRAB_VERTICAL
107
- | GridData .HORIZONTAL_ALIGN_FILL | GridData .VERTICAL_ALIGN_CENTER );
108
- data .widthHint = convertHorizontalDLUsToPixels (IDialogConstants .MINIMUM_MESSAGE_AREA_WIDTH );
109
- label .setLayoutData (data );
110
- label .setFont (parent .getFont ());
111
- textInput = new Text (composite , SWT .SINGLE | SWT .BORDER );
112
- textInput .setLayoutData (new GridData (GridData .GRAB_HORIZONTAL | GridData .HORIZONTAL_ALIGN_FILL ));
113
- textInput .setText (initialLabel );
114
- applyDialogFont (composite );
115
- return composite ;
116
- }
117
-
118
- @ Override
119
- protected void configureShell (Shell newShell ) {
120
- super .configureShell (newShell );
121
- newShell .setText (ActionMessages .BreakpointLabelTitle );
122
-
123
- }
124
-
125
- @ Override
126
- protected void createButtonsForButtonBar (Composite parent ) {
127
- super .createButtonsForButtonBar (parent );
128
- clearButton = createButton (parent , IDialogConstants .BACK_ID , ActionMessages .BreakpointLabelClear ,
129
- false );
130
- clearButton .addSelectionListener (new SelectionAdapter () {
131
- @ Override
132
- public void widgetSelected (SelectionEvent e ) {
133
- textInput .setText ("" ); //$NON-NLS-1$
134
-
135
- }
136
- });
137
- }
138
-
139
- @ Override
140
- protected void buttonPressed (int buttonId ) {
141
- if (buttonId == IDialogConstants .OK_ID ) {
142
- value = textInput .getText ();
143
- } else {
144
- value = null ;
145
- }
146
- super .buttonPressed (buttonId );
147
- }
148
- private String getInput () {
149
- return value ;
150
- }
151
-
152
- }
153
- BreakpointLabelBox dialog = new BreakpointLabelBox (fView .getSite ().getShell (), current );
154
-
155
- if (dialog .open () != Window .OK ) {
156
- return null ;
157
- }
158
- String data = dialog .getInput ();
159
- return data .trim ();
160
- }
161
-
162
150
}
0 commit comments