Search in sources :

Example 1 with DeleteAttribute

use of cz.metacentrum.perun.webgui.json.attributesManager.DeleteAttribute in project perun by CESNET.

the class AttributeDefinitionsTabItem method draw.

public Widget draw() {
    // create main panel for content
    VerticalPanel mainPage = new VerticalPanel();
    mainPage.setWidth("100%");
    // create new instance for jsonCall
    final GetAttributesDefinition attrDef = new GetAttributesDefinition();
    final ExtendedSuggestBox box = new ExtendedSuggestBox(attrDef.getOracle());
    // custom events for reloading when created or deleted
    final JsonCallbackEvents refreshTabEvents = JsonCallbackEvents.mergeEvents(JsonCallbackEvents.refreshTableEvents(attrDef), new JsonCallbackEvents() {

        @Override
        public void onFinished(JavaScriptObject jso) {
            if (box.getSuggestBox().getValue() != null)
                attrDef.filterTable(box.getSuggestBox().getValue());
        }
    });
    // TAB MENU
    TabMenu tabMenu = new TabMenu();
    tabMenu.addWidget(UiElements.getRefreshButton(this));
    // create buttons
    tabMenu.addWidget(TabMenu.getPredefinedButton(ButtonType.CREATE, true, buttonTranslation.createAttributeDefinition(), new ClickHandler() {

        public void onClick(ClickEvent event) {
            session.getTabManager().addTabToCurrentTab(new CreateAttributeDefinitionTabItem());
        }
    }));
    // remove button
    final CustomButton deleteButton = TabMenu.getPredefinedButton(ButtonType.DELETE, buttonTranslation.deleteAttributeDefinition());
    deleteButton.setEnabled(false);
    deleteButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            final ArrayList<AttributeDefinition> attrDefToBeDeleted = attrDef.getTableSelectedList();
            String text = "Following attribute definitions will be deleted.</p><p style=\"color: red;\">All stored values of such attributes will be deleted too!";
            UiElements.showDeleteConfirm(attrDefToBeDeleted, text, new ClickHandler() {

                @Override
                public void onClick(ClickEvent clickEvent) {
                    // TODO - SHOULD HAVE ONLY ONE CALLBACK TO CORE
                    for (int i = 0; i < attrDefToBeDeleted.size(); i++) {
                        final int x = i;
                        DeleteAttribute request = new DeleteAttribute(JsonCallbackEvents.disableButtonEvents(deleteButton, new JsonCallbackEvents() {

                            @Override
                            public void onFinished(JavaScriptObject jso) {
                                // remove deleted attrs from table
                                attrDef.removeFromBackupTable(attrDefToBeDeleted.get(x));
                            }
                        }));
                        request.deleteAttributeDefinition(attrDefToBeDeleted.get(i).getId());
                    }
                }
            });
        }
    });
    tabMenu.addWidget(deleteButton);
    // filter box
    tabMenu.addFilterWidget(box, new PerunSearchEvent() {

        public void searchFor(String text) {
            attrDef.filterTable(text);
        }
    }, buttonTranslation.filterAttributeDefinition());
    final CustomButton saveButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
    saveButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent clickEvent) {
            final ArrayList<AttributeDefinition> list = attrDef.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                // TODO - SHOULD HAVE ONLY ONE CALLBACK TO CORE
                for (int i = 0; i < list.size(); i++) {
                    final int x = i;
                    UpdateAttribute request = new UpdateAttribute(JsonCallbackEvents.disableButtonEvents(saveButton, new JsonCallbackEvents() {

                        @Override
                        public void onFinished(JavaScriptObject jso) {
                            attrDef.getSelectionModel().setSelected(list.get(x), false);
                        }
                    }));
                    request.updateAttribute(list.get(i));
                }
            }
        }
    });
    tabMenu.addWidget(saveButton);
    // if checkbox is checked only entityless attributes will be shown
    CheckBox showEntitylessBox = new CheckBox("Show only Entityless Attributes");
    showEntitylessBox.setValue(false);
    showEntitylessBox.addValueChangeHandler(valueChangeEvent -> {
        if (showEntitylessBox.getValue()) {
            attrDef.setEntity("entityless");
        } else {
            attrDef.setEntities(new HashSet<>());
        }
        attrDef.retrieveData();
    });
    tabMenu.addWidget(showEntitylessBox);
    // add menu to page
    mainPage.add(tabMenu);
    mainPage.setCellHeight(tabMenu, "30px");
    CellTable<AttributeDefinition> attrDefTable = attrDef.getTable(new FieldUpdater<AttributeDefinition, String>() {

        @Override
        public void update(int index, AttributeDefinition object, String value) {
            if (object.getEntity().equals("entityless")) {
                session.getTabManager().addTabToCurrentTab(new EntitylessAttributesDetailTabItem(object), true);
            } else {
                session.getTabManager().addTabToCurrentTab(new AttributeDefinitionDetailTabItem(object), true);
            }
        }
    });
    attrDefTable.setStyleName("perun-table");
    ScrollPanel scrollTable = new ScrollPanel(attrDefTable);
    scrollTable.addStyleName("perun-tableScrollPanel");
    JsonUtils.addTableManagedButton(attrDef, attrDefTable, deleteButton);
    // put page into scroll panel
    mainPage.add(scrollTable);
    session.getUiElements().resizePerunTable(scrollTable, 350, this);
    this.contentWidget.setWidget(mainPage);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) UpdateAttribute(cz.metacentrum.perun.webgui.json.attributesManager.UpdateAttribute) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.webgui.model.AttributeDefinition) ExtendedSuggestBox(cz.metacentrum.perun.webgui.widgets.ExtendedSuggestBox) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) EntitylessAttributesDetailTabItem(cz.metacentrum.perun.webgui.tabs.entitylessattributestabs.EntitylessAttributesDetailTabItem) PerunSearchEvent(cz.metacentrum.perun.webgui.client.resources.PerunSearchEvent) DeleteAttribute(cz.metacentrum.perun.webgui.json.attributesManager.DeleteAttribute) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) GetAttributesDefinition(cz.metacentrum.perun.webgui.json.attributesManager.GetAttributesDefinition)

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 PerunSearchEvent (cz.metacentrum.perun.webgui.client.resources.PerunSearchEvent)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 DeleteAttribute (cz.metacentrum.perun.webgui.json.attributesManager.DeleteAttribute)1 GetAttributesDefinition (cz.metacentrum.perun.webgui.json.attributesManager.GetAttributesDefinition)1 UpdateAttribute (cz.metacentrum.perun.webgui.json.attributesManager.UpdateAttribute)1 AttributeDefinition (cz.metacentrum.perun.webgui.model.AttributeDefinition)1 EntitylessAttributesDetailTabItem (cz.metacentrum.perun.webgui.tabs.entitylessattributestabs.EntitylessAttributesDetailTabItem)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ExtendedSuggestBox (cz.metacentrum.perun.webgui.widgets.ExtendedSuggestBox)1 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1 ArrayList (java.util.ArrayList)1