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.client.base.mixin; 021 022import com.google.gwt.user.client.ui.UIObject; 023import gwt.material.design.client.base.HasColors; 024import gwt.material.design.client.constants.Color; 025 026/** 027 * @author kevzlou7979 028 * @author Ben Dol 029 */ 030public class ColorsMixin<T extends UIObject & HasColors> extends AbstractMixin<T> implements HasColors { 031 032 private Color bgColor; 033 private Color textColor; 034 035 public ColorsMixin(final T widget) { 036 super(widget); 037 } 038 039 @Override 040 public void setBackgroundColor(Color bgColor) { 041 if (this.bgColor != null) { 042 uiObject.removeStyleName(this.bgColor.getCssName()); 043 } 044 this.bgColor = bgColor; 045 046 if (bgColor != null) { 047 uiObject.addStyleName(bgColor.getCssName()); 048 } 049 } 050 051 @Override 052 public Color getBackgroundColor() { 053 return bgColor; 054 } 055 056 @Override 057 public void setTextColor(Color textColor) { 058 if (this.textColor != null) { 059 uiObject.removeStyleName(ensureTextColorFormat(this.textColor.getCssName())); 060 } 061 this.textColor = textColor; 062 063 if (this.textColor != null) { 064 uiObject.addStyleName(ensureTextColorFormat(textColor.getCssName())); 065 } 066 } 067 068 @Override 069 public Color getTextColor() { 070 return textColor; 071 } 072 073 /** 074 * Allow for the use of text shading and auto formatting. 075 */ 076 protected String ensureTextColorFormat(String textColor) { 077 String formatted = ""; 078 boolean mainColor = true; 079 for (String style : textColor.split(" ")) { 080 if (mainColor) { 081 // the main color 082 if (!style.endsWith("-text")) { 083 style += "-text"; 084 } 085 mainColor = false; 086 } else { 087 // the shading type 088 if (!style.startsWith("text-")) { 089 style = " text-" + style; 090 } 091 } 092 formatted += style; 093 } 094 return formatted; 095 } 096}