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