KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > refactoring > BreakpointMoveParticipant


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.core.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.model.IBreakpoint;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.debug.core.IJavaBreakpoint;
26 import org.eclipse.jdt.debug.core.IJavaClassPrepareBreakpoint;
27 import org.eclipse.jdt.debug.core.IJavaExceptionBreakpoint;
28 import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
29 import org.eclipse.jdt.debug.core.IJavaMethodBreakpoint;
30 import org.eclipse.jdt.debug.core.IJavaWatchpoint;
31 import org.eclipse.ltk.core.refactoring.Change;
32 import org.eclipse.ltk.core.refactoring.CompositeChange;
33 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
34 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
35 import org.eclipse.ltk.core.refactoring.participants.MoveParticipant;
36
37 /**
38  * Breakpoint participant for a move refactoring.
39  *
40  * @since 3.2
41  */

42 public abstract class BreakpointMoveParticipant extends MoveParticipant {
43     
44     /**
45      * Element being moved
46      */

47     private IJavaElement fElement;
48     
49     /**
50      * Destination container
51      */

52     private IJavaElement fDestination;
53     
54     /* (non-Javadoc)
55      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#initialize(java.lang.Object)
56      */

57     protected boolean initialize(Object JavaDoc element) {
58         if (element instanceof IJavaElement && accepts((IJavaElement)element)) {
59             fElement = (IJavaElement) element;
60             fDestination = (IJavaElement) getArguments().getDestination();
61         } else {
62             return false;
63         }
64         return true;
65     }
66     
67     /**
68      * Returns the element this refactoring is operating on.
69      *
70      * @return
71      */

72     protected IJavaElement getOriginalElement() {
73         return fElement;
74     }
75     
76     /**
77      * Returns the destination of the move operation.
78      *
79      * @return
80      */

81     protected IJavaElement getDestination() {
82         return fDestination;
83     }
84     
85     /**
86      * Returns whether this given element is a valid target for this operation.
87      *
88      * @param element
89      * @return
90      */

91     protected abstract boolean accepts(IJavaElement element);
92
93     /* (non-Javadoc)
94      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#getName()
95      */

96     public String JavaDoc getName() {
97         return RefactoringMessages.BreakpointRenameParticipant_0;
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#checkConditions(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
102      */

103     public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) throws OperationCanceledException {
104         return new RefactoringStatus();
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#createChange(org.eclipse.core.runtime.IProgressMonitor)
109      */

110     public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
111         List JavaDoc changes = new ArrayList JavaDoc();
112         IResource resource = getBreakpointContainer();
113         IMarker[] markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
114         gatherChanges(markers, changes);
115         if (changes.size() > 1) {
116             return new CompositeChange(RefactoringMessages.BreakpointRenameParticipant_1, (Change[]) changes.toArray(new Change[changes.size()]));
117         } else if (changes.size() == 1) {
118             return (Change) changes.get(0);
119         }
120         return null;
121     }
122     
123     /**
124      * Gathers refactoring specific changes. Subclasses must override.
125      *
126      * @param breakpoint markers to consider during the change
127      * @param list to add changes to
128      * @return changes for this refactoring.
129      * @throws CoreException
130      * @throws OperationCanceledException
131      */

132     protected abstract void gatherChanges(IMarker[] markers, List JavaDoc changes) throws CoreException, OperationCanceledException;
133     
134     /**
135      * Returns the resource that should be considered when searching for affected breakpoints.
136      *
137      * @return resource to search for breakpoint markers.
138      */

139     protected IResource getBreakpointContainer() {
140         return fElement.getResource();
141     }
142     
143     /**
144      * Returns the breakpoint associated with the given marker.
145      *
146      * @param marker breakpoint marker
147      * @return breakpoint or <code>null</code>
148      */

149     protected IBreakpoint getBreakpoint(IMarker marker) {
150         return DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(marker);
151     }
152     
153     /**
154      * Creates a specific type of change for a breakpoint that is changing types.
155      *
156      * @return type change or <code>null</code>
157      */

158     protected Change createTypeChange(IJavaBreakpoint breakpoint, IType destType, IType originalType) throws CoreException {
159         if (breakpoint instanceof IJavaWatchpoint) {
160             return new WatchpointTypeChange((IJavaWatchpoint) breakpoint, destType, originalType);
161         } else if (breakpoint instanceof IJavaClassPrepareBreakpoint) {
162             return new ClassPrepareBreakpointTypeChange((IJavaClassPrepareBreakpoint) breakpoint, destType);
163         } else if (breakpoint instanceof IJavaMethodBreakpoint) {
164             return new MethodBreakpointTypeChange((IJavaMethodBreakpoint) breakpoint, destType);
165         } else if (breakpoint instanceof IJavaExceptionBreakpoint) {
166             return new ExceptionBreakpointTypeChange((IJavaExceptionBreakpoint) breakpoint, destType);
167         } else if (breakpoint instanceof IJavaLineBreakpoint) {
168             return new LineBreakpointTypeChange((IJavaLineBreakpoint) breakpoint, destType);
169         }
170         return null;
171     }
172     
173     /**
174      * Returns whether the given target type is contained in the specified container type.
175      *
176      * @param container
177      * @param target
178      * @return
179      */

180     protected boolean isContained(IJavaElement container, IType type) {
181         IJavaElement parent = type;
182         while (parent != null) {
183             if (parent.equals(container)) {
184                 return true;
185             }
186             parent = parent.getParent();
187         }
188         return false;
189     }
190 }
191
Popular Tags