tabs: fixing invalidation on tab property changes
[fedora-idea.git] / platform / platform-api / src / com / intellij / ui / ComponentTreeWatcher.java
blob7269f977489763ddf955a57891c5631df1979073
1 /*
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;
20 import javax.swing.*;
21 import java.awt.*;
22 import java.awt.event.ContainerEvent;
23 import java.awt.event.ContainerListener;
25 /**
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.
29 * @since 5.1
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;
50 if (object == null) {
51 return true;
53 for (Class aClass : myControlsToIgnore) {
54 if (ReflectionCache.isAssignable(aClass, object.getClass())) {
55 return true;
58 return false;
61 public final void register(Component parentComponent) {
62 if (shouldBeIgnored(parentComponent)) {
63 return;
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);