001package gwt.material.design.client.base.mixin;
002
003/*
004 * #%L
005 * GwtBootstrap3
006 * %%
007 * Copyright (C) 2013 GwtBootstrap3
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.dom.client.AnchorElement;
024import com.google.gwt.dom.client.ButtonElement;
025import com.google.gwt.dom.client.Element;
026import com.google.gwt.dom.client.InputElement;
027import com.google.gwt.user.client.ui.Focusable;
028import com.google.gwt.user.client.ui.UIObject;
029
030/**
031 * @author Sven Jacobs
032 */
033public class FocusableMixin<T extends UIObject & Focusable> extends AbstractMixin<T> implements Focusable {
034
035    public FocusableMixin(final T uiObject) {
036        super(uiObject);
037    }
038
039    @Override
040    public int getTabIndex() {
041        return uiObject.getElement().getTabIndex();
042    }
043
044    @Override
045    public void setTabIndex(final int index) {
046        uiObject.getElement().setTabIndex(index);
047    }
048
049    @Override
050    public void setAccessKey(final char key) {
051        final Element element = uiObject.getElement();
052        final String accessKey = Character.toString(key);
053
054        if (AnchorElement.is(element)) {
055            AnchorElement.as(element).setAccessKey(accessKey);
056        } else if (ButtonElement.is(element)) {
057            ButtonElement.as(element).setAccessKey(accessKey);
058        } else if (InputElement.is(element)) {
059            InputElement.as(element).setAccessKey(accessKey);
060        }
061    }
062
063    @Override
064    public void setFocus(final boolean focused) {
065        if (focused) {
066            uiObject.getElement().focus();
067        } else {
068            uiObject.getElement().blur();
069        }
070    }
071}