001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.addins.client.avatar;
021
022import com.google.gwt.dom.client.Document;
023import gwt.material.design.addins.client.MaterialAddins;
024import gwt.material.design.addins.client.avatar.js.JsAvatar;
025import gwt.material.design.client.MaterialDesignBase;
026import gwt.material.design.client.base.AbstractValueWidget;
027import gwt.material.design.client.base.JsLoader;
028
029//@formatter:off
030
031/**
032 * Generated avatar based on @link(https://jdenticon.com/)
033 * provides a unique avatar based on unique name.
034 * <p>
035 * <h3>XML Namespace Declaration</h3>
036 * <pre>
037 * {@code
038 * xmlns:ma='urn:import:gwt.material.design.addins.client'
039 * }
040 * </pre>
041 * <p>
042 * <h3>UiBinder Usage:</h3>
043 * <pre>
044 * {@code
045 *
046 * <ma:avatar.MaterialAvatar name="kevzlou7979" width="80" height="80"/>
047 *
048 * }
049 * </pre>
050 *
051 * @author kevzlou7979
052 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#avatar">Material Avatar</a>
053 * @see <a href="https://github.com/dmester/jdenticon">Jdenticon 1.3.2</a>
054 */
055//@formatter:on
056public class MaterialAvatar extends AbstractValueWidget<String> implements JsLoader {
057
058    static {
059        if (MaterialAddins.isDebug()) {
060            MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs());
061            MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.md5DebugJs());
062        } else {
063            MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.jdenticonJs());
064            MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.md5Js());
065        }
066    }
067
068    private String value;
069    public MaterialAvatar() {
070        super(Document.get().createCanvasElement());
071    }
072
073    public MaterialAvatar(String name) {
074        this();
075        setValue(name, false);
076    }
077
078    @Override
079    protected void onLoad() {
080        super.onLoad();
081
082        load();
083    }
084
085    @Override
086    public void load() {
087        JsAvatar.jdenticon();
088    }
089
090    @Override
091    public void unload() {}
092
093    @Override
094    public void reload() {
095        unload();
096        load();
097    }
098
099    /**
100     * Replaced by {@link MaterialAvatar#getValue()}
101     */
102    @Deprecated
103    public String getName() {
104        return getValue();
105    }
106
107    /**
108     * Replaced by {@link MaterialAvatar#setValue(Object)}
109     * @param name
110     */
111    @Deprecated
112    public void setName(String name) {
113        setValue(name, true);
114    }
115
116    @Override
117    public void setValue(String value, boolean fireEvents) {
118        super.setValue(value, fireEvents);
119        getElement().setAttribute("data-jdenticon-hash", generateHashCode(value));
120    }
121
122    @Override
123    public String getValue() {
124        return value;
125    }
126
127    @Override
128    public void setWidth(String width) {
129        getElement().setAttribute("width", width);
130    }
131
132    @Override
133    public void setHeight(String height) {
134        getElement().setAttribute("height", height);
135    }
136
137    @Override
138    public int getWidth() {
139        String width = getElement().getAttribute("width");
140        return width != null ? Integer.parseInt(width) : 0;
141    }
142
143    public int getHeight() {
144        String height = getElement().getAttribute("height");
145        return height != null ? Integer.parseInt(height) : 0;
146    }
147
148    /**
149     * Allowing to set the dimension of the Avatar component.
150     * @param width - the width dimension of the avatar without any Unit suffix (e.i 100)
151     * @param height - the height dimension of the avatar without any Unit suffix (e.i 100)
152     */
153    public void setDimension(int width, int height) {
154        setWidth(String.valueOf(width));
155        setHeight(String.valueOf(height));
156        reload();
157    }
158
159    /**
160     * Generate hash code - needed by jdenticon to generate avatar.
161     */
162    protected String generateHashCode(String value) {
163        this.value = value;
164        return JsAvatar.md5(value);
165    }
166
167
168}