-
-
Notifications
You must be signed in to change notification settings - Fork 23
AsynchronousEventNotifier.java
Mark Rotteveel edited this page Jul 19, 2015
·
1 revision
import org.firebirdsql.event.EventManager;
import org.firebirdsql.event.FBEventManager;
import org.firebirdsql.event.EventListener;
import org.firebirdsql.event.DatabaseEvent;
import org.firebirdsql.gds.impl.GDSType;
import javax.swing.*;
import java.util.Date;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class AsynchronousEventNotifier implements EventListener {
private EventManager eventManager;
private JTextArea textArea;
public AsynchronousEventNotifier(String database,
String user, String pass){
eventManager = new FBEventManager(GDSType.getType("PURE_JAVA"));
eventManager.setDatabase(database);
eventManager.setUser(user);
eventManager.setPassword(pass);
}
public void run(String [] eventNames){
buildGui();
try {
eventManager.connect();
for (int i = 0; i < eventNames.length; i++){
eventManager.addEventListener(eventNames[i], this);
}
} catch (SQLException se){
throw new RuntimeException(se);
}
}
private void buildGui(){
JFrame frame = new JFrame("Asynchronous Event Notifier");
JPanel panel = new JPanel();
JButton clearButton = new JButton("Clear");
frame.getContentPane().add(panel);
panel.setLayout(new BorderLayout());
this.textArea = new JTextArea();
this.textArea.setEditable(false);
panel.add(new JScrollPane(this.textArea));
clearButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
AsynchronousEventNotifier.this.textArea.setText("");
}
});
panel.add(clearButton, BorderLayout.SOUTH);
panel.setPreferredSize(new Dimension(400, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
public void eventOccurred(DatabaseEvent event){
addText(new Date().toString()
+ event.getEventName() + " occurred "
+ event.getEventCount() + " time(s)\n");
}
private void addText(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
JTextArea textArea =
AsynchronousEventNotifier.this.textArea;
textArea.setText(textArea.getText() + text);
}
});
}
public static void main(String [] args){
if (args.length < 4){
System.err.println("Usage: java AsynchronousEventNotifier "
+ "<database path> <user> <password> <event name> "
+ "<event name>...");
System.exit(1);
}
AsynchronousEventNotifier notifier =
new AsynchronousEventNotifier(args[0], args[1], args[2]);
String[] eventNames = new String[args.length - 3];
System.arraycopy(args, 3, eventNames, 0, args.length - 3);
notifier.run(eventNames);
}
}