RenamePackageFragment.java :  » Workflow-Engines » osbl-1_0 » refactor » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » refactor » RenamePackageFragment.java
/**
 * 
 */
package refactor;

import java.util.Iterator;
import java.util.List;

import logging.LoggingPlugin;
import model.ModelModifier;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.CompilationUnit;

import core.AbstractAST;
import core.IRefactoring;
import core.ManipulateHelper;
import core.RefactorInfo;
import core.Util;

/**
 * @author sh
 *
 */
public class RenamePackageFragment extends AbstractAST 
  implements IRefactoring {
  
  private String newName = null;
  
  private Log log = LogFactory.getLog(getClass());

  public RenamePackageFragment(String newName) {
    super();
    this.newName = newName;
  }

  /** 
   * Renames the given package fragment.
   * It automatically renames the package name
   * and the package declarations in the compilatin units.
   * 
   * @param modelElement the package fragment to get renamed
   * @see refactor.core.IRefactoring#modification(org.eclipse.jdt.core.IJavaElement)
   */
  public void modification(IJavaElement modelElement) {
    try {
      ((IPackageFragment)modelElement).rename(newName, true, null);
    } 
    catch (JavaModelException e) {
      String message = e.getMessage();
      ILog logger = LoggingPlugin.getDefault().getLog();
      String symName = LoggingPlugin.getDefault().getBundle().getSymbolicName();
      logger.log(new Status(IStatus.ERROR,symName,0,message,e));
      throw new RuntimeException(e);
    } 
  }

  /**
   * Scans all model files in the workspace
   * and updates implementations property 
   * of the concerning model elements if needed.
   * 
   * @param modelElement the compilation unit to modify
   * @see refactor.core.IRefactoring#updateModel(org.eclipse.jdt.core.IJavaElement)
   */
  public void updateModel(IJavaElement modelElement) {      
    // start the model update
    new ModelModifier().doImplementationModification(modelElement.getElementName(), this.newName);
  }
  
  /**
   * Udates the package declarations on all compilatin units
   * found in the workspace.
   * 
   * @param modelElement the package fragment to modify
   * @see refactor.core.IRefactoring#updateSource(org.eclipse.jdt.core.IJavaElement)
   */
  public void updateSource(IJavaElement modelElement) {  
    String oldName = ((IPackageFragment)modelElement).getElementName();
        
    PackageFragmentVisitor packageVisitor = new PackageFragmentVisitor();
    CompilationUnit u = null;
    
    List<ICompilationUnit> units = Util.getICompilationUnits();
    for (Iterator<ICompilationUnit> iterator = units.iterator(); iterator.hasNext();) {
      u = parse((ICompilationUnit) iterator.next());    
      packageVisitor.process(u,oldName,newName);
      ManipulateHelper.saveDirectlyModifiedUnit(u);
    }
  }
  
  public void process(RefactorInfo info) {}

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.