001/* 002 * Copyright 2012 GWT-Bootstrap 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.github.gwtbootstrap.client.ui; 017 018import com.github.gwtbootstrap.client.ui.base.AddOn; 019import com.github.gwtbootstrap.client.ui.base.DivWidget; 020import com.github.gwtbootstrap.client.ui.constants.IconType; 021import com.google.gwt.uibinder.client.UiChild; 022import com.google.gwt.user.client.ui.IsWidget; 023 024/** 025 * append/prepend {@link AddOn} for input tags. 026 * <p> 027 * If you want to append/prepend add-on,<br/> 028 * Set appendText/prependText or appendIcon/prependIcon attribute. 029 * 030 * <h3>UiBinder Usage:</h3> 031 * 032 * <pre> 033 * {@code 034 * <!-- append text --> 035 * <b:InputAddOn appendText="@"> 036 * <g:TextBox/> 037 * </b:InputAddOn> 038 * 039 * <!-- above setting is same as blow --> 040 * <div class="input-append"> 041 * <span class="add-on">@</span><input type="text"> 042 * </div> 043 * 044 * 045 * <!-- prepend text--> 046 * <b:InputAddOn prependText="@"> 047 * <g:TextBox/> 048 * </b:InputAddOn> 049 * 050 * <!-- above setting is same as blow --> 051 * <div class="input-prepend"> 052 * <input type="text"><span class="add-on">@</span> 053 * </div> 054 * 055 * <!-- append icon and prepend text--> 056 * <b:InputAddOn appendIcon="STAR" prependText="@"> 057 * <g:TextBox/> 058 * </b:InputAddOn> 059 * 060 * <!-- above setting is same as blow --> 061 * <div class="input-append input-prepend"> 062 * <span class="add-on"><i class="icon-star"></i></span><input type="text"><span class="add-on">@</span> 063 * </div> 064 * } 065 * </pre> 066 * 067 * </p> 068 * 069 * @since 2.0.4.0 070 * 071 * @author ohashi keisuke 072 * @see AddOn 073 * @see http://twitter.github.com/bootstrap/base-css.html#forms 074 */ 075public class InputAddOn extends DivWidget { 076 077 /** prepend add-on */ 078 private AddOn prependAddOn = new AddOn(); 079 080 /** append-add-on */ 081 private AddOn appendAddOn = new AddOn(); 082 083 /** set append add-on */ 084 private boolean append = false; 085 086 /** 087 * Creates an empty widget. 088 */ 089 public InputAddOn() { 090 super(); 091 } 092 093 /** 094 * set prepend text 095 * 096 * @param prependText 097 * prepend text 098 */ 099 public void setPrependText(String prependText) { 100 setPrependStyle(); 101 102 prependAddOn.setText(prependText); 103 } 104 105 /** 106 * set prepend icon 107 * 108 * @param prependIcon 109 * IconType 110 */ 111 public void setPrependIcon(IconType prependIcon) { 112 setPrependStyle(); 113 114 prependAddOn.setIcon(prependIcon); 115 } 116 117 private void setPrependStyle() { 118 this.setStyleName("input-prepend", true); 119 if (!prependAddOn.isAttached()) { 120 insert(prependAddOn, 0); 121 } 122 } 123 124 /** 125 * set append text 126 * 127 * @param appendText 128 * append text 129 */ 130 public void setAppendText(String appendText) { 131 setAppendStyle(); 132 133 appendAddOn.setText(appendText); 134 } 135 136 /** 137 * set append icon 138 * 139 * @param appendIcon 140 * append icon 141 */ 142 public void setAppendIcon(IconType appendIcon) { 143 setAppendStyle(); 144 145 appendAddOn.setIcon(appendIcon); 146 } 147 148 private void setAppendStyle() { 149 this.setStyleName("input-append", true); 150 append = true; 151 152 if (isAttached() && !appendAddOn.isAttached()) { 153 add(appendAddOn); 154 } 155 156 } 157 158 /** 159 * Add prepend widget. its method is for uibinder syntax. 160 * @param w Addred widget to prepend addon. 161 */ 162 @UiChild(limit = 1, tagname = "prependWidget") 163 public void addPrependWidget(IsWidget w) { 164 setPrependStyle(); 165 prependAddOn.addWidget(w); 166 } 167 168 /** 169 * Add append widget. its method is for uibinder syntax. 170 * @param w Addred widget to append addon. 171 */ 172 @UiChild(limit = 1, tagname = "appendWidget") 173 public void addAppendWidget(IsWidget w) { 174 setAppendStyle(); 175 appendAddOn.addWidget(w); 176 } 177 178 179 /** 180 * {@inheritDoc} 181 */ 182 @Override 183 protected void onLoad() { 184 185 // if setup append add-on and it is not attached,add as last child. 186 if (append && !appendAddOn.isAttached()) { 187 add(appendAddOn); 188 } 189 super.onLoad(); 190 } 191 192}