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