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    /***
63     * Notifies all the registered listeners that a new unique
64     * logger name has been added to this model
65     * @param loggerName
66     */
67    private void notifyListeners(String loggerName)
68    {
69      LoggerNameListener[] eventListeners = (LoggerNameListener[]) listenerList.getListeners(LoggerNameListener.class);
70  
71      for (int i = 0; i < eventListeners.length; i++)
72      {
73        LoggerNameListener listener = eventListeners[i];
74        listener.loggerNameAdded(loggerName);
75      }    
76    }
77  
78    /* (non-Javadoc)
79     * @see org.apache.log4j.chainsaw.LoggerNameModel#addLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
80     */
81    public void addLoggerNameListener(LoggerNameListener l) {
82      listenerList.add(LoggerNameListener.class, l);
83    }
84  
85    /* (non-Javadoc)
86     * @see org.apache.log4j.chainsaw.LoggerNameModel#removeLoggerNameListener(org.apache.log4j.chainsaw.LoggerNameListener)
87     */
88    public void removeLoggerNameListener(LoggerNameListener l) {
89      listenerList.remove(LoggerNameListener.class, l);
90    }
91  }