]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java
Introduce WrapLayout to replace FlowLayout
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / internal / awt / RecursiveContainerListener.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java
new file mode 100644 (file)
index 0000000..4e561f7
--- /dev/null
@@ -0,0 +1,101 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007 SAS Institute.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     SAS Institute - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.internal.awt;\r
+\r
+import java.awt.Component;\r
+import java.awt.Container;\r
+import java.awt.EventQueue;\r
+import java.awt.event.ContainerEvent;\r
+import java.awt.event.ContainerListener;\r
+\r
+class RecursiveContainerListener implements ContainerListener {\r
+    private final ContainerListener listener;\r
+    \r
+    RecursiveContainerListener(ContainerListener listener) {\r
+        assert listener != null;\r
+        \r
+        this.listener = listener;\r
+    }\r
+\r
+    private void handleAdd(Container source, Component c) {\r
+        assert source != null;\r
+        assert c != null;\r
+        assert listener != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+        \r
+        // System.out.println("Listening to: " + c);\r
+        listener.componentAdded(new ContainerEvent(source, ContainerEvent.COMPONENT_ADDED, c));\r
+        if (c instanceof Container) {\r
+            ((Container)c).addContainerListener(this);\r
+        }\r
+    }\r
+    \r
+    private void handleRemove(Container source, Component c) {\r
+        assert source != null;\r
+        assert c != null;\r
+        assert listener != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+\r
+        // System.out.println("Stopped Listening to: " + c);\r
+        listener.componentRemoved(new ContainerEvent(source, ContainerEvent.COMPONENT_REMOVED, c));\r
+        if (c instanceof Container) {\r
+            ((Container)c).removeContainerListener(this);\r
+        }\r
+    }\r
+    \r
+    private void handleAllAdds(Container source, Component child) {\r
+        assert source != null;\r
+        assert child != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+\r
+        if (child instanceof Container) {\r
+            Container container = (Container)child;\r
+            Component[] children = container.getComponents();\r
+            for (int i = 0; i < children.length; i++) {\r
+                handleAllAdds(container, children[i]);\r
+            }\r
+        }\r
+        handleAdd(source, child);\r
+    }\r
+    \r
+    private void handleAllRemoves(Container source, Component child) {\r
+        assert source != null;\r
+        assert child != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+\r
+        if (child instanceof Container) {\r
+            Container container = (Container)child;\r
+            Component[] children = container.getComponents();\r
+            for (int i = 0; i < children.length; i++) {\r
+                handleAllRemoves(container, children[i]);\r
+            }\r
+        }\r
+        handleRemove(source, child);\r
+\r
+    }\r
+\r
+    public void componentAdded(ContainerEvent e) {\r
+        assert e != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+        \r
+        Container source = (Container)e.getSource();\r
+        handleAllAdds(source, e.getChild());\r
+    }\r
+    \r
+    public void componentRemoved(ContainerEvent e) {\r
+        assert e != null;\r
+        assert EventQueue.isDispatchThread();    // On AWT event thread\r
+        \r
+        Container source = (Container)e.getSource();\r
+        handleAllRemoves(source, e.getChild());\r
+    }\r
+}\r
+\r