Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Log.java
1 
16 package net.squiz.matrix.debug;
17 
18 import java.util.*;
19 import java.io.*;
20 import net.squiz.matrix.core.*;
21 import javax.swing.*;
22 import javax.swing.table.*;
23 import java.awt.event.*;
24 import java.awt.*;
25 import java.text.DateFormat;
26 
27 public class Log {
28 
29  private static Vector messages = new Vector();
30 
31  // cannot instantiate
32  private Log() {}
33 
34  public static void log(String message, Class originator) {
35  messages.add(new Message(message, originator));
36  }
37 
38  public static void log(String message, Class originator, Throwable t) {
39  messages.add(new Message(message, originator, t));
40  }
41 
42  public static class Message {
43  private String message;
44  private Class cls;
45  private Throwable t;
46  private Date date;
47 
48  public Message(String message, Class cls) {
49  this.message = message;
50  this.cls = cls;
51  date = new Date();
52  }
53 
54  public Message(String message, Class cls, Throwable t) {
55  this.message = message;
56  this.cls = cls;
57  this.t = t;
58  date = new Date();
59  }
60 
61  public String toString() {
62  return cls + " - " + message;
63  }
64 
65  public String getCls() {
66  return cls.getName();
67  }
68 
69  public String getMessage() {
70  return message;
71  }
72 
73  public Throwable getThrowable() {
74  return t;
75  }
76 
77  public Date getDate() {
78  return date;
79  }
80  }
81 
82  private static String getThrowableStackTrace(Throwable t) {
83  if (t == null)
84  return "";
85  ByteArrayOutputStream bos = new ByteArrayOutputStream();
86  PrintWriter pw = new PrintWriter(bos, true);
87  t.printStackTrace(pw);
88 
89  return bos.toString();
90  }
91 
92  private static String getMessagesAsText() {
93  Iterator iterator = messages.iterator();
94 
95  String str = "";
96  while (iterator.hasNext()) {
97  Message message = (Message) iterator.next();
98  str += message.getDate() + "\t";
99  str += message.getCls() + "\t";
100  str += message.getMessage() + "\n";
101  str += getThrowableStackTrace(message.getThrowable());
102  str += "-------------------------------------------\n";
103  }
104  return str;
105  }
106 
107  public static void openLogs() {
108  final JFrame frame = new JFrame();
109  JPanel buttonPanel = new JPanel();
110  frame.getContentPane().setLayout(new BorderLayout());
111  buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
112 
113  ActionListener exportListener = new ActionListener() {
114  public void actionPerformed(ActionEvent evt) {
115  JTextPane textPane = new JTextPane();
116  textPane.setEditable(false);
117  textPane.setText(getMessagesAsText());
118 
119  JFrame frame = new JFrame();
120  frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
121  frame.getContentPane().add(new JScrollPane(textPane));
122  frame.setSize(600, 400);
123 
124  GUIUtilities.showInScreenCenter(frame);
125  }
126  };
127 
128  ActionListener closeListener = new ActionListener() {
129  public void actionPerformed(ActionEvent evt) {
130  frame.dispose();
131  }
132  };
133 
134  JButton exportButton = new JButton(Matrix.translate("asset_map_button_export"));
135  JButton closeButton = new JButton(Matrix.translate("asset_map_button_close"));
136  exportButton.addActionListener(exportListener);
137  closeButton.addActionListener(closeListener);
138 
139  buttonPanel.add(exportButton);
140  buttonPanel.add(closeButton);
141 
142  final Message[] messagesArr = (Message[]) messages.toArray(new Message[messages.size()]);
143 
144  TableModel dataModel = new AbstractTableModel() {
145 
146  private String [] columns = {
147  Matrix.translate("asset_map_log_column_id"),
148  Matrix.translate("asset_map_log_column_date"),
149  Matrix.translate("asset_map_log_column_class"),
150  Matrix.translate("asset_map_log_column_message"),
151  Matrix.translate("asset_map_log_column_exception"),
152  };
153 
154  public int getColumnCount() {
155  return columns.length;
156  }
157  public int getRowCount() {
158  return messagesArr.length;
159  }
160  public Object getValueAt(int row, int col) {
161  switch (col) {
162  case 0:
163  return new Integer(row);
164  case 1:
165  return messagesArr[row].getDate();
166  case 2:
167  return messagesArr[row].getCls();
168  case 3:
169  return messagesArr[row].getMessage();
170  case 4:
171  if (messagesArr[row].getThrowable() != null)
172  return getThrowableStackTrace(messagesArr[row].getThrowable());
173  }
174  return null;
175  }
176 
177  public String getColumnName(int columnIndex) {
178  return columns[columnIndex];
179  }
180  };
181  JTable table = new JTable(dataModel);
182  frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
183  frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
184  frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
185  frame.setSize(400, 300);
186 
187  GUIUtilities.showInScreenCenter(frame);
188  }
189 }