final NamedObj object = getTarget();
// Do this as a change request since it may add a new icon.
ChangeRequest request = new ChangeRequest(this, "Edit Custom Icon") {
protected void _execute() throws Exception {
EditorIcon icon = null;
List iconList = object.attributeList(EditorIcon.class);
if (iconList.size() > 0) {
// Get the last icon.
icon = (EditorIcon) iconList.get(iconList.size() - 1);
}
if (icon == null) {
icon = new EditorIcon(object, "_icon");
} else if (icon instanceof XMLIcon) {
// There is an icon currently that is not custom.
// Without trashing the _iconDescription, we can remove
// this icon and replace it.
icon.setContainer(null);
icon = new EditorIcon(object, "_icon");
// Propagate this to derived objects, being
// careful to not trash their custom icons
// if they have them. However, there is a trickiness.
// They may not have a custom icon, but rather have
// an instance of XMLIcon. We have to remove that
// first.
Iterator derivedObjects = object.getDerivedList()
.iterator();
while (derivedObjects.hasNext()) {
NamedObj derived = (NamedObj) derivedObjects.next();
// See whether it has an icon.
EditorIcon derivedIcon = null;
List derivedIconList = derived
.attributeList(EditorIcon.class);
if (derivedIconList.size() > 0) {
// Get the last icon.
derivedIcon = (EditorIcon) derivedIconList
.get(derivedIconList.size() - 1);
}
if (derivedIcon instanceof XMLIcon) {
// There is an icon currently that is not custom.
// Without trashing the _iconDescription, we can remove
// this icon and replace it.
derivedIcon.setContainer(null);
}
}
// Now it is safe to propagate.
icon.propagateExistence();