001package gwt.material.design.client.ui; 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 com.google.gwt.dom.client.Element; 025import com.google.gwt.dom.client.Style; 026 027import gwt.material.design.client.base.mixin.ColorsMixin; 028import gwt.material.design.client.base.mixin.CssNameMixin; 029import gwt.material.design.client.base.mixin.ToggleStyleMixin; 030import gwt.material.design.client.constants.IconPosition; 031import gwt.material.design.client.constants.IconSize; 032import gwt.material.design.client.base.AbstractButton; 033import gwt.material.design.client.base.HasIcon; 034import gwt.material.design.client.base.HasSeparator; 035import gwt.material.design.client.constants.IconType; 036 037//@formatter:off 038 039/** 040 * We have included 740 Material Design Icons courtesy of Google. 041 * You can download them directly from the Material Design specs. 042 * 043 * <h3>UiBinder Usage:</h3> 044 * <pre> 045 *{@code <m:MaterialIcon waves="LIGHT" iconType="POLYMER"/> 046 * <m:MaterialIcon waves="LIGHT" iconType="POLYMER" textColor="blue" type="CIRCLE"/> 047 * <m:MaterialIcon waves="LIGHT" iconType="POLYMER" backgroundColor="blue" textColor="white" type="CIRCLE" tooltip="Tooltip" tooltipLocation="BOTTOM"/>} 048 * </pre> 049 * 050 * @author kevzlou7979 051 * @author Ben Dol 052 * @see <a href="http://www.google.com/design/icons/">Search Google Icons</a> 053 * @see <a href="http://gwt-material-demo.herokuapp.com/#icons">Material Icons Documentation</a> 054 */ 055//@formatter:on 056public class MaterialIcon extends AbstractButton implements HasSeparator, HasIcon { 057 058 private final CssNameMixin<MaterialIcon, IconPosition> posMixin = new CssNameMixin<>(this); 059 private final CssNameMixin<MaterialIcon, IconSize> sizeMixin = new CssNameMixin<>(this); 060 private final ToggleStyleMixin<MaterialIcon> prefixMixin = new ToggleStyleMixin<>(this, "prefix"); 061 private final ColorsMixin<MaterialIcon> colorsMixin = new ColorsMixin<>(this); 062 /** 063 * Creates an empty icon. 064 */ 065 public MaterialIcon() { 066 super("material-icons"); 067 } 068 069 /** 070 * Sets a simple icon with a given type. 071 */ 072 public MaterialIcon(IconType iconType) { 073 this(); 074 setIconType(iconType); 075 } 076 077 /** 078 * Sets an icon with textColor and backgroundColor. 079 */ 080 public MaterialIcon(IconType iconType, String textColor, String bgColor) { 081 this(); 082 setIconType(iconType); 083 setTextColor(textColor); 084 setBackgroundColor(bgColor); 085 } 086 087 public void setInnerText(String innerText) { 088 getElement().setInnerText(innerText); 089 } 090 091 @Override 092 protected Element createElement() { 093 return Document.get().createElement("i"); 094 } 095 096 @Override 097 public MaterialIcon getIcon() { 098 return this; 099 } 100 101 public IconType getIconType() { 102 return IconType.fromStyleName(getElement().getInnerText()); 103 } 104 105 @Override 106 public void setIconType(IconType icon) { 107 getElement().setInnerText(icon.getCssName()); 108 } 109 110 @Override 111 public void setIconPosition(IconPosition position) { 112 posMixin.setCssName(position); 113 } 114 115 @Override 116 public void setIconSize(IconSize size) { 117 sizeMixin.setCssName(size); 118 } 119 120 public IconSize getIconSize() { 121 return sizeMixin.getCssName(); 122 } 123 124 @Override 125 public void setIconColor(String iconColor) { 126 colorsMixin.setTextColor(iconColor); 127 } 128 129 @Override 130 public void setIconFontSize(double size, Style.Unit unit) { 131 getElement().getStyle().setFontSize(size, unit); 132 } 133 134 @Override 135 public void setIconPrefix(boolean prefix) { 136 prefixMixin.setOn(prefix); 137 } 138 139 @Override 140 public boolean isIconPrefix() { 141 return prefixMixin.isOn(); 142 } 143}