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
9 * SAS Institute - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.utils.ui.internal.awt;
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;
19 class RecursiveContainerListener implements ContainerListener {
20 private final ContainerListener listener;
22 RecursiveContainerListener(ContainerListener listener) {
23 assert listener != null;
25 this.listener = listener;
28 private void handleAdd(Container source, Component c) {
29 assert source != null;
31 assert listener != null;
32 assert EventQueue.isDispatchThread(); // On AWT event thread
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);
41 private void handleRemove(Container source, Component c) {
42 assert source != null;
44 assert listener != null;
45 assert EventQueue.isDispatchThread(); // On AWT event thread
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);
54 private void handleAllAdds(Container source, Component child) {
55 assert source != null;
57 assert EventQueue.isDispatchThread(); // On AWT event thread
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]);
66 handleAdd(source, child);
69 private void handleAllRemoves(Container source, Component child) {
70 assert source != null;
72 assert EventQueue.isDispatchThread(); // On AWT event thread
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]);
81 handleRemove(source, child);
85 public void componentAdded(ContainerEvent e) {
87 assert EventQueue.isDispatchThread(); // On AWT event thread
89 Container source = (Container)e.getSource();
90 handleAllAdds(source, e.getChild());
93 public void componentRemoved(ContainerEvent e) {
95 assert EventQueue.isDispatchThread(); // On AWT event thread
97 Container source = (Container)e.getSource();
98 handleAllRemoves(source, e.getChild());