/**
  * Creates a compilation unit.
  *
  * @exception ModelException if unable to create the compilation unit.
  */
 protected void executeOperation() throws ModelException {
   try {
     // beginTask(Messages.operation_createUnitProgress, 2);
     ModelElementDelta delta = newModelElementDelta();
     ISourceModule unit = getSourceModule();
     IScriptFolder pkg = (IScriptFolder) getParentElement();
     IContainer folder = (IContainer) pkg.getResource();
     worked(1);
     IFile compilationUnitFile = folder.getFile(new Path(fName));
     if (compilationUnitFile.exists()) {
       // update the contents of the existing unit if fForce is true
       if (force) {
         IBuffer buffer = unit.getBuffer();
         if (buffer == null) return;
         buffer.setContents(fSource);
         unit.save(new NullProgressMonitor(), false);
         resultElements = new IModelElement[] {unit};
         if (!Util.isExcluded(unit) && unit.getParent().exists()) {
           for (int i = 0; i < resultElements.length; i++) {
             delta.changed(resultElements[i], IModelElementDelta.F_CONTENT);
           }
           addDelta(delta);
         }
       } else {
         throw new ModelException(
             new ModelStatus(
                 IModelStatusConstants.NAME_COLLISION,
                 Messages.bind(
                     Messages.status_nameCollision,
                     compilationUnitFile.getFullPath().toString())));
       }
     } else {
       try {
         String encoding = null;
         try {
           encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible
         } catch (CoreException ce) {
           // use no encoding
         }
         InputStream stream =
             new ByteArrayInputStream(
                 encoding == null ? fSource.getBytes() : fSource.getBytes(encoding));
         createFile(folder, unit.getElementName(), stream, force);
         resultElements = new IModelElement[] {unit};
         if (!Util.isExcluded(unit) && unit.getParent().exists()) {
           for (int i = 0; i < resultElements.length; i++) {
             delta.added(resultElements[i]);
           }
           addDelta(delta);
         }
       } catch (IOException e) {
         throw new ModelException(e, IModelStatusConstants.IO_EXCEPTION);
       }
     }
     worked(1);
   } finally {
     done();
   }
 }
Beispiel #2
0
  protected boolean buildStructure(
      OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource)
      throws ModelException {
    // check whether this folder can be opened
    if (!underlyingResource.isAccessible()) throw newNotPresentException();

    int kind = getKind();

    if (kind == IProjectFragment.K_SOURCE && Util.isExcluded(this)) throw newNotPresentException();

    // add modules from resources
    HashSet vChildren = new HashSet();
    try {
      ProjectFragment root = getProjectFragment();
      char[][] inclusionPatterns = root.fullInclusionPatternChars();
      char[][] exclusionPatterns = root.fullExclusionPatternChars();
      IResource[] members = ((IContainer) underlyingResource).members();
      for (int i = 0, max = members.length; i < max; i++) {
        IResource child = members[i];
        if (child.getType() != IResource.FOLDER
            && !Util.isExcluded(child, inclusionPatterns, exclusionPatterns)) {
          IModelElement childElement;
          if (kind == IProjectFragment.K_SOURCE && Util.isValidSourceModule(this, child)) {
            childElement = getSourceModule(child.getName());
            vChildren.add(childElement);
          }
        }
      }
    } catch (CoreException e) {
      throw new ModelException(e);
    }

    if (kind == IProjectFragment.K_SOURCE) {
      // add primary source modules
      ISourceModule[] primarySourceModules = getSourceModules(DefaultWorkingCopyOwner.PRIMARY);
      for (int i = 0, length = primarySourceModules.length; i < length; i++) {
        ISourceModule primary = primarySourceModules[i];
        vChildren.add(primary);
      }
    }

    IModelElement[] children = new IModelElement[vChildren.size()];
    vChildren.toArray(children);
    info.setChildren(children);
    return true;
  }
Beispiel #3
0
 public ISourceModule[] getSourceModules(WorkingCopyOwner owner) {
   ISourceModule[] workingCopies =
       ModelManager.getModelManager().getWorkingCopies(owner, false /* don't add primary */);
   if (workingCopies == null) return ModelManager.NO_WORKING_COPY;
   int length = workingCopies.length;
   ISourceModule[] result = new ISourceModule[length];
   int index = 0;
   for (int i = 0; i < length; i++) {
     ISourceModule wc = workingCopies[i];
     IResource res = wc.getResource();
     boolean valid;
     if (res != null) valid = Util.isValidSourceModule(this, res);
     else valid = Util.isValidSourceModule(this, wc.getPath());
     if (equals(wc.getParent()) && !Util.isExcluded(wc) && valid) {
       result[index++] = wc;
     }
   }
   if (index != length) {
     System.arraycopy(result, 0, result = new ISourceModule[index], 0, index);
   }
   return result;
 }