protected boolean hasMappedParents(Collection<?> inputs, Collection<?> outputs) {
   Collection<Object> parents = new HashSet<Object>();
   for (Object input : inputs) {
     parents.add(domain.getParent(input));
   }
   for (Object output : outputs) {
     parents.add(domain.getParent(output));
   }
   return !getAllMappings(parents).isEmpty();
 }
  public boolean isAttachedObject(Object object) {
    Object root = object;
    // FB used to check for parent != null.
    // Need to find the top most model object not including the resource.
    // parent instanceof Eobject only checks for trees in model object space.
    for (Object parent = domain.getParent(object);
        parent instanceof EObject;
        parent = domain.getParent(parent)) {
      root = parent;
    }

    return getInputs().contains(root) || getOutputs().contains(root);
  }
  public void setDomain(MappingDomain domain) {
    if (this.domain != domain) {
      if (this.domain != null) {
        eAdapters.remove(mappedObjectListener);
      }

      this.domain = domain;
      domain.setMappingRoot(this);

      mappedObjectListener =
          new AdapterImpl() {
            @Override
            public void notifyChanged(Notification notification) {
              Object feature = notification.getFeature();
              if (feature == MappingPackage.eINSTANCE.getMapping_Inputs()
                  || feature == MappingPackage.eINSTANCE.getMapping_Outputs()) {
                initializeMappedObjectStates();
              }
            }
          };
      eAdapters().add(mappedObjectListener);

      initializeMappedObjectStates();
    }
  }
 protected Collection<Object> getTypeClassifiers(Collection<?> collection) {
   Collection<Object> types = new ArrayList<Object>();
   for (Object object : collection) {
     Object type = domain.getTypeClassifier(object);
     if (type != null) {
       types.add(type);
     }
   }
   return types;
 }
  public boolean canRemoveMapping(Mapping mapping) {
    int enablementFlags = domain.getMappingEnablementFlags();
    if (mapping.getNestedIn() == null
        || (enablementFlags & MappingDomain.ENABLE_UNMAPPED_PARENTS) == 0
            && hasMappedChildren(mapping)) {
      return false;
    }

    return true;
  }
 protected boolean hasCompatibleMetaObjects(Collection<?> inputs, Collection<?> outputs) {
   for (Object input : inputs) {
     EObject inputType = ((EObject) input).eClass();
     EObject convertedInputType = domain.getOutputMetaObject(inputType);
     for (Object output : outputs) {
       EObject outputType = ((EObject) output).eClass();
       if (convertedInputType != outputType) {
         return false;
       }
     }
   }
   return true;
 }
  public void refreshMappedObjectStates(Mapping subtree) {
    for (Object input : subtree.getInputs()) {
      for (Iterator<?> objects = domain.treeIterator(input); objects.hasNext(); ) {
        Object object = objects.next();
        MappedObjectState mappedObjectState = getMappedObjectState(object);
        if (mappedObjectState != null) {
          mappedObjectState.setInput();
        }
      }
    }

    for (Object output : subtree.getOutputs()) {
      for (Iterator<?> objects = domain.treeIterator(output); objects.hasNext(); ) {
        MappedObjectState mappedObjectState = getMappedObjectState(objects.next());
        if (mappedObjectState != null) {
          mappedObjectState.setOutput();
        }
      }
    }

    for (Iterator<Mapping> mappings = subtree.treeIterator(); mappings.hasNext(); ) {
      Mapping mapping = mappings.next();
      for (Object input : mapping.getInputs()) {
        MappedObjectState mappedObjectState = getMappedObjectState(input);
        if (mappedObjectState != null) {
          mappedObjectState.getMappings().add(mapping);
        }
      }

      for (Object output : mapping.getOutputs()) {
        MappedObjectState mappedObjectState = getMappedObjectState(output);
        if (mappedObjectState != null) {
          mappedObjectState.getMappings().add(mapping);
        }
      }
    }
  }
  public Mapping getParentMapping(Collection<?> collection) {
    // Barring a better result, this will be the result.
    //
    Mapping result = this;

    // Cache the tree path for each object.
    //
    final Collection<List<?>> allTreePaths = new ArrayList<List<?>>();
    for (Object object : collection) {
      allTreePaths.add(domain.getTreePath(object));
    }

    // Iterate over the mappings in the tree.
    //
    OuterLoop:
    for (TreeIterator<Mapping> mappings = treeIterator(); mappings.hasNext(); ) {
      Mapping mapping = mappings.next();

      // Check to make sure that every object in the collection has an ancestor that is contained in
      // this mapping.
      //
      for (Iterator<List<?>> treePaths = allTreePaths.iterator(); treePaths.hasNext(); ) {
        List<?> treePath = treePaths.next();
        Collection<?> mappedObjects = mapping.getMappedObjects();
        mappedObjects.retainAll(treePath);

        // If the intersection is empty, i.e., no ancestor is in the mapping...
        //
        if (mappedObjects.isEmpty()) {
          // If this mapping isn't a parent, it's children definitely won't be either.
          //
          mappings.prune();
          continue OuterLoop;
        }
      }

      // Make sure the collections aren't identical...
      //
      Collection<?> mappedObjects = mapping.getMappedObjects();
      if (!collection.containsAll(mappedObjects) || !mappedObjects.containsAll(collection)) {
        result = mapping;
      }
    }

    return result;
  }
  public boolean canCreateMapping(Collection<?> inputs, Collection<?> outputs, Mapping mapping) {
    if (domain == null) {
      return false;
    }
    int enablementFlags = domain.getMappingEnablementFlags();
    if ((enablementFlags & MappingDomain.ENABLE_EMPTY_INPUTS) == 0 && inputs.size() == 0
        || (enablementFlags & MappingDomain.ENABLE_EMPTY_OUTPUTS) == 0 && outputs.size() == 0
        || inputs.size() == 0 && outputs.size() == 0
        || (enablementFlags & MappingDomain.ENABLE_MULTIPLE_INPUTS) == 0 && inputs.size() > 1
        || (enablementFlags & MappingDomain.ENABLE_MULTIPLE_OUTPUTS) == 0 && outputs.size() > 1
        || (enablementFlags & MappingDomain.ENABLE_MULTIPLE_INPUT_MAPPINGS) == 0
            && isMapped(inputs, mapping)
        || (enablementFlags & MappingDomain.ENABLE_MULTIPLE_OUTPUT_MAPPINGS) == 0
            && isMapped(outputs, mapping)
        || (enablementFlags & MappingDomain.ENABLE_UNMAPPED_PARENTS) == 0
            && !hasMappedParents(inputs, outputs)
        || (enablementFlags & MappingDomain.ENABLE_INCOMPATIBLE_METAOBJECTS) == 0
            && !hasCompatibleMetaObjects(inputs, outputs)
        || (enablementFlags & MappingDomain.ENABLE_INCOMPATIBLE_TYPE_CLASSIFIERS) == 0
            && !hasCompatibleTypes(inputs, outputs)) {
      return false;
    }

    for (Object input : inputs) {
      if (!isAttachedObject(input)) {
        return false;
      }
    }

    for (Object output : outputs) {
      if (!isAttachedObject(output)) {
        return false;
      }
    }

    return true;
  }