View Javadoc

1   package com.explosion.expf.menusandtools.menu.popup;
2   
3   import java.awt.Component;
4   import java.awt.Container;
5   import java.awt.event.ContainerEvent;
6   import java.awt.event.ContainerListener;
7   
8   
9   /* =============================================================================
10   *       
11   *     Copyright 2004 Stephen Cowx
12   *
13   *     Licensed under the Apache License, Version 2.0 (the "License");
14   *     you may not use this file except in compliance with the License.
15   *     You may obtain a copy of the License at
16   *
17   *     http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *     Unless required by applicable law or agreed to in writing, software
20   *     distributed under the License is distributed on an "AS IS" BASIS,
21   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *     See the License for the specific language governing permissions and
23   *     limitations under the License.
24   * 
25   * =============================================================================
26   */
27  
28  /***
29   * @author Stephen Cowx
30   * Created on 04-Mar-2005
31   */
32  public class ExpPopupContainerListener implements ContainerListener
33  {
34  
35      private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(ExpPopupContainerListener.class);
36      
37      ExpPopupMouseListener mouseListener;
38      
39      /***
40       * Constructor, takes a mouseListener as an argument
41       * @param mouseListener
42       */
43      public ExpPopupContainerListener(ExpPopupMouseListener mouseListener)
44      {
45          this.mouseListener = mouseListener;
46      }
47      
48      /***
49       * Adds this listener to every container in the container as well
50       * as adding the mouse listener to every component in the container.
51       * @param event
52       */
53      public void componentAdded(ContainerEvent event) {
54          addListener(event.getChild());
55      }
56  
57      /***
58       * Removes this listener from each of the components
59       * in the 
60       * @see java.awt.event.ContainerListener#componentRemoved(java.awt.event.ContainerEvent)
61       * @param event
62       */
63      public void componentRemoved(ContainerEvent event) {
64          removeListener(event.getChild());
65      }
66  
67      /***
68       * Adds the mouselistener to this component as well as its children 
69       * if the component is a container then it recursively adds itself to that 
70       * container too
71       * @param c
72       */
73      private void addListener(Component c) {
74          if (c == null) { return; }
75          
76          c.addMouseListener(mouseListener);
77          
78          if (!(c instanceof Container)) { return; }
79          Container container = (Container) c; // Checks for components added 
80          container.addContainerListener(this);
81          Component[] children =  container.getComponents(); 
82          if (children != null || children.length > 0)
83          {
84  	        for   (int i=0;i<children.length;i++)
85  	        {
86  	            addListener(children[i]);
87  	        }
88          }
89      }
90      
91      /***
92       * REmoves the mouselistener from this component as well as its children 
93       * if the component is a container then it recursively removes itself from that 
94       * container too
95       * @param c
96       */
97      private void removeListener(Component c ) {
98          if (c == null) { return; }
99          
100         c.removeMouseListener(mouseListener);
101         
102         if (!(c instanceof Container)) { return; }
103         
104         Container container = (Container) c; 
105         container.removeContainerListener(this);
106         
107         Component[] children =  container.getComponents(); 
108         if (children != null || children.length > 0)
109         {
110 	        for   (int i=0;i<children.length;i++)
111 	        {
112 	            removeListener(children[i]);
113 	        }
114         }
115     }
116     
117 }