Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » doAddContentChild IllegalStateException
doAddContentChild IllegalStateException [message #1774709] Wed, 18 October 2017 22:28 Go to next message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 35
Registered: September 2009
Member
I get an IllegalStateException thrown when I add a new child to my model. The exception originates from the end of the code below (which is from org.eclipse.gef.mvc.fx.parts.AbstractContentPart). I get it because I completely ignore the index that doAddContentChild is given. So, I get an exception that looks like this:

java.lang.IllegalStateException: doAddContentChild(Object, int) did not add content child edu.illinois.mobius.atomic.advise.models.impl.AccessImpl@6802d023 (name: Access 1, codename: Access1, useDefault: true) (xpos: 517.0, ypos: 334.0, editMask: 0) at index 22, but at index 0.


I ignore the index because my model does not store all of the content children in a single List, but across a variety of structures. I was planning to extend AbstractContentPart and override addContentChild without that postcondition checking, but that method is final. So, the only other solution I can think of is to create my own AbstractContentPart that extends AbstractVisualPart and copy a bunch of code, which seems like a Bad Idea (TM). Is there a better solution?

@Override
	public final void addContentChild(Object contentChild, int index) {
		List<Object> oldContentChildren = new ArrayList<>(
				doGetContentChildren());
		if (oldContentChildren.contains(contentChild)) {
			int oldIndex = oldContentChildren.indexOf(contentChild);
			if (oldIndex == index) {
				throw new IllegalArgumentException("Cannot add " + contentChild
						+ " because its already contained at given index "
						+ index);
			} else {
				throw new IllegalArgumentException("Cannot add " + contentChild
						+ " because its already a content child at index "
						+ oldIndex);
			}
		}
		doAddContentChild(contentChild, index);
		// check doAddContentChild(Object, int) does not violate postconditions
		List<? extends Object> newContentChildren = doGetContentChildren();
		if (!newContentChildren.contains(contentChild)) {
			throw new IllegalStateException(
					"doAddContentChild(Object, int) did not add content child "
							+ contentChild + " .");
		}
		int newIndex = newContentChildren.indexOf(contentChild);
		if (newIndex != index) {
			throw new IllegalStateException(
					"doAddContentChild(Object, int) did not add content child "
							+ contentChild + " at index " + index
							+ ", but at index " + newIndex + ".");
		}
		contentChildren.setAll(newContentChildren);
	}

[Updated on: Wed, 18 October 2017 22:33]

Report message to a moderator

Re: doAddContentChild IllegalStateException [message #1774810 is a reply to message #1774709] Thu, 19 October 2017 20:41 Go to previous messageGo to next message
Ken Keefe is currently offline Ken KeefeFriend
Messages: 35
Registered: September 2009
Member
Ok, I came up with another solution. I created another list object in the Part that should satisfy the extra checking addContentChild does. Still seems like a waste of memory, but I don't see a better solution.

package edu.illinois.mobius.atomic.advise.ui.gef.parts;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.mvc.fx.parts.AbstractContentPart;
import org.eclipse.gef.mvc.fx.parts.IVisualPart;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;

import edu.illinois.mobius.atomic.advise.models.AEGNode;
import edu.illinois.mobius.atomic.advise.models.Arc;
import edu.illinois.mobius.atomic.advise.models.AttackExecutionGraph;
import javafx.scene.Group;
import javafx.scene.Node;

public class AttackExecutionGraphPart extends AbstractContentPart<Group> {
	
	private ArrayList<Object> children = new ArrayList<Object>();
	
	@Override
	protected void doAddChildVisual(IVisualPart<? extends Node> child, int index) {
		getVisual().getChildren().add(child.getVisual());
	}
	
	@Override
	protected void doAddContentChild(Object contentChild, int index) {
		if(contentChild instanceof AEGNode) {
			getContent().addNode((AEGNode)contentChild);
			children.add(index, contentChild);
		} else if(contentChild instanceof Arc) {
			getContent().addArc((Arc)contentChild);
			children.add(index, contentChild);
		} else {
            throw new IllegalArgumentException("contentChild has invalid type: " + contentChild.getClass());
        }
	}
	
	@Override
	protected Group doCreateVisual() {
		return new Group();
	}
	
	@Override
    protected SetMultimap<? extends Object, String> doGetContentAnchorages() {
        return HashMultimap.create();
    }

    @Override
    protected List<? extends Object> doGetContentChildren() {
    		for(Object o : getContent().getAllNodes())
    			if(!children.contains(o))
    				children.add(o);
    		for(Object o : getContent().getArcElements())
    			if(!children.contains(o))
    				children.add(o);
    		
    		return children;
    }

    @Override
    protected void doRefreshVisual(Group visual) {}

    @Override
    protected void doRemoveChildVisual(IVisualPart<? extends Node> child, int index) {
        getVisual().getChildren().remove(child.getVisual());
    }

    @Override
    protected void doRemoveContentChild(Object contentChild) {
        if (contentChild instanceof AEGNode) {
            getContent().removeNode((AEGNode)contentChild);
            children.remove(contentChild);
        } else if (contentChild instanceof Arc) {
        		getContent().removeArc((Arc)contentChild);
        		children.remove(contentChild);
        } else {
            throw new IllegalArgumentException("contentChild has invalid type: " + contentChild.getClass());
        }
    }
	
	@Override
	public AttackExecutionGraph getContent() {
		return (AttackExecutionGraph)super.getContent();
	}

}
Re: doAddContentChild IllegalStateException [message #1775366 is a reply to message #1774810] Sat, 28 October 2017 11:34 Go to previous message
Alexander Nyssen is currently offline Alexander NyssenFriend
Messages: 235
Registered: July 2009
Location: L√ľnen
Senior Member
The order of children indicates the z-ordering of their visualizations. The contract synchronization also relies on this (as it will reorder children). That is why the post-condition is checked (and the method is made final). If your model children are not ordered, you have to introduce an ordering just within your ContentPart, e.g. in the way you have done.
Previous Topic:Depth-first Traversal problem in large-scale model.
Next Topic:[GEF5] AbstractVisualPart activate while addChild
Goto Forum:
  


Current Time: Sun Sep 23 03:24:58 GMT 2018

Powered by FUDForum. Page generated in 0.01994 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top