gwt.material.design.client.ui.MaterialFAB Maven / Gradle / Ivy
/*
* #%L
* GwtMaterial
* %%
* Copyright (C) 2015 - 2017 GwtMaterialDesign
* %%
* 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%
*/
package gwt.material.design.client.ui;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.logical.shared.*;
import com.google.gwt.event.shared.HandlerRegistration;
import gwt.material.design.client.base.HasAxis;
import gwt.material.design.client.base.HasOpenClose;
import gwt.material.design.client.base.HasType;
import gwt.material.design.client.base.MaterialWidget;
import gwt.material.design.client.base.mixin.CssNameMixin;
import gwt.material.design.client.base.mixin.CssTypeMixin;
import gwt.material.design.client.constants.Axis;
import gwt.material.design.client.constants.CssName;
import gwt.material.design.client.constants.FABType;
import static gwt.material.design.client.js.JsMaterialElement.$;
//@formatter:off
/**
* Floating action buttons are used for a promoted action. They
* are distinguished by a circled icon floating above the UI and
* have motion behaviors that include morphing, launching, and a
* transferring anchor point.
*
*
UiBinder Usage:
*
* {@code
*
*
*
*
*
*
*
* }
*
*
*
* @author kevzlou7979
* @see Material FAB
* @see Material Design Specification
*/
//@formatter:on
public class MaterialFAB extends MaterialWidget implements HasType, HasAxis, HasCloseHandlers,
HasOpenHandlers, HasOpenClose {
private CssTypeMixin typeMixin;
private CssNameMixin axisMixin;
public MaterialFAB() {
super(Document.get().createDivElement(), CssName.FIXED_ACTION_BTN);
}
@Override
protected void onLoad() {
super.onLoad();
if (getType() == FABType.CLICK_ONLY) {
registerHandler(addClickHandler(clickEvent -> {
if (isEnabled()) {
if (!isOpen()) {
open();
} else {
close();
}
}
}));
} else {
registerHandler(addMouseOverHandler(mouseOverEvent -> OpenEvent.fire(this, this)));
registerHandler(addMouseOutHandler(mouseOutEvent -> CloseEvent.fire(this, this)));
}
}
/**
* Open the FAB programmatically
*/
@Override
public void open() {
open(true);
}
/**
* Open the FAB programmatically
*
* @param fireEvent flag whether this component fires Open Event
*/
public void open(boolean fireEvent) {
if (fireEvent) {
OpenEvent.fire(this, this);
}
$(getElement()).openFAB();
}
/**
* Close the FAB programmatically
*/
@Override
public void close() {
close(true);
}
/**
* Close the FAB programmatically
*
* @param fireEvent flag whether this component fires Close Event
*/
public void close(boolean fireEvent) {
if (fireEvent) {
CloseEvent.fire(this, this);
}
$(getElement()).closeFAB();
}
@Override
public boolean isOpen() {
return getElement().hasClassName(CssName.ACTIVE);
}
@Override
public void setType(FABType type) {
getTypeMixin().setType(type);
}
@Override
public FABType getType() {
return getTypeMixin().getType();
}
@Override
public void setAxis(Axis axis) {
getAxisMixin().setCssName(axis);
}
@Override
public Axis getAxis() {
return getAxisMixin().getCssName();
}
@Override
public HandlerRegistration addCloseHandler(CloseHandler handler) {
return addHandler(handler, CloseEvent.getType());
}
@Override
public HandlerRegistration addOpenHandler(OpenHandler handler) {
return addHandler(handler, OpenEvent.getType());
}
protected CssTypeMixin getTypeMixin() {
if (typeMixin == null) {
typeMixin = new CssTypeMixin<>(this);
}
return typeMixin;
}
protected CssNameMixin getAxisMixin() {
if (axisMixin == null) {
axisMixin = new CssNameMixin<>(this);
}
return axisMixin;
}
}