KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > BreakpointToggleAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.debug.ui.actions;
12
13  
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IMarkerDelta;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.DebugPlugin;
20 import org.eclipse.debug.core.IBreakpointManager;
21 import org.eclipse.debug.core.IBreakpointsListener;
22 import org.eclipse.debug.core.model.IBreakpoint;
23 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
24 import org.eclipse.jdt.internal.debug.ui.ExceptionHandler;
25 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.IObjectActionDelegate;
30 import org.eclipse.ui.IPartListener;
31 import org.eclipse.ui.IWorkbenchPart;
32
33 /**
34  * Provides a general toggle action for breakpoints to reuse
35  */

36 public abstract class BreakpointToggleAction implements IObjectActionDelegate, IBreakpointsListener, IPartListener {
37     
38     private IStructuredSelection fSelection;
39     private IAction fAction;
40     private IWorkbenchPart fPart;
41
42     /**
43      * @see IActionDelegate#run(IAction)
44      */

45     public void run(IAction action) {
46         IStructuredSelection selection= getStructuredSelection();
47         Iterator JavaDoc itr= selection.iterator();
48         while (itr.hasNext()) {
49             try {
50                 IJavaBreakpoint breakpoint= (IJavaBreakpoint) itr.next();
51                 doAction(breakpoint);
52             } catch (CoreException e) {
53                 String JavaDoc title= ActionMessages.BreakpointAction_Breakpoint_configuration_1;
54                 String JavaDoc message= ActionMessages.BreakpointAction_Exceptions_occurred_attempting_to_modify_breakpoint__2;
55                 ExceptionHandler.handle(e, title, message);
56             }
57         }
58     }
59
60     /**
61      * @see IActionDelegate#selectionChanged(IAction, ISelection)
62      */

63     public void selectionChanged(IAction action, ISelection selection) {
64         setAction(action);
65         if (selection.isEmpty()) {
66             setStructuredSelection(null);
67             return;
68         }
69         if (selection instanceof IStructuredSelection) {
70             setStructuredSelection((IStructuredSelection)selection);
71             boolean enabled = isEnabledFor(getStructuredSelection());
72             action.setEnabled(enabled);
73             if (enabled && isToggleAction()) {
74                 IBreakpoint breakpoint = (IBreakpoint)getStructuredSelection().getFirstElement();
75                 if (breakpoint instanceof IJavaBreakpoint) {
76                     try {
77                         action.setChecked(getToggleState((IJavaBreakpoint) breakpoint));
78                     } catch (CoreException e) {
79                         JDIDebugUIPlugin.log(e);
80                     }
81                 }
82             }
83         }
84     }
85
86     /**
87      * Returns if the action is a checkable action. i.e. if we should bother updating checked state
88      * @return if the action is a checkable action
89      *
90      * @since 3.3
91      */

92     protected boolean isToggleAction() {
93         return true;
94     }
95     
96     /**
97      * Toggle the state of this action
98      */

99     public abstract void doAction(IJavaBreakpoint breakpoint) throws CoreException;
100     
101     /**
102      * Returns whether this action is currently toggled on
103      */

104     protected abstract boolean getToggleState(IJavaBreakpoint breakpoint) throws CoreException;
105     
106     /**
107      * Get the current selection
108      */

109     protected IStructuredSelection getStructuredSelection() {
110         return fSelection;
111     }
112     
113     /**
114      * Allows the current structured selection to be set
115      * @param selection the new selection
116      */

117     protected void setStructuredSelection(IStructuredSelection selection) {
118         fSelection= selection;
119     }
120     
121     /**
122      * Returns if the underlying action should be enabled for the given selection
123      * @param selection
124      * @return if the underlying action should be enabled for the given selection
125      */

126     public abstract boolean isEnabledFor(IStructuredSelection selection);
127
128     /**
129      * Get the breakpoint manager for the debug plugin
130      */

131     protected IBreakpointManager getBreakpointManager() {
132         return DebugPlugin.getDefault().getBreakpointManager();
133     }
134     
135     /**
136      * Get the breakpoint associated with the given marker
137      */

138     protected IBreakpoint getBreakpoint(IMarker marker) {
139         return getBreakpointManager().getBreakpoint(marker);
140     }
141
142     /**
143      * Returns the underlying <code>IAction</code> for this delegate
144      * @return the underlying <code>IAction</code> for this delegate
145      */

146     protected IAction getAction() {
147         return fAction;
148     }
149
150     /**
151      * Allows the underlying <code>IAction</code> for this delegate to be set
152      * @param action the new action to set for this delegate
153      */

154     protected void setAction(IAction action) {
155         fAction = action;
156     }
157     
158     /**
159      * @see IBreakpointsListener#breakpointsAdded(IBreakpoint[])
160      */

161     public void breakpointsAdded(IBreakpoint[] breakpoints) {
162     }
163
164     /**
165      * @see IBreakpointsListener#breakpointsChanged(IBreakpoint[], IMarkerDelta[])
166      */

167     public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
168         if (getAction() != null) {
169             IStructuredSelection selection= getStructuredSelection();
170             if (selection != null) {
171                 IBreakpoint selectedBreakpoint= (IBreakpoint)selection.getFirstElement();
172                 for (int i = 0; i < breakpoints.length; i++) {
173                     IBreakpoint breakpoint = breakpoints[i];
174                     if (selectedBreakpoint.equals(breakpoint)) {
175                         selectionChanged(getAction(), selection);
176                         return;
177                     }
178                 }
179             }
180         }
181     }
182
183     /**
184      * @see IBreakpointsListener#breakpointsRemoved(IBreakpoint[], IMarkerDelta[])
185      */

186     public void breakpointsRemoved(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
187     }
188     
189     /**
190      * Returns the <code>IWorkbenchPart</code> this delegate is associated with
191      * @return the <code>IWorkbenchPart</code> this delegate is associated with
192      */

193     protected IWorkbenchPart getPart() {
194         return fPart;
195     }
196
197     /**
198      * Allows the <code>IWorkbenchPart</code> to be set for this delegate
199      * @param part the new part to set
200      */

201     protected void setPart(IWorkbenchPart part) {
202         fPart = part;
203     }
204     
205     /**
206      * @see IPartListener#partActivated(IWorkbenchPart)
207      */

208     public void partActivated(IWorkbenchPart part) {
209     }
210
211     /**
212      * @see IPartListener#partBroughtToTop(IWorkbenchPart)
213      */

214     public void partBroughtToTop(IWorkbenchPart part) {
215     }
216
217     /**
218      * @see IPartListener#partClosed(IWorkbenchPart)
219      */

220     public void partClosed(IWorkbenchPart part) {
221         if (part == getPart()) {
222             getBreakpointManager().removeBreakpointListener(this);
223             part.getSite().getPage().removePartListener(this);
224         }
225     }
226
227     /**
228      * @see IPartListener#partDeactivated(IWorkbenchPart)
229      */

230     public void partDeactivated(IWorkbenchPart part) {
231     }
232
233     /**
234      * @see IPartListener#partOpened(IWorkbenchPart)
235      */

236     public void partOpened(IWorkbenchPart part) {
237     }
238     
239     /**
240      * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
241      */

242     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
243         IWorkbenchPart oldPart= getPart();
244         if (oldPart != null) {
245             getPart().getSite().getPage().removePartListener(this);
246         }
247         
248         getBreakpointManager().addBreakpointListener(this);
249         setPart(targetPart);
250         targetPart.getSite().getPage().addPartListener(this);
251     }
252 }
253
254
Popular Tags