diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2016-08-10 14:18:47 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-08-26 07:49:25 +0000 |
commit | 4a6a632b1f57a1ddbb437036cb5d17f98f827b47 (patch) | |
tree | 0a2091302a829adcfe86fd4d77545a271fa5c0b6 /client | |
parent | 24a888a2d1dc198d1d88c9e0dbdde1cfe8b0a7f1 (diff) | |
download | vaadin-framework-4a6a632b1f57a1ddbb437036cb5d17f98f827b47.tar.gz vaadin-framework-4a6a632b1f57a1ddbb437036cb5d17f98f827b47.zip |
Update TextArea to extend AbstractFieldNew (#53)
Change-Id: I72400695f3b015add07be9709e48b5f99ce619cf
Diffstat (limited to 'client')
3 files changed, 182 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VTextArea.java b/client/src/main/java/com/vaadin/client/ui/VTextArea.java new file mode 100644 index 0000000000..c63fbda38c --- /dev/null +++ b/client/src/main/java/com/vaadin/client/ui/VTextArea.java @@ -0,0 +1,107 @@ +/* + * Copyright 2000-2016 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.client.ui; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.Style.Overflow; +import com.google.gwt.dom.client.Style.WhiteSpace; +import com.google.gwt.dom.client.TextAreaElement; +import com.google.gwt.event.dom.client.KeyCodes; +import com.google.gwt.event.dom.client.KeyDownEvent; +import com.google.gwt.event.dom.client.KeyDownHandler; +import com.google.gwt.user.client.DOM; +import com.vaadin.client.BrowserInfo; +import com.vaadin.client.WidgetUtil; +import com.vaadin.client.ui.dd.DragImageModifier; + +/** + * This class represents a multiline textfield (textarea). + * + * @author Vaadin Ltd. + * + */ +public class VTextArea extends VTextField implements DragImageModifier { + + public static final String CLASSNAME = "v-textarea"; + + private EnterDownHandler enterDownHandler = new EnterDownHandler(); + private boolean wordWrap = true; + + private class EnterDownHandler implements KeyDownHandler { + @Override + public void onKeyDown(KeyDownEvent event) { + // Fix for #12424/13811 - if the key being pressed is enter, we stop + // propagation of the KeyDownEvents if there were no modifier keys + // also pressed. This prevents shortcuts that are bound to only the + // enter key from being processed but allows usage of e.g. + // shift-enter or ctrl-enter. + if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER + && !event.isAnyModifierKeyDown()) { + event.stopPropagation(); + } + } + } + + public VTextArea() { + super(DOM.createTextArea()); + setStyleName(CLASSNAME); + addKeyDownHandler(enterDownHandler); + } + + public TextAreaElement getTextAreaElement() { + return super.getElement().cast(); + } + + public void setRows(int rows) { + getTextAreaElement().setRows(rows); + } + + public void setWordWrap(boolean wordWrap) { + if (wordWrap == this.wordWrap) { + return; + } + if (wordWrap) { + getElement().removeAttribute("wrap"); + getElement().getStyle().clearOverflow(); + getElement().getStyle().clearWhiteSpace(); + } else { + getElement().setAttribute("wrap", "off"); + getElement().getStyle().setOverflow(Overflow.AUTO); + getElement().getStyle().setWhiteSpace(WhiteSpace.PRE); + } + if (BrowserInfo.get().isOpera() + || (BrowserInfo.get().isWebkit() && wordWrap)) { + // Opera fails to dynamically update the wrap attribute so we detach + // and reattach the whole TextArea. + // Webkit fails to properly reflow the text when enabling wrapping, + // same workaround + WidgetUtil.detachAttach(getElement()); + } + this.wordWrap = wordWrap; + } + + @Override + public void modifyDragImage(Element element) { + // Fix for #13557 - drag image doesn't show original text area text. + // It happens because "value" property is not copied into the cloned + // element + String value = getElement().getPropertyString("value"); + if (value != null) { + element.setPropertyString("value", value); + } + } +} diff --git a/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java b/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java new file mode 100644 index 0000000000..9ad4c8b277 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/ui/textarea/TextAreaConnector.java @@ -0,0 +1,75 @@ +/* + * Copyright 2000-2016 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.client.ui.textarea; + +import com.google.gwt.dom.client.Style; +import com.google.gwt.event.dom.client.MouseUpEvent; +import com.google.gwt.event.dom.client.MouseUpHandler; +import com.vaadin.client.ui.VTextArea; +import com.vaadin.client.ui.textfield.TextFieldConnector; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.textarea.TextAreaServerRpc; +import com.vaadin.shared.ui.textarea.TextAreaState; +import com.vaadin.ui.TextArea; + +@Connect(TextArea.class) +public class TextAreaConnector extends TextFieldConnector { + + @Override + public TextAreaState getState() { + return (TextAreaState) super.getState(); + } + + @Override + public VTextArea getWidget() { + return (VTextArea) super.getWidget(); + } + + @Override + protected void init() { + super.init(); + getWidget().addMouseUpHandler(new ResizeMouseUpHandler()); + } + + /* + * Workaround to handle the resize on the mouse up. + */ + private class ResizeMouseUpHandler implements MouseUpHandler { + + @Override + public void onMouseUp(MouseUpEvent event) { + Style elementStyle = getWidget().getElement().getStyle(); + + String newHeight = elementStyle.getHeight(); + String newWidth = elementStyle.getWidth(); + + if (newHeight == null) { + newHeight = ""; + } + if (newWidth == null) { + newWidth = ""; + } + + if (!newHeight.equals(getState().height)) { + getRpcProxy(TextAreaServerRpc.class).setHeight(newHeight); + } + if (!newWidth.equals(getState().width)) { + getRpcProxy(TextAreaServerRpc.class).setWidth(newWidth); + } + } + } +} diff --git a/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java b/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java index 2c1c64b633..46e02c18ee 100644 --- a/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/textfield/TextFieldConnector.java @@ -13,7 +13,6 @@ * License for the specific language governing permissions and limitations under * the License. */ - package com.vaadin.client.ui.textfield; import com.google.gwt.core.client.Scheduler; |