doAddContentChild IllegalStateException [message #1774709] |
Wed, 18 October 2017 22:28  |
Ken Keefe Messages: 38 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 #1775366 is a reply to message #1774810] |
Sat, 28 October 2017 11:34  |
|
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.
|
|
|
Powered by
FUDForum. Page generated in 0.02399 seconds