KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > model > Breakpoint


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.core.model;
12
13  
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceRuleFactory;
19 import org.eclipse.core.resources.IWorkspace;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.PlatformObject;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.core.runtime.jobs.ISchedulingRule;
28 import org.eclipse.debug.core.DebugException;
29 import org.eclipse.debug.core.DebugPlugin;
30 import org.eclipse.debug.core.IBreakpointManager;
31 import org.eclipse.debug.internal.core.DebugCoreMessages;
32
33 /**
34  * Abstract implementation of a breakpoint. This class is
35  * intended to be subclassed by implementations
36  * of breakpoints.
37  *
38  * @see IBreakpoint
39  * @since 2.0
40  */

41
42 public abstract class Breakpoint extends PlatformObject implements IBreakpoint {
43     
44     static {
45         // making sure that the BreakpointManager is correctly initialized
46
// before any breakpoint marker related operation (see bug 54993)
47
DebugPlugin.getDefault().getBreakpointManager().getBreakpoints();
48     }
49                 
50     /**
51      * Underlying marker.
52      */

53     private IMarker fMarker= null;
54     
55     /**
56      * @see IBreakpoint#setMarker(IMarker)
57      */

58     public void setMarker(IMarker marker) throws CoreException {
59         fMarker= marker;
60     }
61     
62     /**
63      * @see Object#equals(Object)
64      */

65     public boolean equals(Object JavaDoc item) {
66         if (item instanceof IBreakpoint) {
67             return getMarker().equals(((IBreakpoint)item).getMarker());
68         }
69         return false;
70     }
71     
72     /**
73      * @see Object#hashCode()
74      */

75     public int hashCode() {
76         return getMarker().hashCode();
77     }
78         
79     /**
80      * @see IBreakpoint#setEnabled(boolean)
81      */

82     public void setEnabled(boolean enabled) throws CoreException {
83         if (enabled != isEnabled()) {
84             setAttribute(ENABLED, enabled);
85         }
86     }
87     
88     /**
89      * @see IBreakpoint#isEnabled()
90      */

91     public boolean isEnabled() throws CoreException {
92         return getMarker().getAttribute(ENABLED, false);
93     }
94     
95     /**
96      * @see IBreakpoint#isRegistered()
97      */

98     public boolean isRegistered() throws CoreException {
99             IMarker marker= getMarker();
100             return marker.exists() && marker.getAttribute(REGISTERED, true);
101     }
102     
103     /**
104      * @see IBreakpoint#setRegistered(boolean)
105      */

106     public void setRegistered(boolean registered) throws CoreException {
107         if (isRegistered() != registered) {
108             setAttribute(REGISTERED, registered);
109             IBreakpointManager mgr = DebugPlugin.getDefault().getBreakpointManager();
110             if (registered) {
111                 mgr.addBreakpoint(this);
112             } else {
113                 mgr.removeBreakpoint(this, false);
114             }
115         }
116     }
117
118     /**
119      * @see IBreakpoint#delete()
120      */

121     public void delete() throws CoreException {
122         DebugPlugin.getDefault().getBreakpointManager().removeBreakpoint(this, false);
123         getMarker().delete();
124     }
125
126     /**
127      * @see IBreakpoint#getMarker()
128      */

129     public IMarker getMarker() {
130         return fMarker;
131     }
132
133     /**
134      * @see IBreakpoint#isPersisted()
135      */

136     public boolean isPersisted() throws CoreException {
137         return getMarker().getAttribute(PERSISTED, true);
138     }
139
140     /**
141      * @see IBreakpoint#setPersisted(boolean)
142      */

143     public void setPersisted(boolean persisted) throws CoreException {
144         if (isPersisted() != persisted) {
145             setAttributes(new String JavaDoc[] {PERSISTED, IMarker.TRANSIENT}, new Object JavaDoc[] {Boolean.valueOf(persisted), Boolean.valueOf(!persisted)});
146         }
147     }
148     
149     /**
150      * Convenience method to set the given boolean attribute of
151      * this breakpoint's underlying marker in a workspace
152      * runnable. Setting marker attributes in a workspace runnable
153      * prevents deadlock.
154      *
155      * @param attributeName attribute name
156      * @param value attribute value
157      * @exception CoreException is setting the attribute fails
158      * @see IMarker#setAttribute(java.lang.String, boolean)
159      */

160     protected void setAttribute(final String JavaDoc attributeName, final boolean value) throws CoreException {
161         IWorkspace workspace= ResourcesPlugin.getWorkspace();
162         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
163                 public void run(IProgressMonitor monitor) throws CoreException {
164                     ensureMarker().setAttribute(attributeName, value);
165                 }
166             };
167             
168         workspace.run(runnable, getMarkerRule(), 0, null);
169     }
170     
171     /**
172      * Convenience method to set the given integer attribute of
173      * this breakpoint's underlying marker in a workspace
174      * runnable. Setting marker attributes in a workspace runnable
175      * prevents deadlock.
176      *
177      * @param attributeName attribute name
178      * @param value attribute value
179      * @exception CoreException is setting the attribute fails
180      * @see IMarker#setAttribute(java.lang.String, int)
181      */

182     protected void setAttribute(final String JavaDoc attributeName, final int value) throws CoreException {
183         IWorkspace workspace= ResourcesPlugin.getWorkspace();
184         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
185                 public void run(IProgressMonitor monitor) throws CoreException {
186                     ensureMarker().setAttribute(attributeName, value);
187                 }
188             };
189             
190         workspace.run(runnable, getMarkerRule(), 0, null);
191     }
192
193     /**
194      * Convenience method to set the given attribute of
195      * this breakpoint's underlying marker in a workspace
196      * runnable. Setting marker attributes in a workspace runnable
197      * prevents deadlock.
198      *
199      * @param attributeName attribute name
200      * @param value attribute value
201      * @exception CoreException is setting the attribute fails
202      * @see IMarker#setAttribute(java.lang.String, java.lang.Object)
203      */

204     protected void setAttribute(final String JavaDoc attributeName, final Object JavaDoc value) throws CoreException {
205         IWorkspace workspace= ResourcesPlugin.getWorkspace();
206         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
207                 public void run(IProgressMonitor monitor) throws CoreException {
208                     ensureMarker().setAttribute(attributeName, value);
209                 }
210             };
211             
212         workspace.run(runnable, getMarkerRule(), 0, null);
213     }
214
215     /**
216      * Convenience method to set the given attributes of
217      * this breakpoint's underlying marker in a workspace
218      * runnable. Setting marker attributes in a workspace runnable
219      * prevents deadlock.
220      *
221      * @param attributeNames attribute names
222      * @param values attribute values
223      * @exception CoreException is setting the attributes fails
224      * @see IMarker#setAttributes(java.lang.String[], java.lang.Object[])
225      */

226     protected void setAttributes(final String JavaDoc[] attributeNames, final Object JavaDoc[] values) throws CoreException {
227         IWorkspace workspace= ResourcesPlugin.getWorkspace();
228         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
229                 public void run(IProgressMonitor monitor) throws CoreException {
230                     ensureMarker().setAttributes(attributeNames, values);
231                 }
232             };
233             
234         workspace.run(runnable, getMarkerRule(), IWorkspace.AVOID_UPDATE, null);
235     }
236
237     /**
238      * Convenience method to set the attributes of
239      * this breakpoint's underlying marker in a workspace
240      * runnable. Setting marker attributes in a workspace runnable
241      * prevents deadlock.
242      *
243      * @param attributes attribute map
244      * @exception CoreException is setting the attributes fails
245      * @see IMarker#setAttributes(java.util.Map)
246      */

247     protected void setAttributes(final Map JavaDoc attributes) throws CoreException{
248         IWorkspace workspace= ResourcesPlugin.getWorkspace();
249         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
250                 public void run(IProgressMonitor monitor) throws CoreException {
251                     ensureMarker().setAttributes(attributes);
252                 }
253             };
254             
255         workspace.run(runnable, getMarkerRule(), IWorkspace.AVOID_UPDATE, null);
256     }
257
258     /**
259      * Returns the marker associated with this breakpoint.
260      *
261      * @return breakpoint marker
262      * @exception DebugException if no marker is associated with
263      * this breakpoint or the associated marker does not exist
264      */

265     protected IMarker ensureMarker() throws DebugException {
266         IMarker m = getMarker();
267         if (m == null || !m.exists()) {
268             throw new DebugException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugException.REQUEST_FAILED,
269                 DebugCoreMessages.Breakpoint_no_associated_marker, null));
270         }
271         return m;
272     }
273     
274     /**
275      * Returns whether this breakpoint has an associated marker that exists.
276      *
277      * @return returns whether this breakpoint has an associated marker that exists
278      * @since 2.1
279      */

280     protected boolean markerExists() {
281         IMarker m = getMarker();
282         return (m != null && m.exists());
283     }
284
285     /**
286      * Returns a scheduling rule to use when modifying markers on the given resource,
287      * possibly <code>null</code>.
288      *
289      * @param resource a resource on which a marker will be created, modified, or deleted
290      * @return a scheduling rule to use when modifying markers on the given resource
291      * possibly <code>null</code>
292      * @since 3.1
293      */

294     protected ISchedulingRule getMarkerRule(IResource resource) {
295         ISchedulingRule rule = null;
296         if (resource != null) {
297             IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
298             rule = ruleFactory.markerRule(resource);
299         }
300         return rule;
301     }
302     
303     /**
304      * Returns a scheduling rule to use when modifying or deleting this breakpoint's marker,
305      * possibly <code>null</code>. This method is only valid when this breakpoint's
306      * marker has already been created. When creating a marker on a specific resource,
307      * use <code>getMarkerRule(IResource)</code> instead.
308      *
309      * @return a scheduling rule to use when modifying or deleting this breakpoint's marker
310      * @since 3.1
311      */

312     protected ISchedulingRule getMarkerRule() {
313         ISchedulingRule rule = null;
314         IMarker marker = getMarker();
315         if (marker != null) {
316             IResource resource = marker.getResource();
317             if (resource != null) {
318                 IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
319                 rule = ruleFactory.markerRule(resource);
320             }
321         }
322         return rule;
323     }
324     
325     /**
326      * Execute the given workspace runnable with the scheduling rule to use when running the operation.
327      *
328      * @param rule the rule to use when running the operation
329      * @param wr the runnable operation
330      * @throws DebugException If a core exception occurs performing the operation
331      * @since 3.1
332      */

333     protected void run(ISchedulingRule rule, IWorkspaceRunnable wr) throws DebugException {
334         try {
335             ResourcesPlugin.getWorkspace().run(wr, rule, 0, null);
336         } catch (CoreException e) {
337             throw new DebugException(e.getStatus());
338         }
339     }
340     
341 }
342
Popular Tags