import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.HasHTML;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractFieldConnector;
import com.vaadin.client.ui.Icon;
private TooltipInfo tooltipInfo = null;
+ private boolean captionAsHtml = false;
+
/**
* Creates a caption that is not linked to a {@link ComponentConnector}.
*
captionText.setInnerHTML(" ");
}
} else {
- DOM.setInnerText(captionText, c);
+ setCaptionText(captionText, owner.getState());
}
} else if (captionText != null) {
captionText.setInnerHTML(" ");
}
} else {
- DOM.setInnerText(captionText, caption);
+ if (captionAsHtml) {
+ captionText.setInnerHTML(caption);
+ } else {
+ captionText.setInnerText(caption);
+ }
}
} else if (captionText != null) {
return el.vOwnerPid;
}-*/;
+ /**
+ * Sets whether the caption is rendered as HTML.
+ * <p>
+ * Default is false
+ *
+ * @param captionAsHtml
+ * true if the captions are rendered as HTML, false if rendered
+ * as plain text
+ */
+ public void setCaptionAsHtml(boolean captionAsHtml) {
+ this.captionAsHtml = captionAsHtml;
+ }
+
+ /**
+ * Checks whether captions are rendered as HTML.
+ * <p>
+ * Default is false
+ *
+ * @return true if the captions are rendered as HTML, false if rendered as
+ * plain text
+ */
+ public boolean isCaptionAsHtml() {
+ return captionAsHtml;
+ }
+
+ /**
+ * Sets the text of the given caption element to the caption found in the
+ * state.
+ * <p>
+ * Uses {@link AbstractComponentState#captionAsHtml} to determine whether to
+ * set the caption as html or plain text
+ *
+ * @since 7.4
+ * @param captionElement
+ * the target element
+ * @param state
+ * the state from which to read the caption text and mode
+ */
+ public static void setCaptionText(Element captionElement,
+ AbstractComponentState state) {
+ if (state.captionAsHtml) {
+ captionElement.setInnerHTML(state.caption);
+ } else {
+ captionElement.setInnerText(state.caption);
+ }
+
+ }
+
+ /**
+ * Sets the text of the given widget to the caption found in the state.
+ * <p>
+ * Uses {@link AbstractComponentState#captionAsHtml} to determine whether to
+ * set the caption as html or plain text
+ *
+ * @since 7.4
+ * @param widget
+ * the target widget
+ * @param state
+ * the state from which to read the caption text and mode
+ */
+ public static void setCaptionText(HasHTML widget,
+ AbstractComponentState state) {
+ if (state.captionAsHtml) {
+ widget.setHTML(state.caption);
+ } else {
+ widget.setText(state.caption);
+ }
+
+ }
+
}
}
public void setCaption(String c, String iconURL) {
- String html = Util.escapeHTML(c);
+ setCaption(c, iconURL, false);
+ }
+
+ public void setCaption(String c, String iconURL, boolean asHtml) {
+ String html = c;
+ if (!asHtml) {
+ c = Util.escapeHTML(c);
+ }
// Provide information to assistive device users that a sub window was
// opened
import com.google.gwt.user.client.DOM;
import com.vaadin.client.EventHelper;
import com.vaadin.client.MouseEventDetailsBuilder;
+import com.vaadin.client.VCaption;
import com.vaadin.client.annotations.OnStateChange;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
blurHandlerRegistration);
}
- @OnStateChange({ "caption", "htmlContentAllowed" })
+ @OnStateChange({ "caption", "captionAsHtml" })
void setCaption() {
- String caption = getState().caption;
- if (getState().htmlContentAllowed) {
- getWidget().setHtml(caption);
- } else {
- getWidget().setText(caption);
- }
+ VCaption.setCaptionText(getWidget().captionElement, getState());
}
@OnStateChange("iconAltText")
import com.google.gwt.user.client.Event;
import com.vaadin.client.EventHelper;
import com.vaadin.client.MouseEventDetailsBuilder;
+import com.vaadin.client.VCaption;
import com.vaadin.client.VTooltip;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractFieldConnector;
}
// Set text
- getWidget().setText(getState().caption);
+ VCaption.setCaptionText(getWidget(), getState());
+
getWidget().setValue(getState().checked);
getWidget().immediate = getState().immediate;
}
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.VCaption;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.ui.VColorPickerArea;
import com.vaadin.shared.ui.Connect;
@Override
protected void setCaption(String caption) {
- if (getState().htmlContentAllowed) {
- getWidget().setHTML(caption);
- } else {
- getWidget().setText(caption);
- }
+ VCaption.setCaptionText(getWidget(), getState());
}
@Override
@Override
protected void setCaption(String caption) {
- if (getState().htmlContentAllowed) {
+ if (getState().captionAsHtml) {
getWidget().setHtml(caption);
} else {
getWidget().setText(caption);
import com.vaadin.client.Paintable;
import com.vaadin.client.TooltipInfo;
import com.vaadin.client.UIDL;
+import com.vaadin.client.VCaption;
import com.vaadin.client.ui.AbstractComponentContainerConnector;
import com.vaadin.client.ui.ShortcutActionHandler;
import com.vaadin.client.ui.VForm;
boolean legendEmpty = true;
if (getState().caption != null) {
- getWidget().caption.setInnerText(getState().caption);
+ VCaption.setCaptionText(getWidget().caption, getState());
legendEmpty = false;
} else {
getWidget().caption.setInnerText("");
VCaption caption = layoutSlot.getCaption();
if (caption == null) {
caption = new VCaption(childConnector, getConnection());
-
Widget widget = childConnector.getWidget();
layout.setCaption(widget, caption);
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.user.client.DOM;
+import com.vaadin.client.VCaption;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.client.ui.Icon;
getWidget().targetHeight = getState().targetHeight;
// Set link caption
- getWidget().captionElement.setInnerText(getState().caption);
+ VCaption.setCaptionText(getWidget().captionElement, getState());
// handle error
if (null != getState().errorMessage) {
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.vaadin.client.EventHelper;
+import com.vaadin.client.VCaption;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.client.ui.Icon;
blurHandlerRegistration);
// Set text
- if (getState().htmlContentAllowed) {
- getWidget().setHTML(getState().caption);
- } else {
- getWidget().setText(getState().caption);
- }
+ VCaption.setCaptionText(getWidget(), getState());
// handle error
if (null != getState().errorMessage) {
}
slot.setCaption(caption, icon, styles, error, showError, required,
- enabled);
+ enabled, child.getState().captionAsHtml);
AriaHelper.handleInputRequired(child.getWidget(), required);
AriaHelper.handleInputInvalid(child.getWidget(), showError);
}
/**
- * Set the caption of the slot
+ * Set the caption of the slot as text
*
* @param captionText
* The text of the caption
*/
public void setCaption(String captionText, Icon icon, List<String> styles,
String error, boolean showError, boolean required, boolean enabled) {
+ setCaption(captionText, icon, styles, error, showError, required,
+ enabled, false);
+ }
+
+ /**
+ * Set the caption of the slot
+ *
+ * @param captionText
+ * The text of the caption
+ * @param icon
+ * The icon
+ * @param styles
+ * The style names
+ * @param error
+ * The error message
+ * @param showError
+ * Should the error message be shown
+ * @param required
+ * Is the (field) required
+ * @param enabled
+ * Is the component enabled
+ * @param captionAsHtml
+ * true if the caption should be rendered as HTML, false
+ * otherwise
+ */
+ public void setCaption(String captionText, Icon icon, List<String> styles,
+ String error, boolean showError, boolean required, boolean enabled,
+ boolean captionAsHtml) {
// TODO place for optimization: check if any of these have changed
// since last time, and only run those changes
if (captionText.trim().equals("")) {
this.captionText.setInnerHTML(" ");
} else {
- this.captionText.setInnerText(captionText);
+ if (captionAsHtml) {
+ this.captionText.setInnerHTML(captionText);
+ } else {
+ this.captionText.setInnerText(captionText);
+ }
}
} else if (this.captionText != null) {
this.captionText.removeFromParent();
* @param htmlContentAllowed
* <code>true</code> if caption is rendered as HTML,
* <code>false</code> otherwise
+ * @deprecated as of , use {@link #setCaptionAsHtml(boolean)} instead
*/
+ @Deprecated
public void setHtmlContentAllowed(boolean htmlContentAllowed) {
- getState().htmlContentAllowed = htmlContentAllowed;
+ setCaptionAsHtml(htmlContentAllowed);
}
/**
*
* @return <code>true</code> if the caption text is to be rendered as HTML,
* <code>false</code> otherwise
+ * @deprecated as of , use {@link #isCaptionAsHtml()} instead
*/
+ @Deprecated
public boolean isHtmlContentAllowed() {
- return getState(false).htmlContentAllowed;
+ return isCaptionAsHtml();
}
}
getState().caption = caption;
}
+ /**
+ * Sets whether the caption is rendered as HTML.
+ * <p>
+ * If set to true, the captions are rendered in the browser as HTML and the
+ * developer is responsible for ensuring no harmful HTML is used. If set to
+ * false, the caption is rendered in the browser as plain text.
+ * <p>
+ * The default is false, i.e. to render that caption as plain text.
+ *
+ * @param captionAsHtml
+ * true if the captions are rendered as HTML, false if rendered
+ * as plain text
+ */
+ public void setCaptionAsHtml(boolean captionAsHtml) {
+ getState().captionAsHtml = captionAsHtml;
+ }
+
+ /**
+ * Checks whether captions are rendered as HTML
+ * <p>
+ * The default is false, i.e. to render that caption as plain text.
+ *
+ * @return true if the captions are rendered as HTML, false if rendered as
+ * plain text
+ */
+ public boolean isCaptionAsHtml() {
+ return getState(false).captionAsHtml;
+ }
+
/*
* Don't add a JavaDoc comment here, we use the default documentation from
* implemented interface.
/**
* Set whether the caption text is rendered as HTML or not. You might need
- * to retheme button to allow higher content than the original text style.
+ * to re-theme button to allow higher content than the original text style.
*
* If set to true, the captions are passed to the browser as html and the
* developer is responsible for ensuring no harmful html is used. If set to
* <code>false</code> otherwise
*/
public void setHtmlContentAllowed(boolean htmlContentAllowed) {
- getState().htmlContentAllowed = htmlContentAllowed;
+ getState().captionAsHtml = htmlContentAllowed;
}
/**
* <code>false</code> otherwise
*/
public boolean isHtmlContentAllowed() {
- return getState(false).htmlContentAllowed;
+ return getState(false).captionAsHtml;
}
/*
result.add("icon-alternate-text");
result.add("click-shortcut");
result.add("html-content-allowed");
+ result.add("caption-as-html");
return result;
}
// TODO this could be an object with more information, but currently the UI
// only uses the message
public String errorMessage = null;
+ public boolean captionAsHtml = false;
}
}
public boolean disableOnClick = false;
public int clickShortcutKeyCode = 0;
- /**
- * If caption should be rendered in HTML
- */
- public boolean htmlContentAllowed = false;
public String iconAltText = "";
}
public boolean showDefaultCaption;
- public boolean htmlContentAllowed;
}
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.server.ThemeResource;
import com.vaadin.server.UserError;
-import com.vaadin.tests.components.TestBase;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
-public class CaptionsInLayouts extends TestBase {
+public class CaptionsInLayouts extends AbstractTestUI {
private static final Object CAPTION = "CAPTION";
private static final Object CLASS = "C";
private HorizontalLayout layoutParent = new HorizontalLayout();
@Override
- protected void setup() {
+ protected void setup(VaadinRequest request) {
// setTheme("tests-tickets");
addComponent(createLayoutSelect());
addComponent(toggleRequired());
}
@Override
- protected String getDescription() {
+ protected String getTestDescription() {
return "Tests what happens when the caption changes in various layouts. Behavior should be consistent.";
}
--- /dev/null
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * 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.vaadin.tests.layouts;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.AbsoluteLayout;
+import com.vaadin.ui.AbstractComponent;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.CheckBox;
+import com.vaadin.ui.ColorPicker;
+import com.vaadin.ui.ColorPickerArea;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.Form;
+import com.vaadin.ui.GridLayout;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Link;
+import com.vaadin.ui.NativeButton;
+import com.vaadin.ui.Panel;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.Window;
+
+/**
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class HtmlInCaption extends AbstractTestUI {
+
+ @Override
+ protected void setup(VaadinRequest request) {
+ HorizontalLayout main = new HorizontalLayout();
+ addComponent(main);
+ VerticalLayout components = new VerticalLayout();
+ components.setId("components");
+ VerticalLayout layouts = new VerticalLayout();
+ layouts.setId("layouts");
+ main.addComponent(layouts);
+ main.addComponent(components);
+
+ createComponents(components);
+ createLayouts(layouts);
+
+ Window w = new Window();
+ w.setCaption(getTextCaption("Window"));
+ w.setPositionX(600);
+ addWindow(w);
+
+ w = new Window();
+ w.setCaptionAsHtml(true);
+ w.setCaption(getHtmlCaption("Window"));
+ w.setPositionX(600);
+ w.setPositionY(100);
+ addWindow(w);
+ }
+
+ private void createLayouts(VerticalLayout layouts) {
+ VerticalLayout vl = new VerticalLayout(tf(false), tf(true));
+ vl.setCaption("VerticalLayout");
+ layouts.addComponent(vl);
+
+ HorizontalLayout hl = new HorizontalLayout(tf(false), tf(true));
+ hl.setCaption("HorizontalLayout");
+ layouts.addComponent(hl);
+
+ GridLayout gl = new GridLayout(2, 1);
+ gl.setCaption("GridLayout");
+ gl.addComponents(tf(false), tf(true));
+ layouts.addComponent(gl);
+
+ CssLayout cl = new CssLayout();
+ cl.setCaption("CssLayout");
+ cl.addComponents(tf(false), tf(true));
+ layouts.addComponent(cl);
+
+ AbsoluteLayout al = new AbsoluteLayout();
+ al.setCaption("AbsoluteLayout");
+ al.setWidth("300px");
+ al.setHeight("200px");
+ al.addComponent(tf(false), "top:30px");
+ al.addComponent(tf(true), "top: 100px");
+ layouts.addComponent(al);
+ }
+
+ private void createComponents(VerticalLayout components) {
+ createComponent(components, Button.class);
+ createComponent(components, NativeButton.class);
+ createComponent(components, CheckBox.class);
+ createComponent(components, Link.class);
+
+ createComponent(components, Panel.class);
+ createComponent(components, ColorPicker.class);
+ createComponent(components, ColorPickerArea.class);
+ createComponent(components, Form.class);
+
+ }
+
+ private void createComponent(VerticalLayout components,
+ Class<? extends AbstractComponent> class1) {
+ AbstractComponent ac;
+ try {
+ ac = class1.newInstance();
+ ac.setCaption(getTextCaption(class1.getSimpleName()));
+ components.addComponent(ac);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ try {
+ ac = class1.newInstance();
+ ac.setCaption(getHtmlCaption(class1.getSimpleName()));
+ ac.setCaptionAsHtml(true);
+ components.addComponent(ac);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ private Component tf(boolean htmlCaption) {
+ TextField tf = new TextField();
+ if (htmlCaption) {
+ tf.setCaptionAsHtml(htmlCaption);
+ tf.setCaption(getHtmlCaption(""));
+ } else {
+ tf.setCaption(getTextCaption(""));
+ }
+ return tf;
+ }
+
+ private String getTextCaption(String string) {
+ return "<b>Plain text " + string + "</b>";
+ }
+
+ private String getHtmlCaption(String string) {
+ return "<b><font color='red'>HTML " + string + "</font></b>";
+ }
+
+ @Override
+ protected Integer getTicketNumber() {
+ return 9426;
+ }
+
+}