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 com.thoughtworks.xstream.XStream;
20  import com.thoughtworks.xstream.io.xml.DomDriver;
21  
22  import javax.swing.event.ChangeEvent;
23  import javax.swing.event.ChangeListener;
24  import javax.swing.event.EventListenerList;
25  import java.io.Reader;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.EventListener;
30  import java.util.List;
31  
32  public class MRUFileList {
33  
34      private static MRUFileList log4jList = new MRUFileList();
35      private static final int DEFAULT_MRU_SIZE = 5;
36  
37      private List<URL> fileList = new ArrayList<>();
38      private int size = DEFAULT_MRU_SIZE;
39  
40      private static transient EventListenerList listeners = new EventListenerList();
41  
42      private MRUFileList() {
43  
44      }
45  
46      public static void addChangeListener(ChangeListener listener) {
47          listeners.add(ChangeListener.class, listener);
48      }
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       * @param url
61       */
62      public void opened(URL url) {
63          // first remove any existence of the URL already, make sure we don't have dupes
64          fileList.remove(url);
65          // now make sure we obey the size property,  leaving room for 1 more to be added at the front
66          while (fileList.size() >= size) {
67              fileList.remove(fileList.size() - 1);
68          }
69          fileList.add(0, url);
70          fireChangeEvent();
71      }
72  
73      private static void fireChangeEvent() {
74  
75          ChangeEvent event = null;
76          EventListener[] eventListeners = listeners.getListeners(ChangeListener.class);
77          for (EventListener eventListener : eventListeners) {
78              ChangeListener listener = (ChangeListener) eventListener;
79              if (event == null) {
80                  event = new ChangeEvent(MRUFileList.class);
81              }
82              listener.stateChanged(event);
83          }
84      }
85  
86      /**
87       * Returns an <b>unmodifiable</b> List of the MRU opened file list within Chainsaw
88       *
89       * @return
90       */
91      public List getMRUList() {
92          return Collections.unmodifiableList(fileList);
93      }
94  
95      public static MRUFileList log4jMRU() {
96          return log4jList;
97      }
98  
99      public static void loadLog4jMRUListFromXML(String xml) {
100         XStream xstream = new XStream(new DomDriver());
101         log4jList = (MRUFileList) xstream.fromXML(xml);
102         fireChangeEvent();
103     }
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 }