View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   */
20  package org.apache.log4j.chainsaw;
21  
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import javax.swing.event.EventListenerList;
28  
29  
30  /**
31   * An implementation of LoggerNameModel which can be used as a delegate
32   * 
33   * @author Paul Smith <psmith@apache.org>
34   */
35  public class LoggerNameModelSupport implements LoggerNameModel {
36    
37    private Set loggerNameSet = new HashSet();
38    private EventListenerList listenerList = new EventListenerList();
39    
40  
41    /* (non-Javadoc)
42     * @see org.apache.log4j.chainsaw.LoggerNameModel#getLoggerNames()
43     */
44    public Collection getLoggerNames() {
45      return Collections.unmodifiableSet(loggerNameSet);
46    }
47  
48    /* (non-Javadoc)
49     * @see org.apache.log4j.chainsaw.LoggerNameModel#addLoggerName(java.lang.String)
50     */
51    public boolean addLoggerName(String loggerName) {
52      boolean isNew = loggerNameSet.add(loggerName);
53      
54      if(isNew)
55      {
56        notifyListeners(loggerName);
57      }
58      
59      return isNew;
60    }
61  
62    public void reset() {
63        loggerNameSet.clear();
64        LoggerNameListener[] eventListeners = (LoggerNameListener[]) listenerList.getListeners(LoggerNameListener.class);
65  
66        for (int i = 0; i < eventListeners.length; i++)
67        {
68          LoggerNameListener listener = eventListeners[i];
69          listener.reset();
70        }
71    }
72  
73    /**
74     * Notifies all the registered listeners that a new unique
75     * logger name has been added to this model
76     * @param loggerName
77     */
78    private void notifyListeners(String loggerName)
79    {
80      LoggerNameListener[] eventListeners = (LoggerNameListener[]) listenerList.getListeners(LoggerNameListener.class);
81  
82      for (int i = 0; i < eventListeners.length; i++)
83      {
84        LoggerNameListener listener = eventListeners[i];
85        listener.loggerNameAdded(loggerName);
86      }    
87    }
88  
89    /* (non-Javadoc)
90     * @see org.apache.log4j.chainsaw.LoggerNameModel#addLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
91     */
92    public void addLoggerNameListener(LoggerNameListener l) {
93      listenerList.add(LoggerNameListener.class, l);
94    }
95  
96    /* (non-Javadoc)
97     * @see org.apache.log4j.chainsaw.LoggerNameModel#removeLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
98     */
99    public void removeLoggerNameListener(LoggerNameListener l) {
100     listenerList.remove(LoggerNameListener.class, l);
101   }
102 }