001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.client.base; 021 022import com.google.gwt.user.client.Timer; 023 024/** 025 * A <code>Timer</code> that is canceled if a new request is made. 026 * 027 * @author Ben Dol 028 */ 029public abstract class InterruptibleTask { 030 031 private Timer timer; 032 033 /** 034 * Creates a new delayed task. 035 */ 036 public InterruptibleTask() { 037 timer = new Timer() { 038 public void run() { 039 onExecute(); 040 } 041 }; 042 } 043 044 /** 045 * Cancels the task. 046 */ 047 public void cancel() { 048 timer.cancel(); 049 } 050 051 /** 052 * Cancels any running timers and starts a new one. 053 * 054 * @param delay the delay in ms 055 */ 056 public void delay(int delay) { 057 timer.cancel(); 058 if (delay > 0) { 059 timer.schedule(delay); 060 } else { 061 timer.run(); 062 } 063 } 064 065 /** 066 * Called when the task should execute. 067 */ 068 public abstract void onExecute(); 069}