001package gwt.material.design.client.base.helper; 002 003/* 004 * #%L 005 * GwtMaterial 006 * %% 007 * Copyright (C) 2015 GwtMaterialDesign 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import com.google.gwt.event.dom.client.MouseDownEvent; 024import com.google.gwt.event.dom.client.MouseDownHandler; 025import com.google.gwt.event.dom.client.MouseOutEvent; 026import com.google.gwt.event.dom.client.MouseOutHandler; 027import com.google.gwt.event.dom.client.MouseUpEvent; 028import com.google.gwt.event.dom.client.MouseUpHandler; 029import com.google.gwt.event.dom.client.TouchCancelEvent; 030import com.google.gwt.event.dom.client.TouchCancelHandler; 031import com.google.gwt.event.dom.client.TouchEndEvent; 032import com.google.gwt.event.dom.client.TouchEndHandler; 033import com.google.gwt.event.dom.client.TouchStartEvent; 034import com.google.gwt.event.dom.client.TouchStartHandler; 035import com.google.gwt.user.client.Event; 036import com.google.gwt.user.client.Window; 037import com.google.gwt.user.client.ui.Widget; 038 039/** 040 * This static helper class is supposed to collect common methods used for multiple kind of UI classes. 041 * It is defined as abstract to prohibit 042 * 043 */ 044public final class UiHelper { 045 046 private static String PRESSED_CSS_STYLE_NAME = "pressed"; 047 048 /** 049 * Adds a mouse pressed handler to a widget. Adds a CSS style ('pressed', 050 * {@link #PRESSED_CSS_STYLE_NAME}) to the widget as long as the mouse is 051 * pressed (or the user touches the widget). See 052 * {@link #addMousePressedHandlers(Widget, String)}. 053 * 054 * @param widget The widget to which the "pressed" style musst be applied 055 */ 056 public static void addMousePressedHandlers(final Widget widget) { 057 addMousePressedHandlers(widget, PRESSED_CSS_STYLE_NAME); 058 } 059 060 /** 061 * Adds a mouse pressed handler to a widget. Adds a CSS style to the widget 062 * as long as the mouse is pressed (or the user touches the widget on mobile browser). 063 * 064 * @param widget The widget to which the style must be applied for mouse/touch event 065 * @param cssStyleName CSS style name to be applied 066 */ 067 public static void addMousePressedHandlers(final Widget widget, final String cssStyleName) { 068 widget.sinkEvents(Event.ONMOUSEDOWN); 069 widget.sinkEvents(Event.ONMOUSEUP); 070 widget.sinkEvents(Event.ONMOUSEOUT); 071 widget.sinkEvents(Event.TOUCHEVENTS); 072 073 widget.addHandler(new MouseDownHandler() { 074 @Override 075 public void onMouseDown(MouseDownEvent event) { 076 widget.addStyleName(cssStyleName); 077 } 078 }, MouseDownEvent.getType()); 079 080 widget.addHandler(new MouseUpHandler() { 081 @Override 082 public void onMouseUp(MouseUpEvent event) { 083 widget.removeStyleName(cssStyleName); 084 } 085 }, MouseUpEvent.getType()); 086 087 widget.addHandler(new MouseOutHandler() { 088 @Override 089 public void onMouseOut(MouseOutEvent event) { 090 widget.removeStyleName(cssStyleName); 091 } 092 }, MouseOutEvent.getType()); 093 094 // Touch Events 095 widget.addHandler(new TouchStartHandler() { 096 @Override 097 public void onTouchStart(TouchStartEvent event) { 098 widget.addStyleName(cssStyleName); 099 } 100 }, TouchStartEvent.getType()); 101 102 widget.addHandler(new TouchEndHandler() { 103 @Override 104 public void onTouchEnd(TouchEndEvent event) { 105 widget.removeStyleName(cssStyleName); 106 107 } 108 }, TouchEndEvent.getType()); 109 110 widget.addHandler(new TouchCancelHandler() { 111 @Override 112 public void onTouchCancel(TouchCancelEvent event) { 113 widget.removeStyleName(cssStyleName); 114 } 115 }, TouchCancelEvent.getType()); 116 } 117 118 public static int calculateSpaceToBottom(Widget widget) { 119 return Window.getClientHeight() - widget.getAbsoluteTop() - widget.getOffsetHeight(); 120 } 121}