Skip to content

Commit

Permalink
JBIDE-21764 Duplicates of environment variables should be forbidden
Browse files Browse the repository at this point in the history
  • Loading branch information
scabanovich authored and adietish committed Feb 26, 2016
1 parent 7a2589b commit 36cb020
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Expand Up @@ -8,9 +8,10 @@
******************************************************************************/
package org.jboss.tools.openshift.internal.ui.validator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
Expand All @@ -30,13 +31,16 @@ public class EnvironmentVarKeyValidator implements IValidator {
+ "including the character '_', allowed anywhere except first position.";

private final IStatus FAILED;
private static final IStatus NAME_IS_USED_ERROR = ValidationStatus.error("An environment variable with this name already exists");
private Collection<String> usedKeys;

public EnvironmentVarKeyValidator() {
this("environment variable name");
public EnvironmentVarKeyValidator(Collection<String> usedKeys) {
this("environment variable name", usedKeys);
}

public EnvironmentVarKeyValidator(String element) {
public EnvironmentVarKeyValidator(String element, Collection<String> usedKeys) {
FAILED = ValidationStatus.error(NLS.bind(failureMessage, element));
this.usedKeys = usedKeys != null ? usedKeys : new ArrayList<String>(0);
}

@Override
Expand All @@ -47,6 +51,9 @@ public IStatus validate(Object paramObject) {
if(!CIDENTIFIER_REGEXP.matcher(value).matches()) {
return FAILED;
}
if(usedKeys.contains(value)) {
return NAME_IS_USED_ERROR;
}

return ValidationStatus.OK_STATUS;
}
Expand Down
Expand Up @@ -11,6 +11,10 @@
package org.jboss.tools.openshift.internal.ui.wizard.deployimage;


import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
Expand Down Expand Up @@ -273,14 +277,16 @@ public void doubleClick(DoubleClickEvent event) {

private void handleEvent() {
EnvironmentVariable var = UIUtils.getFirstElement(envViewer.getSelection(), EnvironmentVariable.class);
Set<String> usedKeys = model.getEnvironmentVariables().stream().map(v -> v.getKey()).collect(Collectors.toSet());
usedKeys.remove(var.getKey());
IKeyValueWizardModel<IKeyValueItem> dialogModel = new KeyValueWizardModelBuilder<IKeyValueItem>(var)
.windowTitle(ENVIRONMENT_VARIABLE_LABEL)
.title("Edit " + ENVIRONMENT_VARIABLE_LABEL)
.description(NLS.bind("Edit the {0}.", ENVIRONMENT_VARIABLE_LABEL.toLowerCase()))
.keyLabel(ENVIRONMENT_VARIABLE_LABEL)
.editableKey(var.isNew())
.groupLabel(ENVIRONMENT_VARIABLE_LABEL)
.keyAfterConvertValidator(new EnvironmentVarKeyValidator())
.keyAfterConvertValidator(new EnvironmentVarKeyValidator(usedKeys))
.build();
OkCancelButtonWizardDialog dialog =
new OkCancelButtonWizardDialog(getShell(),
Expand All @@ -296,13 +302,14 @@ private SelectionListener onAdd() {
return new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Set<String> usedKeys = model.getEnvironmentVariables().stream().map(v -> v.getKey()).collect(Collectors.toSet());
IKeyValueWizardModel<KeyValueItem> dialogModel = new KeyValueWizardModelBuilder<KeyValueItem>()
.windowTitle(ENVIRONMENT_VARIABLE_LABEL)
.title("Add " + ENVIRONMENT_VARIABLE_LABEL)
.description(NLS.bind("Add an {0}.", ENVIRONMENT_VARIABLE_LABEL.toLowerCase()))
.keyLabel("Name")
.groupLabel(ENVIRONMENT_VARIABLE_LABEL)
.keyAfterConvertValidator(new EnvironmentVarKeyValidator())
.keyAfterConvertValidator(new EnvironmentVarKeyValidator(usedKeys))
.build();
OkCancelButtonWizardDialog dialog =
new OkCancelButtonWizardDialog(getShell(),
Expand Down

0 comments on commit 36cb020

Please sign in to comment.