1 11 package org.eclipse.jdt.internal.debug.core.refactoring; 12 13 import java.text.MessageFormat ; 14 import java.util.Map ; 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 35 public class JavaWatchpointFieldNameChange extends Change { 36 37 private IJavaWatchpoint fWatchpoint; 38 private String fNewName; 39 private String fOldName; 40 private IType fDeclaringType; 41 42 public static Change createChange(IField field, String newName) throws CoreException { 43 String typeName= field.getDeclaringType().getFullyQualifiedName(); 44 String 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 newName) throws CoreException { 60 fWatchpoint= watchpoint; 61 fNewName= newName; 62 fOldName= fWatchpoint.getFieldName(); 63 fDeclaringType= BreakpointUtils.getType(watchpoint); 64 } 65 66 69 public String getName() { 70 return MessageFormat.format(RefactoringMessages.JavaWatchpointFieldNameChange_1, new String [] {fDeclaringType.getElementName(), fOldName}); } 72 73 76 public void initializeValidationData(IProgressMonitor pm) { 77 } 78 79 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 [] {fDeclaringType.getElementName(), fOldName})); } 87 return status; 88 } 89 90 93 public Change perform(IProgressMonitor pm) throws CoreException { 94 IField newField= fDeclaringType.getField(fNewName); 95 Map 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 120 public Object getModifiedElement() { 121 return fWatchpoint; 122 } 123 124 } 125 | Popular Tags |