2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.ui
;
18 import com
.intellij
.util
.ReflectionCache
;
22 import java
.awt
.event
.ContainerEvent
;
23 import java
.awt
.event
.ContainerListener
;
26 * Utility class for adding a specific listener to all components in a Swing component tree,
27 * with the possibility to exclude components of specific types.
31 public abstract class ComponentTreeWatcher
{
32 protected final Class
[] myControlsToIgnore
;
34 protected ComponentTreeWatcher(final Class
[] controlsToIgnore
) {
35 myControlsToIgnore
= controlsToIgnore
;
38 private final ContainerListener myContainerListener
= new ContainerListener() {
39 public void componentAdded(ContainerEvent e
) {
40 register(e
.getChild());
43 public void componentRemoved(ContainerEvent e
) {
44 unregister(e
.getChild());
48 private boolean shouldBeIgnored(Object object
) {
49 if (object
instanceof CellRendererPane
) return true;
53 for (Class aClass
: myControlsToIgnore
) {
54 if (ReflectionCache
.isAssignable(aClass
, object
.getClass())) {
61 public final void register(Component parentComponent
) {
62 if (shouldBeIgnored(parentComponent
)) {
66 if (parentComponent
instanceof Container
) {
67 Container container
= (Container
)parentComponent
;
68 for (int i
= 0; i
< container
.getComponentCount(); i
++) {
69 register(container
.getComponent(i
));
71 container
.addContainerListener(myContainerListener
);
74 processComponent(parentComponent
);
77 protected abstract void processComponent(Component parentComponent
);
79 private void unregister(Component component
) {
81 if (component
instanceof Container
) {
82 Container container
= (Container
)component
;
83 for (int i
= 0; i
< container
.getComponentCount(); i
++) {
84 unregister(container
.getComponent(i
));
86 container
.removeContainerListener(myContainerListener
);
89 unprocessComponent(component
);
92 protected abstract void unprocessComponent(Component component
);