org.apache.openaz.xacml.admin.view.windows.AttributeValueEditorWindow.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.openaz.xacml.admin.view.windows.AttributeValueEditorWindow.java

Source

/*
 *  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.windows;

import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;

import org.apache.openaz.xacml.admin.XacmlAdminUI;
import org.apache.openaz.xacml.admin.jpa.Datatype;
import org.apache.openaz.xacml.admin.util.JPAUtils;
import org.apache.openaz.xacml.admin.view.validators.ValidatorFactory;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.fieldfactory.SingleSelectConverter;
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.event.ShortcutAction.KeyCode;
import com.vaadin.shared.ui.combobox.FilteringMode;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
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.ComboBox;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class AttributeValueEditorWindow extends Window {
    /*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

    @AutoGenerated
    private VerticalLayout mainLayout;
    @AutoGenerated
    private Button buttonSave;
    @AutoGenerated
    private TextField textFieldValue;
    @AutoGenerated
    private ComboBox comboBoxDatatype;
    private static final long serialVersionUID = 1L;
    private final AttributeValueEditorWindow self = this;
    private final Datatype datatypeRestriction;
    private final AttributeValueType value;
    private boolean isSaved = false;

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public AttributeValueEditorWindow(AttributeValueType value, Datatype datatypeRestriction) {
        buildMainLayout();
        //setCompositionRoot(mainLayout);
        setContent(mainLayout);
        //
        // Save
        //
        this.value = value;
        this.datatypeRestriction = datatypeRestriction;
        //
        // Make sure the value has the same datatype as the restriction
        //
        if (this.datatypeRestriction != null) {
            this.value.setDataType(this.datatypeRestriction.getXacmlId());
        }
        //
        // Set our shortcuts
        //
        this.setCloseShortcut(KeyCode.ESCAPE);
        //
        // Initialize GUI
        //
        this.initializeCombo();
        this.initializeTextField();
        this.initializeButtons();
        //
        // Focus?
        //
        if (this.datatypeRestriction == null) {
            this.comboBoxDatatype.focus();
        } else {
            this.textFieldValue.focus();
        }
    }

    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);
                }
            }
        });
    }

    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());
        }
    }

    protected void saveValue(int i, String value) {
        //
        // Get the content - TODO
        //
        if (this.value.getContent().isEmpty()) {
            this.value.getContent().add(value);
            return;
        }
        Object o = this.value.getContent().get(i);
        if (o == null) {
            //
            // Add it into the list - ensure its a
            //
            assert this.value.getContent().size() == i;
            this.value.getContent().add(value);
        } else {
            //
            // Overwrite the previous object
            //
            this.value.getContent().set(i, value);
        }
    }

    protected void initializeButtons() {
        this.buttonSave.setClickShortcut(KeyCode.ENTER);
        this.buttonSave.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                try {
                    //
                    // Make sure it validates (i.e. call the Validators)
                    //
                    self.comboBoxDatatype.validate();
                    self.textFieldValue.validate();
                    //
                    // Yes
                    //
                    self.isSaved = true;
                    //
                    // Close
                    //
                    self.close();
                } catch (InvalidValueException e) { //NOPMD
                    //
                    // Vaadin with update GUI displaying the error
                    //
                }
            }
        });
    }

    public boolean isSaved() {
        return this.isSaved;
    }

    public String getValue() {
        return this.textFieldValue.getValue();
    }

    public AttributeValueType getAttribute() {
        return this.value;
    }

    public Datatype getDatatype() {
        return JPAUtils.findDatatype(this.value.getDataType());
    }

    @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");

        // comboBoxDatatype
        comboBoxDatatype = new ComboBox();
        comboBoxDatatype.setCaption("Select Datatype");
        comboBoxDatatype.setImmediate(false);
        comboBoxDatatype.setWidth("-1px");
        comboBoxDatatype.setHeight("-1px");
        comboBoxDatatype.setInvalidAllowed(false);
        comboBoxDatatype.setRequired(true);
        mainLayout.addComponent(comboBoxDatatype);

        // textFieldValue
        textFieldValue = new TextField();
        textFieldValue.setCaption("Attribute Value");
        textFieldValue.setImmediate(false);
        textFieldValue.setWidth("100.0%");
        textFieldValue.setHeight("-1px");
        textFieldValue.setInvalidAllowed(false);
        textFieldValue.setRequired(true);
        mainLayout.addComponent(textFieldValue);

        // buttonSave
        buttonSave = new Button();
        buttonSave.setCaption("Save");
        buttonSave.setImmediate(true);
        buttonSave.setWidth("-1px");
        buttonSave.setHeight("-1px");
        mainLayout.addComponent(buttonSave);
        mainLayout.setComponentAlignment(buttonSave, new Alignment(48));

        return mainLayout;
    }

}