KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenameParticipant;
36
37 /**
38  * Breakpoint participant for a rename refactoring.
39  *
40  * @since 3.2
41  */

42 public abstract class BreakpointRenameParticipant extends RenameParticipant {
43     
44     /**
45      * Element being renamed
46      */

47     private IJavaElement fElement;
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#initialize(java.lang.Object)
51      */

52     protected boolean initialize(Object JavaDoc element) {
53         if (element instanceof IJavaElement && accepts((IJavaElement)element)) {
54             fElement = (IJavaElement) element;
55         } else {
56             return false;
57         }
58         return true;
59     }
60     
61     /**
62      * Returns the element this refactoring is operating on.
63      *
64      * @return
65      */

66     protected IJavaElement getOriginalElement() {
67         return fElement;
68     }
69     
70     /**
71      * Returns whether this given element is a valid target for this operation.
72      *
73      * @param element
74      * @return
75      */

76     protected abstract boolean accepts(IJavaElement element);
77
78     /* (non-Javadoc)
79      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#getName()
80      */

81     public String JavaDoc getName() {
82         return RefactoringMessages.BreakpointRenameParticipant_0;
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#checkConditions(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext)
87      */

88     public RefactoringStatus checkConditions(IProgressMonitor pm, CheckConditionsContext context) throws OperationCanceledException {
89         return new RefactoringStatus();
90     }
91
92     /* (non-Javadoc)
93      * @see org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant#createChange(org.eclipse.core.runtime.IProgressMonitor)
94      */

95     public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
96         List JavaDoc changes = new ArrayList JavaDoc();
97         IResource resource = getBreakpointContainer();
98         IMarker[] markers= resource.findMarkers(IBreakpoint.BREAKPOINT_MARKER, true, IResource.DEPTH_INFINITE);
99         gatherChanges(markers, changes, getArguments().getNewName());
100         if (changes.size() > 1) {
101             return new CompositeChange(RefactoringMessages.BreakpointRenameParticipant_1, (Change[]) changes.toArray(new Change[changes.size()]));
102         } else if (changes.size() == 1) {
103             return (Change) changes.get(0);
104         }
105         return null;
106     }
107     
108     /**
109      * Gathers refactoring specific changes. Subclasses must override.
110      *
111      * @param breakpoint markers to consider during the change
112      * @param list to add changes to
113      * @param name of the element being renamed
114      * @return changes for this refactoring.
115      * @throws CoreException
116      * @throws OperationCanceledException
117      */

118     protected abstract void gatherChanges(IMarker[] markers, List JavaDoc changes, String JavaDoc destName) throws CoreException, OperationCanceledException;
119     
120     /**
121      * Returns the resource that should be considered when searching for affected breakpoints.
122      *
123      * @return resource to search for breakpoint markers.
124      */

125     protected IResource getBreakpointContainer() {
126         return fElement.getResource();
127     }
128     
129     /**
130      * Returns the breakpoint associated with the given marker.
131      *
132      * @param marker breakpoint marker
133      * @return breakpoint or <code>null</code>
134      */

135     protected IBreakpoint getBreakpoint(IMarker marker) {
136         return DebugPlugin.getDefault().getBreakpointManager().getBreakpoint(marker);
137     }
138     
139     /**
140      * Creates a specific type of change for a breakpoint that is changing types.
141      *
142      * @return type change or <code>null</code>
143      */

144     protected Change createTypeChange(IJavaBreakpoint breakpoint, IType destType, IType originalType) throws CoreException {
145         if (breakpoint instanceof IJavaWatchpoint) {
146             return new WatchpointTypeChange((IJavaWatchpoint) breakpoint, destType, originalType);
147         } else if (breakpoint instanceof IJavaClassPrepareBreakpoint) {
148             return new ClassPrepareBreakpointTypeChange((IJavaClassPrepareBreakpoint) breakpoint, destType);
149         } else if (breakpoint instanceof IJavaMethodBreakpoint) {
150             return new MethodBreakpointTypeChange((IJavaMethodBreakpoint) breakpoint, destType);
151         } else if (breakpoint instanceof IJavaExceptionBreakpoint) {
152             return new ExceptionBreakpointTypeChange((IJavaExceptionBreakpoint) breakpoint, destType);
153         } else if (breakpoint instanceof IJavaLineBreakpoint) {
154             return new LineBreakpointTypeChange((IJavaLineBreakpoint) breakpoint, destType);
155         }
156         return null;
157     }
158     
159     /**
160      * Returns whether the given target type is contained in the specified container type.
161      *
162      * @param container
163      * @param target
164      * @return
165      */

166     protected boolean isContained(IJavaElement container, IType type) {
167         IJavaElement parent = type;
168         while (parent != null) {
169             if (parent.equals(container)) {
170                 return true;
171             }
172             parent = parent.getParent();
173         }
174         return false;
175     }
176 }
177
Popular Tags