Search in sources :

Example 1 with ApplicationFormItemData

use of cz.metacentrum.perun.webgui.model.ApplicationFormItemData in project perun by CESNET.

the class GetApplicationDataById method prepareApplicationForm.

/**
 * Prepares the widgets from the items as A DISPLAY FOR THE USER
 */
public void prepareApplicationForm() {
    FlexTable ft = new FlexTable();
    ft.setWidth("100%");
    ft.setCellPadding(5);
    ft.setCellSpacing(0);
    ft.setStyleName("stripped");
    FlexCellFormatter fcf = ft.getFlexCellFormatter();
    String locale = "en";
    if (!Utils.getNativeLanguage().isEmpty() && !LocaleInfo.getCurrentLocale().getLocaleName().equals("default") && !LocaleInfo.getCurrentLocale().getLocaleName().equals("en")) {
        locale = Utils.getNativeLanguage().get(0);
    }
    if (this.embedded) {
        Attribute attr = this.user.getAttribute("urn:perun:user:attribute-def:def:preferredMail");
        if (attr != null) {
            ft.setHTML(0, 0, "<strong>" + attr.getDisplayName() + "</strong>");
            ft.setWidget(0, 2, new HTML(SafeHtmlUtils.fromString(attr.getValue())));
            // format
            fcf.setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
            fcf.setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
            fcf.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
            fcf.setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
            fcf.setWidth(0, 0, "25%");
            fcf.setWidth(0, 2, "75%");
            contents.setWidget(ft);
        }
    } else {
        int i = 0;
        for (final ApplicationFormItemData item : applFormItems) {
            RegistrarFormItemGenerator gen = new RegistrarFormItemGenerator(item.getFormItem(), (item.getValue() != null) ? SafeHtmlUtils.fromString(item.getValue()).asString() : null, locale);
            this.applFormGenerators.add(gen);
            // show only visible items - show also hidden to perun admin and vo/group admin
            if (!gen.isVisible() && !(session.isPerunAdmin() || session.isVoAdmin() || session.isGroupAdmin())) {
                continue;
            }
            // if only for admin
            if (!showAdminItems && gen.isVisibleOnlyToAdmin()) {
                continue;
            }
            // WITH LABEL (input box ...)
            if (gen.isLabelShown()) {
                // don't show password
                if (!item.getFormItem().getType().equalsIgnoreCase("PASSWORD")) {
                    // FIXME for now, we can only detect that the item was hidden for the 'ALWAYS' option. This will be for the most cases.
                    if (item.getFormItem().getHidden().equals("ALWAYS")) {
                        // hidden
                        ft.setHTML(i, 0, "<strong>" + SafeHtmlUtils.fromString(gen.getLabelOrShortname()).asString() + "</strong><br /><i>(value provided by external source)</i>");
                    // FIXME for now, we can only detect that the item was disabled for the 'ALWAYS' option. This will be for the most cases.
                    } else if (item.getFormItem().getDisabled().equals("ALWAYS")) {
                        // show
                        ft.setHTML(i, 0, "<strong>" + SafeHtmlUtils.fromString(gen.getLabelOrShortname()).asString() + "</strong><br /><i>(value provided by external source)</i>");
                    } else {
                        ft.setHTML(i, 0, "<strong>" + SafeHtmlUtils.fromString(gen.getLabelOrShortname()).asString() + "</strong>");
                    }
                    if (PerunWebSession.getInstance().isPerunAdmin() && gen.isUpdatable() && editable) {
                        final int finalI = i;
                        CustomButton editButton = new CustomButton("", "Editor form item data.", SmallIcons.INSTANCE.applicationFormEditIcon(), new ClickHandler() {

                            @Override
                            public void onClick(ClickEvent event) {
                                TextArea content = new TextArea();
                                if (item.getValue() != null) {
                                    content.setText(item.getValue());
                                }
                                content.setSize("350px", "100px");
                                Confirm confirm = new Confirm("Edit item: " + SafeHtmlUtils.fromString(gen.getLabelOrShortname()).asString(), content, new ClickHandler() {

                                    @Override
                                    public void onClick(ClickEvent event) {
                                        // save old value and push new value
                                        String previousValue = item.getValue();
                                        item.setValue(content.getText());
                                        UpdateFormItemData update = new UpdateFormItemData(new JsonCallbackEvents() {

                                            @Override
                                            public void onFinished(JavaScriptObject jso) {
                                                // update local UI
                                                ft.setWidget(finalI, 2, new HTML((item.getValue() != null) ? (SafeHtmlUtils.fromString(item.getValue()).asString()) : null));
                                            }

                                            @Override
                                            public void onError(PerunError error) {
                                                // put back old value
                                                item.setValue(previousValue);
                                            }
                                        });
                                        update.updateFormItemData(appId, item);
                                    }
                                }, "Update form item", true);
                                confirm.setNonScrollable(true);
                                confirm.show();
                            }
                        });
                        ft.setWidget(i, 1, editButton);
                    }
                    // 1 = value
                    ft.setWidget(i, 2, new HTML((item.getValue() != null) ? (SafeHtmlUtils.fromString(item.getValue()).asString()) : null));
                    // format
                    fcf.setVerticalAlignment(i, 0, HasVerticalAlignment.ALIGN_TOP);
                    fcf.setVerticalAlignment(i, 2, HasVerticalAlignment.ALIGN_TOP);
                    fcf.setHorizontalAlignment(i, 0, HasHorizontalAlignment.ALIGN_RIGHT);
                    fcf.setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_LEFT);
                    fcf.setWidth(i, 0, "25%");
                    fcf.setWidth(i, 2, "75%");
                }
            }
            i++;
        }
        // set empty text
        if (!applFormItems.isEmpty()) {
            contents.setWidget(ft);
        }
    }
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) Attribute(cz.metacentrum.perun.webgui.model.Attribute) FlexCellFormatter(com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) Confirm(cz.metacentrum.perun.webgui.widgets.Confirm) ApplicationFormItemData(cz.metacentrum.perun.webgui.model.ApplicationFormItemData) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) RegistrarFormItemGenerator(cz.metacentrum.perun.webgui.client.applicationresources.RegistrarFormItemGenerator) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) PerunError(cz.metacentrum.perun.webgui.model.PerunError)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 FlexCellFormatter (com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter)1 RegistrarFormItemGenerator (cz.metacentrum.perun.webgui.client.applicationresources.RegistrarFormItemGenerator)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 ApplicationFormItemData (cz.metacentrum.perun.webgui.model.ApplicationFormItemData)1 Attribute (cz.metacentrum.perun.webgui.model.Attribute)1 PerunError (cz.metacentrum.perun.webgui.model.PerunError)1 Confirm (cz.metacentrum.perun.webgui.widgets.Confirm)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1