Browse Source

Declarative support for AbstractTextField (#7749)

Change-Id: Ice244cd73825bde44fe02cf3d4b53d4eff5a5c35
tags/7.4.0.beta1
Matti Hosio 9 years ago
parent
commit
775276d4a9

+ 56
- 0
server/src/com/vaadin/ui/AbstractTextField.java View File

@@ -16,8 +16,12 @@

package com.vaadin.ui;

import java.util.Collection;
import java.util.Map;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;

import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.FieldEvents.BlurNotifier;
@@ -31,6 +35,8 @@ import com.vaadin.server.PaintException;
import com.vaadin.server.PaintTarget;
import com.vaadin.shared.ui.textfield.AbstractTextFieldState;
import com.vaadin.shared.ui.textfield.TextFieldConstants;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;

public abstract class AbstractTextField extends AbstractField<String> implements
BlurNotifier, FocusNotifier, TextChangeNotifier, LegacyComponent {
@@ -757,4 +763,54 @@ public abstract class AbstractTextField extends AbstractField<String> implements
removeBlurListener(listener);
}

/*
* (non-Javadoc)
*
* @see
* com.vaadin.ui.AbstractField#synchronizeFromDesign(org.jsoup.nodes.Element
* , com.vaadin.ui.declarative.DesignContext)
*/
@Override
public void synchronizeFromDesign(Element design,
DesignContext designContext) {
super.synchronizeFromDesign(design, designContext);
AbstractTextField def = designContext.getDefaultInstance(this
.getClass());
Attributes attr = design.attributes();
int maxLength = DesignAttributeHandler.readAttribute("maxlength", attr,
def.getMaxLength(), Integer.class);
setMaxLength(maxLength);
}

/*
* (non-Javadoc)
*
* @see com.vaadin.ui.AbstractField#getCustomAttributes()
*/
@Override
protected Collection<String> getCustomAttributes() {
Collection<String> customAttributes = super.getCustomAttributes();
customAttributes.add("maxlength");
customAttributes.add("max-length"); // to prevent this appearing in
// output
return customAttributes;
}

/*
* (non-Javadoc)
*
* @see
* com.vaadin.ui.AbstractField#synchronizeToDesign(org.jsoup.nodes.Element,
* com.vaadin.ui.declarative.DesignContext)
*/
@Override
public void synchronizeToDesign(Element design, DesignContext designContext) {
super.synchronizeToDesign(design, designContext);
AbstractTextField def = designContext.getDefaultInstance(this
.getClass());
Attributes attr = design.attributes();
DesignAttributeHandler.writeAttribute("maxlength", attr,
getMaxLength(), def.getMaxLength(), Integer.class);
}

}

+ 7
- 2
server/src/com/vaadin/ui/declarative/DesignAttributeHandler.java View File

@@ -403,6 +403,10 @@ public class DesignAttributeHandler implements Serializable {
if (targetType == Resource.class) {
return parseResource(value);
}
if (Enum.class.isAssignableFrom(targetType)) {
return Enum.valueOf((Class<? extends Enum>) targetType,
value.toUpperCase());
}
return null;
}

@@ -519,8 +523,9 @@ public class DesignAttributeHandler implements Serializable {
*/
private static boolean isSupported(Class<?> valueType) {
return valueType != null
&& (valueType.isPrimitive() || supportedClasses
.contains(valueType));
&& (valueType.isPrimitive()
|| supportedClasses.contains(valueType) || Enum.class
.isAssignableFrom(valueType));
}

/**

+ 3
- 1
server/tests/src/com/vaadin/tests/layoutparser/all-components.html View File

@@ -39,7 +39,9 @@
</v-css-layout>
<!-- abstract field -->
<v-text-field buffered validation-visible=false invalid-committed invalid-allowed=false required required-error="This is a required field" conversion-error="Input {0} cannot be parsed" tabindex=3 readonly/>
<v-text-field buffered validation-visible=false invalid-committed invalid-allowed=false required required-error="This is a required field" conversion-error="Input {0} cannot be parsed" tabindex=3 readonly />
<!-- abstract text field -->
<v-text-field null-representation="" null-setting-allowed maxlength=10 columns=5 input-prompt="Please enter a value" text-change-event-mode="eager" text-change-timeout=2 />
</v-vertical-layout>
</body>

+ 73
- 0
server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeFromDesign.java View File

@@ -0,0 +1,73 @@
/*
* 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.server.component.abstracttextfield;

import junit.framework.TestCase;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;

import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.TextField;
import com.vaadin.ui.declarative.DesignContext;

/**
* Test case for reading the attributes of the AbstractTextField from design
*/
public class TestSynchronizeFromDesign extends TestCase {

private DesignContext ctx;

@Override
protected void setUp() throws Exception {
super.setUp();
ctx = new DesignContext();
}

public void testAttributes() {
Element design = createDesign();
AbstractTextField component = getComponent();
component.synchronizeFromDesign(design, ctx);
assertEquals("this-is-null", component.getNullRepresentation());
assertEquals(true, component.isNullSettingAllowed());
assertEquals(5, component.getMaxLength());
assertEquals(3, component.getColumns());
assertEquals("input", component.getInputPrompt());
assertEquals(TextChangeEventMode.EAGER,
component.getTextChangeEventMode());
assertEquals(100, component.getTextChangeTimeout());
}

private AbstractTextField getComponent() {
return new TextField();
}

private Element createDesign() {
Attributes attributes = new Attributes();
attributes.put("null-representation", "this-is-null");
attributes.put("null-setting-allowed", "true");
attributes.put("maxlength", "5");
attributes.put("columns", "3");
attributes.put("input-prompt", "input");
attributes.put("text-change-event-mode", "eager");
attributes.put("text-change-timeout", "100");
Element node = new Element(Tag.valueOf("v-text-field"), "", attributes);
return node;
}

}

+ 73
- 0
server/tests/src/com/vaadin/tests/server/component/abstracttextfield/TestSynchronizeToDesign.java View File

@@ -0,0 +1,73 @@
/*
* 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.server.component.abstracttextfield;

import junit.framework.TestCase;

import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;

import com.vaadin.ui.AbstractTextField;
import com.vaadin.ui.AbstractTextField.TextChangeEventMode;
import com.vaadin.ui.TextField;
import com.vaadin.ui.declarative.DesignContext;

/**
* Test case for writing the attributes of the AbstractTextField to design
*
* @author Vaadin Ltd
*/
public class TestSynchronizeToDesign extends TestCase {

private DesignContext ctx;

@Override
protected void setUp() throws Exception {
super.setUp();
ctx = new DesignContext();
}

public void testSynchronizetestAttributes() {
Element design = createDesign();
AbstractTextField component = getComponent();
component.setNullRepresentation("this-is-null");
component.setNullSettingAllowed(true);
component.setMaxLength(5);
component.setColumns(3);
component.setInputPrompt("input");
component.setTextChangeEventMode(TextChangeEventMode.EAGER);
component.setTextChangeTimeout(100);
component.synchronizeToDesign(design, ctx);
assertEquals("this-is-null", design.attr("null-representation"));
assertEquals("true", design.attr("null-setting-allowed"));
assertEquals("5", design.attr("maxlength"));
assertEquals("3", design.attr("columns"));
assertEquals("input", design.attr("input-prompt"));
assertEquals("EAGER", design.attr("text-change-event-mode"));
assertEquals("100", design.attr("text-change-timeout"));
}

private AbstractTextField getComponent() {
return new TextField();
}

private Element createDesign() {
Attributes attr = new Attributes();
return new Element(Tag.valueOf("v-text-field"), "", attr);
}

}

Loading…
Cancel
Save