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