KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.model.IBreakpoint;
21 import org.eclipse.debug.ui.IBreakpointOrganizerDelegate;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.jface.viewers.AbstractTreeViewer;
25 import org.eclipse.jface.viewers.ITreeContentProvider;
26 import org.eclipse.jface.viewers.Viewer;
27
28 /**
29  * Content provider for the breakpoints view
30  */

31 public class BreakpointsContentProvider implements ITreeContentProvider, IPropertyChangeListener {
32     
33     private IBreakpointOrganizer[] fOrganizers = null;
34     private BreakpointsViewer fViewer;
35     private Object JavaDoc[] fElements;
36     private boolean fDisposed = false;
37     
38     /* (non-Javadoc)
39      * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
40      */

41     public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
42         if (parentElement.equals(DebugPlugin.getDefault().getBreakpointManager())) {
43             return fElements;
44         } else if (parentElement instanceof BreakpointContainer) {
45             return ((BreakpointContainer)parentElement).getChildren();
46         }
47         return new Object JavaDoc[0];
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
52      */

53     public Object JavaDoc getParent(Object JavaDoc element) {
54         return null;
55     }
56
57     /* (non-Javadoc)
58      * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
59      */

60     public boolean hasChildren(Object JavaDoc element) {
61         return getChildren(element).length > 0;
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
66      */

67     public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
68         return getChildren(inputElement);
69     }
70
71     /* (non-Javadoc)
72      * @see org.eclipse.jface.viewers.IContentProvider#dispose()
73      */

74     public void dispose() {
75         fDisposed = true;
76         fElements = null;
77         setOrganizers(null);
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
82      */

83     public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
84         fViewer = (BreakpointsViewer)viewer;
85         if (newInput != null) {
86             reorganize();
87         }
88     }
89     
90     /**
91      * Sets the nested order of breakpoint organizers, or <code>null</code>
92      * if none.
93      *
94      * @param organizers the nested order of breakpoint organizers, or <code>null</code>
95      * if none
96      */

97     public void setOrganizers(IBreakpointOrganizer[] organizers) {
98         if (organizers != null && organizers.length == 0) {
99             organizers = null;
100         }
101         // remove previous listeners
102
if (fOrganizers != null) {
103             for (int i = 0; i < fOrganizers.length; i++) {
104                 fOrganizers[i].removePropertyChangeListener(this);
105             }
106         }
107         fOrganizers = organizers;
108         // add listeners
109
if (fOrganizers != null) {
110             for (int i = 0; i < fOrganizers.length; i++) {
111                 fOrganizers[i].addPropertyChangeListener(this);
112             }
113         }
114         if (!fDisposed) {
115             fViewer.getControl().setRedraw(false);
116             // maintain expandsion based on visible breakpoints
117
IBreakpoint[] breakpoints = null;
118             if (isShowingGroups()) {
119                 breakpoints = fViewer.getVisibleBreakpoints();
120             }
121             reorganize();
122             if (isShowingGroups()) {
123                 // restore expansion
124
for (int i = 0; i < fElements.length; i++) {
125                     BreakpointContainer container = (BreakpointContainer) fElements[i];
126                     for (int j = 0; j < breakpoints.length; j++) {
127                         if (container.contains(breakpoints[j])) {
128                             fViewer.expandToLevel(container, AbstractTreeViewer.ALL_LEVELS);
129                             fViewer.updateCheckedState(container);
130                             break;
131                         }
132                     }
133                     
134                 }
135             }
136             fViewer.getControl().setRedraw(true);
137         }
138     }
139     
140     /**
141      * Returns the root containers containing the given breakpoint, or <code>null</code>
142      * if none
143      *
144      * @param breakpoint
145      * @return root containers containing the given breakpoint or <code>null</code>
146      */

147     public BreakpointContainer[] getRoots(IBreakpoint breakpoint) {
148         if (isShowingGroups()) {
149             List JavaDoc list = new ArrayList JavaDoc();
150             for (int i = 0; i < fElements.length; i++) {
151                 BreakpointContainer container = (BreakpointContainer) fElements[i];
152                 if (container.contains(breakpoint)) {
153                     list.add(container);
154                 }
155             }
156             return (BreakpointContainer[]) list.toArray(new BreakpointContainer[list.size()]);
157         }
158         return null;
159     }
160     
161     /**
162      * Returns the nested order of breakpoint organizers being used, or <code>null</code>
163      * if none.
164      *
165      * @return the nested order of breakpoint organizers being used, or <code>null</code>
166      * if none
167      */

168     IBreakpointOrganizer[] getOrganizers() {
169         return fOrganizers;
170     }
171     
172     /**
173      * Organizes the breakpoints based on nested categories, if any.
174      */

175     protected void reorganize() {
176         IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
177         if (fOrganizers == null) {
178             fElements = breakpoints;
179         } else {
180             IBreakpointOrganizer organizer = fOrganizers[0];
181             Map JavaDoc categoriesToContainers = new HashMap JavaDoc();
182             for (int i = 0; i < breakpoints.length; i++) {
183                 IBreakpoint breakpoint = breakpoints[i];
184                 IAdaptable[] categories = organizer.getCategories(breakpoint);
185                 if (categories == null || categories.length == 0) {
186                     categories = OtherBreakpointCategory.getCategories(organizer);
187                 }
188                 for (int j = 0; j < categories.length; j++) {
189                     IAdaptable category = categories[j];
190                     BreakpointContainer container = (BreakpointContainer) categoriesToContainers.get(category);
191                     if (container == null) {
192                         IBreakpointOrganizer[] nesting = null;
193                         if (fOrganizers.length > 1) {
194                             nesting = new IBreakpointOrganizer[fOrganizers.length - 1];
195                             System.arraycopy(fOrganizers, 1, nesting, 0, nesting.length);
196                         }
197                         container = new BreakpointContainer(category, organizer, nesting);
198                         categoriesToContainers.put(category, container);
199                     }
200                     container.addBreakpoint(breakpoint);
201                 }
202             }
203             // add empty categories
204
IAdaptable[] emptyCategories = organizer.getCategories();
205             if (emptyCategories != null) {
206                 for (int i = 0; i < emptyCategories.length; i++) {
207                     IAdaptable category = emptyCategories[i];
208                     BreakpointContainer container = (BreakpointContainer) categoriesToContainers.get(category);
209                     if (container == null) {
210                         container = new BreakpointContainer(category, organizer, null);
211                         categoriesToContainers.put(category, container);
212                     }
213                 }
214             }
215             fElements = categoriesToContainers.values().toArray();
216         }
217         fViewer.getControl().setRedraw(false);
218         fViewer.refresh();
219         fViewer.getControl().setRedraw(true);
220     }
221
222     /* (non-Javadoc)
223      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
224      */

225     public void propertyChange(PropertyChangeEvent event) {
226         if (event.getProperty().equals(IBreakpointOrganizerDelegate.P_CATEGORY_CHANGED)) {
227             // TODO: only re-organize if showing the changed category
228
reorganize();
229         }
230     }
231     
232     /**
233      * Returns the existing containers the given breakpoint is contained in, or <code>null</code>.
234      *
235      * @param breakpoint
236      * @return the existing containers the given breakpoint is contained in, or <code>null</code>
237      */

238     protected BreakpointContainer[] getContainers(IBreakpoint breakpoint) {
239         if (isShowingGroups()) {
240             IAdaptable[] categories = fOrganizers[0].getCategories(breakpoint);
241             if (categories == null || categories.length == 0) {
242                 categories = OtherBreakpointCategory.getCategories(fOrganizers[0]);
243             }
244             BreakpointContainer[] containers = new BreakpointContainer[categories.length];
245             int index = 0;
246             for (int i = 0; i < fElements.length; i++) {
247                 BreakpointContainer container = (BreakpointContainer)fElements[i];
248                 for (int j = 0; j < categories.length; j++) {
249                     IAdaptable category = categories[j];
250                     if (container.getCategory().equals(category)) {
251                         containers[index] = container;
252                         index++;
253                     }
254                 }
255             }
256             return containers;
257         }
258         return null;
259     }
260
261     /**
262      * Returns whether content is grouped by categories.
263      *
264      * @return whether content is grouped by categories
265      */

266     protected boolean isShowingGroups() {
267         return fOrganizers != null;
268     }
269 }
270
Popular Tags