001: /**
002: *
003: */package refactor;
004:
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import logging.LoggingPlugin;
009: import model.ModelModifier;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.eclipse.core.runtime.ILog;
014: import org.eclipse.core.runtime.IStatus;
015: import org.eclipse.core.runtime.Status;
016: import org.eclipse.jdt.core.ICompilationUnit;
017: import org.eclipse.jdt.core.IJavaElement;
018: import org.eclipse.jdt.core.IPackageFragment;
019: import org.eclipse.jdt.core.JavaModelException;
020: import org.eclipse.jdt.core.dom.CompilationUnit;
021:
022: import core.AbstractAST;
023: import core.IRefactoring;
024: import core.ManipulateHelper;
025: import core.RefactorInfo;
026: import core.Util;
027:
028: /**
029: * @author sh
030: *
031: */
032: public class RenamePackageFragment extends AbstractAST implements
033: IRefactoring {
034:
035: private String newName = null;
036:
037: private Log log = LogFactory.getLog(getClass());
038:
039: public RenamePackageFragment(String newName) {
040: super ();
041: this .newName = newName;
042: }
043:
044: /**
045: * Renames the given package fragment.
046: * It automatically renames the package name
047: * and the package declarations in the compilatin units.
048: *
049: * @param modelElement the package fragment to get renamed
050: * @see refactor.core.IRefactoring#modification(org.eclipse.jdt.core.IJavaElement)
051: */
052: public void modification(IJavaElement modelElement) {
053: try {
054: ((IPackageFragment) modelElement).rename(newName, true,
055: null);
056: } catch (JavaModelException e) {
057: String message = e.getMessage();
058: ILog logger = LoggingPlugin.getDefault().getLog();
059: String symName = LoggingPlugin.getDefault().getBundle()
060: .getSymbolicName();
061: logger
062: .log(new Status(IStatus.ERROR, symName, 0, message,
063: e));
064: throw new RuntimeException(e);
065: }
066: }
067:
068: /**
069: * Scans all model files in the workspace
070: * and updates implementations property
071: * of the concerning model elements if needed.
072: *
073: * @param modelElement the compilation unit to modify
074: * @see refactor.core.IRefactoring#updateModel(org.eclipse.jdt.core.IJavaElement)
075: */
076: public void updateModel(IJavaElement modelElement) {
077: // start the model update
078: new ModelModifier().doImplementationModification(modelElement
079: .getElementName(), this .newName);
080: }
081:
082: /**
083: * Udates the package declarations on all compilatin units
084: * found in the workspace.
085: *
086: * @param modelElement the package fragment to modify
087: * @see refactor.core.IRefactoring#updateSource(org.eclipse.jdt.core.IJavaElement)
088: */
089: public void updateSource(IJavaElement modelElement) {
090: String oldName = ((IPackageFragment) modelElement)
091: .getElementName();
092:
093: PackageFragmentVisitor packageVisitor = new PackageFragmentVisitor();
094: CompilationUnit u = null;
095:
096: List<ICompilationUnit> units = Util.getICompilationUnits();
097: for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator
098: .hasNext();) {
099: u = parse((ICompilationUnit) iterator.next());
100: packageVisitor.process(u, oldName, newName);
101: ManipulateHelper.saveDirectlyModifiedUnit(u);
102: }
103: }
104:
105: public void process(RefactorInfo info) {
106: }
107:
108: }
|