Search in sources :

Example 1 with IBinaryVisitor

use of org.whole.lang.visitors.IBinaryVisitor in project whole by wholeplatform.

the class CommandFactory method create.

public Command create(PartRequest request) {
    IPartRequestHandler handler;
    // tries to handle operation with custom handlers
    List<IPartRequestHandler> handlerList = handlersMap.get(request.getRequestType());
    if (handlerList != null && !handlerList.isEmpty()) {
        Iterator<IPartRequestHandler> handlerIterator = handlerList.iterator();
        while (handlerIterator.hasNext()) {
            handler = (IPartRequestHandler) handlerIterator.next();
            if (handler.canHandlePartRequest(request))
                return handler.handlePartRequest(request);
        }
    }
    // handle basic requests
    if (EntityUtils.isComposite(request.getModelEntity()) && request.getRequestType() == PartRequest.MOVE_CHILD)
        return moveIndexedFeature.create(request);
    if (request.getRequestType() == PartRequest.MOVE_ORPHAN_CHILD) {
        if (EntityUtils.isComposite(request.getModelEntity()))
            return orphanIndexedFeature.create(request);
        else
            return orphanFeature.create(request);
    }
    if (request.getRequestType() == PartRequest.DELETE) {
        if (EntityUtils.isComposite(request.getParentModelEntity()))
            return removeIndexedFeature.create(request);
        else
            // was else if (EntityUtils.isSimple(request.getParentModelEntity()))
            return removeFeature.create(request);
    }
    // handle composite and simple requests
    if (isDnDRequest(request)) {
        boolean isCompositeAddCommand, isReplaceCommand;
        isCompositeAddCommand = isReplaceCommand = request.size() > 0;
        IEntity targetEntity = request.getModelEntity();
        boolean isCloneRequest = request.getRequestType() == PartRequest.CLONE_CHILD;
        HashSet<IEntity> targetEntityAncestors = new HashSet<IEntity>();
        GenericTraversalFactory tf = GenericTraversalFactory.instance;
        IBinaryVisitor ancestorsVisitor = tf.ancestors(tf.collect(tf.identity(), targetEntityAncestors));
        ancestorsVisitor.visit(targetEntity);
        for (IEntityPart dndPart : request) {
            IEntity dndEntity = dndPart.getModelEntity();
            if (targetEntityAncestors.contains(dndEntity) && !isCloneRequest)
                return UnexecutableCommand.INSTANCE;
            EntityDescriptor<?> dndEntityDescriptor = dndEntity.wGetEntityDescriptor();
            isCompositeAddCommand &= EntityUtils.isAddable(targetEntity, dndEntityDescriptor) || EntityUtils.isComposite(targetEntity);
            isReplaceCommand &= EntityUtils.isReplaceable(targetEntity, dndEntityDescriptor);
        }
        if (isCompositeAddCommand)
            return createCompositeAddCommand(request);
        else if (isReplaceCommand)
            return createReplaceCommand(request);
        else
            return UnexecutableCommand.INSTANCE;
    }
    return null;
}
Also used : IEntity(org.whole.lang.model.IEntity) GenericTraversalFactory(org.whole.lang.visitors.GenericTraversalFactory) IBinaryVisitor(org.whole.lang.visitors.IBinaryVisitor) IEntityPart(org.whole.lang.ui.editparts.IEntityPart) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 IEntity (org.whole.lang.model.IEntity)1 IEntityPart (org.whole.lang.ui.editparts.IEntityPart)1 GenericTraversalFactory (org.whole.lang.visitors.GenericTraversalFactory)1 IBinaryVisitor (org.whole.lang.visitors.IBinaryVisitor)1