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 java.io.File;
23  import java.io.FileReader;
24  import java.io.FileWriter;
25  
26  /**
27   * Loads/Saves the MRU lists from preferences
28   *
29   * @author psmith
30   */
31  public class MRUFileListPreferenceSaver implements SettingsListener {
32  
33      private static final MRUFileListPreferenceSaver instance = new MRUFileListPreferenceSaver();
34  
35      public static final MRUFileListPreferenceSaver getInstance() {
36          return instance;
37      }
38  
39      private MRUFileListPreferenceSaver() {
40      }
41  
42      public void loadSettings(LoadSettingsEvent event) {
43          File file = getMRULocation(SettingsManager.getInstance().getSettingsDirectory());
44          if (file.exists()) {
45              try {
46                  MRUFileList.loadLog4jMRUListFromReader(new FileReader(file));
47              } catch (Exception e) {
48  //                TODO exception handling
49                  e.printStackTrace();
50              }
51          }
52  
53      }
54  
55      public void saveSettings(SaveSettingsEvent event) {
56          XStream stream = new XStream(new DomDriver());
57          try {
58              File file = getMRULocation(event.getSettingsLocation());
59              System.out.println("Writing MRU ->" + file.getAbsolutePath());
60              FileWriter writer = new FileWriter(file);
61              stream.toXML(MRUFileList.log4jMRU(), writer);
62              writer.close();
63          } catch (Exception e) {
64  //            TODO exception handling
65              e.printStackTrace();
66          }
67      }
68  
69      private File getMRULocation(File dir) {
70          File file = new File(dir, "mru.xml");
71          return file;
72      }
73  
74  }