Example #1
0
 @Override
 public void delete() {
   try {
     jdtCu.delete(false, MyMonitor.currentMonitor());
   } catch (JavaModelException e) {
     throw new MyRuntimeException(e);
   }
 }
Example #2
0
 @Override
 public void setName(String name) {
   try {
     IPackageFragment jdtPkg = (IPackageFragment) jdtCu.getParent();
     jdtCu.rename(name + ".java", false, MyMonitor.currentMonitor());
     jdtCu = jdtPkg.getCompilationUnit(name + ".java");
   } catch (JavaModelException e) {
     throw new MyRuntimeException("Rename " + this + " to " + name, e);
   }
 }
Example #3
0
 @Override
 public void setMPackage(ModifiableMPackage destMPackage) {
   try {
     String destPackageName = destMPackage.getName();
     IPackageFragmentRoot srcRoot = (IPackageFragmentRoot) jdtCu.getParent().getParent();
     IPackageFragment jdtDestPackage = srcRoot.getPackageFragment(destPackageName);
     jdtCu.move(jdtDestPackage, null, null, false, MyMonitor.currentMonitor());
     jdtCu = jdtDestPackage.getCompilationUnit(jdtCu.getElementName());
   } catch (JavaModelException e) {
     throw new MyRuntimeException("Move " + this + " to " + destMPackage, e);
   }
 }
Example #4
0
  public MClassJDT(MPackageJDT mpkg, String name) {
    String fullname = "???." + name;
    ICompilationUnit workingCopy = null;
    try {
      IPackageFragment jdtPackage = (IPackageFragment) mpkg.getJdtPackageInfo().getParent();
      fullname = jdtPackage.getElementName() + "." + name;
      if (jdtPackage.getCompilationUnit(name + ".java").exists())
        throw new MyRuntimeException("Class with name '" + fullname + "' already exists.");
      IProgressMonitor monitor = MyMonitor.currentMonitor();

      String content = "public class " + name + " {}";
      ICompilationUnit cu = jdtPackage.createCompilationUnit(name + ".java", content, true, null);
      workingCopy = cu.getWorkingCopy(monitor);
      workingCopy.createPackageDeclaration(jdtPackage.getElementName(), monitor);

      String source = ((IOpenable) workingCopy).getBuffer().getContents();

      Map<?, ?> options = EclipseUtils.getJdtCorePreferences(jdtPackage);

      // instantiate the default code formatter with the given options
      final CodeFormatter codeFormatter =
          ToolFactory.createCodeFormatter(options, ToolFactory.M_FORMAT_NEW);

      TextEdit edit =
          codeFormatter.format(
              CodeFormatter.K_COMPILATION_UNIT, source, 0, source.length(), 0, null);

      if (edit == null) {
        throw new MyRuntimeException("Can't format the source: " + source);
      }

      workingCopy.applyTextEdit(edit, monitor);
      workingCopy.commitWorkingCopy(false, monitor);
      workingCopy.discardWorkingCopy();

      jdtCu = jdtPackage.getCompilationUnit(name + ".java");
    } catch (JavaModelException e) {
      throw new MyRuntimeException("Can not create '" + fullname + "' class.", e);
    } finally {
      if (workingCopy != null)
        try {
          workingCopy.discardWorkingCopy();
        } catch (JavaModelException e) {
          throw new MyRuntimeException(e);
        }
    }
  }