private boolean deleteComponentsPostAssert (Collection<DesignComponent> components) { for (DesignComponent component : components) assert ! Debug.isComponentReferencedInRootTree (component) : "Component (" + component + ") is referenced still after deletion"; // NOI18N return true; }
/** * Checks whether a specified component is referenced from the main tree of components under document root. * @param referenceComponent the reference component * @return true, if the reference component is referenced */ public static boolean isComponentReferencedInRootTree (DesignComponent referenceComponent) { assert referenceComponent != null; return isComponentReferencedInRootTree (referenceComponent.getDocument ().getRootComponent (), referenceComponent); }
private static boolean isComponentReferencedInRootTree (DesignComponent treeComponent, DesignComponent referenceComponent) { ArrayList<DesignComponent> referenced = new ArrayList<DesignComponent> (); ComponentDescriptor descriptor = treeComponent.getComponentDescriptor (); if (descriptor != null) { for (PropertyDescriptor propertyDescriptor : descriptor.getPropertyDescriptors ()) { PropertyValue propertyValue = treeComponent.readProperty (propertyDescriptor.getName ()); referenced.clear (); Debug.collectAllComponentReferences (propertyValue, referenced); if (referenced.contains (referenceComponent)) return true; } } for (DesignComponent child : treeComponent.getComponents ()) if (isComponentReferencedInRootTree (child, referenceComponent)) return true; return false; }
/** * Removes a component from the component tree and removes references to this component from all property values * in all components in the document. * <p> * Note: It does not allows to remove the root component. * @param component the component */ public void deleteComponent (DesignComponent component) { assert transactionManager.isWriteAccess (); assert component != null && component != rootComponent; assert component.getDocument () == this; Collection<DesignComponent> components = component.getComponents (); if (components.size () > 0) Debug.warning ("Children has to be deleted before deleting the component", component, components); // NOI18N component.removeFromParentComponent (); ComponentDescriptor descriptor = component.getComponentDescriptor (); if (descriptor != null) for (PropertyDescriptor property : descriptor.getPropertyDescriptors ()) component.resetToDefault (property.getName ()); if (selectedComponents.contains (component)) { HashSet<DesignComponent> selected = new HashSet<DesignComponent> (selectedComponents); selected.remove (component); setSelectedComponents ("deleteComponent", selected); // NOI18N } assert ! Debug.isComponentReferencedInRootTree (component) : "Component (" + component + ") is referenced still after deletion"; // NOI18N }