/** * Configures a field with the settings set for this FieldBinder. * <p> * By default this updates the buffered, read only and enabled state of the * field. Also adds validators when applicable. Fields with read only data * source are always configured as read only. * * @param field * The field to update */ protected void configureField(Field<?> field) { field.setBuffered(isBuffered()); field.setEnabled(isEnabled()); if (field.getPropertyDataSource().isReadOnly()) { field.setReadOnly(true); } else { field.setReadOnly(isReadOnly()); } }
protected List<String> getNames() { Map<Object, String> objectToNameMap = provider.getNamesMap(); if (fieldProvider == null) { return new ArrayList<>(objectToNameMap.values()); } final Map<Object, String> clonedMap = new HashMap<>(objectToNameMap); final Map<Object, Field<String>> fieldValuesToMerge = fieldProvider.getObjectFieldMap(); for (Map.Entry<Object, Field<String>> eachEntry : fieldValuesToMerge.entrySet()) { clonedMap.put(eachEntry.getKey(), eachEntry.getValue().getValue()); } return new ArrayList<>(clonedMap.values()); }
/** * Clears field and any possible existing binding. * * @param field * The field to be cleared * @since 7.7.5 */ protected void clearField(Field<?> field) { // Clear any possible existing binding to clear the field field.setPropertyDataSource(null); boolean fieldReadOnly = field.isReadOnly(); if (!fieldReadOnly) { field.clear(); } else { // Temporarily make the field read-write so we can clear the // value. Needed because setPropertyDataSource(null) does not // currently clear the field // (https://dev.vaadin.com/ticket/14733) field.setReadOnly(false); field.clear(); field.setReadOnly(true); } }
private void setVisible(Field<?> field, boolean visible) { field.setEnabled(visible); field.setRequired(visible); field.setVisible(visible); }
private void enableField(Field<?> field, boolean enabled) { field.setEnabled(enabled); // we revert changes, when we disable the fields if (!enabled) { field.discard(); // hide validation errors when disabled if (field instanceof AbstractComponent) { ((AbstractComponent) field).setComponentError(null); } } } @Override
+ propertyId + "' can not be found."); final Object value = oldField.getPropertyDataSource() == null ? oldField.getValue() : oldField.getPropertyDataSource().getValue(); newField.setCaption(oldField.getCaption()); newField.setReadOnly(oldField.isReadOnly()); newField.setBuffered(oldField.isBuffered()); final Property<?> property = oldField.getPropertyDataSource(); oldField.setPropertyDataSource(null); newField.setPropertyDataSource(property); fields.put(propertyId, newField); newField.addListener(fieldValueChangeListener); oldField.removeListener(fieldValueChangeListener);
@Test @Ignore("See http://dev.vaadin.com/ticket/10663") public void multiSelectTwinColFieldTest() throws Exception { // GIVEN definition.setMultiselect(true); twinSelect = new TwinColSelectFieldFactory(definition, baseItem, null, null, new MockComponentProvider()); Field field = twinSelect.createField(); // WHEN ArrayList<String> selected = new ArrayList<>(); selected.add("1"); selected.add("3"); field.setValue(selected); // THEN assertEquals(2, ((Collection) field.getValue()).toArray().length); assertEquals("1", ((Collection) field.getValue()).toArray()[0]); assertEquals("3", ((Collection) field.getValue()).toArray()[1]); }
/** * Sets the read only state to the given value for all fields with writable * data source. Fields with read only data source will always be set to read * only. * * @param fieldsReadOnly * true to set the fields with writable data source to read only, * false to set them to read write */ public void setReadOnly(boolean fieldsReadOnly) { readOnly = fieldsReadOnly; for (Field<?> field : getFields()) { if (field.getPropertyDataSource() == null || !field.getPropertyDataSource().isReadOnly()) { field.setReadOnly(fieldsReadOnly); } else { field.setReadOnly(true); } } }
@SuppressWarnings("unchecked") protected Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) { ColumnModel model = getColumnModel(String.valueOf(propertyId)); boolean editable = tableEditable && itemId.equals(editableId) && model.isEditable() ; Field<?> f = null; Class<?> type = container.getType(propertyId); f = createFieldForType(type); f.setCaption(null); f.setWidth("100%"); f.setReadOnly(!editable); if (f instanceof AbstractField) { if (model.getConverter() == null) { model.setConverter(findDefaultConverter(model,type)); } if (model.getConverter() != null) ((AbstractField<String>)f).setConverter((Converter<String, ?>)model.generateConverter(type)); } return f; }
private void commitTransactions() { for (Field<?> f : fieldToPropertyId.keySet()) { ((Property.Transactional<?>) f.getPropertyDataSource()).commit(); } }
keyLength.setEnabled(false); keyLength.setReadOnly(true); createButton.setEnabled(false); publicKey.setReadOnly(true);
@Test public void testGetHiddenFieldPropertyDataSourceWhenItemNodeDoesNotIncludeHiddenProperty() throws Exception { // GIVEN definition.setDefaultValue("test"); factory = new HiddenFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); factory.setComponentProvider(componentProvider); // WHEN Field<?> field = factory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertNotNull(p); assertEquals("test", p.getValue().toString()); assertEquals("test", baseItem.getItemProperty("hiddenProperty").getValue()); assertEquals("test", field.getValue()); }
@Override public void selectionChanged(SelectionChangedEvent changeEvent) { if (parameter != null) { try { blockListenerOrValidators = true; setData(changeEvent.getSelectedBean()); setItemId(changeEvent.getSelectedItemId()); final BeanItem beanItem = new BeanItem(changeEvent.getSelectedBean(), parameter.getEditablePropertyName(), parameter.getNonEditablePropertyName()); // this is a hack, but we need to know if the selection changed beanItem.addItemProperty("selected", changeEvent.getSelectedItem().getItemProperty("selected")); captionLabel.setValue(String.format("<b>%s</b>", parameter.getCaption())); fieldGroup = new FieldGroup(); fieldGroup.setBuffered(false); fieldGroup.bind(selectedField, "selected"); fieldGroup.bind(editableField, parameter.getEditablePropertyName()); fieldGroup.bind(nonEditableField, parameter.getNonEditablePropertyName()); fieldGroup.setItemDataSource(beanItem); fieldGroup.getField(parameter.getNonEditablePropertyName()).setCaption(parameter.getNonEditablePropertyCaption()); fieldGroup.getField(parameter.getNonEditablePropertyName()).setReadOnly(true); fieldGroup.getField(parameter.getEditablePropertyName()).setCaption(parameter.getEditablePropertyCaption()); fieldGroup.getField(parameter.getEditablePropertyName()).setReadOnly(false); updateEnabledState(); UIHelper.validateField(editableField, true); } finally { blockListenerOrValidators = false; } } }
@Override public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) { Field<?> field = super.createField(container, itemId, propertyId, uiContext); if (propertyId.equals("Key")) { field.setReadOnly(true); } else { field.setSizeFull(); } return field; } });
/** * Binds an item property to a field generated by TableFieldFactory. The * default behavior is to bind property straight to Field. If * Property.Viewer type property (e.g. PropertyFormatter) is already set for * field, the property is bound to that Property.Viewer. * * @param rowId * @param colId * @param property * @param field * @since 6.7.3 */ protected void bindPropertyToField(Object rowId, Object colId, Property property, Field field) { // check if field has a property that is Viewer set. In that case we // expect developer has e.g. PropertyFormatter that he wishes to use and // assign the property to the Viewer instead. boolean hasFilterProperty = field.getPropertyDataSource() != null && (field.getPropertyDataSource() instanceof Property.Viewer); if (hasFilterProperty) { ((Property.Viewer) field.getPropertyDataSource()) .setPropertyDataSource(property); } else { field.setPropertyDataSource(property); } }
@Test public void simpleInitializationTest() { // GIVEN fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<Object> field = fieldFactory.createField(); // THEN assertTrue(TextField.class.isAssignableFrom(field.getClass())); assertEquals(definition, fieldFactory.getFieldDefinition()); assertEquals(false, field.isRequired()); assertEquals("label", field.getCaption()); assertEquals(false, field.getPropertyDataSource().isReadOnly()); assertEquals(true, field.getPropertyDataSource() instanceof TransformedProperty); }
/** * Sets the component's to read-only mode to the specified state. * * @see Component#setReadOnly(boolean) */ @Override public void setReadOnly(boolean readOnly) { super.setReadOnly(readOnly); for (final Object id : propertyIds) { fields.get(id).setReadOnly(readOnly); } }
@Test public void setReadOnlyDefaultValue() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); checkBoxField = new CheckBoxFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); checkBoxField.setComponentProvider(componentProvider); definition.setDefaultValue("true"); definition.setReadOnly(true); // WHEN Field<Boolean> field = checkBoxField.createField(); // THEN assertEquals(true, field.getValue()); assertTrue(field.isReadOnly()); CheckBox checkBox = (CheckBox) ((CheckBoxField) field).iterator().next(); assertEquals(true, checkBox.getValue()); assertTrue(checkBox.isReadOnly()); }
@Test public void changePropertyValueTest() throws Exception { // GIVEN fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN field.setValue("new Value"); // THEN Node res = ((JcrNodeAdapter) baseItem).applyChanges(); assertEquals(true, res.hasProperty(propertyName)); assertEquals("new Value", res.getProperty(propertyName).getString()); assertEquals(PropertyType.STRING, res.getProperty(propertyName).getType()); Property p = baseItem.getItemProperty(propertyName); assertEquals(field.getPropertyDataSource().getValue(), p.getValue()); assertEquals("new Value", p.getValue()); assertEquals(String.class, p.getValue().getClass()); }
getAnnotation( NotNull.class); if (notNullAnnotation != null && !field.isReadOnly()) { field.setRequired(true); Locale locale = getLocale(); if (locale == null) { getField(property).setRequiredError(msg);