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 */
016 package com.github.gwtbootstrap.client.ui.base;
017
018 import com.github.gwtbootstrap.client.ui.constants.Constants;
019 import com.google.gwt.user.client.Element;
020
021
022 /**
023 * The helper class for {@link HasPlaceholder} interface.
024 *
025 * @since 2.0.4.0
026 * @author ohashi keisuke
027 *
028 */
029 public class PlaceholderHelper {
030
031 //TODO 2012/05/04 ohashi keisuke. Support for non HTML5 Browser.
032 //I designed this class as non static class.
033 //Maybe we can use deffered binding for non HTML5 Browser support.
034
035 /**
036 * Set a placeholder attribute (HTML5) to the element.
037 *
038 * @param element the element be set
039 * @param placeholder
040 * the String to show. Use a empty string or null to remove the attribute.
041 */
042 public void setPlaceholer(Element element , String placeholder) {
043 if (placeholder == null || placeholder.trim().isEmpty()) {
044 element.removeAttribute(Constants.PLACEHOLDER);
045 return;
046 }
047 element.setAttribute(Constants.PLACEHOLDER, placeholder);
048 }
049
050 /**
051 * Get a placeholder attribute (HTML5) from the element.
052 * <p>
053 * If the element don't have placeholder attribute,This return empty string.
054 * </p>
055 * @param element the element
056 * @return placeholder attribute.
057 */
058 public String getPlaceholder(Element element) {
059 return element.hasAttribute(Constants.PLACEHOLDER)
060 ? element.getAttribute(Constants.PLACEHOLDER)
061 : "";
062 }
063 }