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 java.util.List; 019 020import com.github.gwtbootstrap.client.ui.base.HasAlternateSize; 021import com.github.gwtbootstrap.client.ui.base.HasId; 022import com.github.gwtbootstrap.client.ui.base.HasSize; 023import com.github.gwtbootstrap.client.ui.base.IsSearchQuery; 024import com.github.gwtbootstrap.client.ui.base.SearchQueryStyleHelper; 025import com.github.gwtbootstrap.client.ui.base.SizeHelper; 026import com.github.gwtbootstrap.client.ui.base.StyleHelper; 027import com.github.gwtbootstrap.client.ui.constants.AlternateSize; 028import com.github.gwtbootstrap.client.ui.constants.Constants; 029import com.github.gwtbootstrap.client.ui.constants.ControlGroupType; 030import com.google.gwt.dom.client.Element; 031import com.google.gwt.editor.client.EditorError; 032import com.google.gwt.editor.client.HasEditorErrors; 033import com.google.gwt.editor.client.IsEditor; 034import com.google.gwt.editor.ui.client.adapters.HasTextEditor; 035import com.google.gwt.safehtml.shared.SafeHtmlBuilder; 036import com.google.gwt.user.client.ui.HasText; 037import com.google.gwt.user.client.ui.Widget; 038 039 040/** 041 * A FileInput for Bootstrap form. 042 * @since 2.0.4.0 043 * @author ohashi keisuke 044 * 045 */ 046public class FileUpload extends com.google.gwt.user.client.ui.FileUpload 047 implements HasSize, HasAlternateSize, IsSearchQuery, HasId, 048 HasText, IsEditor<HasTextEditor>, HasEditorErrors<String> { 049 050 /** Widget for control decoration on <code>EditorError</code>s */ 051 private Widget controlGroup;// could be a ControlGroup widget 052 /** Widget where <code>EditorError</code>s messages will be placed */ 053 private Widget errorLabel;// could be a HelpInline widget 054 055 { 056 setStyleName(""); 057 } 058 059 /** 060 * 061 * @see com.google.gwt.editor.client.HasEditorErrors#showErrors(java.util.List) 062 */ 063 @Override 064 public void showErrors(List<EditorError> errors) { 065 Widget decoratedWidget = controlGroup != null? controlGroup : this; 066 if(errors != null && !errors.isEmpty()) { 067 StyleHelper.addStyle(decoratedWidget, ControlGroupType.ERROR); 068 SafeHtmlBuilder sb = new SafeHtmlBuilder(); 069 for (EditorError error : errors) { 070 if(error.getEditor() == this) { 071 error.setConsumed(true); 072 sb.appendEscaped(error.getMessage()); 073 sb.appendHtmlConstant("<br />"); 074 } 075 } 076 setErrorLabelText(sb.toSafeHtml().asString()); 077 } else { 078 StyleHelper.removeStyle(decoratedWidget, ControlGroupType.ERROR); 079 setErrorLabelText(""); 080 } 081 } 082 083 /** 084 * The widget that will be decorated on <code>EditorError</code>s will be added de <code>ControlGroupType.ERROR</code> style. 085 * It can be a ControlGroup or any widget. 086 * @param controlGroup 087 */ 088 public void setControlGroup(Widget controlGroup) { 089 this.controlGroup = controlGroup; 090 } 091 /** 092 * Widget where <code>EditorError</code>s messages will be placed. 093 * It can be a HelpBlock or any other widget. 094 * @param errorLabel 095 */ 096 public void setErrorLabel(Widget errorLabel) { 097 this.errorLabel = errorLabel; 098 } 099 100 /** 101 * Sets the content of the <code>EditorError</code>s messages inside de <code>errorLabel</code>. 102 * This implementation uses {@link Element#setInnerHTML(String)} to set the content. 103 * @param errorMessage 104 */ 105 protected void setErrorLabelText(String errorMessage) { 106 if(errorLabel != null) { 107 errorLabel.getElement().setInnerHTML(errorMessage); 108 } 109 } 110 111 @Override 112 public HasTextEditor asEditor() { 113 return HasTextEditor.of(this); 114 } 115 116 @Override 117 public String getText() { 118 String filename = getFilename(); 119 return filename != null && !"".equals(filename.trim())? 120 filename : null; 121 } 122 123 @Override 124 public void setText(String value) { 125 try { 126 getElement().setAttribute("value", value); 127 } catch (Exception ignored) { 128 } 129 } 130 131 /** 132 * {@inheritDoc} 133 */ 134 @Override 135 public void setAlternateSize(AlternateSize size) { 136 StyleHelper.changeStyle(this, size, AlternateSize.class); 137 } 138 139 /** 140 * {@inheritDoc} 141 */ 142 @Override 143 public void setSize(int size) { 144 SizeHelper.setSize(this, size); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 @Override 151 public void setSearchQuery(boolean searchQuery) { 152 SearchQueryStyleHelper.setSearchQuery(this, searchQuery); 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 @Override 159 public boolean isSearchQuery() { 160 return SearchQueryStyleHelper.isSearchQuery(this); 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 @Override 167 public String getId() { 168 return getElement().getId(); 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 @Override 175 public void setId(String id) { 176 getElement().setId(id); 177 } 178 179 /** 180 * {@inheritDoc} 181 */ 182 @Override 183 public void setEnabled(boolean enabled) { 184 super.setEnabled(enabled); 185 if(enabled) { 186 removeStyleName(Constants.DISABLED); 187 } else { 188 addStyleName(Constants.DISABLED); 189 } 190 } 191}