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  
18  package org.apache.log4j.chainsaw;
19  
20  import java.awt.event.ActionEvent;
21  import java.awt.event.InputEvent;
22  import java.awt.event.KeyEvent;
23  import java.io.BufferedWriter;
24  import java.io.File;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.io.PrintWriter;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import javax.swing.AbstractAction;
32  import javax.swing.Action;
33  import javax.swing.ImageIcon;
34  import javax.swing.JFileChooser;
35  import javax.swing.KeyStroke;
36  
37  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
38  import org.apache.log4j.spi.LoggingEvent;
39  import org.apache.log4j.xml.XMLLayout;
40  
41  
42  /***
43   * Allows the user to specify a particular file to which the current tab's
44   * displayed events will be saved.
45   *
46   * @author Scott Deboy <sdeboy@apache.org>
47   * @author Paul Smith <psmith@apache.org>
48   * @author Stephen Pain
49   */
50  class FileSaveAction extends AbstractAction {
51    private LogUI parent;
52    private JFileChooser chooser = null;
53  
54    /***
55     * This action must have a reference to a LogUI
56     * in order to retrieve events to save
57     *
58     */
59    public FileSaveAction(LogUI parent) {
60      super("Save as...");
61  
62      putValue(
63        Action.ACCELERATOR_KEY,
64        KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
65      putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
66      putValue(
67        Action.SHORT_DESCRIPTION, "Saves displayed events for the current tab");
68      putValue(Action.SMALL_ICON, new ImageIcon(ChainsawIcons.FILE_SAVE_AS));
69      this.parent = parent;
70    }
71    
72    /*
73     * When the user chooses the Save action,
74     * a File chooser is presented to allow them to
75     * find an XML file to save events to.
76     *
77     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
78     */
79    public void actionPerformed(ActionEvent e) {
80  
81      if( chooser == null ){
82        chooser = new JFileChooser();
83      }
84      
85      chooser.setAcceptAllFileFilterUsed(true);
86      chooser.setDialogTitle("Save Events to XML file...");
87      chooser.showSaveDialog(parent);
88  
89      File selectedFile = chooser.getSelectedFile();
90      XMLLayout layout = new XMLLayout();
91      LoggingEvent event = null;
92      PrintWriter out = null;
93  
94      if (selectedFile != null) {
95        try {
96          List v = parent.getCurrentLogPanel().getFilteredEvents();
97  
98          if (((v != null) && (v.size() == 0)) || (v == null)) {
99            //no events to save
100           return;
101         }
102 
103         Iterator iter = v.iterator();
104 
105         out =
106           new PrintWriter(new BufferedWriter(new FileWriter(selectedFile)));
107 
108         while (iter.hasNext()) {
109           event = (LoggingEvent) iter.next();
110           layout.setLocationInfo(event.getThrowableInformation() != null);
111           out.write(layout.format(event));
112         }
113       } catch (IOException ioe) {
114         ioe.printStackTrace();
115       } finally {
116         if (out != null) {
117           out.flush();
118           out.close();
119         }
120       }
121     }
122   }
123 }