blob: 968719ac35330265b9f6497f244fd5f86608889c [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.openaz.xacml.admin.view.components;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.openaz.xacml.admin.jpa.Attribute;
import org.apache.openaz.xacml.admin.jpa.ConstraintType;
import org.apache.openaz.xacml.admin.jpa.ConstraintValue;
import org.apache.openaz.xacml.admin.util.AdminNotification;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.Validator;
import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
public class RegexpEditorComponent extends CustomComponent {
@AutoGenerated
private VerticalLayout mainLayout;
@AutoGenerated
private Panel panelTester;
@AutoGenerated
private VerticalLayout verticalLayout_2;
@AutoGenerated
private Button buttonTest;
@AutoGenerated
private TextField textFieldTestValue;
@AutoGenerated
private TextField textFieldExpression;
private static final long serialVersionUID = -1L;
private static final Log logger = LogFactory.getLog(RegexpEditorComponent.class);
private RegexpEditorComponent self = this;
private final Attribute attribute;
private ConstraintValue value = null;
/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
public RegexpEditorComponent(Attribute attribute) {
buildMainLayout();
setCompositionRoot(mainLayout);
this.attribute = attribute;
initializeTextField();
initializeTestPanel();
}
private void initializeTextField() {
//
// Find our reg expression value if it exists
//
for (ConstraintValue value : this.attribute.getConstraintValues()) {
if (value.getProperty().equals(ConstraintType.REGEXP_TYPE)) {
if (this.value == null) {
this.textFieldExpression.setValue(value.getValue());
this.value = value;
} else {
logger.warn("Multiple regexp constraint values: " + value);
value.setAttribute(null);
this.attribute.removeConstraintValue(value);
}
} else {
value.setAttribute(null);
this.attribute.removeConstraintValue(value);
}
}
//
// Was something found?
//
if (this.value == null) {
ConstraintValue newValue = new ConstraintValue(ConstraintType.REGEXP_TYPE, "");
newValue.setAttribute(this.attribute);
this.attribute.addConstraintValue(newValue);
this.value = newValue;
}
//
// Save the values when they changes
//
this.textFieldExpression.addValueChangeListener(new ValueChangeListener() {
private static final long serialVersionUID = 1L;
@Override
public void valueChange(ValueChangeEvent event) {
self.value.setValue(self.textFieldExpression.getValue());
}
});
}
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);
}
});
}
@AutoGenerated
private VerticalLayout buildMainLayout() {
// common part: create layout
mainLayout = new VerticalLayout();
mainLayout.setImmediate(false);
mainLayout.setWidth("-1px");
mainLayout.setHeight("-1px");
mainLayout.setMargin(true);
mainLayout.setSpacing(true);
// top-level component properties
setWidth("-1px");
setHeight("-1px");
// textFieldExpression
textFieldExpression = new TextField();
textFieldExpression.setCaption("Regular Expression");
textFieldExpression.setImmediate(true);
textFieldExpression
.setDescription("Create a regular expression used to constrain attribute values.");
textFieldExpression.setWidth("-1px");
textFieldExpression.setHeight("-1px");
textFieldExpression.setInvalidAllowed(false);
textFieldExpression.setInputPrompt("eg. [a-zA-Z0-9]");
mainLayout.addComponent(textFieldExpression);
mainLayout.setExpandRatio(textFieldExpression, 1.0f);
// panelTester
panelTester = buildPanelTester();
mainLayout.addComponent(panelTester);
mainLayout.setExpandRatio(panelTester, 1.0f);
return mainLayout;
}
@AutoGenerated
private Panel buildPanelTester() {
// common part: create layout
panelTester = new Panel();
panelTester.setCaption("Test The Expression");
panelTester.setImmediate(false);
panelTester.setWidth("-1px");
panelTester.setHeight("-1px");
// verticalLayout_2
verticalLayout_2 = buildVerticalLayout_2();
panelTester.setContent(verticalLayout_2);
return panelTester;
}
@AutoGenerated
private VerticalLayout buildVerticalLayout_2() {
// common part: create layout
verticalLayout_2 = new VerticalLayout();
verticalLayout_2.setImmediate(false);
verticalLayout_2.setWidth("100.0%");
verticalLayout_2.setHeight("100.0%");
verticalLayout_2.setMargin(false);
// textFieldTestValue
textFieldTestValue = new TextField();
textFieldTestValue.setCaption("Test Value");
textFieldTestValue.setImmediate(true);
textFieldTestValue
.setDescription("Enter a value to match against the regular expression.");
textFieldTestValue.setWidth("-1px");
textFieldTestValue.setHeight("-1px");
textFieldTestValue.setInputPrompt("eg. example");
verticalLayout_2.addComponent(textFieldTestValue);
// buttonTest
buttonTest = new Button();
buttonTest.setCaption("Test");
buttonTest.setImmediate(true);
buttonTest.setWidth("-1px");
buttonTest.setHeight("-1px");
verticalLayout_2.addComponent(buttonTest);
verticalLayout_2.setComponentAlignment(buttonTest, new Alignment(48));
return verticalLayout_2;
}
}