KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdt.internal.debug.core.refactoring;
12
13 import java.text.MessageFormat JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18
19 import org.eclipse.jdt.core.IField;
20 import org.eclipse.jdt.core.IType;
21
22 import org.eclipse.jdt.debug.core.IJavaWatchpoint;
23 import org.eclipse.jdt.debug.core.JDIDebugModel;
24
25 import org.eclipse.debug.core.DebugPlugin;
26 import org.eclipse.debug.core.IBreakpointManager;
27 import org.eclipse.debug.core.model.IBreakpoint;
28
29 import org.eclipse.jdt.internal.debug.ui.BreakpointUtils;
30 import org.eclipse.ltk.core.refactoring.Change;
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32
33 /**
34  */

35 public class JavaWatchpointFieldNameChange extends Change {
36     
37     private IJavaWatchpoint fWatchpoint;
38     private String JavaDoc fNewName;
39     private String JavaDoc fOldName;
40     private IType fDeclaringType;
41
42     public static Change createChange(IField field, String JavaDoc newName) throws CoreException {
43         String JavaDoc typeName= field.getDeclaringType().getFullyQualifiedName();
44         String JavaDoc fieldName= field.getElementName();
45         IBreakpointManager breakpointManager= DebugPlugin.getDefault().getBreakpointManager();
46         IBreakpoint[] breakpoints= breakpointManager.getBreakpoints(JDIDebugModel.getPluginIdentifier());
47         for (int i= 0; i < breakpoints.length; i++) {
48             IBreakpoint breakpoint= breakpoints[i];
49             if (breakpoint instanceof IJavaWatchpoint) {
50                 IJavaWatchpoint watchpoint= (IJavaWatchpoint)breakpoint;
51                 if (typeName.equals(watchpoint.getTypeName()) && fieldName.equals(watchpoint.getFieldName())) {
52                     return new JavaWatchpointFieldNameChange(watchpoint, newName);
53                 }
54             }
55         }
56         return null;
57     }
58
59     public JavaWatchpointFieldNameChange(IJavaWatchpoint watchpoint, String JavaDoc newName) throws CoreException {
60         fWatchpoint= watchpoint;
61         fNewName= newName;
62         fOldName= fWatchpoint.getFieldName();
63         fDeclaringType= BreakpointUtils.getType(watchpoint);
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#getName()
68      */

69     public String JavaDoc getName() {
70         return MessageFormat.format(RefactoringMessages.JavaWatchpointFieldNameChange_1, new String JavaDoc[] {fDeclaringType.getElementName(), fOldName}); //$NON-NLS-1$
71
}
72
73     /* (non-Javadoc)
74      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#initializeValidationData(org.eclipse.core.runtime.IProgressMonitor)
75      */

76     public void initializeValidationData(IProgressMonitor pm) {
77     }
78     
79     /* (non-Javadoc)
80      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#isValid(org.eclipse.core.runtime.IProgressMonitor)
81      */

82     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
83         RefactoringStatus status= new RefactoringStatus();
84         if (!fWatchpoint.isRegistered()) {
85             status.addFatalError(MessageFormat.format(RefactoringMessages.JavaWatchpointFieldNameChange_2, new String JavaDoc[] {fDeclaringType.getElementName(), fOldName})); //$NON-NLS-1$
86
}
87         return status;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
92      */

93     public Change perform(IProgressMonitor pm) throws CoreException {
94         IField newField= fDeclaringType.getField(fNewName);
95         Map JavaDoc attributes= fWatchpoint.getMarker().getAttributes();
96         boolean isAccess= fWatchpoint.isAccess();
97         boolean isModification= fWatchpoint.isModification();
98         boolean isEnable= fWatchpoint.isEnabled();
99         BreakpointUtils.addJavaBreakpointAttributes(attributes, newField);
100         IJavaWatchpoint newWatchpoint= JDIDebugModel.createWatchpoint(
101                 fWatchpoint.getMarker().getResource(),
102                 fWatchpoint.getTypeName(),
103                 fNewName,
104                 fWatchpoint.getLineNumber(),
105                 fWatchpoint.getCharStart(),
106                 fWatchpoint.getCharEnd(),
107                 fWatchpoint.getHitCount(),
108                 true,
109                 attributes);
110         newWatchpoint.setAccess(isAccess);
111         newWatchpoint.setModification(isModification);
112         newWatchpoint.setEnabled(isEnable);
113         fWatchpoint.delete();
114         return new JavaWatchpointFieldNameChange(newWatchpoint, fOldName);
115     }
116
117     /* (non-Javadoc)
118      * @see org.eclipse.jdt.internal.corext.refactoring.base.Change#getModifiedElement()
119      */

120     public Object JavaDoc getModifiedElement() {
121         return fWatchpoint;
122     }
123
124 }
125
Popular Tags