Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2012 JBoss Inc Licensed 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.kie.workbench.common.screens.datamodeller.client.widgets;
 
 
 import java.util.*;
 
 public class DataObjectFieldEditor extends Composite {
 
     interface DataObjectFieldEditorUIBinder
             extends UiBinder<WidgetDataObjectFieldEditor> {
 
     };
 
 
     @UiField
     Label titleLabel;
 
     @UiField
     TextBox name;
 
     @UiField
     TextBox label;
 
     @UiField
 
     @UiField
 
     @UiField
 
     @UiField
 
     @UiField
 
     @UiField
 
     @Inject
 
     @Inject
 
 
 
    private DataObjectTO dataObject;
    private ObjectPropertyTO objectField;
    private DataModelerContext context;
    public DataObjectFieldEditor() {
        initWidget(.createAndBindUi(this));
            @Override
            public void onChange(ChangeEvent event) {
                typeChanged(event);
            }
        });
    }
    public DataObjectTO getDataObject() {
        return ;
    }
    public void setDataObject(DataObjectTO dataObject) {
        this. = dataObject;
    }
    public ObjectPropertyTO getObjectField() {
        return ;
    }
    public void setObjectField(ObjectPropertyTO objectField) {
        this. = objectField;
    }
    public DataModelerContext getContext() {
        return ;
    }
    public void setContext(DataModelerContext context) {
        this. = context;
        initTypeList();
    }
    private DataModelTO getDataModel() {
        return getContext() != null ? getContext().getDataModel() : null;
    }
    // Event notifications
    private void notifyFieldChange(String memberNameObject oldValueObject newValue) {
        DataObjectFieldChangeEvent changeEvent = new DataObjectFieldChangeEvent(.getDataModel(), getDataObject(), getObjectField(), memberNameoldValuenewValue);
        // Notify helper directly
        getContext().getHelper().dataModelChanged(changeEvent);
        .fire(changeEvent);
    }
    // Event observers
    private void onFieldSelected(@Observes DataObjectFieldSelectedEvent event) {
        if (event.isFrom(getDataModel())) {
            loadDataObjectField(event.getCurrentDataObject(), event.getCurrentField());
        }
    }
        // When all attributes from the current object have been deleted clean
        if (event.isFrom(getDataModel())) {
            if (getDataObject().getProperties().size() == 0) {
                clean();
            }
        }
    }
    private void onDataObjectChange(@Observes DataObjectChangeEvent event) {
        if (event.isFrom(getDataModel())) {
            if ("name".equals(event.getPropertyName()) ||
                "packageName".equals(event.getPropertyName()) ||
                "label".equals(event.getPropertyName())) {
                initTypeList();
                setSelectedType();
            }
        }
    }
    private void onDataObjectCreated(@Observes DataObjectCreatedEvent event) {
        if (event.isFrom(getDataModel())) {
            initTypeList();
        }
    }
    private void onDataObjectDeleted(@Observes DataObjectDeletedEvent event) {
        if (event.isFrom(getDataModel())) {
            initTypeList();
            if (getDataModel() != null && getDataModel().getDataObjects().size() == 0) {
                clean();
                setDataObject(null);
                setObjectField(null);
            }
        }
    }
    private void onDataObjectSelected(@Observes DataObjectSelectedEvent event) {
        if (event.isFrom(getDataModel())) {
            clean();
            setDataObject(event.getCurrentDataObject());
            setObjectField(null);
        }
    }
    private void loadDataObjectField(DataObjectTO dataObjectObjectPropertyTO objectField) {
        clean();
        initTypeList();
        if (dataObject != null && objectField != null) {
            setDataObject(dataObject);
            setObjectField(objectField);
            .setText(getObjectField().getName());
            AnnotationTO annotation = objectField.getAnnotation(.);
            if (annotation != null) {
                .setText( (Stringannotation.getValue(.) );
            }
            annotation = objectField.getAnnotation(.);
            if (annotation != null) {
                .setText( (Stringannotation.getValue(.));
            }
            setSelectedType();
            annotation = objectField.getAnnotation(.);
            if (annotation != null) {
                .setValue(.);
            }
            annotation = objectField.getAnnotation(.);
            if (annotation != null) {
                .setText( (Stringannotation.getValue(.));
            }
        }
    }
    // TODO listen to DataObjectFieldDeletedEvent?
    // Event handlers
    @UiHandler("name")
    void nameChanged(ValueChangeEvent<Stringevent) {
        if (getObjectField() == nullreturn;
        // Set widgets to errorpopup for styling purposes etc.
        .setTitleWidget();
        .setValueWidget();
        final String oldValue = getObjectField().getName();
        final String newValue = DataModelerUtils.getInstance().unCapitalize(.getValue());
        // In case an invalid name (entered before), was corrected to the original value, don't do anything but reset the label style
        if (oldValue.equalsIgnoreCase(.getValue())) {
            .setText(oldValue);
            .setStyleName(null);
            return;
        }
        .isValidIdentifier(newValuenew ValidatorCallback() {
            @Override
            public void onFailure() {
            }
            @Override
            public void onSuccess() {
                .isUniqueAttributeName(newValuegetDataObject(), new ValidatorCallback() {
                    @Override
                    public void onFailure() {
                        .showMessage(..validation_error_object_attribute_already_exists(newValue));
                    }
                    @Override
                    public void onSuccess() {
                        .setStyleName(null);
                        .setName(newValue);
                        notifyFieldChange("name"oldValuenewValue);
                    }
                });
            }
        });
    }
    @UiHandler("label")
    void labelChanged(final ValueChangeEvent<Stringevent) {
        if (getObjectField() == nullreturn;
        String oldValue = null;
        final String _label = .getValue();
        if (annotation != null) {
            oldValue = AnnotationValueHandler.getInstance().getStringValue(annotation.);
            if ( _label != null && !"".equals(_label) ) annotation.setValue(._label);
            else getObjectField().removeAnnotation(annotation);
        } else {
            if ( _label != null && !"".equals(_label) ) {
            }
        }
        // TODO replace 'label' literal with annotation definition constant
        notifyFieldChange("label"oldValue_label);
    }
    @UiHandler("description")
    void descriptionChanged(final ValueChangeEvent<Stringevent) {
        if (getObjectField() == nullreturn;
        String oldValue = null;
        final String _description = .getValue();
        if (annotation != null) {
            oldValue = AnnotationValueHandler.getInstance().getStringValue(annotation.);
            if ( _description != null && !"".equals(_description) ) annotation.setValue(._description);
            else getObjectField().removeAnnotation(annotation);
        } else {
            if ( _description != null && !"".equals(_description) ) {
            }
        }
        notifyFieldChange(.oldValue_description);
    }
    private void typeChanged(ChangeEvent event) {
        if (getObjectField() == nullreturn;
        String oldValue = getObjectField().getClassName();
        String type = .getValue();
        if (DataModelerUtils.isMultipleType(type)) {
            type = DataModelerUtils.getCanonicalClassName(type);
            getObjectField().setMultiple(true);
        } else {
            getObjectField().setMultiple(false);
        }
        getObjectField().setClassName(type);
        if (getContext().getHelper().isBaseType(type)) {
            getObjectField().setBaseType(true);
        } else {
            // Un-reference former type reference and set the new one
            getObjectField().setBaseType(false);
            getContext().getHelper().dataObjectUnReferenced(oldValuegetDataObject().getClassName());
            getContext().getHelper().dataObjectReferenced(typegetDataObject().getClassName());
        }
        notifyFieldChange("className"oldValuetype);
    }
    @UiHandler("equalsSelector")
    void equalsChanged(final ClickEvent event) {
        if (getObjectField() == nullreturn;
        Boolean oldEquals = null;
        if (annotation != null) {
            Object annotationValue = annotation.getValue(.);
            oldEquals = annotationValue != null ? (BooleanannotationValue : .;
        }
        final Boolean setEquals = .getValue();
        if (annotation != null && !setEqualsgetObjectField().removeAnnotation(annotation);
        else if (annotation == null && setEqualsgetObjectField().addAnnotation(new AnnotationTO(getContext().getAnnotationDefinitions().get(.)));
        notifyFieldChange(.oldEqualssetEquals);
    }
    @UiHandler("positionText")
    void positionChanged(final ValueChangeEvent<Stringevent) {
        if (getObjectField() == nullreturn;
        // Set widgets to errorpopup for styling purposes etc.
        final String oldPosition = (annotation != null) ? annotation.getValue(.).toString() : "";
        final String newPosition = .getValue();
        // In case an invalid position (entered before), was corrected to the original value, don't do anything but reset the label style
        if (oldPosition.equalsIgnoreCase(newPosition)) {
            .setStyleName(null);
            return;
        }
        .isValidPosition(newPositionnew ValidatorCallback() {
            @Override
            public void onFailure() {
            }
            @Override
            public void onSuccess() {
                AnnotationTO annotation = getObjectField().getAnnotation(.);
                if (annotation != null) {
                    if ( newPosition != null && !"".equals(newPosition) ) annotation.setValue(.newPosition);
                    else getObjectField().removeAnnotation(annotation);
                } else {
                    if ( newPosition != null && !"".equals(newPosition) ) {
                    }
                }
                notifyFieldChange(.oldPositionnewPosition);
            }
        });
    }
    private void initTypeList() {
        .clear();
        SortedMap<StringStringtypeNames = new TreeMap<StringString>();
        if (getDataModel() != null) {
            // First add all base types, ordered
            for (Map.Entry<StringStringbaseType : getContext().getHelper().getOrderedBaseTypes().entrySet()) {
                .addItem(baseType.getKey(), baseType.getValue());
                // TODO add multiple types for base types?
            }
            // Second add all model types, ordered
            for (DataObjectTO dataObject : getDataModel().getDataObjects()) {
                String className = dataObject.getClassName();
                String className_m = className + .;
                String classLabel = DataModelerUtils.getDataObjectFullLabel(dataObject);
                String classLabel_m = classLabel  + .;
                typeNames.put(classLabelclassName);
                typeNames.put(classLabel_mclassName_m);
            }
            for (Map.Entry<StringStringtypeName : typeNames.entrySet()) {
                .addItem(typeName.getKey(), typeName.getValue());
            }
            // Then add all external types, ordered
            typeNames.clear();
            for (String extClass : getDataModel().getExternalClasses()) {
                String extClass_m = extClass + .;
                typeNames.put(. + extClassextClass);
                typeNames.put(. + extClass_mextClass_m);
            }
            for (Map.Entry<StringStringtypeName : typeNames.entrySet()) {
                .addItem(typeName.getKey(), typeName.getValue());
            }
        }
    }
    private void setSelectedType() {
        String type = getObjectField() != null ?
                (getObjectField().getClassName() + (getObjectField().isMultiple() ? .:"")) : null;
        .setSelectedValue(type);
    }
    private void clean() {
        .setStyleName(null);
        .setText(null);
        .setText(null);
        .setText(null);
        .setSelectedValue(null);
        .setStyleName(null);
        .setText(null);
    }
    // TODO extract this to parent widget to avoid duplicate code
    private class DataObjectFieldEditorErrorPopup extends ErrorPopup {
        private Widget titleWidget;
        private Widget valueWidget;
        private DataObjectFieldEditorErrorPopup() {
            setAfterCloseEvent(new Command() {
                @Override
                public void execute() {
                    .setStyleName("text-error");
                    if ( instanceof Focusable) ((FocusWidget)).setFocus(true);
                    if ( instanceof ValueBoxBase) ((ValueBoxBase)).selectAll();
                    clearWidgets();
                }
            });
        }
        private void setTitleWidget(Widget titleWidget){this. = titleWidget;titleWidget.setStyleName(null);}
        private void setValueWidget(Widget valueWidget){this. = valueWidget;}
        private void clearWidgets() {
             = null;
             = null;
        }
    }
New to GrepCode? Check out our FAQ X