package org.mote.wiimote.whiteboard.util;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

import javax.swing.JOptionPane;

import org.jdesktop.application.Application;
import org.mote.wiimote.whiteboard.WiimoteDataHandler;
import org.mote.wiimote.whiteboard.WiimoteWhiteboard;

import com.intel.bluetooth.NotSupportedIOException;

public class WiiRemoteJErrorHandler extends Handler {
	
	public WiiRemoteJErrorHandler(WiimoteDataHandler dh) {
		setLevel(Level.SEVERE);
	}
	
	@Override
	public void close() throws SecurityException {
	}

	@Override
	public void flush() {
	}

	@Override
	public void publish(LogRecord record) {
		final Throwable thrown = record.getThrown();
		if (thrown != null) {
			thrown.printStackTrace();
			if (thrown.getCause() instanceof NotSupportedIOException) {
				
				String msg = thrown.getMessage() + "\n" + thrown.getCause().getMessage();
				JOptionPane.showMessageDialog(null, msg, WiimoteWhiteboard.getProperty("id"), JOptionPane.ERROR_MESSAGE);
				Application.getInstance().exit();
			}
		}
	}

}
