package ls.ui.msg;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import ls.util.msg.ErrorHandler;
import ls.util.msg.MessageHandler;
import ls.util.msg.MessageManager;
/**
* A swing panel that can be used to display messages in a swing UI.
* It's implemented as a message and error handler.
*
* @author Lior
* @see MessageHandler
* @see ErrorHandler
* @see MessageManager
*/
public class MessagePanel extends JPanel implements MessageHandler, ErrorHandler
{
private static final long serialVersionUID = 1L;
private JEditorPane editorPane = null;
public MessagePanel()
{
super();
initComponents();
// MessageManager.registerErrorHandler(this);
// MessageManager.registerMessageHandler(this);
}
/**
* Init the UI of the panel
*/
private void initComponents()
{
this.setLayout(new BorderLayout());
editorPane = new JEditorPane();
editorPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(editorPane);
this.add(scrollPane,BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
JButton btn = new JButton("Clear");
btn.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
editorPane.setText("");
}
});
buttonPanel.add(btn);
this.add(buttonPanel,BorderLayout.SOUTH);
}
/**
* Append a line of text to the message area
* @param line The line to add, must not be null
*/
private void appendLine(String line)
{
if (line == null) return;
String t = this.editorPane.getText();
t += line + '\n';
this.editorPane.setText(t);
this.editorPane.scrollToReference(line);
}
public void handleError(String errMsg)
{
appendLine("ERROR: " + errMsg);
}
public boolean handleException(Exception exn)
{
handleError(exn.getMessage());
return true;
}
public void handleMsg(String msg)
{
appendLine(msg);
}
}