/*
 * Copyright 2012-2015 JetBrains s.r.o
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.jetpad.base.edt;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Timer;
import jetbrains.jetpad.base.Registration;

import java.util.logging.Level;
import java.util.logging.Logger;

public final class JsEventDispatchThread implements EventDispatchThread {
  private static final Logger LOG = Logger.getLogger(JsEventDispatchThread.class.getName());
  public static final JsEventDispatchThread INSTANCE = new JsEventDispatchThread();

  private JsEventDispatchThread() {
  }

  @Override
  public void schedule(final Runnable r) {
    Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
      @Override
      public void execute() {
        try {
          r.run();
        } catch (Throwable t) {
          LOG.log(Level.SEVERE, "Runnable submitted to JsEventDispatchThread failed", t);
        }
      }
    });
  }

  @Override
  public Registration schedule(int delay, final Runnable r) {
    final Timer timer = new Timer() {
      @Override
      public void run() {
        try {
          r.run();
        } catch (Throwable t) {
          LOG.log(Level.SEVERE, "Runnable submitted to JsEventDispatchThread failed", t);
        }
      }
    };
    timer.schedule(delay);
    return timerReg(timer);
  }

  @Override
  public Registration scheduleRepeating(int period, final Runnable r) {
    final Timer timer = new Timer() {
      @Override
      public void run() {
        try {
          r.run();
        } catch (Throwable t) {
          LOG.log(Level.SEVERE, "Runnable submitted to JsEventDispatchThread failed", t);
        }
      }
    };
    timer.scheduleRepeating(period);
    return timerReg(timer);
  }

  private Registration timerReg(final Timer timer) {
    return new Registration() {
      @Override
      protected void doRemove() {
        if (timer.isRunning()) {
          timer.cancel();
        }
      }
    };
  }
}

@@ -760,25 +760,24 @@
 ration;%0A
-%0A
 import j
 ava.util
@@ -772,63 +772,46 @@
 rt j
-ava.util.logging.Level;%0Aimport java.util.logging.Logg
+etbrains.jetpad.base.ThrowableHandl
 er
+s
 ;%0A%0Ap
@@ -887,101 +887,8 @@
 d %7B%0A
-  private static final Logger LOG = Logger.getLogger(JsEventDispatchThread.class.getName());%0A
   pu
@@ -1260,84 +1260,33 @@
     
-LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, 
+ThrowableHandlers.handle(
 t);%0A
@@ -1549,84 +1549,33 @@
     
-LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, 
+ThrowableHandlers.handle(
 t);%0A
@@ -1902,84 +1902,33 @@
     
-LOG.log(Level.SEVERE, %22Runnable submitted to JsEventDispatchThread failed%22, 
+ThrowableHandlers.handle(
 t);%0A

use ThrowableHandlers at JsEDT instead of just logging