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

com.vaadin.v7.client.ui.richtextarea.RichTextAreaConnector Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.v7.client.ui.richtextarea;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.vaadin.client.ApplicationConnection;
import com.vaadin.client.Paintable;
import com.vaadin.client.UIDL;
import com.vaadin.client.ui.SimpleManagedLayout;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.Connect.LoadStyle;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.v7.client.ui.AbstractFieldConnector;
import com.vaadin.v7.client.ui.VRichTextArea;
import com.vaadin.v7.shared.ui.textarea.RichTextAreaState;
import com.vaadin.v7.ui.RichTextArea;

@Connect(value = RichTextArea.class, loadStyle = LoadStyle.LAZY)
public class RichTextAreaConnector extends AbstractFieldConnector
        implements Paintable, SimpleManagedLayout {

    /*
     * Last value received from the server
     */
    private String cachedValue = "";

    @Override
    protected void init() {
        getWidget().addBlurHandler(new BlurHandler() {

            @Override
            public void onBlur(BlurEvent event) {
                flush();
            }
        });
        getLayoutManager().registerDependency(this,
                getWidget().formatter.getElement());
    }

    @Override
    public void onUnregister() {
        super.onUnregister();
        getLayoutManager().unregisterDependency(this,
                getWidget().formatter.getElement());
    }

    @Override
    public void updateFromUIDL(final UIDL uidl, ApplicationConnection client) {
        getWidget().client = client;
        getWidget().id = uidl.getId();

        if (uidl.hasVariable("text")) {
            String newValue = uidl.getStringVariable("text");
            if (!SharedUtil.equals(newValue, cachedValue)) {
                getWidget().setValue(newValue);
                cachedValue = newValue;
            }
        }

        if (!isRealUpdate(uidl)) {
            return;
        }

        getWidget().setEnabled(isEnabled());
        getWidget().setReadOnly(isReadOnly());
        getWidget().immediate = getState().immediate;
        int newMaxLength = uidl.hasAttribute("maxLength")
                ? uidl.getIntAttribute("maxLength") : -1;
        if (newMaxLength >= 0) {
            if (getWidget().maxLength == -1) {
                getWidget().keyPressHandler = getWidget().rta
                        .addKeyPressHandler(getWidget());
            }
            getWidget().maxLength = newMaxLength;
        } else if (getWidget().maxLength != -1) {
            getWidget().getElement().setAttribute("maxlength", "");
            getWidget().maxLength = -1;
            getWidget().keyPressHandler.removeHandler();
        }

        if (uidl.hasAttribute("selectAll")) {
            getWidget().selectAll();
        }

    }

    @Override
    public VRichTextArea getWidget() {
        return (VRichTextArea) super.getWidget();
    }

    @Override
    public void flush() {
        if (getConnection() != null && getConnectorId() != null) {
            final String html = getWidget().getSanitizedValue();
            if (!html.equals(cachedValue)) {
                cachedValue = html;
                getConnection().updateVariable(getConnectorId(), "text", html,
                        getState().immediate);
            }
        }
    }

    @Override
    public void layout() {
        if (!isUndefinedHeight()) {
            int rootElementInnerHeight = getLayoutManager()
                    .getInnerHeight(getWidget().getElement());
            int formatterHeight = getLayoutManager()
                    .getOuterHeight(getWidget().formatter.getElement());
            int editorHeight = rootElementInnerHeight - formatterHeight;
            if (editorHeight < 0) {
                editorHeight = 0;
            }
            getWidget().rta.setHeight(editorHeight + "px");
        }
    }

    @Override
    public RichTextAreaState getState() {
        return (RichTextAreaState) super.getState();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy