Search in sources :

Example 16 with SetAttributes

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

the class GroupSettingsTabItem method draw.

public Widget draw() {
    titleWidget.setText(Utils.getStrippedStringWithEllipsis(group.getName()) + ": settings");
    // Main panel
    final VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    final TabMenu menu = new TabMenu();
    // Adds menu to the main panel
    vp.add(menu);
    vp.setCellHeight(menu, "30px");
    // IDS
    final Map<String, Integer> ids = new HashMap<String, Integer>();
    ids.put("group", groupId);
    // define GET ATTRIBUTES callback
    final GetAttributesV2 jsonCallback = new GetAttributesV2(true);
    jsonCallback.setIds(ids);
    if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
        jsonCallback.setCheckable(false);
    final CellTable<Attribute> table = jsonCallback.getEmptyTable();
    table.setWidth("100%");
    table.addStyleName("perun-table");
    ScrollPanel sp = new ScrollPanel(table);
    sp.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp, 350, this);
    vp.add(sp);
    // define GET RES. REQ ATTRIBUTES callback
    final GetResourceRequiredAttributesV2 reqAttrs = new GetResourceRequiredAttributesV2(null, JsonCallbackEvents.passDataToAnotherCallback(jsonCallback));
    // GROUP RESOURCES LISTBOX
    final ListBoxWithObjects<Resource> resourceDropDown = new ListBoxWithObjects<Resource>();
    resourceDropDown.setTitle("By selecting a resource you will switch to either group-resource or member-resource setting (you must pick member too)");
    // fill table with group attributes on page load
    if (lastSelectedMemberId == 0 && lastSelectedResourceId == 0) {
        jsonCallback.retrieveData();
    } else {
        // load attrs by listbox selection after calls are done
        Scheduler.get().scheduleFixedPeriod(new Scheduler.RepeatingCommand() {

            @Override
            public boolean execute() {
                // load proper table if something was selected
                if (resCallDone && memCallDone) {
                    // trigger change event
                    DomEvent.fireNativeEvent(Document.get().createChangeEvent(), resourceDropDown);
                    return false;
                }
                return true;
            }
        }, 200);
    }
    // fill resources listbox
    if (resources == null) {
        // get assigned resources
        GetAssignedResources resources = new GetAssignedResources(groupId, PerunEntity.GROUP, new JsonCallbackEvents() {

            public void onFinished(JavaScriptObject jso) {
                ArrayList<Resource> resources = JsonUtils.jsoAsList(jso);
                resourceDropDown.clear();
                if (resources != null && !resources.isEmpty()) {
                    resources = new TableSorter<Resource>().sortByName(resources);
                    resourceDropDown.addNotSelectedOption();
                    for (int i = 0; i < resources.size(); i++) {
                        resourceDropDown.addItem(resources.get(i));
                        if (lastSelectedResourceId == resources.get(i).getId()) {
                            resourceDropDown.setSelected(resources.get(i), true);
                        }
                    }
                } else {
                    resourceDropDown.addItem("No resources available");
                }
                resCallDone = true;
            }

            public void onError(PerunError error) {
                resourceDropDown.clear();
                resourceDropDown.addItem("Error while loading");
                resCallDone = true;
            }

            public void onLoadingStart() {
                resCallDone = false;
                resourceDropDown.clear();
                resourceDropDown.addItem("Loading...");
            }
        });
        resources.retrieveData();
    } else {
        // use predefined resources
        resourceDropDown.addNotSelectedOption();
        resources = new TableSorter<Resource>().sortByName(resources);
        resourceDropDown.addAllItems(resources);
    }
    // GROUP MEMBERS LISTBOX
    final ListBoxWithObjects<RichMember> membersDropDown = new ListBoxWithObjects<RichMember>();
    membersDropDown.setTitle("By selecting a member you will switch to either member or member-resource settings (you must pick resource too)");
    // fill members listbox
    GetGroupRichMembers members = new GetGroupRichMembers(groupId, new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            ArrayList<RichMember> mems = JsonUtils.jsoAsList(jso);
            membersDropDown.clear();
            if (mems != null && !mems.isEmpty()) {
                mems = new TableSorter<RichMember>().sortByName(mems);
                membersDropDown.addNotSelectedOption();
                for (int i = 0; i < mems.size(); i++) {
                    membersDropDown.addItem(mems.get(i));
                    if (mems.get(i).getId() == lastSelectedMemberId) {
                        membersDropDown.setSelected(mems.get(i), true);
                    }
                }
            } else {
                membersDropDown.addItem("No members available");
            }
            memCallDone = true;
        }

        public void onError(PerunError error) {
            membersDropDown.clear();
            membersDropDown.addItem("Error while loading");
            memCallDone = true;
        }

        public void onLoadingStart() {
            memCallDone = false;
            membersDropDown.clear();
            membersDropDown.addItem("Loading...");
        }
    });
    members.retrieveData();
    // CHECKBOXES "FOR ALL MEMBERS"
    final CheckBox memChb = new CheckBox();
    memChb.setText("To all members");
    memChb.setVisible(false);
    memChb.setTitle("Uses this setting for all members of this group.");
    memChb.setValue(false);
    // SAVE CHANGES BUTTON
    final CustomButton saveChangesButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
    if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
        saveChangesButton.setEnabled(false);
    // click handler to save group and member-user attributes
    final ClickHandler saveAttrsClickHandler = new ClickHandler() {

        public void onClick(ClickEvent event) {
            final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                // refresh table and disable button a event for GetAttributes
                final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(saveChangesButton, JsonCallbackEvents.refreshTableEvents(jsonCallback));
                if (memChb.getValue() == true) {
                    // to all members selected
                    UiElements.generateInfo(WidgetTranslation.INSTANCE.saveConfirmTitle(), "Same value(s) for selected attribute(s) will be set to ALL members of group.", new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            ArrayList<RichMember> memList = membersDropDown.getAllObjects();
                            for (int i = 0; i < memList.size(); i++) {
                                SetAttributes request = new SetAttributes();
                                if (i == memList.size() - 1) {
                                    request.setEvents(events);
                                } else {
                                    request.setEvents(JsonCallbackEvents.disableButtonEvents(saveChangesButton));
                                }
                                Map<String, Integer> ids = new HashMap<String, Integer>();
                                ids.put("member", memList.get(i).getId());
                                ids.put("workWithUserAttributes", 1);
                                request.setAttributes(ids, list);
                            }
                        }
                    });
                } else {
                    // just one group / memeber
                    SetAttributes request = new SetAttributes(events);
                    request.setAttributes(jsonCallback.getIds(), list);
                }
            }
        }
    };
    // click handler to save required attributes
    final ClickHandler saveReqAttrsClickHandler = new ClickHandler() {

        public void onClick(ClickEvent event) {
            final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
            // check if not empty
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                // refresh table and disable button a event for GetResourceRequiredAttributes
                final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(saveChangesButton, new JsonCallbackEvents() {

                    public void onFinished(JavaScriptObject jso) {
                        jsonCallback.clearTable();
                        // set back resourceToGetServicesFrom
                        Map<String, Integer> ids = reqAttrs.getIds();
                        if (ids.containsKey("resource")) {
                            ids.put("resourceToGetServicesFrom", ids.get("resource"));
                        }
                        if (ids.containsKey("user")) {
                            ids.put("workWithUserAttributes", 1);
                        }
                        if (ids.containsKey("resource") && ids.containsKey("group")) {
                            ids.put("workWithGroupAttributes", 1);
                        }
                        reqAttrs.setIds(ids);
                        reqAttrs.retrieveData();
                    }
                });
                final SetAttributes request = new SetAttributes();
                if (memChb.getValue() == true) {
                    // to all members selected
                    UiElements.generateInfo(WidgetTranslation.INSTANCE.saveConfirmTitle(), "Same value(s) for selected attribute(s) will be set to ALL members of group.", new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            ArrayList<RichMember> memList = membersDropDown.getAllObjects();
                            for (int i = 0; i < memList.size(); i++) {
                                if (i == 0) {
                                    events.onLoadingStart();
                                }
                                // trigger disable button if first
                                Map<String, Integer> ids = new HashMap<String, Integer>();
                                ids.put("member", memList.get(i).getId());
                                ids.put("user", memList.get(i).getUserId());
                                ids.put("resource", resourceDropDown.getSelectedObject().getId());
                                ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
                                if (i == memList.size() - 1) {
                                    request.setEvents(events);
                                }
                                // set events to last to get refresh and button enable
                                request.setAttributes(ids, list);
                            }
                        }
                    });
                } else {
                    // group or member
                    // disable button events
                    request.setEvents(events);
                    request.setAttributes(reqAttrs.getIds(), list);
                }
            }
        }
    };
    // REMOVE BUTTON
    final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
    if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
        removeButton.setEnabled(false);
    // click handler to remove group and member-user attributes
    final ClickHandler removeAttrsClickHandler = new ClickHandler() {

        public void onClick(ClickEvent event) {
            final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                // refresh table and disable button a event for GetAttributes
                final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(removeButton, JsonCallbackEvents.refreshTableEvents(jsonCallback));
                final RemoveAttributes request = new RemoveAttributes();
                if (memChb.getValue() == true) {
                    // to all members selected
                    UiElements.generateInfo(WidgetTranslation.INSTANCE.deleteConfirmTitle(), "Selected attribute(s) will be removed from ALL members of group.", new ClickHandler() {

                        public void onClick(ClickEvent event) {
                            ArrayList<RichMember> memList = membersDropDown.getAllObjects();
                            for (int i = 0; i < memList.size(); i++) {
                                // trigger disable button if first
                                if (i == 0) {
                                    events.onLoadingStart();
                                }
                                Map<String, Integer> ids = new HashMap<String, Integer>();
                                ids.put("member", memList.get(i).getId());
                                ids.put("workWithUserAttributes", 1);
                                // set events to last to get refresh and button enable
                                if (i == memList.size() - 1) {
                                    request.setEvents(events);
                                }
                                request.removeAttributes(ids, list);
                            }
                        }
                    });
                } else {
                    // just one group / memeber
                    // disable button events
                    request.setEvents(events);
                    request.removeAttributes(jsonCallback.getIds(), list);
                }
            }
        }
    };
    // click handler to remove ResourceRequiredAttributes
    final ClickHandler removeReqAttrsClickHandler = new ClickHandler() {

        public void onClick(ClickEvent event) {
            final ArrayList<Attribute> list = jsonCallback.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                // refresh table and disable button a event for GetAttributes
                final JsonCallbackEvents events = JsonCallbackEvents.disableButtonEvents(removeButton, new JsonCallbackEvents() {

                    public void onFinished(JavaScriptObject jso) {
                        jsonCallback.clearTable();
                        // set back resourceToGetServicesFrom
                        Map<String, Integer> ids = reqAttrs.getIds();
                        if (ids.containsKey("resource")) {
                            ids.put("resourceToGetServicesFrom", ids.get("resource"));
                        }
                        if (ids.containsKey("user")) {
                            ids.put("workWithUserAttributes", 1);
                        }
                        if (ids.containsKey("resource") && ids.containsKey("group")) {
                            ids.put("workWithGroupAttributes", 1);
                        }
                        reqAttrs.setIds(ids);
                        reqAttrs.retrieveData();
                    }
                });
                final RemoveAttributes request = new RemoveAttributes();
                if (memChb.getValue() == true) {
                    // to all members selected
                    UiElements.generateInfo(WidgetTranslation.INSTANCE.deleteConfirmTitle(), "Selected attribute(s) will be removed from ALL members of group.", new ClickHandler() {

                        @Override
                        public void onClick(ClickEvent event) {
                            ArrayList<RichMember> memList = membersDropDown.getAllObjects();
                            for (int i = 0; i < memList.size(); i++) {
                                // trigger disable button if first
                                if (i == 0) {
                                    events.onLoadingStart();
                                }
                                Map<String, Integer> ids = new HashMap<String, Integer>();
                                ids.put("member", memList.get(i).getId());
                                ids.put("user", memList.get(i).getUserId());
                                ids.put("resource", resourceDropDown.getSelectedObject().getId());
                                ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
                                // set events to last to get refresh and button enable
                                if (i == memList.size() - 1) {
                                    request.setEvents(events);
                                }
                                request.removeAttributes(ids, list);
                            }
                        }
                    });
                } else {
                    // just one group / member
                    // disable button events
                    request.setEvents(events);
                    request.removeAttributes(reqAttrs.getIds(), list);
                }
            }
        }
    };
    // SET NEW ATTR BUTTON
    final CustomButton setNewAttributeButton = TabMenu.getPredefinedButton(ButtonType.ADD, true, ButtonTranslation.INSTANCE.setNewAttributes());
    if (!session.isGroupAdmin(groupId) && !session.isVoAdmin(group.getVoId()))
        setNewAttributeButton.setEnabled(false);
    // click handler
    setNewAttributeButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ids.clear();
            if (resourceDropDown.getSelectedIndex() > 0) {
                if (membersDropDown.getSelectedIndex() > 0) {
                    ids.put("member", membersDropDown.getSelectedObject().getId());
                    ids.put("user", membersDropDown.getSelectedObject().getUserId());
                    ids.put("resource", resourceDropDown.getSelectedObject().getId());
                    ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
                } else {
                    ids.put("resource", resourceDropDown.getSelectedObject().getId());
                    ids.put("group", groupId);
                    ids.put("workWithGroupAttributes", 1);
                }
            } else {
                if (membersDropDown.getSelectedIndex() > 0) {
                    ids.put("member", membersDropDown.getSelectedObject().getId());
                    ids.put("user", membersDropDown.getSelectedObject().getUserId());
                } else {
                    ids.put("group", groupId);
                }
            }
            session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, jsonCallback.getList()), true);
        }
    });
    // ClickHandlersRegistration - save onClicks for GetAttributes
    final ArrayList<HandlerRegistration> clickHandlers = new ArrayList<HandlerRegistration>();
    clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
    clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
    // DEFINE CHANGE HANDLER FOR ALL DROP DOWNS
    ChangeHandler changeHandler = new ChangeHandler() {

        public void onChange(ChangeEvent event) {
            int resIndex = resourceDropDown.getSelectedIndex();
            int memIndex = membersDropDown.getSelectedIndex();
            // no resource or member selected
            if (resIndex == 0 && memIndex == 0) {
                lastSelectedMemberId = 0;
                lastSelectedResourceId = 0;
                memChb.setValue(false);
                memChb.setVisible(false);
                jsonCallback.clearTable();
                ids.clear();
                ids.put("group", groupId);
                jsonCallback.setIds(ids);
                jsonCallback.retrieveData();
                for (HandlerRegistration handler : clickHandlers) {
                    if (handler != null) {
                        handler.removeHandler();
                    }
                }
                clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
                clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
            }
            // no resource but member selected
            if (resIndex == 0 && memIndex > 0) {
                lastSelectedMemberId = membersDropDown.getSelectedObject().getId();
                lastSelectedResourceId = 0;
                memChb.setValue(false);
                memChb.setVisible(true);
                jsonCallback.clearTable();
                ids.clear();
                ids.put("member", membersDropDown.getSelectedObject().getId());
                ids.put("workWithUserAttributes", 1);
                jsonCallback.setIds(ids);
                jsonCallback.retrieveData();
                for (HandlerRegistration handler : clickHandlers) {
                    if (handler != null) {
                        handler.removeHandler();
                    }
                }
                clickHandlers.add(saveChangesButton.addClickHandler(saveAttrsClickHandler));
                clickHandlers.add(removeButton.addClickHandler(removeAttrsClickHandler));
            }
            // resource and no member selected
            if (resIndex > 0 && memIndex == 0) {
                lastSelectedMemberId = 0;
                lastSelectedResourceId = resourceDropDown.getSelectedObject().getId();
                memChb.setValue(false);
                memChb.setVisible(false);
                jsonCallback.clearTable();
                ids.clear();
                ids.put("group", groupId);
                ids.put("resource", resourceDropDown.getSelectedObject().getId());
                ids.put("resourceToGetServicesFrom", ids.get("resource"));
                ids.put("workWithGroupAttributes", 1);
                reqAttrs.setIds(ids);
                reqAttrs.retrieveData();
                // set ids back to make saveChanges work
                ids.remove("resourceToGetServicesFrom");
                reqAttrs.setIds(ids);
                for (HandlerRegistration handler : clickHandlers) {
                    if (handler != null) {
                        handler.removeHandler();
                    }
                }
                clickHandlers.add(saveChangesButton.addClickHandler(saveReqAttrsClickHandler));
                clickHandlers.add(removeButton.addClickHandler(removeReqAttrsClickHandler));
            }
            // resource and member selected
            if (resIndex > 0 && memIndex > 0) {
                lastSelectedMemberId = membersDropDown.getSelectedObject().getId();
                lastSelectedResourceId = resourceDropDown.getSelectedObject().getId();
                memChb.setValue(false);
                memChb.setVisible(true);
                jsonCallback.clearTable();
                ids.clear();
                ids.put("resource", resourceDropDown.getSelectedObject().getId());
                ids.put("resourceToGetServicesFrom", ids.get("resource"));
                ids.put("member", membersDropDown.getSelectedObject().getId());
                ids.put("workWithUserAttributes", 1);
                reqAttrs.setIds(ids);
                reqAttrs.retrieveData();
                // set ids back to make saveChanges work
                ids.clear();
                ids.put("member", membersDropDown.getSelectedObject().getId());
                ids.put("user", membersDropDown.getSelectedObject().getUser().getId());
                ids.put("facility", resourceDropDown.getSelectedObject().getFacilityId());
                ids.put("resource", resourceDropDown.getSelectedObject().getId());
                reqAttrs.setIds(ids);
                for (HandlerRegistration handler : clickHandlers) {
                    if (handler != null) {
                        handler.removeHandler();
                    }
                }
                clickHandlers.add(saveChangesButton.addClickHandler(saveReqAttrsClickHandler));
                clickHandlers.add(removeButton.addClickHandler(removeReqAttrsClickHandler));
            }
        }
    };
    // ADD CHANGE HANDLERS TO DROP-DOWN MENUS
    resourceDropDown.addChangeHandler(changeHandler);
    membersDropDown.addChangeHandler(changeHandler);
    // PUT ELEMENTS IN MENU
    menu.addWidget(UiElements.getRefreshButton(this));
    menu.addWidget(saveChangesButton);
    // menu.addWidget(resChb);
    menu.addWidget(memChb);
    menu.addWidget(setNewAttributeButton);
    menu.addWidget(removeButton);
    menu.addWidget(new HTML("<strong>Resources:</strong>"));
    menu.addWidget(resourceDropDown);
    menu.addWidget(new HTML("<strong>Members:</strong>"));
    menu.addWidget(membersDropDown);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) HashMap(java.util.HashMap) Scheduler(com.google.gwt.core.client.Scheduler) ArrayList(java.util.ArrayList) SetAttributes(cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) SetNewAttributeTabItem(cz.metacentrum.perun.webgui.tabs.attributestabs.SetNewAttributeTabItem) HandlerRegistration(com.google.gwt.event.shared.HandlerRegistration) GetAttributesV2(cz.metacentrum.perun.webgui.json.attributesManager.GetAttributesV2) ImageResource(com.google.gwt.resources.client.ImageResource) GetResourceRequiredAttributesV2(cz.metacentrum.perun.webgui.json.attributesManager.GetResourceRequiredAttributesV2) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) GetGroupRichMembers(cz.metacentrum.perun.webgui.json.groupsManager.GetGroupRichMembers) GetAssignedResources(cz.metacentrum.perun.webgui.json.resourcesManager.GetAssignedResources) HashMap(java.util.HashMap) Map(java.util.Map) RemoveAttributes(cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)

Example 17 with SetAttributes

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

the class FacilitySettingsTabItem method draw.

public Widget draw() {
    // set title
    titleWidget.setText(Utils.getStrippedStringWithEllipsis(facility.getName()) + ": Service settings");
    // content
    vp.setSize("100%", "100%");
    vp.clear();
    // HORIZONTAL MENU
    TabMenu menu = new TabMenu();
    // Get Attributes method
    final GetRequiredAttributesV2 reqAttrs = new GetRequiredAttributesV2();
    final GetAttributesV2 attrs = new GetAttributesV2();
    attrs.getFacilityAttributes(facilityId);
    // get empty table
    final CellTable<Attribute> table = reqAttrs.getEmptyTable();
    final CellTable<Attribute> table2 = attrs.getEmptyTable();
    sp.setWidget(table);
    sp2.setWidget(table2);
    // ids to retrieve data from rpc
    final Map<String, Integer> ids = new HashMap<String, Integer>();
    ids.put("facility", facility.getId());
    // service switcher checkbox
    final CheckBox switchServicesChb = new CheckBox(WidgetTranslation.INSTANCE.offerAvailableServicesOnly(), false);
    // selected by default - unselected if switch hidden
    switchServicesChb.setValue(lastCheckBoxValue);
    switchServicesChb.setTitle(WidgetTranslation.INSTANCE.offerAvailableServicesOnlyTitle());
    // services listbox
    final ListBoxWithObjects<Service> servList = new ListBoxWithObjects<Service>();
    // on change of service update table
    servList.addChangeHandler(new ChangeHandler() {

        public void onChange(ChangeEvent event) {
            if (servList.getSelectedIndex() == 0) {
                // show all facility attributes
                attrs.retrieveData();
                setTable(false);
                lastServiceId = 0;
                indexInList = 0;
                return;
            }
            if (switchServicesChb.getValue() == true && servList.getSelectedIndex() == 1) {
                // show required attrs for all assigned services
                ids.remove("service");
                lastServiceId = 0;
                indexInList = 1;
            } else if ((switchServicesChb.getValue() == true && servList.getSelectedIndex() > 1) || (switchServicesChb.getValue() == false && servList.getSelectedIndex() > 0)) {
                // show required attrs for selected service
                // >0 when listing all services
                // >1 when listing assigned services
                ids.put("service", servList.getSelectedObject().getId());
                lastServiceId = servList.getSelectedObject().getId();
            }
            // load req attrs
            reqAttrs.setIds(ids);
            reqAttrs.clearTable();
            reqAttrs.retrieveData();
            setTable(true);
        }
    });
    // event which fills the listbox and call getRequiredAttributes
    final JsonCallbackEvents event = new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            servList.clear();
            ArrayList<Service> srv = JsonUtils.jsoAsList(jso);
            srv = new TableSorter<Service>().sortByName(srv);
            for (int i = 0; i < srv.size(); i++) {
                servList.addItem(srv.get(i));
            }
            // no services available - load all facility attrs
            if (servList.isEmpty()) {
                servList.addNotSelectedOption();
                lastServiceId = 0;
                indexInList = 0;
                attrs.retrieveData();
                setTable(false);
                return;
            }
            // offer only available
            if (switchServicesChb.getValue() == true) {
                servList.addNotSelectedOption();
                servList.addAllOption();
                if (lastServiceId == 0) {
                    if (indexInList == 1) {
                        // all
                        servList.setSelectedIndex(1);
                    } else if (indexInList == 0) {
                        // not selected - load all fac attrs
                        servList.setSelectedIndex(0);
                        attrs.retrieveData();
                        setTable(false);
                        return;
                    }
                }
            } else {
                // offer all services
                servList.addNotSelectedOption();
                if (lastServiceId == 0) {
                    // if no service selected, load all fac attrs
                    servList.setSelectedIndex(0);
                    attrs.retrieveData();
                    setTable(false);
                    return;
                }
            }
            // if some service was selected
            if (lastServiceId != 0) {
                // remove service since we can't be sure, it was loaded again
                ids.remove("service");
                // either all or first service in a list
                servList.setSelectedIndex(1);
                for (Service s : servList.getAllObjects()) {
                    if (s.getId() == lastServiceId) {
                        // if found, select it
                        servList.setSelected(s, true);
                        ids.put("service", lastServiceId);
                        break;
                    }
                }
            }
            // get required attrs for service
            reqAttrs.clearTable();
            reqAttrs.setIds(ids);
            reqAttrs.retrieveData();
            setTable(true);
        }

        @Override
        public void onError(PerunError error) {
            servList.clear();
            if (required) {
                ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingError(error);
            } else {
                ((AjaxLoaderImage) table2.getEmptyTableWidget()).loadingError(error);
            }
            servList.addItem("Error while loading");
        }

        @Override
        public void onLoadingStart() {
            servList.removeAllOption();
            servList.removeNotSelectedOption();
            servList.clear();
            servList.addItem("Loading...");
        }
    };
    final GetServices allServices = new GetServices(event);
    final GetFacilityAssignedServices assignedServices = new GetFacilityAssignedServices(facility.getId(), event);
    // if hide and unchecked or just unchecked
    if (!lastCheckBoxValue) {
        allServices.retrieveData();
    } else {
        assignedServices.retrieveData();
    }
    // Save changes button
    final CustomButton saveChangesButton = TabMenu.getPredefinedButton(ButtonType.SAVE, ButtonTranslation.INSTANCE.saveChangesInAttributes());
    final JsonCallbackEvents refreshEvents = JsonCallbackEvents.refreshTableEvents(reqAttrs);
    final JsonCallbackEvents refreshEvents2 = JsonCallbackEvents.refreshTableEvents(attrs);
    final JsonCallbackEvents saveChangesButtonEvent = JsonCallbackEvents.disableButtonEvents(saveChangesButton, refreshEvents);
    final JsonCallbackEvents saveChangesButtonEvent2 = JsonCallbackEvents.disableButtonEvents(saveChangesButton, refreshEvents2);
    saveChangesButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = (required) ? reqAttrs.getTableSelectedList() : attrs.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                SetAttributes request = new SetAttributes((required) ? saveChangesButtonEvent : saveChangesButtonEvent2);
                request.setAttributes(ids, list);
            }
        }
    });
    // Remove attr button
    final CustomButton removeButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeAttributes());
    final JsonCallbackEvents removeButtonEvent = JsonCallbackEvents.disableButtonEvents(removeButton, refreshEvents);
    final JsonCallbackEvents removeButtonEvent2 = JsonCallbackEvents.disableButtonEvents(removeButton, refreshEvents2);
    removeButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            ArrayList<Attribute> list = (required) ? reqAttrs.getTableSelectedList() : attrs.getTableSelectedList();
            if (UiElements.cantSaveEmptyListDialogBox(list)) {
                Map<String, Integer> ids = new HashMap<String, Integer>();
                ids.put("facility", facilityId);
                RemoveAttributes request = new RemoveAttributes((required) ? removeButtonEvent : removeButtonEvent2);
                request.removeAttributes(ids, list);
            }
        }
    });
    // switch serv checkbox
    switchServicesChb.addClickHandler(new ClickHandler() {

        // load proper set of services on click
        public void onClick(ClickEvent event) {
            lastCheckBoxValue = switchServicesChb.getValue();
            if (switchServicesChb.getValue() == true) {
                assignedServices.retrieveData();
            } else {
                allServices.retrieveData();
            }
        }
    });
    // allow to set new (currently unused facility attribute)
    CustomButton setNewAttributeButton = TabMenu.getPredefinedButton(ButtonType.ADD, true, ButtonTranslation.INSTANCE.setNewAttributes(), new ClickHandler() {

        public void onClick(ClickEvent event) {
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("facility", facility.getId());
            session.getTabManager().addTabToCurrentTab(new SetNewAttributeTabItem(ids, (required) ? reqAttrs.getList() : attrs.getList()), true);
        }
    });
    menu.addWidget(UiElements.getRefreshButton(this));
    menu.addWidget(saveChangesButton);
    menu.addWidget(setNewAttributeButton);
    menu.addWidget(removeButton);
    menu.addWidget(new HTML("<strong>Service: </strong>"));
    menu.addWidget(servList);
    if (!hide) {
        menu.addWidget(switchServicesChb);
    }
    /* TODO - not yet implemented

			 CustomButton fillDefaultButton = new CustomButton("Fill default values", SmallIcons.INSTANCE.scriptGoIcon(), new ClickHandler(){
			 public void onClick(ClickEvent event) {
			 Window.alert("not yet implemented");
			 }
			 });
			 fillDefaultButton.setTitle("Fill default values for this service/facility - nothing is saved unless you click on 'Save changes'");
			 CustomButton checkValuesButton = new CustomButton("Check values", SmallIcons.INSTANCE.scriptGearIcon(), new ClickHandler(){
			 public void onClick(ClickEvent event) {
			 Window.alert("not yet implemented");
			 }
			 });
			 checkValuesButton.setTitle("Checks inserted values against current Perun state - nothing is saved unless you click on 'Save changes'");

			 menu.addWidget(fillDefaultButton);
			 menu.addWidget(checkValuesButton);

*/
    // add a class to the table and wrap it into scroll panel
    table.addStyleName("perun-table");
    table.setWidth("100%");
    table2.addStyleName("perun-table");
    table2.setWidth("100%");
    sp.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp, 350, this);
    sp2.addStyleName("perun-tableScrollPanel");
    session.getUiElements().resizePerunTable(sp2, 350, this);
    // add menu and the table to the main panel
    vp.add(menu);
    vp.setCellHeight(menu, "30px");
    // default is required attributes
    setTable(true);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) AjaxLoaderImage(cz.metacentrum.perun.webgui.widgets.AjaxLoaderImage) Attribute(cz.metacentrum.perun.webgui.model.Attribute) HashMap(java.util.HashMap) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) SetAttributes(cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes) GetServices(cz.metacentrum.perun.webgui.json.servicesManager.GetServices) ListBoxWithObjects(cz.metacentrum.perun.webgui.widgets.ListBoxWithObjects) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) SetNewAttributeTabItem(cz.metacentrum.perun.webgui.tabs.attributestabs.SetNewAttributeTabItem) GetRequiredAttributesV2(cz.metacentrum.perun.webgui.json.attributesManager.GetRequiredAttributesV2) GetAttributesV2(cz.metacentrum.perun.webgui.json.attributesManager.GetAttributesV2) Service(cz.metacentrum.perun.webgui.model.Service) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) GetFacilityAssignedServices(cz.metacentrum.perun.webgui.json.servicesManager.GetFacilityAssignedServices) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError) HashMap(java.util.HashMap) Map(java.util.Map) RemoveAttributes(cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)

Aggregations

SetAttributes (cz.metacentrum.perun.webgui.json.attributesManager.SetAttributes)17 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)15 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)14 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)14 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)14 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)13 Map (java.util.Map)12 RemoveAttributes (cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)11 Attribute (cz.metacentrum.perun.webgui.model.Attribute)9 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)9 GetAttributesV2 (cz.metacentrum.perun.webgui.json.attributesManager.GetAttributesV2)8 SetNewAttributeTabItem (cz.metacentrum.perun.webgui.tabs.attributestabs.SetNewAttributeTabItem)8 ChangeEvent (com.google.gwt.event.dom.client.ChangeEvent)4 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)4 GetResourceRequiredAttributesV2 (cz.metacentrum.perun.webgui.json.attributesManager.GetResourceRequiredAttributesV2)3 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)3 FieldUpdater (com.google.gwt.cell.client.FieldUpdater)2 ImageResource (com.google.gwt.resources.client.ImageResource)2