KickJava   Java API By Example, From Geeks To Geeks.

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

49 public class ShowSupportedBreakpointsAction extends ToggleFilterAction implements ISelectionListener {
50
51     /**
52      * The view associated with this action
53      */

54     private BreakpointsView fView;
55     
56     /**
57      * The list of identifiers for the current state
58      */

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

65     class BreakpointFilter extends ViewerFilter {
66         
67         /**
68          * @see ViewerFilter#select(Viewer, Object, Object)
69          */

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

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

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

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

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