001package com.github.gwtbootstrap.client.ui; 002 003import com.github.gwtbootstrap.client.ui.constants.Alignment; 004import com.github.gwtbootstrap.client.ui.constants.IconFlip; 005import com.github.gwtbootstrap.client.ui.constants.IconRotate; 006import com.github.gwtbootstrap.client.ui.constants.IconSize; 007import com.github.gwtbootstrap.client.ui.constants.IconType; 008import com.google.gwt.cell.client.AbstractCell; 009import com.google.gwt.safehtml.shared.SafeHtmlBuilder; 010 011/** 012 * A IconCell supporting icon type, icon size, light, mute, border, spin, alignment, rotate, flip and tooltip 013 * 014 * <h3>Example</h3> 015 * <p> 016 * <pre> 017 * {@code 018 * final IconCell iCell = new IconCell(IconType.INFO_SIGN, IconSize.LARGE); 019 * 020 * Column<OneObject, Void> iconColumn = new Column<OneObject, Void>(iCell) { 021 * 022 * public Void getValue(OneObject object) { 023 * if (object.getOneValue() > 0) { 024 * iCell.setIconType(IconType.PLUS_SIGN); 025 * iCell.setTooltip("High"); 026 * } 027 * if (object.getOneValue() < 0) { 028 * iCell.setIconType(IconType.MINUS_SIGN); 029 * iCell.setTooltip("Low"); 030 * } 031 * return null; 032 * } 033 * }; 034 * } 035 * </pre> 036 * </p> 037 */ 038public class IconCell extends AbstractCell<Void> { 039 040 private IconType iconType; 041 private IconSize iconSize; 042 private boolean light; 043 private boolean muted; 044 private boolean border; 045 private boolean spin; 046 private Alignment alignment; 047 private IconRotate iconRotate; 048 private IconFlip iconFlip; 049 private String tooltip; 050 051 /** 052 * Construct a new {@link IconCell} with the specified icon type 053 * 054 * @param iconType 055 */ 056 public IconCell(IconType iconType) { 057 this(iconType, IconSize.DEFAULT); 058 } 059 060 /** 061 * Construct a new {@link IconCell} with the specified icon type and icon size 062 * 063 * @param iconType 064 * @param iconSize 065 */ 066 public IconCell(IconType iconType, IconSize iconSize) { 067 this.iconType = iconType; 068 this.iconSize = iconSize; 069 light = false; 070 muted = false; 071 border = false; 072 spin = false; 073 alignment = Alignment.NONE; 074 iconRotate = IconRotate.NONE; 075 iconFlip = IconFlip.NONE; 076 } 077 078 @Override 079 public void render(Context context, Void value, SafeHtmlBuilder sb) { 080 081 StringBuilder builder = new StringBuilder(); 082 builder.append("<i") 083 .append(tooltip == null ? "" : " title=\"" + tooltip + "\"") 084 .append(" class=\"") 085 .append(iconType.get()) 086 .append(" ").append(iconSize.get()) 087 .append(light ? " icon-light" : "") 088 .append(muted ? " icon-muted" : "") 089 .append(border ? " icon-border" : "") 090 .append(spin ? " icon-spin" : ""); 091 092 if (alignment == Alignment.LEFT) 093 builder.append(" pull-left"); 094 if (alignment == Alignment.RIGHT) 095 builder.append(" pull-right"); 096 097 if (iconRotate == IconRotate.ROTATE_90) 098 builder.append(" icon-rotate-90"); 099 if (iconRotate == IconRotate.ROTATE_180) 100 builder.append(" icon-rotate-180"); 101 if (iconRotate == IconRotate.ROTATE_270) 102 builder.append(" icon-rotate-270"); 103 104 if (iconFlip == IconFlip.HORIZONTAL) 105 builder.append(" icon-flip-horizontal"); 106 if (iconFlip == IconFlip.VERTICAL) 107 builder.append(" icon-flip-vertical"); 108 109 builder.append("\"></i>"); 110 111 sb.appendHtmlConstant(builder.toString()); 112 } 113 114 public IconType getIconType() { 115 return iconType; 116 } 117 118 public void setIconType(IconType iconType) { 119 this.iconType = iconType; 120 } 121 122 public IconSize getIconSize() { 123 return iconSize; 124 } 125 126 public void setIconSize(IconSize iconSize) { 127 this.iconSize = iconSize; 128 } 129 130 public boolean isLight() { 131 return light; 132 } 133 134 public void setLight(boolean light) { 135 this.light = light; 136 } 137 138 public boolean isMuted() { 139 return muted; 140 } 141 142 public void setMuted(boolean muted) { 143 this.muted = muted; 144 } 145 146 public boolean isBorder() { 147 return border; 148 } 149 150 public void setBorder(boolean border) { 151 this.border = border; 152 } 153 154 public boolean isSpin() { 155 return spin; 156 } 157 158 public void setSpin(boolean spin) { 159 this.spin = spin; 160 } 161 162 public Alignment getAlignment() { 163 return alignment; 164 } 165 166 public void setAlignment(Alignment alignment) { 167 this.alignment = alignment; 168 } 169 170 public IconRotate getIconRotate() { 171 return iconRotate; 172 } 173 174 public void setIconRotate(IconRotate iconRotate) { 175 this.iconRotate = iconRotate; 176 } 177 178 public IconFlip getIconFlip() { 179 return iconFlip; 180 } 181 182 public void setIconFlip(IconFlip iconFlip) { 183 this.iconFlip = iconFlip; 184 } 185 186 public String getTooltip() { 187 return tooltip; 188 } 189 190 public void setTooltip(String tooltip) { 191 this.tooltip = tooltip; 192 } 193 194}