KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > importexport > breakpoints > EmbeddedBreakpointsViewer


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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
12 package org.eclipse.debug.internal.ui.importexport.breakpoints;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.debug.core.model.IBreakpoint;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointContainer;
21 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsContentProvider;
22 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsLabelProvider;
23 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsComparator;
24 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView;
25 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsViewer;
26 import org.eclipse.debug.internal.ui.views.breakpoints.IBreakpointOrganizer;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.jface.viewers.CheckStateChangedEvent;
29 import org.eclipse.jface.viewers.IBaseLabelProvider;
30 import org.eclipse.jface.viewers.ICheckStateListener;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.graphics.Font;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Tree;
39 import org.eclipse.swt.widgets.TreeItem;
40 import org.eclipse.swt.widgets.Widget;
41 import org.eclipse.ui.IViewPart;
42
43 /**
44  * This class creates a simplified debug view that can be used in wizards etc., to emulate the current debug view
45  *
46  * @see WizardExportBreakpointsPage
47  * @see WizardImportBreakpointsPage
48  *
49  * @since 3.2
50  */

51 public class EmbeddedBreakpointsViewer {
52
53     //widgets
54
private IStructuredSelection fSelection = null;
55     private BreakpointsView fView = null;
56     private BreakpointsContentProvider fProvider = null;
57     private Tree fTree = null;
58     private BreakpointsViewer fViewer = null;
59     private ICheckStateListener fCheckListener = new ICheckStateListener() {
60         public void checkStateChanged(CheckStateChangedEvent event) {
61             updateCheckedState(event.getElement(), event.getChecked());
62         }
63     };
64     
65     //constants
66
private static final int HEIGHT_HINT = 150;
67     
68     /**
69      * This constructor uses the selection from the debug view to initialize its selection
70      *
71      * <p>
72      * Neither parent nor input can be null
73      * </p>
74      *
75      * @param parent the parent to add this composite to
76      * @param input the input to the viewer
77      */

78     public EmbeddedBreakpointsViewer(Composite parent, Object JavaDoc input) {
79         Assert.isNotNull(parent);
80         Assert.isNotNull(input);
81         createControl(parent, input, null);
82     }
83     
84     /**
85      * This constructor allows a specific selction to be used in stead of the default
86      *
87      * @param parent the parent composite to add this one to
88      * @param input the input to the viewer
89      * @param selection the selection to set on the viewer
90      */

91     public EmbeddedBreakpointsViewer(Composite parent, Object JavaDoc input, IStructuredSelection selection) {
92         Assert.isNotNull(parent);
93         Assert.isNotNull(input);
94         createControl(parent, input, selection);
95     }
96     
97     /**
98      * Creates the control initialized to the current view, selection, and organization of the breakpoints view
99      * @param parent the parent composite to add this one to.
100      *
101      * @param parent the parent composite to add this one to
102      * @param input the input for the viewer
103      * @param selection the selection for the viewer to be initialized to. If null the selection from the breakpoints view is used
104      */

105     private void createControl(Composite parent, Object JavaDoc input, IStructuredSelection selection) {
106         fSelection = selection;
107         if(fSelection == null) {
108             IViewPart fViewpart = DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().findView(IDebugUIConstants.ID_BREAKPOINT_VIEW);
109             if(fViewpart != null) {
110                 fSelection = (IStructuredSelection)fViewpart.getViewSite().getSelectionProvider().getSelection();
111             }
112             else {
113                 fSelection = new StructuredSelection();
114             }
115         }
116         Font font = parent.getFont();
117         Composite composite = new Composite(parent, SWT.NULL);
118         composite.setLayout(new GridLayout(1, true));
119         GridData grid = new GridData(GridData.FILL_BOTH);
120         grid.heightHint = HEIGHT_HINT;
121         composite.setLayoutData(grid);
122         composite.setFont(font);
123         
124         // create the treeview
125
fTree = new Tree(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CHECK);
126         fProvider = new BreakpointsContentProvider();
127         fView = ((BreakpointsView)DebugUIPlugin.getActiveWorkbenchWindow().getActivePage().findView(IDebugUIConstants.ID_BREAKPOINT_VIEW));
128         fTree.setLayoutData(new GridData(GridData.FILL_BOTH));
129         fViewer = new BreakpointsViewer(fTree);
130         //fix for bug #109008
131
IBaseLabelProvider labelProvider = null;
132         if (fView == null) {
133             labelProvider = new BreakpointsLabelProvider();
134         } else {
135             labelProvider = fView.getCheckboxViewer().getLabelProvider();
136         }
137         fViewer.setComparator(new BreakpointsComparator());
138         fViewer.setLabelProvider(labelProvider);
139         fViewer.addCheckStateListener(fCheckListener);
140         IBreakpointOrganizer[] orgs = null;
141         if(fView != null) {
142              orgs = fView.getBreakpointOrganizers();
143         }
144         fViewer.setContentProvider(fProvider);
145         fViewer.setInput(input);
146         fProvider.setOrganizers(orgs);
147         initViewerState();
148     }
149     
150     /**
151      * Performs the initialization of the viewer from a selection
152      */

153     private void initViewerState() {
154         Object JavaDoc[] items = fSelection.toArray();
155         fViewer.setGrayedElements(new Object JavaDoc[] {});
156         fViewer.setCheckedElements(new Object JavaDoc[] {});
157         ArrayList JavaDoc list = new ArrayList JavaDoc();
158         for(int i = 0; i < items.length; i++) {
159             Object JavaDoc item = items[i];
160             if(item instanceof IBreakpoint) {
161                 list.add(item);
162             }
163             else if (item instanceof BreakpointContainer) {
164                 getBreakpointsFromContainers((BreakpointContainer)item, list);
165             }
166         }
167         for(int i = 0; i < list.size(); i++) {
168             updateCheckedState(list.get(i), true);
169         }
170     }
171     
172     /**
173      * FInds the breakpoints of a given container
174      * @param container the container to get breakpoints from
175      * @param list the list of breakpoints to update state for
176      */

177     private void getBreakpointsFromContainers(BreakpointContainer container, ArrayList JavaDoc list) {
178         Object JavaDoc[] elements = container.getChildren();
179         for(int i = 0; i < elements.length; i++) {
180             if(elements[i] instanceof IBreakpoint) {
181                 list.add(elements[i]);
182             }
183             else {
184                 getBreakpointsFromContainers((BreakpointContainer)elements[i], list);
185             }
186         }
187     }
188     
189     /**
190      * Returns the selection from the viewer with no duplicates
191      * @return the selection from the viewer with no duplicates
192      */

193     public IStructuredSelection getCheckedElements() {
194         Object JavaDoc[] list = fViewer.getCheckedElements();
195         Vector JavaDoc selected = new Vector JavaDoc();
196         for(int i = 0; i < list.length; i++) {
197             if(!selected.contains(list[i])) {
198                 selected.addElement(list[i]);
199             }
200         }
201         return new StructuredSelection(selected);
202     }
203     
204     /**
205      * Allows access to the viewer
206      * @return the viewer
207      */

208     public BreakpointsViewer getViewer() {
209         return fViewer;
210     }
211    
212     /**
213      * finds all occurrences of a widget to update
214      * @param element the element to search for when finding occurrences
215      * @return a list of widget occurrences to update or an empty list
216      */

217     private Widget[] searchItems(Object JavaDoc element) {
218         ArrayList JavaDoc list = new ArrayList JavaDoc();
219         TreeItem[] items = fTree.getItems();
220         for (int i = 0; i < items.length; i++) {
221             findAllOccurrences(items[i], element, list);
222         }
223         return (Widget[]) list.toArray(new Widget[0]);
224     }
225     
226     /**
227      * performs the actual search for items in the tree
228      * @param list the list to add matches to
229      * @param item the item in the tree
230      * @param element the element to compare
231      */

232     private void findAllOccurrences(TreeItem item, Object JavaDoc element, ArrayList JavaDoc list) {
233         if (element.equals(item.getData())) {
234                 list.add(item);
235         }
236         TreeItem[] items = item.getItems();
237         for (int i = 0; i < items.length; i++) {
238             findAllOccurrences(items[i], element, list);
239         }
240     }
241     
242      /**
243      * Update the checked state of the given element and all of its children.
244      *
245      * @param obj the object that has been changed
246      * @param enable the checked status of the obj
247      */

248     private void updateCheckedState(Object JavaDoc obj, boolean enable) {
249             if (obj instanceof IBreakpoint) {
250                 Widget[] list = searchItems(obj);
251                 TreeItem item = null;
252                 for(int i = 0; i < list.length; i++) {
253                     item = (TreeItem)list[i];
254                     item.setChecked(enable);
255                     refreshParents(item);
256                 }
257             }
258             else if (obj instanceof BreakpointContainer) {
259                 ArrayList JavaDoc bps = new ArrayList JavaDoc();
260                 getBreakpointsFromContainers((BreakpointContainer)obj, bps);
261                 for(int j = 0; j < bps.size(); j++) {
262                     updateCheckedState(bps.get(j), enable);
263                 }
264             }
265      }
266
267     /**
268      * refreshes the grayed/checked state of the parents of item
269      * @param item the item to refresh parents of
270      */

271     private void refreshParents(TreeItem item) {
272         TreeItem parent = item.getParentItem();
273         while (parent != null) {
274             int checked = getNumberChildrenChecked(parent);
275             if(checked == 0) {
276                 parent.setGrayed(false);
277                 parent.setChecked(false);
278             }
279             else if(checked == parent.getItemCount()) {
280                 if(getNumberChildrenGrayed(parent) > 0) {
281                     parent.setGrayed(true);
282                 }
283                 else {
284                     parent.setGrayed(false);
285                 }
286                 parent.setChecked(true);
287             }
288             else {
289                 parent.setGrayed(true);
290                 parent.setChecked(true);
291             }
292             parent = parent.getParentItem();
293         }
294     }
295     
296     /**
297      * Gets the number of grayed children for this parent
298      * @param parent the parent to inspect
299      * @return treu is any one or more children is grayed, false otherwise
300      */

301     private int getNumberChildrenGrayed(TreeItem parent) {
302         TreeItem[] children = parent.getItems();
303         int count = 0;
304         for(int i = 0; i < children.length; i++) {
305             if(children[i].getGrayed()) {
306                 count++;
307             }
308         }
309         return count;
310     }
311     
312     /**
313      * Checks to see if all of the children under an given parent are checked or not
314      * @param children the children to check
315      * @return true if all children are checked, false otherwise
316      */

317     private int getNumberChildrenChecked(TreeItem parent) {
318         TreeItem[] children = parent.getItems();
319         int count = 0;
320         for(int i = 0; i < children.length; i++) {
321             if(children[i].getChecked()) {
322                 count++;
323             }
324         }
325         return count;
326     }
327 }
328
Popular Tags