KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > breakpoints > BreakpointsViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.views.breakpoints;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Map.Entry;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.IBreakpointManager;
23 import org.eclipse.debug.core.model.IBreakpoint;
24 import org.eclipse.debug.internal.ui.DebugUIPlugin;
25 import org.eclipse.debug.ui.IBreakpointOrganizerDelegateExtension;
26 import org.eclipse.jface.viewers.CheckboxTreeViewer;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.ITreeContentProvider;
29 import org.eclipse.jface.viewers.TreePath;
30 import org.eclipse.swt.widgets.Item;
31 import org.eclipse.swt.widgets.Tree;
32 import org.eclipse.swt.widgets.TreeItem;
33 import org.eclipse.swt.widgets.Widget;
34
35 /**
36  * Breakpoints viewer.
37  */

38 public class BreakpointsViewer extends CheckboxTreeViewer {
39     
40     /**
41      * Constructs a new breakpoints viewer with the given tree.
42      *
43      * @param tree
44      */

45     public BreakpointsViewer(Tree tree) {
46         super(tree);
47     }
48     
49     /**
50      * Returns the selected items.
51      *
52      * @return seleted items
53      */

54     public Item[] getSelectedItems() {
55         return getSelection(getControl());
56     }
57     
58     /**
59      * Returns the item assocaited with the given element, or <code>null</code>.
60      *
61      * @param element element in breakpoints view
62      * @return item assocaited with the given element, or <code>null</code>
63      */

64     public Widget searchItem(Object JavaDoc element) {
65         return findItem(element);
66     }
67     
68     /**
69      * Refreshes the given item in the tree.
70      *
71      * @param item item to refresh
72      */

73     public void refreshItem(TreeItem item) {
74         updateItem(item, item.getData());
75     }
76     
77     /**
78      * Returns a collection of currently visible breakpoints.
79      *
80      * @return collection of currently visible breakpoints
81      */

82     public IBreakpoint[] getVisibleBreakpoints() {
83         IBreakpointManager manager= DebugPlugin.getDefault().getBreakpointManager();
84         Object JavaDoc[] elements= ((ITreeContentProvider)getContentProvider()).getElements(manager);
85         List JavaDoc list = new ArrayList JavaDoc();
86         for (int i = 0; i < elements.length; i++) {
87             TreeItem item = (TreeItem) searchItem(elements[i]);
88             if (item != null) {
89                 collectExpandedBreakpoints(item, list);
90             }
91         }
92         return (IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]);
93     }
94
95     /**
96      * Adds expanded breakpoints to the list. Traverses children of the given
97      * tree item if any.
98      *
99      * @param item
100      * @param list collection of visible breakpoints
101      */

102     private void collectExpandedBreakpoints(TreeItem item, List JavaDoc list) {
103         Object JavaDoc data = item.getData();
104         if (data instanceof IBreakpoint) {
105             list.add(data);
106             return;
107         }
108         if (item.getExpanded()) {
109             TreeItem[] items = item.getItems();
110             for (int i = 0; i < items.length; i++) {
111                 collectExpandedBreakpoints(items[i], list);
112             }
113         }
114     }
115
116     /**
117      * Sets the selection to a specific tree item
118      *
119      * @param item
120      */

121     protected void setSelection(TreeItem item) {
122         getTree().setSelection(new TreeItem[]{item});
123         updateSelection(getSelection());
124     }
125
126     /**
127      * Returns the container from within the specified path that is the container the breakpoint can be removed from
128      * @param breakpoint the breakpoint to get the container for
129      * @return the first found container that includes the breakpoint that allows removal, or <code>null</code> if none found
130      * @since 3.3
131      */

132     public BreakpointContainer getRemovableContainer(Item item) {
133         if(item == null) {
134             return null;
135         }
136         if(item.getData() instanceof IBreakpoint) {
137             TreePath path = getTreePathFromItem(item);
138             if(path != null) {
139                 IBreakpoint breakpoint = (IBreakpoint) path.getLastSegment();
140                 BreakpointContainer container = null;
141                 for(int i = path.getSegmentCount()-2; i > -1; i--) {
142                     container = (BreakpointContainer) path.getSegment(i);
143                     if(container.contains(breakpoint) && container.getOrganizer().canRemove(breakpoint, container.getCategory())) {
144                         return container;
145                     }
146                 }
147             }
148         }
149         return null;
150     }
151     
152     /**
153      * Returns the addable breakpoint container of the specified breakpoint
154      * @param breakpoint the breakpoint to get the container for
155      * @return the first found addable container for the specified breakpoint or <code>null</code> if none found
156      * @since 3.3
157      */

158     public BreakpointContainer getAddableContainer(Item item) {
159         TreePath path = getTreePathFromItem(item);
160         if(path != null) {
161             Object JavaDoc element = path.getLastSegment();
162             if(element instanceof IBreakpoint) {
163                 BreakpointContainer container = null;
164                 IBreakpoint breakpoint = (IBreakpoint) element;
165                 for(int i = path.getSegmentCount()-2; i > -1; i--) {
166                     container = (BreakpointContainer) path.getSegment(i);
167                     if(container.contains(breakpoint) && container.getOrganizer().canAdd(breakpoint, container.getCategory())) {
168                         return container;
169                     }
170                 }
171             }
172         }
173         return null;
174     }
175     
176     /**
177      * Returns if the selected item in the tree can be dragged
178      * <p>
179      * Scheme:
180      * <ul>
181      * <li>breakpoint containers cannot be dragged</li>
182      * <li>breakpoints can be dragged iff the container they reside in supports the removal of breakpoints</li>
183      * </ul>
184      * </p>
185      * @param element the element to test if it can be dragged
186      * @return true if the selected element can be dragged, false otherwise
187      * @since 3.3
188      */

189     public boolean canDrag(Item[] items) {
190         if(items == null) {
191             return false;
192         }
193         if(items.length == 0) {
194             return false;
195         }
196         for(int i = 0; i < items.length; i++) {
197             if(getRemovableContainer(items[i]) == null) {
198                 return false;
199             }
200         }
201         return true;
202     }
203     
204     /**
205      * Performs the actual removal of breakpoints from their respective (removable) containers on a successful drag operation
206      * @param selection the selection of breakpoints involved in the drag
207      * @since 3.3
208      */

209     public void performDrag(Item[] items) {
210         if(items == null) {
211             return;
212         }
213         Map JavaDoc containersToBreakpoints = new HashMap JavaDoc();
214         BreakpointContainer container = null;
215         IBreakpoint breakpoint = null;
216         for(int i = 0; i < items.length; i++) {
217             if(!items[i].isDisposed()) {
218                 breakpoint = (IBreakpoint)items[i].getData();
219                 container = getRemovableContainer(items[i]);
220                 if(container != null) {
221                     List JavaDoc list = (List JavaDoc) containersToBreakpoints.get(container);
222                     if (list == null) {
223                         list = new ArrayList JavaDoc();
224                         containersToBreakpoints.put(container, list);
225                     }
226                     list.add(breakpoint);
227                 }
228             }
229         }
230         Iterator JavaDoc iterator = containersToBreakpoints.entrySet().iterator();
231         while (iterator.hasNext()) {
232             Entry entry = (Entry) iterator.next();
233             container = (BreakpointContainer) entry.getKey();
234             List JavaDoc list = (List JavaDoc) entry.getValue();
235             IBreakpointOrganizer organizer = container.getOrganizer();
236             IBreakpoint[] breakpoints = (IBreakpoint[]) list.toArray(new IBreakpoint[list.size()]);
237             if (organizer instanceof IBreakpointOrganizerDelegateExtension) {
238                 IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer;
239                 extension.removeBreakpoints(breakpoints, container.getCategory());
240             } else {
241                 for (int i = 0; i < breakpoints.length; i++) {
242                     organizer.removeBreakpoint(breakpoints[i], container.getCategory());
243                 }
244             }
245         }
246     }
247     
248     /**
249      * Determines if the specified element can be dropped into the specified target
250      * <p>
251      * Scheme:
252      * <ul>
253      * <li>Breakpoints can be dropped into working sets</li>
254      * <li>Breakpoints can be dropped into breakpoints, provided there is a droppable parent of the target breakpoint</li>
255      * </ul>
256      * </p>
257      * @param target the target foor the drop
258      * @param element the element we want to drop
259      * @return true if the specified element can be dropped into the specified target, false otherwise
260      * @since 3.3
261      */

262     public boolean canDrop(Item target, IStructuredSelection selection) {
263         if(selection == null || target == null) {
264             return false;
265         }
266         for(Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
267             if(!checkAddableParentContainers(target, (IBreakpoint) iter.next())) {
268                 return false;
269             }
270         }
271         return true;
272     }
273
274     /**
275      * This method is used to determine if there is an addable parent container available for the specified drop target.
276      * <p>
277      * A drop target can be either a <code>BreakpointContainer</code> or an <code>IBreakpoint</code>. This method always checks the entire heirarchy
278      * of the tree path for the specified target in the event one of the parent element does not support dropping.
279      * </p>
280      * @param target
281      * @param breakpoint
282      * @return
283      */

284     private boolean checkAddableParentContainers(Item target, IBreakpoint breakpoint) {
285         BreakpointContainer container = null;
286         TreePath path = getTreePathFromItem(target);
287         if(path != null) {
288             Object JavaDoc element = null;
289             for(int i = path.getSegmentCount()-1; i > -1; i--) {
290                 element = path.getSegment(i);
291                 if(element instanceof BreakpointContainer) {
292                     container = (BreakpointContainer) element;
293                     if(container.contains(breakpoint) || !container.getOrganizer().canAdd(breakpoint, container.getCategory())) {
294                         return false;
295                     }
296                 }
297             }
298         }
299         return true;
300     }
301
302     /**
303      * Performs the actual addition of the selected breakpoints to the specified target
304      * @param target the target to add the selection of breakpoints to
305      * @param selection the selection of breakpoints
306      * @return true if the drop occurred, false otherwise
307      * @since 3.3
308      */

309     public boolean performDrop(Item target, IStructuredSelection selection) {
310         if(target == null || selection == null) {
311             return false;
312         }
313         IBreakpoint breakpoint = null;
314         Object JavaDoc element = target.getData();
315         BreakpointContainer container = (element instanceof BreakpointContainer ? (BreakpointContainer)element : getAddableContainer(target));
316         if(container == null) {
317             return false;
318         }
319         IBreakpointOrganizer organizer = container.getOrganizer();
320         if (organizer instanceof IBreakpointOrganizerDelegateExtension) {
321             IBreakpointOrganizerDelegateExtension extension = (IBreakpointOrganizerDelegateExtension) organizer;
322             Object JavaDoc[] array = selection.toArray();
323             IBreakpoint[] breakpoints = new IBreakpoint[array.length];
324             System.arraycopy(array, 0, breakpoints, 0, array.length);
325             extension.addBreakpoints(breakpoints, container.getCategory());
326         } else {
327             for(Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
328                 breakpoint = (IBreakpoint) iter.next();
329                 organizer.addBreakpoint(breakpoint, container.getCategory());
330             }
331         }
332         expandToLevel(target.getData(), ALL_LEVELS);
333         return true;
334     }
335     
336     /* (non-Javadoc)
337      * @see org.eclipse.jface.viewers.Viewer#refresh()
338      */

339     public void refresh() {
340         super.refresh();
341         initializeCheckedState();
342     }
343
344     /**
345      * Sets the initial checked state of the items in the viewer.
346      */

347     private void initializeCheckedState() {
348         TreeItem[] items = getTree().getItems();
349         for (int i = 0; i < items.length; i++) {
350             updateCheckedState(items[i]);
351         }
352     }
353     
354     /**
355      * Update the checked state up the given element and all of its children.
356      *
357      * @param element
358      */

359     public void updateCheckedState(Object JavaDoc element) {
360         Widget[] widgets = searchItems(element);
361         for (int i = 0; i < widgets.length; i++) {
362             Widget widget = widgets[i];
363             if (widget != null) {
364                 updateCheckedState((TreeItem)widget);
365             }
366         }
367     }
368    
369     /**
370      * finds all occurrences of a widget to update
371      * @param element the element to search for when finding occurrences
372      * @return a list of widget occurrences to update or an empty list
373      */

374     private Widget[] searchItems(Object JavaDoc element) {
375         ArrayList JavaDoc list = new ArrayList JavaDoc();
376         TreeItem[] items = getTree().getItems();
377         for (int i = 0; i < items.length; i++) {
378             findAllOccurrences(items[i], element, list);
379         }
380         return (Widget[]) list.toArray(new Widget[0]);
381     }
382     
383     /**
384      * performs the actual search for items in the tree
385      * @param list the list to add matches to
386      * @param item the item in the tree
387      * @param element the element to compare
388      */

389     private void findAllOccurrences(TreeItem item, Object JavaDoc element, ArrayList JavaDoc list) {
390         if (element.equals(item.getData())) {
391                 list.add(item);
392         }
393         TreeItem[] items = item.getItems();
394         for (int i = 0; i < items.length; i++) {
395             findAllOccurrences(items[i], element, list);
396         }
397     }
398     
399     /**
400      * Update the checked state up the given element and all of its children.
401      *
402      * @param element
403      */

404     public void updateCheckedState(TreeItem item) {
405         Object JavaDoc element = item.getData();
406         if (element instanceof IBreakpoint) {
407             try {
408                 item.setChecked(((IBreakpoint) element).isEnabled());
409                 refreshItem(item);
410             } catch (CoreException e) {
411                 DebugUIPlugin.log(e);
412             }
413         } else if (element instanceof BreakpointContainer) {
414             IBreakpoint[] breakpoints = ((BreakpointContainer) element).getBreakpoints();
415             int enabledChildren= 0;
416             for (int i = 0; i < breakpoints.length; i++) {
417                 IBreakpoint breakpoint = breakpoints[i];
418                 try {
419                     if (breakpoint.isEnabled()) {
420                         enabledChildren++;
421                     }
422                 } catch (CoreException e) {
423                     DebugUIPlugin.log(e);
424                 }
425             }
426             if (enabledChildren == 0) {
427                 // Uncheck the container node if no children are enabled
428
item.setGrayed(false);
429                 item.setChecked(false);
430             } else if (enabledChildren == breakpoints.length) {
431                 // Check the container if all children are enabled
432
item.setGrayed(false);
433                 item.setChecked(true);
434             } else {
435                 // If some but not all children are enabled, gray the container node
436
item.setGrayed(true);
437                 item.setChecked(true);
438             }
439             // Update any children (breakpoints and containers)
440
TreeItem[] items = item.getItems();
441             for (int i = 0; i < items.length; i++) {
442                 updateCheckedState(items[i]);
443             }
444         }
445     }
446 }
447
Popular Tags