Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c72c2333ce0957a5586a9ef0c9839e93bc8a798d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Patrick Chuong (Texas Instruments) - Improve usability of the breakpoint view (Bug 238956)
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.breakpoints;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.breakpoints.provisional.IBreakpointContainer;
import org.eclipse.debug.internal.ui.breakpoints.provisional.IBreakpointOrganizer;
import org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;

/**
 * Breakpoints viewer.
 */
public class BreakpointsViewer extends CheckboxTreeViewer {

    /**
     * Constructs a new breakpoints viewer with the given tree.
     *
     * @param tree the backing tree widget
     */
    public BreakpointsViewer(Tree tree) {
        super(tree);
    }

    /**
     * Returns the selected items.
     *
     * @return selected items
     */
    public Item[] getSelectedItems() {
        return getSelection(getControl());
    }

    /**
     * Returns the item associated with the given element, or <code>null</code>.
     *
     * @param element element in breakpoints view
     * @return item associated with the given element, or <code>null</code>
     */
    public Widget searchItem(Object element) {
        return findItem(element);
    }

    /**
     * Refreshes the given item in the tree.
     *
     * @param item item to refresh
     */
    public void refreshItem(TreeItem item) {
        updateItem(item, item.getData());
    }

    /**
     * Returns a collection of currently visible breakpoints.
     *
     * @return collection of currently visible breakpoints
     */
    public IBreakpoint[] getVisibleBreakpoints() {
        IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
        Object[] elements= ((ITreeContentProvider)getContentProvider()).getElements(manager);
		List<IBreakpoint> list = new ArrayList<>();
        for (int i = 0; i < elements.length; i++) {
            TreeItem item = (TreeItem) searchItem(elements[i]);
            if (item != null) {
                collectExpandedBreakpoints(item, list);
            }
        }
        return list.toArray(new IBreakpoint[list.size()]);
    }

    /**
     * Adds expanded breakpoints to the list. Traverses children of the given
     * tree item if any.
     *
     * @param item the item to get breakpoints from
     * @param list collection of visible breakpoints
     */
	private void collectExpandedBreakpoints(TreeItem item, List<IBreakpoint> list) {
        Object data = item.getData();
        if (data instanceof IBreakpoint) {
			list.add((IBreakpoint) data);
            return;
        }
        if (item.getExpanded()) {
            TreeItem[] items = item.getItems();
            for (int i = 0; i < items.length; i++) {
                collectExpandedBreakpoints(items[i], list);
            }
        }
    }

    /**
     * Sets the selection to a specific tree item
     *
     * @param item the item to set as the current tree selection
     */
    protected void setSelection(TreeItem item) {
    	getTree().setSelection(new TreeItem[]{item});
    	updateSelection(getSelection());
    }

	/**
     * Returns the container from within the specified path that is the container the breakpoint can be removed from
     * @param item the item to get the container for
     * @return the first found container that includes the breakpoint that allows removal, or <code>null</code> if none found
     * @since 3.3
     */
    public IBreakpointContainer getRemovableContainer(Item item) {
    	if(item == null) {
    		return null;
    	}
    	if(item.getData() instanceof IBreakpoint) {
	    	TreePath path = getTreePathFromItem(item);
	    	if(path != null) {
		    	IBreakpoint breakpoint = (IBreakpoint) path.getLastSegment();
		    	IBreakpointContainer container = null;
		    	for(int i = path.getSegmentCount()-2; i > -1; i--) {
		    		container = (IBreakpointContainer) path.getSegment(i);
		    		if(container.contains(breakpoint) && container.getOrganizer().canRemove(breakpoint, container.getCategory())) {
		    			return container;
		    		}
		    	}
	    	}
    	}
    	return null;
    }

    /**
     * Returns the addable breakpoint container of the specified breakpoint
     * @param item the item to get the container for
     * @return the first found addable container for the specified breakpoint or <code>null</code> if none found
     * @since 3.3
     */
    public IBreakpointContainer getAddableContainer(Item item) {
    	TreePath path = getTreePathFromItem(item);
    	if(path != null) {
	    	Object element = path.getLastSegment();
	    	if(element instanceof IBreakpoint) {
		    	IBreakpointContainer container = null;
		    	IBreakpoint breakpoint = (IBreakpoint) element;
		    	for(int i = path.getSegmentCount()-2; i > -1; i--) {
		    		container = (IBreakpointContainer) path.getSegment(i);
		    		if(container.contains(breakpoint) && container.getOrganizer().canAdd(breakpoint, container.getCategory())) {
		    			return container;
		    		}
		    	}
	    	}
    	}
    	return null;
    }

    /**
     * Returns if the selected item in the tree can be dragged
     * <p>
     * Scheme:
     * <ul>
     * <li>breakpoint containers cannot be dragged</li>
     * <li>breakpoints can be dragged iff the container they reside in supports the removal of breakpoints</li>
     * </ul>
     * </p>
     * @param items the items to test if they can be dragged
     * @return true if the selected element can be dragged, false otherwise
     * @since 3.3
     */
    public boolean canDrag(Item[] items) {
    	if(items == null) {
    		return false;
    	}
    	if(items.length == 0) {
    		return false;
    	}
    	for(int i = 0; i < items.length; i++) {
    		if(getRemovableContainer(items[i]) == null) {
    			return false;
    		}
    	}
    	return true;
    }

    /**
     * Performs the actual removal of breakpoints from their respective (removable) containers on a successful drag operation
     * @param items the items involved in the drag
     * @since 3.3
     */
    public void performDrag(Item[] items) {
    	if(items == null) {
    		return;
    	}
		Map<IBreakpointContainer, List<IBreakpoint>> containersToBreakpoints = new HashMap<>();
		IBreakpointContainer container = null;
    	IBreakpoint breakpoint = null;
    	for(int i = 0; i < items.length; i++) {
    		if(!items[i].isDisposed()) {
	    		breakpoint = (IBreakpoint)items[i].getData();
	    		container = getRemovableContainer(items[i]);
	    		if(container != null) {
					List<IBreakpoint> list = containersToBreakpoints.get(container);
	    			if (list == null) {
						list = new ArrayList<>();
	    				containersToBreakpoints.put(container, list);
	    			}
	    			list.add(breakpoint);
	    		}
    		}
    	}
		for (Entry<IBreakpointContainer, List<IBreakpoint>> entry : containersToBreakpoints.entrySet()) {
    		container = entry.getKey();
			List<IBreakpoint> list = entry.getValue();
    		IBreakpointOrganizer organizer = container.getOrganizer();
    		IBreakpoint[] breakpoints = list.toArray(new IBreakpoint[list.size()]);
    		if (organizer instanceof IBreakpointOrganizerDelegateExtension) {
				IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer;
				extension.removeBreakpoints(breakpoints, container.getCategory());
			} else {
				for (int i = 0; i < breakpoints.length; i++) {
					organizer.removeBreakpoint(breakpoints[i], container.getCategory());
				}
			}
    	}
    }

    /**
     * Determines if the specified element can be dropped into the specified target
     * <p>
     * Scheme:
     * <ul>
     * <li>Breakpoints can be dropped into working sets</li>
     * <li>Breakpoints can be dropped into breakpoints, provided there is a droppable parent of the target breakpoint</li>
     * </ul>
     * </p>
     * @param target the target for the drop
     * @param selection the selection we want to drop
     * @return true if the specified element can be dropped into the specified target, false otherwise
     * @since 3.3
     */
    public boolean canDrop(Item target, IStructuredSelection selection) {
    	if(selection == null  || target == null) {
    		return false;
    	}
		for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
    		Object currentObject = iter.next();
    		if (!(currentObject instanceof IBreakpoint) || !checkAddableParentContainers(target, (IBreakpoint)currentObject)){
    			return false;
    		}
    	}
    	return true;
    }

	/**
	 * This method is used to determine if there is an addable parent container available for the specified drop target.
	 * <p>
	 * A drop target can be either a <code>BreakpointContainer</code> or an <code>IBreakpoint</code>. This method always checks the entire hierarchy
	 * of the tree path for the specified target in the event one of the parent element does not support dropping.
	 * </p>
	 * @param target the target to check
	 * @param breakpoint the breakpoint we would like to drop
	 * @return <code>true</code> if there is a parent container we can drop into
	 */
	private boolean checkAddableParentContainers(Item target, IBreakpoint breakpoint) {
		IBreakpointContainer container = null;
		TreePath path = getTreePathFromItem(target);
		if(path != null) {
			Object element = null;
			for(int i = path.getSegmentCount()-1; i > -1; i--) {
				element = path.getSegment(i);
				if(element instanceof IBreakpointContainer) {
					container = (IBreakpointContainer) element;
					if(container.contains(breakpoint) || !container.getOrganizer().canAdd(breakpoint, container.getCategory())) {
		    			return false;
		    		}
				}
			}
		}
		return true;
	}

	/**
     * Performs the actual addition of the selected breakpoints to the specified target
     * @param target the target to add the selection of breakpoints to
     * @param selection the selection of breakpoints
     * @return true if the drop occurred, false otherwise
     * @since 3.3
     */
    public boolean performDrop(Item target, IStructuredSelection selection) {
		if(target == null || selection == null) {
    		return false;
    	}
    	IBreakpoint breakpoint = null;
    	Object element = target.getData();
    	IBreakpointContainer container = (element instanceof IBreakpointContainer ? (IBreakpointContainer)element : getAddableContainer(target));
    	if(container == null) {
			return false;
		}
    	IBreakpointOrganizer organizer = container.getOrganizer();
    	if (organizer instanceof IBreakpointOrganizerDelegateExtension) {
    		IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer;
    		Object[] array = selection.toArray();
    		IBreakpoint[] breakpoints = new IBreakpoint[array.length];
    		System.arraycopy(array, 0, breakpoints, 0, array.length);
    		extension.addBreakpoints(breakpoints, container.getCategory());
    	} else {
			for (Iterator<?> iter = selection.iterator(); iter.hasNext();) {
	    		breakpoint = (IBreakpoint) iter.next();
				organizer.addBreakpoint(breakpoint, container.getCategory());
	    	}
    	}
    	expandToLevel(target.getData(), ALL_LEVELS);
    	return true;
    }

	@Override
	public void refresh() {
		super.refresh();
		initializeCheckedState();
	}

	/**
	 * Sets the initial checked state of the items in the viewer.
	 */
	private void initializeCheckedState() {
		TreeItem[] items = getTree().getItems();
		for (int i = 0; i < items.length; i++) {
			updateCheckedState(items[i]);
		}
	}

    /**
     * Update the checked state up the given element and all of its children.
     *
     * @param element the element to update
     */
	public void updateCheckedState(Object element) {
        Widget[] widgets = searchItems(element);
        for (int i = 0; i < widgets.length; i++) {
            Widget widget = widgets[i];
            if (widget != null) {
                updateCheckedState((TreeItem)widget);
            }
        }
	}

	/**
	 * finds all occurrences of a widget to update
	 * @param element the element to search for when finding occurrences
	 * @return a list of widget occurrences to update or an empty list
	 */
    private Widget[] searchItems(Object element) {
		ArrayList<TreeItem> list = new ArrayList<>();
        TreeItem[] items = getTree().getItems();
        for (int i = 0; i < items.length; i++) {
        	findAllOccurrences(items[i], element, list);
        }
        return list.toArray(new Widget[0]);
    }

    /**
     * performs the actual search for items in the tree
     * @param list the list to add matches to
     * @param item the item in the tree
     * @param element the element to compare
     */
	private void findAllOccurrences(TreeItem item, Object element, ArrayList<TreeItem> list) {
        if (element.equals(item.getData())) {
			list.add(item);
        }
        TreeItem[] items = item.getItems();
        for (int i = 0; i < items.length; i++) {
        	findAllOccurrences(items[i], element, list);
        }
    }

    /**
     * Update the checked state up the given element and all of its children.
     *
     * @param item the item to update
     */
    public void updateCheckedState(TreeItem item) {
        Object element = item.getData();
        if (element instanceof IBreakpoint) {
            try {
                item.setChecked(((IBreakpoint) element).isEnabled());
                refreshItem(item);
            } catch (CoreException e) {
                DebugUIPlugin.log(e);
            }
        } else if (element instanceof IBreakpointContainer) {
            IBreakpoint[] breakpoints = ((IBreakpointContainer) element).getBreakpoints();
            int enabledChildren= 0;
            for (int i = 0; i < breakpoints.length; i++) {
                IBreakpoint breakpoint = breakpoints[i];
                try {
                    if (breakpoint.isEnabled()) {
                        enabledChildren++;
                    }
                } catch (CoreException e) {
                    DebugUIPlugin.log(e);
                }
            }
            if (enabledChildren == 0) {
                // Uncheck the container node if no children are enabled
                item.setGrayed(false);
                item.setChecked(false);
            } else if (enabledChildren == breakpoints.length) {
                // Check the container if all children are enabled
                item.setGrayed(false);
                item.setChecked(true);
            } else {
                // If some but not all children are enabled, gray the container node
                item.setGrayed(true);
                item.setChecked(true);
            }
            // Update any children (breakpoints and containers)
            TreeItem[] items = item.getItems();
            for (int i = 0; i < items.length; i++) {
                updateCheckedState(items[i]);
            }
        }
    }
}

Back to the top