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  package org.apache.log4j.chainsaw.prefs;
18  
19  import java.io.Reader;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.EventListener;
24  import java.util.List;
25  
26  import javax.swing.event.ChangeEvent;
27  import javax.swing.event.ChangeListener;
28  import javax.swing.event.EventListenerList;
29  
30  import com.thoughtworks.xstream.XStream;
31  import com.thoughtworks.xstream.io.xml.DomDriver;
32  
33  public class MRUFileList{
34  
35      private static MRUFileList log4jList = new MRUFileList();
36      private static final int DEFAULT_MRU_SIZE = 5;
37      
38      private List fileList = new ArrayList();
39      private int size = DEFAULT_MRU_SIZE;
40      
41      private static transient EventListenerList listeners = new EventListenerList();
42      
43      private MRUFileList() {
44          
45      }
46      
47      public static void addChangeListener(ChangeListener listener){
48          listeners.add(ChangeListener.class, listener);
49      }
50      public static void removeChangeListener(ChangeListener listener) {
51          listeners.remove(ChangeListener.class, listener);
52      }
53      
54      /**
55       * Call this method when something opens a log file, this method
56       * adds the URL to the list of known URL's, automatically 
57       * rolling the list to ensure the list maintains the
58       * size property
59       * 
60       * 
61       * @param url
62       */
63      public void opened(URL url) {
64          // first remove any existence of the URL already, make sure we don't have dupes
65          fileList.remove(url);
66          // now make sure we obey the size property,  leaving room for 1 more to be added at the front
67          while(fileList.size()>=size) {
68              fileList.remove(fileList.size()-1);
69          }
70          fileList.add(0, url);
71          fireChangeEvent();
72      }
73      
74      private static void fireChangeEvent() {
75          
76          ChangeEvent event = null;
77          EventListener[] eventListeners = listeners.getListeners(ChangeListener.class);
78          for (int i = 0; i < eventListeners.length; i++) {
79              ChangeListener listener = (ChangeListener) eventListeners[i];
80              if(event==null) {
81                  event = new ChangeEvent(MRUFileList.class);
82              }
83              listener.stateChanged(event);
84          }
85      }
86  
87      /**
88       * Returns an <b>unmodifiable</b> List of the MRU opened file list within Chainsaw
89       * 
90       * @return
91       */
92      public List getMRUList() {
93          return Collections.unmodifiableList(fileList);
94      }
95  
96      public static MRUFileList log4jMRU() {
97          return log4jList;
98      }
99      
100     public static void loadLog4jMRUListFromXML(String xml) {
101         XStream xstream = new XStream(new DomDriver());
102         log4jList = (MRUFileList) xstream.fromXML(xml);
103         fireChangeEvent();
104     }
105     public static void loadLog4jMRUListFromReader(Reader reader) {
106         XStream xstream = new XStream(new DomDriver());
107         log4jList = (MRUFileList) xstream.fromXML(reader);
108         fireChangeEvent();
109         
110     }
111         // TODO Auto-generated method stub
112 }