]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java
Merge commit '876ede6'
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / internal / awt / RecursiveContainerListener.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 SAS Institute.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     SAS Institute - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.utils.ui.internal.awt;\r
12 \r
13 import java.awt.Component;\r
14 import java.awt.Container;\r
15 import java.awt.EventQueue;\r
16 import java.awt.event.ContainerEvent;\r
17 import java.awt.event.ContainerListener;\r
18 \r
19 class RecursiveContainerListener implements ContainerListener {\r
20     private final ContainerListener listener;\r
21     \r
22     RecursiveContainerListener(ContainerListener listener) {\r
23         assert listener != null;\r
24         \r
25         this.listener = listener;\r
26     }\r
27 \r
28     private void handleAdd(Container source, Component c) {\r
29         assert source != null;\r
30         assert c != null;\r
31         assert listener != null;\r
32         assert EventQueue.isDispatchThread();    // On AWT event thread\r
33         \r
34         // System.out.println("Listening to: " + c);\r
35         listener.componentAdded(new ContainerEvent(source, ContainerEvent.COMPONENT_ADDED, c));\r
36         if (c instanceof Container) {\r
37             ((Container)c).addContainerListener(this);\r
38         }\r
39     }\r
40     \r
41     private void handleRemove(Container source, Component c) {\r
42         assert source != null;\r
43         assert c != null;\r
44         assert listener != null;\r
45         assert EventQueue.isDispatchThread();    // On AWT event thread\r
46 \r
47         // System.out.println("Stopped Listening to: " + c);\r
48         listener.componentRemoved(new ContainerEvent(source, ContainerEvent.COMPONENT_REMOVED, c));\r
49         if (c instanceof Container) {\r
50             ((Container)c).removeContainerListener(this);\r
51         }\r
52     }\r
53     \r
54     private void handleAllAdds(Container source, Component child) {\r
55         assert source != null;\r
56         assert child != null;\r
57         assert EventQueue.isDispatchThread();    // On AWT event thread\r
58 \r
59         if (child instanceof Container) {\r
60             Container container = (Container)child;\r
61             Component[] children = container.getComponents();\r
62             for (int i = 0; i < children.length; i++) {\r
63                 handleAllAdds(container, children[i]);\r
64             }\r
65         }\r
66         handleAdd(source, child);\r
67     }\r
68     \r
69     private void handleAllRemoves(Container source, Component child) {\r
70         assert source != null;\r
71         assert child != null;\r
72         assert EventQueue.isDispatchThread();    // On AWT event thread\r
73 \r
74         if (child instanceof Container) {\r
75             Container container = (Container)child;\r
76             Component[] children = container.getComponents();\r
77             for (int i = 0; i < children.length; i++) {\r
78                 handleAllRemoves(container, children[i]);\r
79             }\r
80         }\r
81         handleRemove(source, child);\r
82 \r
83     }\r
84 \r
85     public void componentAdded(ContainerEvent e) {\r
86         assert e != null;\r
87         assert EventQueue.isDispatchThread();    // On AWT event thread\r
88         \r
89         Container source = (Container)e.getSource();\r
90         handleAllAdds(source, e.getChild());\r
91     }\r
92     \r
93     public void componentRemoved(ContainerEvent e) {\r
94         assert e != null;\r
95         assert EventQueue.isDispatchThread();    // On AWT event thread\r
96         \r
97         Container source = (Container)e.getSource();\r
98         handleAllRemoves(source, e.getChild());\r
99     }\r
100 }\r
101 \r