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;
    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  import org.apache.log4j.chainsaw.prefs.LoadSettingsEvent;
    27  import org.apache.log4j.chainsaw.prefs.SaveSettingsEvent;
    28  import org.apache.log4j.chainsaw.prefs.SettingsListener;
    29  import org.apache.log4j.chainsaw.prefs.SettingsManager;
    30
    31  /**
    32   * Helper class that helps delegate the work of loading and saving the  values
    33   * of the ApplicationPreferenceModel, allowing that class to remain a simple
    34   * bean.
    35   *
    36   * The Model passed to this class' constructor is the instance of the ApplicationPreference
    37   * that will be saved, and will have properties modified by loading from the
    38   * 'chainsaw.settings.xml' file in the .chainsaw directory of the user's home directory.
    39   *
    40   * @author psmith
    41   *
    42   */
    43  public class ApplicationPreferenceModelSaver implements SettingsListener {
    44
    45      private final ApplicationPreferenceModel model;
    46
    47      /**
    48       * @param model
    49       */
    50      public ApplicationPreferenceModelSaver(final ApplicationPreferenceModel model) {
    51          this.model = model;
    52      }
    53
    54      public void loadSettings(LoadSettingsEvent event) {
    55          XStream stream = new XStream(new DomDriver());
    56          File file = getApplicationPreferenceXMLFile(SettingsManager.getInstance().getSettingsDirectory());
    57          try {
    58              if (file.exists()) {
    59                  FileReader reader = new FileReader(file);
    60                  ApplicationPreferenceModel loadedModel = (ApplicationPreferenceModel) stream
    61                          .fromXML(reader);
    62                  model.apply(loadedModel);
    63                  reader.close();
    64              }
    65          } catch (Exception e) {
    66  //            TODO exception handling
    67              e.printStackTrace();
    68              //unable to process - delete file
    69              file.delete();
    70          }
    71      }
    72
    73      public void saveSettings(SaveSettingsEvent event) {
    74          XStream stream = new XStream(new DomDriver());
    75          try {
    76              File file = getApplicationPreferenceXMLFile(event.getSettingsLocation());
    77              FileWriter writer = new FileWriter(file);
    78              stream.toXML(model, writer);
    79              writer.close();
    80          } catch (Exception e) {
    81  //            TODO exception handling
    82              e.printStackTrace();
    83          }
    84
    85      }
    86
    87      private File getApplicationPreferenceXMLFile(File settingsLocation) {
    88          return new File(settingsLocation, "chainsaw.settings.xml");
    89      }
    90
    91  }