16 package net.squiz.matrix.debug;
20 import net.squiz.matrix.core.*;
22 import javax.swing.table.*;
23 import java.awt.event.*;
25 import java.text.DateFormat;
29 private static Vector messages =
new Vector();
34 public static void log(String message, Class originator) {
35 messages.add(
new Message(message, originator));
38 public static void log(String message, Class originator, Throwable t) {
39 messages.add(
new Message(message, originator, t));
42 public static class Message {
43 private String message;
48 public Message(String message, Class cls) {
49 this.message = message;
54 public Message(String message, Class cls, Throwable t) {
55 this.message = message;
61 public String toString() {
62 return cls +
" - " + message;
65 public String getCls() {
69 public String getMessage() {
73 public Throwable getThrowable() {
77 public Date getDate() {
82 private static String getThrowableStackTrace(Throwable t) {
85 ByteArrayOutputStream bos =
new ByteArrayOutputStream();
86 PrintWriter pw =
new PrintWriter(bos,
true);
87 t.printStackTrace(pw);
89 return bos.toString();
92 private static String getMessagesAsText() {
93 Iterator iterator = messages.iterator();
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";
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));
113 ActionListener exportListener =
new ActionListener() {
114 public void actionPerformed(ActionEvent evt) {
115 JTextPane textPane =
new JTextPane();
116 textPane.setEditable(
false);
117 textPane.setText(getMessagesAsText());
119 JFrame frame =
new JFrame();
120 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
121 frame.getContentPane().add(
new JScrollPane(textPane));
122 frame.setSize(600, 400);
124 GUIUtilities.showInScreenCenter(frame);
128 ActionListener closeListener =
new ActionListener() {
129 public void actionPerformed(ActionEvent evt) {
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);
139 buttonPanel.add(exportButton);
140 buttonPanel.add(closeButton);
142 final Message[] messagesArr = (Message[]) messages.toArray(
new Message[messages.size()]);
144 TableModel dataModel =
new AbstractTableModel() {
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"),
154 public int getColumnCount() {
155 return columns.length;
157 public int getRowCount() {
158 return messagesArr.length;
160 public Object getValueAt(
int row,
int col) {
163 return new Integer(row);
165 return messagesArr[row].getDate();
167 return messagesArr[row].getCls();
169 return messagesArr[row].getMessage();
171 if (messagesArr[row].getThrowable() != null)
172 return getThrowableStackTrace(messagesArr[row].getThrowable());
177 public String getColumnName(
int columnIndex) {
178 return columns[columnIndex];
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);
187 GUIUtilities.showInScreenCenter(frame);