KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > ShowSupportedBreakpointsAction


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.actions;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.debug.core.ILaunch;
18 import org.eclipse.debug.core.model.IBreakpoint;
19 import org.eclipse.debug.core.model.IDebugElement;
20 import org.eclipse.debug.core.model.IDebugTarget;
21 import org.eclipse.debug.core.model.IProcess;
22 import org.eclipse.debug.internal.ui.DebugPluginImages;
23 import org.eclipse.debug.internal.ui.DebugUIPlugin;
24 import org.eclipse.debug.internal.ui.IDebugHelpContextIds;
25 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointContainer;
26 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView;
27 import org.eclipse.debug.ui.IDebugUIConstants;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.jface.viewers.ViewerFilter;
33 import org.eclipse.ui.ISelectionListener;
34 import org.eclipse.ui.IViewPart;
35 import org.eclipse.ui.IWorkbenchPart;
36 import org.eclipse.ui.PlatformUI;
37
38 /**
39  * An view filter action that filters showing breakpoints based on whether
40  * the IDebugTarget of the selected debug element in the launch view supports
41  * the breakpoints.
42  *
43  * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(IBreakpoint)
44  *
45  */

46 public class ShowSupportedBreakpointsAction extends ToggleFilterAction implements ISelectionListener {
47
48     /**
49      * The view associated with this action
50      */

51     private BreakpointsView fView;
52     
53     /**
54      * The list of identifiers for the current state
55      */

56     private List JavaDoc fDebugTargets= new ArrayList JavaDoc(2);
57     
58     /**
59      * A viewer filter that selects breakpoints that have
60      * the same model identifier as the selected debug element
61      */

62     class BreakpointFilter extends ViewerFilter {
63         
64         /**
65          * @see ViewerFilter#select(Viewer, Object, Object)
66          */

67         public boolean select(Viewer viewer, Object JavaDoc parentElement, Object JavaDoc element) {
68             if (element instanceof BreakpointContainer) {
69                 // Breakpoint containers are visible if any of their children are visible.
70
IBreakpoint[] breakpoints = ((BreakpointContainer) element).getBreakpoints();
71                 for (int i = 0; i < breakpoints.length; i++) {
72                     if (select(viewer, element, breakpoints[i])) {
73                         return true;
74                     }
75                 }
76                 return false;
77             }
78             IBreakpoint breakpoint= (IBreakpoint)element;
79             if (fDebugTargets.isEmpty()) {
80                 return true;
81             }
82             Iterator JavaDoc iterator= fDebugTargets.iterator();
83             while (iterator.hasNext()) {
84                 IDebugTarget target = (IDebugTarget) iterator.next();
85                 if (target.supportsBreakpoint(breakpoint)) {
86                     return true;
87                 }
88                 
89             }
90             return false;
91         }
92
93     }
94
95     public ShowSupportedBreakpointsAction(StructuredViewer viewer, IViewPart view) {
96         super();
97         setText(ActionMessages.ShowSupportedBreakpointsAction_Show_For_Selected); //$NON-NLS-1$
98
setToolTipText(ActionMessages.ShowSupportedBreakpointsAction_tooltip); //$NON-NLS-1$
99
setViewerFilter(new BreakpointFilter());
100         setViewer(viewer);
101         setImageDescriptor(DebugPluginImages.getImageDescriptor(IDebugUIConstants.IMG_OBJS_DEBUG_TARGET));
102         setChecked(false);
103         setId(DebugUIPlugin.getUniqueIdentifier() + ".ShowSupportedBreakpointsAction"); //$NON-NLS-1$
104

105         setView(view);
106         PlatformUI.getWorkbench().getHelpSystem().setHelp(
107             this,
108             IDebugHelpContextIds.SHOW_BREAKPOINTS_FOR_MODEL_ACTION);
109         
110     }
111
112     
113         
114     public void dispose() {
115         if (isChecked()) {
116             getView().getSite().getPage().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
117         }
118     }
119     
120     /**
121      * @see ISelectionListener#selectionChanged(IWorkbenchPart, ISelection)
122      */

123     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
124         if (selection instanceof IStructuredSelection) {
125             IStructuredSelection ss= (IStructuredSelection)selection;
126             List JavaDoc debugTargets= getDebugTargets(ss);
127             if (!isChecked()) {
128                 fDebugTargets= debugTargets;
129                 return;
130             }
131             if (debugTargets.isEmpty()) {
132                  if(fDebugTargets.isEmpty()) {
133                     return;
134                  }
135                  reapplyFilters(debugTargets);
136                  return;
137             }
138             if (fDebugTargets.isEmpty()) {
139                 reapplyFilters(debugTargets);
140                 return;
141             }
142             
143             if (debugTargets.size() == fDebugTargets.size()) {
144                 List JavaDoc copy= new ArrayList JavaDoc(debugTargets.size());
145                 Iterator JavaDoc iter= fDebugTargets.iterator();
146                 while (iter.hasNext()) {
147                     IDebugTarget target = (IDebugTarget) iter.next();
148                     Iterator JavaDoc newDebugTargets= debugTargets.iterator();
149                     while (newDebugTargets.hasNext()) {
150                         IDebugTarget newTarget= (IDebugTarget)newDebugTargets.next();
151                         copy.add(newTarget);
152                         if (target.equals(newTarget)) {
153                             newDebugTargets.remove();
154                         }
155                     }
156                 }
157                 //check for real change
158
if (debugTargets.isEmpty()) {
159                     return;
160                 }
161                 reapplyFilters(copy);
162             }
163         }
164     }
165
166     
167     /**
168      * Selection has changed in the debug view
169      * need to reapply the filters.
170      */

171     protected void reapplyFilters(List JavaDoc debugTargets) {
172         fDebugTargets= debugTargets;
173         getViewer().refresh();
174         fView.initializeCheckedState();
175     }
176     
177     protected IViewPart getView() {
178         return fView;
179     }
180
181     protected void setView(IViewPart view) {
182         fView = (BreakpointsView) view;
183     }
184     
185     protected List JavaDoc getDebugTargets(IStructuredSelection ss) {
186         List JavaDoc debugTargets= new ArrayList JavaDoc(2);
187         Iterator JavaDoc i= ss.iterator();
188         while (i.hasNext()) {
189             Object JavaDoc next= i.next();
190             if (next instanceof IDebugElement) {
191                 debugTargets.add(((IDebugElement)next).getDebugTarget());
192             } else if (next instanceof ILaunch) {
193                 IDebugTarget[] targets= ((ILaunch)next).getDebugTargets();
194                 for (int j = 0; j < targets.length; j++) {
195                     debugTargets.add(targets[j]);
196                 }
197             } else if (next instanceof IProcess) {
198                 IDebugTarget target= (IDebugTarget)((IProcess)next).getAdapter(IDebugTarget.class);
199                 if (target != null) {
200                     debugTargets.add(target);
201                 }
202             }
203         }
204         return debugTargets;
205     }
206     
207     /**
208      * Adds or removes the viewer filter depending
209      * on the value of the parameter.
210      */

211     protected void valueChanged(boolean on) {
212         if (getViewer().getControl().isDisposed()) {
213             return;
214         }
215         if (on) {
216             getView().getSite().getPage().addSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
217             ISelection selection= getView().getSite().getPage().getSelection(IDebugUIConstants.ID_DEBUG_VIEW);
218             selectionChanged(null, selection);
219         } else {
220             getView().getSite().getPage().removeSelectionListener(IDebugUIConstants.ID_DEBUG_VIEW, this);
221         }
222         super.valueChanged(on);
223         fView.initializeCheckedState();
224 // if (!on) {
225
// ((BreakpointsView)getView()).initializeCheckedState((CheckboxTreeViewer) getViewer());
226
// }
227
}
228     
229 }
230
Popular Tags