Search in sources :

Example 1 with EditFormItemTabItem

use of cz.metacentrum.perun.webgui.tabs.registrartabs.EditFormItemTabItem in project perun by CESNET.

the class GetFormItems method prepareSettings.

/**
	 * Prepares the widgets from the items as A FORM FOR SETTINGS
	 *
	 * @param items
	 */
public void prepareSettings(final ArrayList<ApplicationFormItem> items) {
    // refresh table events
    final JsonCallbackEvents refreshEvents = new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            prepareSettings(items);
        }
    };
    FlexTable ft = new FlexTable();
    ft.setWidth("100%");
    ft.setCellPadding(8);
    FlexCellFormatter fcf = ft.getFlexCellFormatter();
    ft.addStyleName("borderTable");
    ft.setHTML(0, 0, "<strong>Short name</strong>");
    ft.setHTML(0, 1, "<strong>Type</strong>");
    ft.setHTML(0, 2, "<strong>Preview</strong>");
    ft.setHTML(0, 3, "<strong>Edit</strong>");
    fcf.setStyleName(0, 0, "header");
    fcf.setStyleName(0, 1, "header");
    fcf.setStyleName(0, 2, "header");
    fcf.setStyleName(0, 3, "header");
    String locale = "en";
    if (!Utils.getNativeLanguage().isEmpty() && !LocaleInfo.getCurrentLocale().getLocaleName().equals("default") && !LocaleInfo.getCurrentLocale().getLocaleName().equals("en")) {
        locale = Utils.getNativeLanguage().get(0);
    }
    int i = 1;
    for (final ApplicationFormItem item : items) {
        final int index = i - 1;
        // not yet set locale on config page
        RegistrarFormItemGenerator gen = new RegistrarFormItemGenerator(item, locale);
        // 0 = label
        String label = "";
        label = item.getShortname();
        if (item.isRequired() == true) {
            label += "*";
        }
        ft.setHTML(i, 0, label);
        // 1 = type
        Label type_label = new Label(CreateFormItemTabItem.inputTypes.get(item.getType()));
        type_label.setTitle(item.getType());
        ft.setWidget(i, 1, type_label);
        // 2 = preview
        Widget w = gen.getWidget();
        ft.setWidget(i, 2, w);
        // 3 = EDIT
        FlexTable editTable = new FlexTable();
        editTable.setStyleName("noBorder");
        ft.setWidget(i, 3, editTable);
        // color for items with unsaved changes
        if (item.wasEdited() == true) {
            ft.getFlexCellFormatter().setStyleName(i, 0, "log-changed");
            ft.getFlexCellFormatter().setStyleName(i, 1, "log-changed");
            ft.getFlexCellFormatter().setStyleName(i, 2, "log-changed");
            ft.getFlexCellFormatter().setStyleName(i, 3, "log-changed");
        }
        // mark row for deletion
        if (item.isForDelete()) {
            ft.getFlexCellFormatter().setStyleName(i, 0, "log-error");
            ft.getFlexCellFormatter().setStyleName(i, 1, "log-error");
            ft.getFlexCellFormatter().setStyleName(i, 2, "log-error");
            ft.getFlexCellFormatter().setStyleName(i, 3, "log-error");
            // undelete button
            CustomButton undelete = new CustomButton(ButtonTranslation.INSTANCE.undeleteFormItemButton(), ButtonTranslation.INSTANCE.undeleteFormItem(), SmallIcons.INSTANCE.arrowLeftIcon(), new ClickHandler() {

                public void onClick(ClickEvent event) {
                    items.get(index).setForDelete(false);
                    // refresh
                    prepareSettings(items);
                }
            });
            FlexTable undelTable = new FlexTable();
            undelTable.setStyleName("noBorder");
            undelTable.setHTML(0, 0, "<strong><span style=\"color:red;\">MARKED FOR DELETION</span></strong>");
            undelTable.setWidget(0, 1, undelete);
            ft.setWidget(i, 3, undelTable);
        }
        // color for new items to be saved
        if (item.getId() == 0) {
            ft.getFlexCellFormatter().setStyleName(i, 0, "log-success");
            ft.getFlexCellFormatter().setStyleName(i, 1, "log-success");
            ft.getFlexCellFormatter().setStyleName(i, 2, "log-success");
            ft.getFlexCellFormatter().setStyleName(i, 3, "log-success");
        }
        // up
        PushButton upButton = new PushButton(new Image(SmallIcons.INSTANCE.arrowUpIcon()), new ClickHandler() {

            public void onClick(ClickEvent event) {
                if (index - 1 < 0) {
                    // move to the bottom
                    items.remove(index);
                    items.add(item);
                    for (int i = 0; i < items.size(); i++) {
                        items.get(i).setOrdnum(i);
                    }
                } else {
                    // move it up
                    items.remove(index);
                    items.add(index - 1, item);
                    item.setOrdnum(item.getOrdnum() - 1);
                }
                item.setEdited(true);
                // refresh
                prepareSettings(items);
            }
        });
        editTable.setWidget(0, 0, upButton);
        upButton.setTitle(ButtonTranslation.INSTANCE.moveFormItemUp());
        // down
        PushButton downButton = new PushButton(new Image(SmallIcons.INSTANCE.arrowDownIcon()), new ClickHandler() {

            public void onClick(ClickEvent event) {
                if (index + 1 >= items.size()) {
                    // move to the top
                    items.remove(index);
                    items.add(0, item);
                    for (int i = 0; i < items.size(); i++) {
                        items.get(i).setOrdnum(i);
                    }
                } else {
                    // move it down
                    items.remove(index);
                    items.add(index + 1, item);
                    item.setOrdnum(item.getOrdnum() + 1);
                }
                item.setEdited(true);
                // refresh
                prepareSettings(items);
            }
        });
        editTable.setWidget(0, 1, downButton);
        downButton.setTitle(ButtonTranslation.INSTANCE.moveFormItemDown());
        // edit
        CustomButton editButton = new CustomButton(ButtonTranslation.INSTANCE.editFormItemButton(), ButtonTranslation.INSTANCE.editFormItem(), SmallIcons.INSTANCE.applicationFormEditIcon());
        editButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                session.getTabManager().addTabToCurrentTab(new EditFormItemTabItem(item, refreshEvents));
            }
        });
        editTable.setWidget(0, 2, editButton);
        // remove
        CustomButton removeButton = new CustomButton(ButtonTranslation.INSTANCE.deleteButton(), ButtonTranslation.INSTANCE.deleteFormItem(), SmallIcons.INSTANCE.deleteIcon());
        removeButton.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                boolean forDelete = false;
                for (ApplicationFormItem it : items) {
                    if (it.isForDelete())
                        forDelete = true;
                }
                if (forDelete) {
                    // mark for deletion when save changes
                    items.get(index).setForDelete(true);
                    // remove if newly created
                    if (items.get(index).getId() == 0) {
                        items.remove(index);
                    }
                    // refresh
                    prepareSettings(items);
                } else {
                    HTML text = new HTML("<p>Deleting of form items is <strong>NOT RECOMMENDED!</strong><p>You will loose access to data users submitted in older applications within this form item!<p>Do you want to continue?");
                    Confirm c = new Confirm("Delete confirm", text, new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            // mark for deletion when save changes
                            items.get(index).setForDelete(true);
                            // remove if newly created
                            if (items.get(index).getId() == 0) {
                                items.remove(index);
                            }
                            // refresh
                            prepareSettings(items);
                        }
                    }, true);
                    c.setNonScrollable(true);
                    c.show();
                }
            }
        });
        editTable.setWidget(0, 3, removeButton);
        if ((PerunEntity.GROUP.equals(entity) && !session.isGroupAdmin(id) && !session.isVoAdmin(group.getVoId())) || (PerunEntity.VIRTUAL_ORGANIZATION.equals(entity) && !session.isVoAdmin(id))) {
            editButton.setEnabled(false);
            upButton.setEnabled(false);
            downButton.setEnabled(false);
            removeButton.setEnabled(false);
        }
        // format
        fcf.setHeight(i, 0, "28px");
        fcf.setVerticalAlignment(i, 0, HasVerticalAlignment.ALIGN_MIDDLE);
        fcf.setVerticalAlignment(i, 1, HasVerticalAlignment.ALIGN_MIDDLE);
        fcf.setVerticalAlignment(i, 2, HasVerticalAlignment.ALIGN_MIDDLE);
        i++;
    }
    // set empty table widget
    if (items == null || items.isEmpty()) {
        ft.setWidget(1, 0, loaderImage);
        ft.getFlexCellFormatter().addStyleName(1, 0, "noBorder");
        ft.getFlexCellFormatter().setColSpan(1, 0, 4);
    }
    contents.setWidget(ft);
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) FlexCellFormatter(com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) Confirm(cz.metacentrum.perun.webgui.widgets.Confirm) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) EditFormItemTabItem(cz.metacentrum.perun.webgui.tabs.registrartabs.EditFormItemTabItem) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) RegistrarFormItemGenerator(cz.metacentrum.perun.webgui.client.applicationresources.RegistrarFormItemGenerator) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton)

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 EditFormItemTabItem (cz.metacentrum.perun.webgui.tabs.registrartabs.EditFormItemTabItem)1 AjaxLoaderImage (cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage)1 Confirm (cz.metacentrum.perun.webgui.widgets.Confirm)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1