Java tutorial
package com.cgxlib.core; /* * #%L * CGXlib * %% * Copyright (C) 2016 CGXlib (http://www.cgxlib.com) * %% * 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. * #L% */ import com.cgxlib.xq.client.Function; import com.cgxlib.xq.client.XQ; import com.cgxlib.xq.client.js.JsUtils; import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.Timer; import java.util.HashMap; /** * */ public abstract class CGXComponentBase<V extends ViewHandler<?>> extends XQ { private static final HashMap<String, String> browserTransitionEnds = new HashMap<String, String>(); protected static String browserTransitionEnd; static { browserTransitionEnds.put("WebkitTransition", "webkitTransitionEnd"); browserTransitionEnds.put("MozTransition", "transitionend"); browserTransitionEnds.put("OTransition", "oTransitionEnd otransitionend"); browserTransitionEnds.put("transition", "transitionend"); } protected ViewHandlerFactory<? extends V> factory; protected int transitionDuration = 150; protected boolean transitions = CGXlib.transitions; protected CGXComponentBase(XQ xq, V viewHandler) { super(xq); apply(xq, viewHandler); } protected CGXComponentBase(XQ xq, ViewHandlerFactory<? extends V> factory) { super(xq); this.factory = factory; apply(xq, factory.make()); } protected boolean defaultPrevented(Event e) { boolean defaultPrevented = false; try { defaultPrevented = JsUtils.isDefaultPrevented(e); } catch (Throwable t) { } return defaultPrevented; } protected abstract void apply(XQ xq, V viewHandler); /** * Convenient method to enable tweaking the behaviour of the component that is already defined (HTML in the page), * by passing a different ViewHandler than default. * * @param plugin - the plugin to have it's initialization customizes. * @param <T> * @return */ @SuppressWarnings("unchecked") public <T extends CGXComponentBase<? extends H>, H extends ViewHandler<T>> CGXBuilder<T> builder( Class<T> plugin) { if (CGXlib.plugins != null) { CGXPlugin<T, ViewHandler<T>> p = (CGXPlugin<T, ViewHandler<T>>) CGXlib.plugins.get(plugin); if (p != null) { return new CGXBuilder(this, plugin); } } throw new RuntimeException("No CGXPlugin registered for class " + plugin.getName()); } protected String transitionEnd() { Element el = Document.get().createElement("bootstrap"); for (String name : browserTransitionEnds.keySet()) { if (el.getStyle().getProperty(name) != null) { return browserTransitionEnds.get(name); } } return null; } public CGXComponentBase<V> emulateTransitionEnd(final int duration) { defineTransitionEnd(); final XQ $el = this; final TransitionStatus status = new TransitionStatus(); Function f = new Function() { public void f() { status.done = true; } }; onceWhenTransitionEnds(f); Timer timer = new Timer() { @Override public void run() { if (!status.done) { $el.trigger(browserTransitionEnd); } } }; timer.schedule(duration); return this; } public CGXComponentBase<V> onceWhenTransitionEnds(final Function f) { defineTransitionEnd(); this.bind(browserTransitionEnd, new Function() { @Override public boolean f(final Event e, final Object... arg) { final Function wrapperF = this; Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() { @Override public void execute() { CGXComponentBase.this.unbind(browserTransitionEnd, wrapperF); f.fe(e, arg); } }); return true; } }); return this; } private void defineTransitionEnd() { if (browserTransitionEnd == null) { browserTransitionEnd = transitionEnd(); } } protected boolean transitions() { return transitions; } protected void transitions(boolean transitions) { this.transitions = transitions; } protected int transitionDuration() { return transitionDuration; } protected void transitionDuration(int transitionDuration) { this.transitionDuration = transitionDuration; } private class TransitionStatus { private boolean done; } }