001package org.gwtbootstrap3.client.ui.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> implements Focusable {
034
035    private final T uiObject;
036
037    public FocusableMixin(final T uiObject) {
038        this.uiObject = uiObject;
039    }
040
041    @Override
042    public int getTabIndex() {
043        return uiObject.getElement().getTabIndex();
044    }
045
046    @Override
047    public void setTabIndex(final int index) {
048        uiObject.getElement().setTabIndex(index);
049    }
050
051    @Override
052    public void setAccessKey(final char key) {
053        final Element element = uiObject.getElement();
054        final String accessKey = Character.toString(key);
055
056        if (AnchorElement.is(element)) {
057            AnchorElement.as(element).setAccessKey(accessKey);
058        } else if (ButtonElement.is(element)) {
059            ButtonElement.as(element).setAccessKey(accessKey);
060        } else if (InputElement.is(element)) {
061            InputElement.as(element).setAccessKey(accessKey);
062        }
063    }
064
065    @Override
066    public void setFocus(final boolean focused) {
067        if (focused) {
068            uiObject.getElement().focus();
069        } else {
070            uiObject.getElement().blur();
071        }
072    }
073}