com.vaadin.data.Validator Java Examples

The following examples show how to use com.vaadin.data.Validator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpServerConfigForm.java    From sensorhub with Mozilla Public License 2.0 6 votes vote down vote up
@Override
protected Field<?> buildAndBindField(String label, String propId, Property<?> prop)
{
    Field<?> field = super.buildAndBindField(label, propId, prop);
    
    if (propId.equals(PROP_SERVLET_ROOT))
    {
        field.addValidator(new StringLengthValidator(MSG_REQUIRED_FIELD, 2, 256, false));
    }
    else if (propId.equals(PROP_HTTP_PORT))
    {
        field.setWidth(100, Unit.PIXELS);
        //((TextField)field).getConverter().
        field.addValidator(new Validator() {
            private static final long serialVersionUID = 1L;
            public void validate(Object value) throws InvalidValueException
            {
                int portNum = (Integer)value;
                if (portNum > 10000 || portNum <= 80)
                    throw new InvalidValueException("Port number must be an integer number greater than 80 and lower than 10000");
            }
        });
    }
    
    return field;
}
 
Example #2
Source File: WinServerNetworkConfig.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
private void initValidation() {
    //ネットワーク選択
    String message = ViewMessages.getMessage("IUI-000100");
    networkSelect.setRequired(true);
    networkSelect.setRequiredError(message);

    //IPモード選択
    message = ViewMessages.getMessage("IUI-000126");
    ipModeSelect.setRequired(true);
    ipModeSelect.setRequiredError(message);

    //IPアドレス
    message = ViewMessages.getMessage("IUI-000094", ipAddressField.getCaption());
    ipAddressField.setRequired(false);
    ipAddressField.setRequiredError(message);
    Validator ipAddressFieldValidator = new RegexpValidator(
            "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$",
            ViewMessages.getMessage("IUI-000095", ipAddressField.getCaption()));
    ipAddressField.addValidator(ipAddressFieldValidator);
}
 
Example #3
Source File: NumericQuestion.java    From gazpachoquest with GNU General Public License v3.0 6 votes vote down vote up
public boolean isValid(TextField field, String newValue) {
    boolean valid = true;

    if (StringUtils.isBlank(newValue)) {
        if (field.isRequired()) {
            valid = false;
        }
    } else {
        for (Validator v : field.getValidators()) {
            try {
                v.validate(newValue);
            } catch (InvalidValueException e) {
                valid = false;
                logger.warn(e.getMessage());
                answerField.setComponentError(new UserError(e.getMessage()));
            }
        }
    }
    if (valid) {
        answerField.setComponentError(null);
    }
    return valid;
}
 
Example #4
Source File: AgentForm.java    From doecode with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public AgentForm(RepositoryForm form) {
    this.parent = form;
    
    setSizeUndefined();
    
    GridLayout formContainer = new GridLayout(2,5);
    formContainer.setSpacing(true);
    
    agentTypeCode.addItems(Agent.agentTypes);
    agentTypeSpecific.addItems(Agent.specificTypes);
    
    Validator phoneValidator = new RegexpValidator("^((\\d{3}-|\\(\\d{3}\\)\\s?)?\\d{3}-|^\\d{3}(\\.)?\\d{3}\\3)\\d{4}$", "Please enter a valid phone number.");
    phoneNumber.addValidator(phoneValidator);
            
    formContainer.addComponent(firstName);
    formContainer.addComponent(lastName);
    formContainer.addComponent(agentTypeCode,0,1);
    formContainer.addComponent(agentTypeSpecific,1,1);
    formContainer.addComponent(email,0,2);
    formContainer.addComponent(affiliation,1,2);
    formContainer.addComponent(orcid,0,3);
    formContainer.addComponent(phoneNumber,1,3);
    
    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    buttons.addComponents(save, delete);
    
    save.setStyleName(ValoTheme.BUTTON_FRIENDLY);
    delete.setStyleName(ValoTheme.BUTTON_DANGER);
    
    save.setEnabled(false);
    delete.setEnabled(false);
    
    save.addClickListener(e->this.save());
    delete.addClickListener(e->this.delete());
    
    formContainer.addComponent(buttons, 0,4,1,4);
    
    addComponent(formContainer);
}
 
Example #5
Source File: CommonDialogWindow.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private boolean hasNullValidator(final Component component) {

        if (component instanceof AbstractField<?>) {
            final AbstractField<?> fieldComponent = (AbstractField<?>) component;
            for (final Validator validator : fieldComponent.getValidators()) {
                if (validator instanceof NullValidator) {
                    return true;
                }
            }
        }
        return false;
    }
 
Example #6
Source File: WinServerEdit.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
private void initValidation() {
    String message;
    message = ViewMessages.getMessage("IUI-000094", ipAddressField.getCaption());
    Validator ipAddressFieldValidator = new RegexpValidator(
            "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$",
            ViewMessages.getMessage("IUI-000095", ipAddressField.getCaption()));

    ipAddressField.setRequired(true);
    ipAddressField.setRequiredError(message);
    ipAddressField.addValidator(ipAddressFieldValidator);

    message = ViewMessages.getMessage("IUI-000094", subnetMaskField.getCaption());
    Validator subnetMaskFieldValidator = new RegexpValidator(
            "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$",
            ViewMessages.getMessage("IUI-000095", subnetMaskField.getCaption()));
    subnetMaskField.setRequired(true);
    subnetMaskField.setRequiredError(message);
    subnetMaskField.addValidator(subnetMaskFieldValidator);

    message = ViewMessages.getMessage("IUI-000094", defaultGatewayField.getCaption());
    Validator defaultGatewayFieldValidator = new RegexpValidator(
            "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$",
            ViewMessages.getMessage("IUI-000095", defaultGatewayField.getCaption()));
    defaultGatewayField.setRequired(true);
    defaultGatewayField.setRequiredError(message);
    defaultGatewayField.addValidator(defaultGatewayFieldValidator);
}
 
Example #7
Source File: ValidatorFactory.java    From XACML with MIT License 5 votes vote down vote up
public static Validator	newInstance(Datatype datatype) {
	
	if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_ANYURI)) {
		return new AnyURIValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_BASE64BINARY)) {
		return new Base64BinaryValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_BOOLEAN)) {
		return new BooleanValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_DATE)) {
		return new DateValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_DATETIME)) {
		return new DateTimeValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_DAYTIMEDURATION)) {
		return new DayTimeDurationValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_DNSNAME)) {
		return new DNSNameValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_DOUBLE)) {
		return new DoubleValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_HEXBINARY)) {
		return new HexBinaryValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_INTEGER)) {
		return new IntegerValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_IPADDRESS)) {
		return new IpAddressValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_RFC822NAME)) {
		return new RFC822NameValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_STRING)) {
		return new StringValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_TIME)) {
		return new TimeValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_X500NAME)) {
		return new X500NameValidator();
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_XPATHEXPRESSION)) {
		
	} else if (datatype.getIdentifer().equals(XACML3.ID_DATATYPE_YEARMONTHDURATION)) {
		return new YearMonthDurationValidator();
	}
	
	return null;
}
 
Example #8
Source File: RegexpEditorComponent.java    From XACML with MIT License 5 votes vote down vote up
private void initializeTestPanel() {
	this.textFieldTestValue.setNullRepresentation("");
	
	this.buttonTest.addClickListener(new ClickListener() {
		private static final long serialVersionUID = 1L;

		@Override
		public void buttonClick(ClickEvent event) {
			String testValue = self.textFieldTestValue.getValue();
			if (testValue == null || testValue.length() == 0) {
				return;
			}
			String regExp = self.textFieldExpression.getValue();
			if (regExp == null || regExp.length() == 0) {
				return;
			}
			//
			// Create a validator
			//
			Validator validator = new RegexpValidator(regExp, true, "Regular Expression does NOT match.");
			//
			// Add it
			//
			self.textFieldTestValue.addValidator(validator);
			//
			// Validate
			//
			try {
				self.textFieldTestValue.validate();
				AdminNotification.info("Success! Regular Expression Matches");
			} catch (InvalidValueException e) {
				AdminNotification.warn("Failed, Regular Expression does NOT match");
			}
			//
			// Remove the validator
			//
			self.textFieldTestValue.removeValidator(validator);
		}
	});
}
 
Example #9
Source File: Parameter.java    From chipster with MIT License 5 votes vote down vote up
@SuppressWarnings("serial")
private Validator getMaxValueValidator() {
	return new Validator() {

		@Override
		public void validate(Object value) throws InvalidValueException {
			String v = (String) value;

			// empty is ok
			if (v.isEmpty()) {
				return;
			}

			// integer
			ParameterType t = (ParameterType) type.getValue();
			if (t == ParameterType.INTEGER) {
				// must be integer
				int maxValueInt = getInt(v);

				// if min value exists, must be equal or greater
				isGreaterOrEqualToMinValueInt(maxValueInt);

				// TODO check default after max change
			} 

			
			// DECIMAL
			else if (t == ParameterType.DECIMAL) {
				double maxValueDouble = getDouble(v);
				isGreaterOrEqualToMinValueDouble(maxValueDouble);
			}
		}
	};
}
 
Example #10
Source File: Parameter.java    From chipster with MIT License 5 votes vote down vote up
@SuppressWarnings("serial")
private Validator getMinValueValidator() {
	return new Validator() {

		@Override
		public void validate(Object value) throws InvalidValueException {
			String v = (String) value;

			// empty is ok
			if (v.isEmpty()) {
				return;
			}

			// integer
			ParameterType t = (ParameterType) type.getValue();
			if (t == ParameterType.INTEGER) {
				// must be integer
				int minValueInt = getInt(v);

				// if min value exists, must be equal or greater
				isSmallerOrEqualToMaxValueInt(minValueInt);
			} 

			
			// DECIMAL
			else if (t == ParameterType.DECIMAL) {
				double minValueDouble = getDouble(v);
				isSmallerOrEqualToMaxValueDouble(minValueDouble);
			}
		}
	};
}
 
Example #11
Source File: WinServerEdit.java    From primecloud-controller with GNU General Public License v2.0 4 votes vote down vote up
private void initValidation() {
    String message;
    message = ViewMessages.getMessage("IUI-000027");
    sizeSelect.setRequired(true);
    sizeSelect.setRequiredError(message);

    message = ViewMessages.getMessage("IUI-000028");
    keySelect.setRequired(true);
    keySelect.setRequiredError(message);

    message = ViewMessages.getMessage("IUI-000029");
    grpSelect.setRequired(true);
    grpSelect.setRequiredError(message);

    if (BooleanUtils.isTrue(platform.getPlatformAws().getEuca())) {
        // Eucalyptus の場合は入力必須
        message = ViewMessages.getMessage("IUI-000050");
        zoneSelect.setRequired(true);
        zoneSelect.setRequiredError(message);
    }

    if (BooleanUtils.isTrue(platform.getPlatformAws().getVpc())) {
        // VPCの場合
        message = ViewMessages.getMessage("IUI-000108");
        subnetSelect.setRequired(true);
        subnetSelect.setRequiredError(message);

        privateIpField.setRequired(false);
        Validator privateIpFieldValidator = new RegexpValidator(
                "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$",
                ViewMessages.getMessage("IUI-000095", privateIpField.getCaption()));
        privateIpField.addValidator(privateIpFieldValidator);
    }

    if (BooleanUtils.isTrue(image.getImageAws().getEbsImage()) && image.getImageAws().getRootSize() != null) {
        int maxRootSize = NumberUtils.toInt(Config.getProperty("aws.maxRootSize"), 1024);
        message = ViewMessages.getMessage("IUI-000135", image.getImageAws().getRootSize(), maxRootSize);
        rootSizeField.setRequired(false);
        rootSizeField.addValidator(
                new IntegerRangeValidator(image.getImageAws().getRootSize(), maxRootSize, message));
    }

    if (elasticIpSelect != null) {
        message = ViewMessages.getMessage("IUI-000063");
        elasticIpSelect.setRequired(true);
        elasticIpSelect.setRequiredError(message);
    }
}
 
Example #12
Source File: WinServerEdit.java    From primecloud-controller with GNU General Public License v2.0 4 votes vote down vote up
public void show() {
    // サーバサイズ
    for (String instanceType : image.getImageVmware().getInstanceTypes().split(",")) {
        sizeSelect.addItem(instanceType.trim());
    }
    sizeSelect.select(instance.getVmwareInstance().getInstanceType());

    // キーペア
    for (VmwareKeyPair vmwareKeyPair : vmwareKeyPairs) {
        Item item = keySelect.addItem(vmwareKeyPair.getKeyNo());
        item.getItemProperty(KEY_CAPTION_ID).setValue(vmwareKeyPair.getKeyName());
    }
    keySelect.select(instance.getVmwareInstance().getKeyPairNo());

    // クラスタ
    clusterSelect.setContainerDataSource(new IndexedContainer(clusters));
    clusterSelect.select(instance.getVmwareInstance().getComputeResource());
    if (StringUtils.isNotEmpty(instance.getVmwareInstance().getDatastore())) {
        clusterSelect.setEnabled(false);
    }

    // ルートディスクサイズ
    rootSizeField.setValue(ObjectUtils.toString(instance.getVmwareInstance().getRootSize(), ""));
    if (image.getImageVmware().getRootSize() == null) {
        rootSizeField.setEnabled(false);
    }

    // サーバが停止していない場合、詳細設定タブ自体を変更できないようにする
    InstanceStatus status = InstanceStatus.fromStatus(instance.getInstance().getStatus());
    if (status != InstanceStatus.STOPPED) {
        form.setEnabled(false);
    }

    // サーバが既に作成済みの場合、いくつかの項目を変更できないようにする
    if (StringUtils.isNotEmpty(instance.getVmwareInstance().getDatastore())) {
        rootSizeField.setEnabled(false);
    }

    // サーバが作成済みの場合、ルートディスクサイズの入力チェックを無効にする
    if (StringUtils.isNotEmpty(instance.getVmwareInstance().getDatastore())) {
        List<Validator> validators = new ArrayList<Validator>(rootSizeField.getValidators());
        for (Validator validator : validators) {
            rootSizeField.removeValidator(validator);
        }
    }
}
 
Example #13
Source File: EditPDPGroupWindow.java    From XACML with MIT License 4 votes vote down vote up
protected void initializeText() {
	this.textName.setNullRepresentation("");
	this.textDescription.setNullRepresentation("");
	if (this.group != null) {
		this.textName.setValue(this.group.getName());
		this.textDescription.setValue(this.group.getDescription());
	}
	//
	// Validation
	//
	this.textName.addValidator(new Validator() {
		private static final long serialVersionUID = 1L;

		@Override
		public void validate(Object value) throws InvalidValueException {
			assert(value instanceof String);
			if (value == null) {
				throw new InvalidValueException("The name cannot be blank.");
			}
			// Group names must be unique so that user can distinguish between them (and we can create unique IDs from them)
			for (PDPGroup g : self.groups) {
				if (group != null && g.getId().equals(group.getId())) {
					// ignore this group - we may or may not be changing the name
					continue;
				}
				if (g.getName().equals(value.toString())) {
					throw new InvalidValueException("Name must be unique");
				}
			}
		}
	});
	this.textName.addTextChangeListener(new TextChangeListener() {
		private static final long serialVersionUID = 1L;

		@Override
		public void textChange(TextChangeEvent event) {
			if (event.getText() == null || event.getText().isEmpty()) {
				self.buttonSave.setEnabled(false);
			} else {
				self.buttonSave.setEnabled(true);
			}
		}
	});
}
 
Example #14
Source File: AttributeValueEditorWindow.java    From XACML with MIT License 4 votes vote down vote up
protected void initializeCombo() {
	this.comboBoxDatatype.setContainerDataSource(((XacmlAdminUI) UI.getCurrent()).getDatatypes());
	this.comboBoxDatatype.setItemCaptionMode(ItemCaptionMode.PROPERTY);
	this.comboBoxDatatype.setItemCaptionPropertyId("xacmlId");
	this.comboBoxDatatype.setFilteringMode(FilteringMode.CONTAINS);
	this.comboBoxDatatype.setImmediate(true);
	this.comboBoxDatatype.setNullSelectionAllowed(false);
	this.comboBoxDatatype.setConverter(new SingleSelectConverter<Object>(this.comboBoxDatatype));
	//
	// Select a value if its defined
	//
	if (this.datatypeRestriction != null) {
		this.comboBoxDatatype.select(this.datatypeRestriction.getId());
	} else if (this.value.getDataType() != null) {
		this.comboBoxDatatype.select(JPAUtils.findDatatype(this.value.getDataType()).getId());
	}
	//
	// Can the user change the datatype?
	//
	if (this.datatypeRestriction != null) {
		this.comboBoxDatatype.setEnabled(false);
		return;
	}
	//
	// Listen to events
	//
	this.comboBoxDatatype.addValueChangeListener(new ValueChangeListener() {
		private static final long serialVersionUID = 1L;

		@Override
		public void valueChange(ValueChangeEvent event) {
			Object id = self.comboBoxDatatype.getValue();
			assert (id != null);
			//
			// Get the entity and save it
			//
			EntityItem<Datatype> entity = ((XacmlAdminUI) UI.getCurrent()).getDatatypes().getItem(id);
			self.value.setDataType(entity.getEntity().getXacmlId());
			//
			// Reset the validator
			//
			self.textFieldValue.removeAllValidators();
			Validator validator = ValidatorFactory.newInstance(entity.getEntity());
			if (validator != null) {
				self.textFieldValue.addValidator(validator);
			}
		}
	});
}
 
Example #15
Source File: AttributeValueEditorWindow.java    From XACML with MIT License 4 votes vote down vote up
protected void initializeTextField() {
	//
	// GUI properties
	//
	this.textFieldValue.setImmediate(true);
	this.textFieldValue.setNullRepresentation("");
	//
	// Setup validator
	//
	if (this.datatypeRestriction != null) {
		Validator validator = ValidatorFactory.newInstance(this.datatypeRestriction);
		if (validator != null) {
			this.textFieldValue.addValidator(validator);
		}
	}
	//
	// Text change or Value Change?
	//
	this.textFieldValue.addValueChangeListener(new ValueChangeListener() {
		private static final long serialVersionUID = 1L;

		@Override
		public void valueChange(ValueChangeEvent event) {
			//
			// Save the new value. TODO - assuming position 0 of content list.
			//
			self.saveValue(0, self.textFieldValue.getValue());
			//
			// Setup the save button
			//
			if (self.textFieldValue.getValue() == null || self.textFieldValue.getValue().isEmpty()) {
				self.buttonSave.setEnabled(false);
			} else {
				self.buttonSave.setEnabled(true);
			}
		}
	});
	//
	// Initialize the value
	//
	if (this.value != null && this.value.getContent().isEmpty() == false) {
		//
		// TODO - If there are multiple Content objects...Right now we work with the first one only.
		//
		this.textFieldValue.setValue(this.value.getContent().get(0).toString());
	}
}
 
Example #16
Source File: Parameter.java    From chipster with MIT License 4 votes vote down vote up
@SuppressWarnings("serial")
private Validator getDefaultValueValidator() {
	return new Validator() {

		@Override
		public void validate(Object value) throws InvalidValueException {
			
			String v = (String) value;
			
			// empty is ok
			if (v.isEmpty()) {
				return;
			}

			ParameterType t = (ParameterType) type.getValue();

			if (t == ParameterType.INTEGER) {
				// must be integer
				int defaultValueInt = getInt(v);
				
				// if min value exists, must be equal or greater
				isGreaterOrEqualToMinValueInt(defaultValueInt);
				
				// if max value exist, must be smaller or equal
				isSmallerOrEqualToMaxValueInt(defaultValueInt);
			}

			// DECIMAL
			else if (t == ParameterType.DECIMAL) {
				// must be double
				double defaultValueDouble = getDouble(v);
				
				// if min value exists, must be equal or greater
				isGreaterOrEqualToMinValueDouble(defaultValueDouble);

				// if max value exist, must be smaller or equal
				isSmallerOrEqualToMaxValueDouble(defaultValueDouble);
			}
		}
	};
}