123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.terminal.gwt.client.ui.richtextarea;
-
- import com.google.gwt.user.client.Command;
- import com.google.gwt.user.client.DOM;
- import com.google.gwt.user.client.DeferredCommand;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.ui.ChangeListener;
- import com.google.gwt.user.client.ui.Composite;
- import com.google.gwt.user.client.ui.FlowPanel;
- import com.google.gwt.user.client.ui.FocusListener;
- import com.google.gwt.user.client.ui.HTML;
- import com.google.gwt.user.client.ui.KeyboardListener;
- import com.google.gwt.user.client.ui.RichTextArea;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.terminal.gwt.client.ApplicationConnection;
- import com.vaadin.terminal.gwt.client.BrowserInfo;
- import com.vaadin.terminal.gwt.client.Paintable;
- import com.vaadin.terminal.gwt.client.UIDL;
- import com.vaadin.terminal.gwt.client.Util;
- import com.vaadin.terminal.gwt.client.ui.Field;
-
- /**
- * This class implements a basic client side rich text editor component.
- *
- * @author IT Mill Ltd.
- *
- */
- public class VRichTextArea extends Composite implements Paintable, Field,
- ChangeListener, FocusListener, KeyboardListener {
-
- /**
- * The input node CSS classname.
- */
- public static final String CLASSNAME = "i-richtextarea";
-
- protected String id;
-
- protected ApplicationConnection client;
-
- private boolean immediate = false;
-
- private RichTextArea rta = new RichTextArea();
-
- private VRichTextToolbar formatter = new VRichTextToolbar(rta);
-
- private HTML html = new HTML();
-
- private final FlowPanel fp = new FlowPanel();
-
- private boolean enabled = true;
-
- private int extraHorizontalPixels = -1;
- private int extraVerticalPixels = -1;
-
- private int maxLength = -1;
-
- private int toolbarNaturalWidth = 500;
-
- public VRichTextArea() {
- fp.add(formatter);
-
- rta.setWidth("100%");
- rta.addFocusListener(this);
-
- fp.add(rta);
-
- initWidget(fp);
- setStyleName(CLASSNAME);
-
- }
-
- public void setEnabled(boolean enabled) {
- if (this.enabled != enabled) {
- rta.setEnabled(enabled);
- if (enabled) {
- fp.remove(html);
- fp.add(rta);
- } else {
- html.setHTML(rta.getHTML());
- fp.remove(rta);
- fp.add(html);
- }
-
- this.enabled = enabled;
- }
- }
-
- public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
- this.client = client;
- id = uidl.getId();
-
- if (uidl.hasVariable("text")) {
- if (BrowserInfo.get().isIE()) {
- // rta is rather buggy in IE (as pretty much everything is)
- // it needs some "shaking" not to fall into uneditable state
- // see #2374
- rta.getBasicFormatter().toggleBold();
- rta.getBasicFormatter().toggleBold();
- }
- rta.setHTML(uidl.getStringVariable("text"));
-
- }
- setEnabled(!uidl.getBooleanAttribute("disabled"));
-
- if (client.updateComponent(this, uidl, true)) {
- return;
- }
-
- immediate = uidl.getBooleanAttribute("immediate");
- int newMaxLength = uidl.hasAttribute("maxLength") ? uidl
- .getIntAttribute("maxLength") : -1;
- if (newMaxLength >= 0) {
- if (maxLength == -1) {
- rta.addKeyboardListener(this);
- }
- maxLength = newMaxLength;
- } else if (maxLength != -1) {
- getElement().setAttribute("maxlength", "");
- maxLength = -1;
- rta.removeKeyboardListener(this);
- }
- }
-
- public void onChange(Widget sender) {
- if (client != null && id != null) {
- client.updateVariable(id, "text", rta.getText(), immediate);
- }
- }
-
- public void onFocus(Widget sender) {
-
- }
-
- public void onLostFocus(Widget sender) {
- final String html = rta.getHTML();
- client.updateVariable(id, "text", html, immediate);
-
- }
-
- /**
- * @return space used by components paddings and borders
- */
- private int getExtraHorizontalPixels() {
- if (extraHorizontalPixels < 0) {
- detectExtraSizes();
- }
- return extraHorizontalPixels;
- }
-
- /**
- * @return space used by components paddings and borders
- */
- private int getExtraVerticalPixels() {
- if (extraVerticalPixels < 0) {
- detectExtraSizes();
- }
- return extraVerticalPixels;
- }
-
- /**
- * Detects space used by components paddings and borders.
- */
- private void detectExtraSizes() {
- Element clone = Util.cloneNode(getElement(), false);
- DOM.setElementAttribute(clone, "id", "");
- DOM.setStyleAttribute(clone, "visibility", "hidden");
- DOM.setStyleAttribute(clone, "position", "absolute");
- // due FF3 bug set size to 10px and later subtract it from extra pixels
- DOM.setStyleAttribute(clone, "width", "10px");
- DOM.setStyleAttribute(clone, "height", "10px");
- DOM.appendChild(DOM.getParent(getElement()), clone);
- extraHorizontalPixels = DOM.getElementPropertyInt(clone, "offsetWidth") - 10;
- extraVerticalPixels = DOM.getElementPropertyInt(clone, "offsetHeight") - 10;
-
- DOM.removeChild(DOM.getParent(getElement()), clone);
- }
-
- @Override
- public void setHeight(String height) {
- if (height.endsWith("px")) {
- int h = Integer.parseInt(height.substring(0, height.length() - 2));
- h -= getExtraVerticalPixels();
- if (h < 0) {
- h = 0;
- }
-
- super.setHeight(h + "px");
- } else {
- super.setHeight(height);
- }
-
- if (height == null || height.equals("")) {
- rta.setHeight("");
- } else {
- int editorHeight = getOffsetHeight() - getExtraVerticalPixels()
- - formatter.getOffsetHeight();
- rta.setHeight(editorHeight + "px");
- }
- }
-
- @Override
- public void setWidth(String width) {
- if (width.endsWith("px")) {
- int w = Integer.parseInt(width.substring(0, width.length() - 2));
- w -= getExtraHorizontalPixels();
- if (w < 0) {
- w = 0;
- }
-
- super.setWidth(w + "px");
- } else if (width.equals("")) {
- /*
- * IE cannot calculate the width of the 100% iframe correctly if
- * there is no width specified for the parent. In this case we would
- * use the toolbar but IE cannot calculate the width of that one
- * correctly either in all cases. So we end up using a default width
- * for a RichTextArea with no width definition in all browsers (for
- * compatibility).
- */
-
- super.setWidth(toolbarNaturalWidth + "px");
- } else {
- super.setWidth(width);
- }
- }
-
- public void onKeyDown(Widget sender, char keyCode, int modifiers) {
- // NOP
- }
-
- public void onKeyPress(Widget sender, char keyCode, int modifiers) {
- if (maxLength >= 0) {
- DeferredCommand.addCommand(new Command() {
- public void execute() {
- if (rta.getHTML().length() > maxLength) {
- rta.setHTML(rta.getHTML().substring(0, maxLength));
- }
- }
- });
- }
- }
-
- public void onKeyUp(Widget sender, char keyCode, int modifiers) {
- // NOP
- }
-
- }
|