/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.util;

import com.intellij.openapi.util.Getter;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Arrays;
import java.util.EventListener;

import static com.intellij.util.ui.UIUtil.uiTraverser;

/**
 * Pushes events down the UI components hierarchy.
 *
 * @author gregsh
 */
public class ComponentTreeEventDispatcher<T extends EventListener> {

  private final Class<T> myListenerClass;
  private final T myMulticaster;

  public static <T extends EventListener> ComponentTreeEventDispatcher<T> create(@NotNull Class<T> listenerClass) {
    return new ComponentTreeEventDispatcher<T>(listenerClass);
  }

  private ComponentTreeEventDispatcher(@NotNull Class<T> listenerClass) {
    myListenerClass = listenerClass;
    myMulticaster = EventDispatcher.createMulticaster(listenerClass, new Getter<Iterable<T>>() {
      @Override
      public Iterable<T> get() {
        return uiTraverser(null)
          .withRoots(Arrays.asList(Window.getWindows()))
          .postOrderDfsTraversal()
          .filter(myListenerClass);
      }
    });
  }

  @NotNull
  public T getMulticaster() {
    return myMulticaster;
  }

}

@@ -674,41 +674,137 @@
 ort 
-org.jetbrains.annotations.NotNull
+com.intellij.util.containers.JBTreeTraverser;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0Ai
@@ -1273,24 +1273,211 @@
 nerClass) %7B%0A
+    return create(null, listenerClass);%0A  %7D%0A%0A  public static %3CT extends EventListener%3E ComponentTreeEventDispatcher%3CT%3E create(@Nullable Component root, @NotNull Class%3CT%3E listenerClass) %7B%0A
     return n
@@ -1511,16 +1511,22 @@
 cher%3CT%3E(
+root, 
 listener
@@ -1577,16 +1577,48 @@
 patcher(
+@Nullable final Component root, 
 @NotNull
@@ -1835,22 +1835,54 @@
         
-return
+JBTreeTraverser%3CComponent%3E traverser =
  uiTrave
@@ -1890,13 +1890,14 @@
 ser(
-null)
+root);
 %0A   
@@ -1901,18 +1901,55 @@
         
-  
+if (root == null) traverser = traverser
 .withRoo
@@ -1986,16 +1986,17 @@
 dows()))
+;
 %0A       
@@ -1996,18 +1996,32 @@
         
-  
+return traverser
 .postOrd
@@ -2040,19 +2040,8 @@
 al()
-%0A          
 .fil

introduce optional root component