001package gwt.material.design.addins.client.avatar; 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 com.google.gwt.dom.client.Document; 024import gwt.material.design.addins.client.MaterialAddins; 025import gwt.material.design.addins.client.dnd.MaterialDndClientBundle; 026import gwt.material.design.addins.client.dnd.MaterialDndDebugClientBundle; 027import gwt.material.design.client.MaterialDesignBase; 028import gwt.material.design.client.base.MaterialWidget; 029 030//@formatter:off 031 032/** 033 * Generated avatar based on @link(https://jdenticon.com/) 034 * provides a unique avatar based on unique name. 035 * 036 * <h3>XML Namespace Declaration</h3> 037 * <pre> 038 * {@code 039 * xmlns:ma='urn:import:gwt.material.design.addins.client' 040 * } 041 * </pre> 042 * 043 * <h3>UiBinder Usage:</h3> 044 * <pre> 045 * {@code 046 * 047 * <ma:avatar.MaterialAvatar name="kevzlou7979" width="80" height="80"/> 048 * 049 * } 050 * </pre> 051 * 052 * @author kevzlou7979 053 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#avatar">Material Avatar</a> 054 */ 055//@formatter:on 056public class MaterialAvatar extends MaterialWidget { 057 058 static { 059 if(MaterialAddins.isDebug()) { 060 MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs()); 061 MaterialDesignBase.injectDebugJs(MaterialAvatarDebugClientBundle.INSTANCE.jdenticonDebugJs()); 062 } else { 063 MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.jdenticonJs()); 064 MaterialDesignBase.injectJs(MaterialAvatarClientBundle.INSTANCE.md5Js()); 065 } 066 067 } 068 069 private String name; 070 071 public MaterialAvatar() { 072 super(Document.get().createCanvasElement()); 073 } 074 075 public MaterialAvatar(String name) { 076 this(); 077 setName(name); 078 } 079 080 @Override 081 protected void onLoad() { 082 super.onLoad(); 083 if(getName() != null) { 084 initialize(); 085 } 086 } 087 088 /** 089 * Get the name of the avatar 090 * @return 091 */ 092 public String getName() { 093 return name; 094 } 095 096 /** 097 * Set the name of the avatar and hashed it using md5 js library to 098 * pass it into jdenticon avatar process 099 * @param name 100 */ 101 public void setName(String name) { 102 this.name = name; 103 } 104 105 @Override 106 public void setWidth(String width) { 107 getElement().setAttribute("width", width); 108 } 109 110 @Override 111 public void setHeight(String height) { 112 getElement().setAttribute("height", height); 113 } 114 115 /** 116 * Generate hash code - needed by jdenticon to generate avatar 117 * @param value 118 * @return 119 */ 120 protected native String generateHashCode(String value) /*-{ 121 return $wnd.md5(value); 122 }-*/; 123 124 /** 125 * Initialize the avatar process - useful when trying to update your avatar 126 */ 127 public void initialize() { 128 getElement().setAttribute("data-jdenticon-hash", generateHashCode(getName())); 129 update(); 130 } 131 132 protected native void update() /*-{ 133 $wnd.jdenticon(); 134 }-*/; 135}