View Javadoc
1   package net.logAnalyzer.reports;
2   
3   import java.awt.Font;
4   import java.awt.image.BufferedImage;
5   import java.io.FileWriter;
6   import java.io.IOException;
7   
8   import javax.swing.JComponent;
9   import javax.swing.JScrollPane;
10  import javax.swing.JTable;
11  
12  import net.logAnalyzer.utils.gui.GraphicsAttributes;
13  import net.logAnalyzer.utils.gui.ResizableTableHeader;
14  import net.logAnalyzer.utils.gui.TXTReportTableModel;
15  
16  /***
17   * This class is a wrapper used to encapsulate a {@link  String} in a
18   * {@link net.logAnalyzer.reports.LAReport} object.
19   * 
20   * @author Karim REFEYTON
21   * @version 0.1
22   */
23  public class TXTReport implements LAReport {
24      /***
25       * Report name.
26       */
27      private String name = null;
28  
29      /***
30       * Wrapped string report.
31       */
32      private String report = null;
33  
34      /***
35       * Wrap the specified document in a new wrapper instance.
36       * 
37       * @param name
38       *            Name of the report.
39       * @param report
40       *            String to wrap as a report.
41       */
42      public TXTReport(String name, String report) {
43          this.name = name;
44          this.report = report;
45      }
46  
47      /***
48       * Return the file extension of the report : <tt>txt</tt>.
49       * 
50       * @return Report file name extension.
51       * @see LAReport#getFileExtension()
52       */
53      public String getFileExtension() {
54          return "txt";
55      }
56  
57      /***
58       * Return the name of the report.
59       * 
60       * @return Report name.
61       * @see net.logAnalyzer.reports.LAReport#getName()
62       */
63      public String getName() {
64          return this.name;
65      }
66  
67      /***
68       * Set the name of the report.
69       * 
70       * @param name
71       *            Report name.
72       * @see net.logAnalyzer.reports.LAReport#setName(String)
73       */
74      public void setName(String name) {
75          this.name = name;
76      }
77  
78      /***
79       * Return the mime type of the report.
80       * 
81       * @return Mime type of the report.
82       * @see net.logAnalyzer.reports.LAReport#getMimeType()
83       */
84      public String getMimeType() {
85          return "txt/plain";
86      }
87  
88      /***
89       * Save the report with the specified filename. The filename can contain a
90       * relative or absolute path.
91       * <p>
92       * If the file exists, it is overwritten.
93       * </p>
94       * 
95       * @param filename
96       *            Name of the output file.
97       * @throws IOException
98       *             If an I/O exception occurs.
99       * @see net.logAnalyzer.reports.LAReport#saveToFile(java.lang.String)
100      */
101     public void saveToFile(String filename) throws IOException {
102         FileWriter writer = new FileWriter(filename);
103         writer.write(this.report.toString());
104         writer.close();
105     }
106 
107     /***
108      * Create an image from the report as a {@link BufferedImage}.
109      * <p>
110      * This implementation returns always <tt>null</tt>.
111      * </p>
112      * 
113      * @param width
114      *            Image width.
115      * @param height
116      *            Image height.
117      * @return Image from the report; <tt>null</tt> if unsupported feature.
118      */
119     public BufferedImage createBufferedImage(int width, int height) {
120         return null;
121     }
122 
123     /***
124      * Create the GUI displaying the report as a {@link javax.swing.JLabel}
125      * component.
126      * 
127      * @return GUI showing the report.
128      * @see net.logAnalyzer.reports.LAReport#createGUI()
129      */
130     public JComponent createGUI() {
131         
132         JTable area = new JTable(new TXTReportTableModel(this.report.toString()));
133         ResizableTableHeader header = new ResizableTableHeader(area.getColumnModel());
134         header.setIncludeHeaderWidth(true);
135         header.setBackground(GraphicsAttributes.BACKGROUND_COLOR);
136         header.setForeground(GraphicsAttributes.QUICKTITLE_FOREGROUND_COLOR);
137         header.setFont(header.getFont().deriveFont(Font.BOLD));
138         area.setTableHeader(header);
139         JScrollPane scrollPane = new JScrollPane(area);
140         scrollPane
141             .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
142         scrollPane
143             .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
144         scrollPane.setName(getName());
145         
146         return scrollPane;
147     }
148 
149     /***
150      * Return the string representation of the report.
151      * 
152      * @return String report.
153      * @see net.logAnalyzer.reports.LAReport#toString()
154      */
155     public String toString() {
156         return this.report.toString();
157     }
158 }