GroupNode.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package holeg.model;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.logging.Logger;
  5. import java.util.stream.Stream;
  6. import holeg.preferences.ImagePreference;
  7. import holeg.serialize.PostDeserialize;
  8. public class GroupNode extends AbstractCanvasObject implements PostDeserialize {
  9. private static final Logger log = Logger.getLogger(AbstractCanvasObject.class.getName());
  10. private final ArrayList<HolonObject> objectList = new ArrayList<>();
  11. private final ArrayList<HolonSwitch> switchList = new ArrayList<>();
  12. private final ArrayList<Node> nodeList = new ArrayList<>();
  13. private final ArrayList<GroupNode> groupNodeList = new ArrayList<>();
  14. public GroupNode(String nodeName) {
  15. super(nodeName);
  16. }
  17. @Override
  18. public String getImagePath() {
  19. return ImagePreference.Canvas.GroupNode;
  20. }
  21. public void addAll(Stream<AbstractCanvasObject> stream) {
  22. stream.forEach(this::add);
  23. }
  24. public void addAll(Collection<AbstractCanvasObject> collection) {
  25. collection.forEach(this::add);
  26. }
  27. public void add(AbstractCanvasObject object) {
  28. if (object instanceof HolonObject hObject) {
  29. objectList.add(hObject);
  30. }else if(object instanceof HolonSwitch hSwitch) {
  31. switchList.add(hSwitch);
  32. }else if(object instanceof Node node) {
  33. nodeList.add(node);
  34. }else if(object instanceof GroupNode groupNode) {
  35. groupNodeList.add(groupNode);
  36. }
  37. object.setGroupNode(this);
  38. }
  39. public void remove(AbstractCanvasObject object) {
  40. if (object instanceof HolonObject hObject) {
  41. objectList.remove(hObject);
  42. }else if(object instanceof HolonSwitch hSwitch) {
  43. switchList.remove(hSwitch);
  44. }else if(object instanceof Node node) {
  45. nodeList.remove(node);
  46. }else if(object instanceof GroupNode groupNode) {
  47. groupNodeList.remove(groupNode);
  48. }
  49. object.setGroupNode(null);
  50. }
  51. public void removeAll(Stream<AbstractCanvasObject> stream){
  52. stream.forEach(this::remove);
  53. }
  54. public void removeAll(Collection<AbstractCanvasObject> collection){
  55. collection.forEach(this::remove);
  56. }
  57. public Stream<AbstractCanvasObject> getObjectsInThisLayer() {
  58. return Stream.of(objectList.stream(), switchList.stream(), nodeList.stream(), groupNodeList.stream())
  59. .flatMap(s -> s);
  60. }
  61. public Stream<AbstractCanvasObject> getAllObjectsRecursive() {
  62. Stream<AbstractCanvasObject> objects = Stream.of(objectList.stream(), switchList.stream(), nodeList.stream())
  63. .flatMap(s -> s);
  64. return Stream.concat(objects,
  65. groupNodeList.stream().flatMap(GroupNode::getObjectsInThisLayer));
  66. }
  67. public Stream<HolonObject> getAllHolonObjectsRecursive() {
  68. return Stream.concat(objectList.stream(),
  69. groupNodeList.stream().flatMap(GroupNode::getAllHolonObjectsRecursive));
  70. }
  71. public Stream<HolonSwitch> getAllSwitchObjectsRecursive() {
  72. return Stream.concat(switchList.stream(),
  73. groupNodeList.stream().flatMap(GroupNode::getAllSwitchObjectsRecursive));
  74. }
  75. public Stream<Node> getAllNodeObjectsRecursive() {
  76. return Stream.concat(nodeList.stream(),
  77. groupNodeList.stream().flatMap(GroupNode::getAllNodeObjectsRecursive));
  78. }
  79. public Stream<GroupNode> getAllGroupNodeObjectsRecursive() {
  80. return Stream.concat(groupNodeList.stream(),
  81. groupNodeList.stream().flatMap(GroupNode::getAllGroupNodeObjectsRecursive));
  82. }
  83. public Stream<HolonObject> getHolonObjects() {
  84. return objectList.stream();
  85. }
  86. public Stream<HolonSwitch> getSwitches() {
  87. return switchList.stream();
  88. }
  89. public Stream<Node> getNodes() {
  90. return nodeList.stream();
  91. }
  92. public Stream<GroupNode> getGroupNodes() {
  93. return groupNodeList.stream();
  94. }
  95. public Stream<HolonElement> getHolonElements(){
  96. return objectList.stream().flatMap(HolonObject::elementsStream);
  97. }
  98. public void clear() {
  99. objectList.clear();
  100. switchList.clear();
  101. nodeList.clear();
  102. groupNodeList.clear();
  103. }
  104. @Override
  105. public void postDeserialize() {
  106. getObjectsInThisLayer().forEach(obj -> obj.setGroupNode(this));
  107. }
  108. }