All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.user.client.ui.ValueLabel Maven / Gradle / Ivy

There is a newer version: 2.7.0.vaadin7
Show newest version
/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.user.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.adapters.TakesValueEditor;
import com.google.gwt.text.shared.Parser;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.text.shared.ToStringRenderer;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.TakesValue;

import java.text.ParseException;

/**
 * A label displaying its value through a renderer.
 * 
 * @param  the value type.
 */
public class ValueLabel extends LabelBase implements TakesValue,
    IsEditor> {

  /**
   * Creates a ValueLabel widget that wraps an existing <span> element.
   * 

* The ValueLabel's value will be null, whether the element being * wrapped has content or not. Use {@link #wrap(Element, Renderer, Parser)} to * parse the initial element's content to initialize the ValueLabel's value. *

* This element must already be attached to the document. If the element is * removed from the document, you must call * {@link RootPanel#detachNow(Widget)}. * * @param element the element to be wrapped * @param renderer the renderer used to render values into the element */ public static ValueLabel wrap(Element element, Renderer renderer) { // Assert that the element is attached. assert Document.get().getBody().isOrHasChild(element); ValueLabel label = new ValueLabel(element, renderer); // Mark it attached and remember it for cleanup. label.onAttach(); RootPanel.detachOnWindowClose(label); return label; } /** * Creates a ValueLabel widget that wraps an existing <span> element. *

* The ValueLabel's value will be initialized with the element's content, * passed through the parser. *

* This element must already be attached to the document. If the element is * removed from the document, you must call * {@link RootPanel#detachNow(Widget)}. * * @param element the element to be wrapped * @param renderer the renderer used to render values into the element * @param parser the parser used to initialize the ValueLabel's value from the * element's content */ public static ValueLabel wrap(Element element, Renderer renderer, Parser parser) throws ParseException { ValueLabel label = wrap(element, renderer); label.setValue(parser.parse(element.getInnerText())); // Mark it attached and remember it for cleanup. label.onAttach(); RootPanel.detachOnWindowClose(label); return label; } private final Renderer renderer; private T value; private LeafValueEditor editor; public ValueLabel() { this(ToStringRenderer.instance()); } /** * Creates an empty value label. * * @param renderer */ @UiConstructor public ValueLabel(Renderer renderer) { super(true); this.renderer = renderer; } /** * This constructor may be used by subclasses to explicitly use an existing * element. This element must be either a <span> or a <div> * element. * * @param element the element to be used */ protected ValueLabel(Element element, Renderer renderer) { super(element); this.renderer = renderer; } public LeafValueEditor asEditor() { if (editor == null) { editor = TakesValueEditor.of(this); } return editor; } public T getValue() { return value; } public void setValue(T value) { this.value = value; directionalTextHelper.setTextOrHtml(renderer.render(value), false); updateHorizontalAlignment(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy