001package gwt.material.design.client.base;
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 static com.google.gwt.dom.client.BrowserEvents.CLICK;
024import gwt.material.design.client.ui.MaterialButton;
025
026import com.google.gwt.cell.client.AbstractCell;
027import com.google.gwt.cell.client.ValueUpdater;
028import com.google.gwt.dom.client.Element;
029import com.google.gwt.dom.client.EventTarget;
030import com.google.gwt.dom.client.NativeEvent;
031import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
032import com.google.gwt.user.client.DOM;
033
034public class MaterialButtonCell extends AbstractCell<MaterialButton> {
035
036    public MaterialButtonCell() {
037        super("click", "keydown");
038    }
039
040    @Override
041    public void onBrowserEvent(Context context, Element parent, MaterialButton value, NativeEvent event, ValueUpdater<MaterialButton> valueUpdater) {
042         super.onBrowserEvent(context, parent, value, event, valueUpdater);
043        if (CLICK.equals(event.getType())) {
044            EventTarget eventTarget = event.getEventTarget();
045            if (!Element.is(eventTarget)) {
046                return;
047            }
048            if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
049                // Ignore clicks that occur outside of the main element.
050                onEnterKeyDown(context, parent, value, event, valueUpdater);
051            }
052        }
053    }
054
055    @SuppressWarnings("deprecation")
056    @Override
057    public void render(Context context, MaterialButton value, SafeHtmlBuilder sb) {
058        value.getIcon().addStyleName("material-icons");
059        sb.appendHtmlConstant(DOM.toString(value.getElement()));
060    }
061
062    @Override
063    protected void onEnterKeyDown(Context context,
064                                  Element parent,
065                                  MaterialButton value,
066                                  NativeEvent event,
067                                  ValueUpdater<MaterialButton> valueUpdater) {
068        if (valueUpdater != null) {
069            valueUpdater.update(value);
070        }
071    }
072}