001: /**
002: *
003: */package refactor;
004:
005: import java.lang.reflect.InvocationTargetException;
006:
007: import org.eclipse.core.runtime.IProgressMonitor;
008: import org.eclipse.jdt.core.ICompilationUnit;
009: import org.eclipse.jdt.core.IJavaElement;
010: import org.eclipse.jdt.core.IPackageFragment;
011: import org.eclipse.jdt.core.dom.FieldDeclaration;
012: import org.eclipse.jface.operation.IRunnableWithProgress;
013:
014: import core.IRefactoring;
015: import core.RenameImplementation;
016:
017: /**
018: * This class starts the monitored refactoring progress. This progress consists of three
019: * parts. At the beginning the modify operation, then source update and the model update
020: * progress.
021: *
022: * @author sh
023: *
024: */
025: public class RenameRefactoring {
026:
027: /**
028: * Starts the monitored rename refactoring progress.
029: *
030: * @param selection the selected object
031: * @param newName the new name to set
032: */
033: public void startRenameRefactoring(Object selection, String newName) {
034: /*
035: try {
036: IRunnableWithProgress op = new RefacturingProgress(selection, newName);
037: Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
038: new ProgressMonitorDialog(shell).run(true, true, op);
039: } catch (InvocationTargetException e) {
040: throw new RuntimeException(e + " Refactoring Progress could not be invoked !");
041: } catch (InterruptedException e) {
042: throw new RuntimeException(e + " Refactoring Progress could not be invoked !");
043: }
044: */
045: if (selection instanceof IPackageFragment)
046: renamePackageFragment((IPackageFragment) selection, newName);
047:
048: if (selection instanceof ICompilationUnit)
049: renameCompilationUnit((ICompilationUnit) selection, newName);
050:
051: if (selection instanceof FieldDeclaration)
052: renameFieldDeclaration((FieldDeclaration) selection,
053: newName);
054:
055: // if(selection instanceof IType)
056: // if(selection instanceof IMethod)
057: }
058:
059: /**
060: * Renames a compilation unit.
061: *
062: * @param unit the compilation unit to rename
063: * @param newName the new name to set
064: */
065: private void renameCompilationUnit(ICompilationUnit unit,
066: String newName) {
067: //rename(new RenameCompilationUnit(newName), unit);
068: }
069:
070: /**
071: * Renames a package fragment.
072: *
073: * @param packagefragment the package fragment to rename
074: * @param newName the new name to set
075: */
076: private void renamePackageFragment(
077: IPackageFragment packagefragment, String newName) {
078: rename(new RenamePackageFragment(newName), packagefragment);
079: }
080:
081: /**
082: * Renames a field declaration.
083: *
084: * @param fielddeclaration the field declaration to rename
085: * @param newName the new name to set
086: */
087: private void renameFieldDeclaration(
088: FieldDeclaration fielddeclaration, String newName) {
089: /*
090: RenameFieldDeclaration renFD = new RenameFieldDeclaration(newName);
091: // 1. do the modification
092: renFD.modification(fielddeclaration);
093:
094: // 2. modify the source
095: renFD.updateSource(fielddeclaration);
096:
097: // 3. do the model update
098: renFD.updateModel(fielddeclaration);
099: */
100: }
101:
102: /**
103: * Does the three steps of the refactoring
104: *
105: * @param refObj the kind of refactoring to execute
106: * @param element the element to refactor
107: */
108: private void rename(IRefactoring refObj, IJavaElement element) {
109: if (refObj instanceof RenamePackageFragment)
110: refObj = (RenamePackageFragment) refObj;
111:
112: if (refObj instanceof RenameImplementation)
113: //refObj = (RenameCompilationUnit)refObj;
114:
115: // 1. do the modification
116: refObj.modification(element);
117:
118: // 2. modify the source
119: refObj.updateSource(element);
120:
121: // 3. do the model update
122: refObj.updateModel(element);
123: }
124:
125: /**
126: * A helper class to initialize the Progress Dialog with the
127: * selected object and the the name to set.
128: * In the run method the refactoring will be started.
129: *
130: * @author sh
131: *
132: */
133: private class RefacturingProgress implements IRunnableWithProgress {
134: private Object selection = null;
135: private String newName = null;
136:
137: public RefacturingProgress(Object selection, String newName) {
138: super ();
139: this .selection = selection;
140: this .newName = newName;
141: }
142:
143: public void run(IProgressMonitor monitor)
144: throws InvocationTargetException, InterruptedException {
145:
146: if (selection instanceof IPackageFragment)
147: renamePackageFragment((IPackageFragment) selection,
148: newName);
149:
150: if (selection instanceof ICompilationUnit)
151: renameCompilationUnit((ICompilationUnit) selection,
152: newName);
153:
154: // if(selection instanceof IType)
155: // if(selection instanceof IMethod)
156: }
157: }
158: }
|