View Javadoc
1   package net.logAnalyzer.reports;
2   
3   import java.awt.BorderLayout;
4   import java.awt.event.KeyEvent;
5   import java.awt.image.BufferedImage;
6   import java.io.FileWriter;
7   import java.io.IOException;
8   
9   import javax.swing.JComponent;
10  import javax.swing.JEditorPane;
11  import javax.swing.JScrollPane;
12  import javax.swing.JTabbedPane;
13  
14  import net.logAnalyzer.gui.config.XMLDocumentModel;
15  import net.logAnalyzer.gui.config.XMLTreeCellRenderer;
16  import net.logAnalyzer.resources.LAResourceBundle;
17  import net.logAnalyzer.utils.XMLUtils;
18  import net.logAnalyzer.utils.gui.GraphicsAttributes;
19  import net.logAnalyzer.utils.gui.treetable.JTreeTable;
20  
21  import org.w3c.dom.Document;
22  
23  /***
24   * This class is a wrapper used to encapsulate a {@link  org.w3c.dom.Document} in a
25   * {@link net.logAnalyzer.reports.LAReport} object.
26   * @author Karim REFEYTON
27   * @version 0.1
28   */
29  public class XMLReport implements LAReport {
30      /***
31       * Report name.
32       */
33      private String name = null;
34  
35      /***
36       * Wrapped document.
37       */
38      private Document document = null;
39  
40      /***
41       * Wrap the specified document in a new wrapper instance.
42       * @param name Name of the report.
43       * @param report Document to wrap as a report.
44       */
45      public XMLReport(String name, Document report) {
46          this.name = name;
47          this.document = report;
48      }
49  
50      /***
51       * Return the file extension of the report : <tt>xml</tt>.
52       * @return Report file name extension.
53       * @see LAReport#getFileExtension()
54       */
55      public String getFileExtension() {
56          return "xml";
57      }
58  
59      /***
60       * Return the name of the report.
61       * @return Report name.
62       * @see net.logAnalyzer.reports.LAReport#getName()
63       */
64      public String getName() {
65          return this.name;
66      }
67  
68      /***
69       * Set the name of the report.
70       * @param name Report name.
71       * @see net.logAnalyzer.reports.LAReport#setName(String)
72       */
73      public void setName(String name) {
74          this.name = name;
75      }
76  
77      /***
78       * Return the mime type of the report.
79       * @return Mime type of the report.
80       * @see net.logAnalyzer.reports.LAReport#getMimeType()
81       */
82      public String getMimeType() {
83          return "text/xml";
84      }
85  
86      /***
87       * Save the report with the specified filename. The filename can contain a relative or absolute
88       * path.
89       * <p>
90       * If the file exists, it is overwritten.
91       * </p>
92       * @param filename Name of the output file.
93       * @throws IOException If an I/O exception occurs.
94       * @see net.logAnalyzer.reports.LAReport#saveToFile(String)
95       */
96      public void saveToFile(String filename) throws IOException {
97          FileWriter writer = new FileWriter(filename);
98          writer.write(this.toString());
99          writer.close();
100     }
101 
102     /***
103      * Create an image from the report as a {@link BufferedImage}.
104      * <p>
105      * This implementation returns always <tt>null</tt>.
106      * </p>
107      * @param width Image width.
108      * @param height Image height.
109      * @return Image from the report; <tt>null</tt> if unsupported feature.
110      */
111     public BufferedImage createBufferedImage(int width, int height) {
112         return null;
113     }
114 
115     /***
116      * Create the GUI displaying the report as a {@link javax.swing.JLabel} component.
117      * @return GUI showing the report.
118      * @see net.logAnalyzer.reports.LAReport#createGUI()
119      */
120     public JComponent createGUI() {
121 
122         JTreeTable treeTable = new JTreeTable(new XMLDocumentModel(document));
123         treeTable.setSelectionBackground(GraphicsAttributes.BACKGROUND_COLOR);
124         treeTable.setGridColor(GraphicsAttributes.BACKGROUND_COLOR);
125         treeTable.setShowGrid(true);
126         treeTable.setTreeCellRenderer(new XMLTreeCellRenderer());
127 
128         JTabbedPane tabbedPane = new JTabbedPane();
129         tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
130         tabbedPane.addTab("Design", LAResourceBundle.getIcon("XMLConfigView.icon"),
131                 new JScrollPane(treeTable), "Design View");
132         tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
133 
134         JEditorPane area;
135         try {
136             area = new JEditorPane("text/plain", XMLUtils.toString(document));
137         } catch (IOException e) {
138             // logger.error(e.getMessage());
139             area = new JEditorPane("text/plain", "Error during the reading phase");
140         }
141         area.setName(this.name);
142         area.setEditable(false);
143         tabbedPane.addTab("Source", LAResourceBundle.getIcon("XMLConfigView.icon.text"),
144                 new JScrollPane(area), "Source View");
145         tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
146         tabbedPane.setName(getName());
147 
148         return tabbedPane;
149     }
150 
151     /***
152      * Serialize the wrapped document to a string.
153      * @return String serialization of the wrapped document, or <tt>null</tt> if an
154      *         {@link IOException} occurs.
155      * @see net.logAnalyzer.reports.LAReport#toString()
156      * @see XMLUtils#toString(Document)
157      */
158     public String toString() {
159         try {
160             return XMLUtils.toString(document);
161         } catch (IOException ioe) {
162             return null;
163         }
164     }
165 }