Search in sources :

Example 1 with GetRichDestinations

use of cz.metacentrum.perun.webgui.json.servicesManager.GetRichDestinations in project perun by CESNET.

the class ServiceDestinationsTabItem method draw.

public Widget draw() {
    this.titleWidget.setText("Service destinations");
    final VerticalPanel vp = new VerticalPanel();
    vp.setSize("100%", "100%");
    TabMenu menu = new TabMenu();
    menu.addWidget(UiElements.getRefreshButton(this));
    // Adds menu to the main panel
    vp.add(menu);
    vp.setCellHeight(menu, "30px");
    // buttons
    final CustomButton addDestButton = TabMenu.getPredefinedButton(ButtonType.ADD, true, ButtonTranslation.INSTANCE.addDestination());
    final CustomButton removeDestButton = TabMenu.getPredefinedButton(ButtonType.REMOVE, ButtonTranslation.INSTANCE.removeSelectedDestinations());
    menu.addWidget(addDestButton);
    menu.addWidget(removeDestButton);
    final CustomButton blockButton = new CustomButton(ButtonTranslation.INSTANCE.blockPropagationButton(), ButtonTranslation.INSTANCE.blockServicesOnFacility(), SmallIcons.INSTANCE.stopIcon());
    final CustomButton allowButton = new CustomButton(ButtonTranslation.INSTANCE.allowPropagationButton(), ButtonTranslation.INSTANCE.allowServicesOnFacility(), SmallIcons.INSTANCE.acceptIcon());
    menu.addWidget(allowButton);
    menu.addWidget(blockButton);
    menu.addWidget(new HTML("<strong>Selected facility:</strong>"));
    // listbox with facilities
    final ListBoxWithObjects<Facility> ls = new ListBoxWithObjects<Facility>();
    menu.addWidget(ls);
    // get empty table used for destinations
    final GetAllRichDestinations callback = new GetAllRichDestinations(null, service);
    callback.showFacilityColumn(true);
    callback.showServiceColumn(false);
    // do not make callback yet
    final CellTable<Destination> table = callback.getEmptyTable();
    if (lastSelectedFacilityId == 0) {
        callback.retrieveData();
    }
    // refresh events called when selection changes or callback ends
    final JsonCallbackEvents refreshEvents = new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            if (ls.getSelectedIndex() == 0) {
                // fills table with destinations of all facilities
                callback.clearTable();
                callback.retrieveData();
            } else {
                callback.clearTable();
                ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingStart();
                // fills table with destinations of selected facility
                JsonCallbackEvents localEvents = new JsonCallbackEvents() {

                    @Override
                    public void onFinished(JavaScriptObject jso) {
                        ArrayList<Destination> dest = JsonUtils.jsoAsList(jso);
                        for (Destination d : dest) {
                            // d.setFacility(ls.getSelectedObject());
                            // d.setService(service);
                            callback.addToTable(d);
                        }
                        ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingFinished();
                    }

                    @Override
                    public void onError(PerunError error) {
                        ((AjaxLoaderImage) table.getEmptyTableWidget()).loadingError(error);
                    }
                };
                final GetRichDestinations callback = new GetRichDestinations(ls.getSelectedObject(), service, localEvents);
                callback.retrieveData();
            }
        }
    };
    // fills listbox and table with dest. for all service facilities
    JsonCallbackEvents events = new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            ls.clear();
            ArrayList<Facility> facs = JsonUtils.jsoAsList(jso);
            facs = new TableSorter<Facility>().sortByName(facs);
            // if no facility
            if (facs.size() == 0) {
                ls.addItem("No facility available");
                return;
            }
            for (int i = 0; i < facs.size(); i++) {
                ls.addItem(facs.get(i));
                if (facs.get(i).getId() == lastSelectedFacilityId) {
                    ls.setSelected(facs.get(i), true);
                }
            }
            ls.addAllOption();
            if (lastSelectedFacilityId == 0) {
                // select all
                ls.setItemSelected(0, true);
            } else {
                // was selected
                addDestButton.setEnabled(true);
                refreshEvents.onFinished(null);
            }
        }

        public void onError(PerunError error) {
            ls.addItem("Error while loading");
            addDestButton.setEnabled(false);
        }

        public void onLoadingStart() {
            ls.clear();
            ls.addItem("Loading...");
            addDestButton.setEnabled(false);
        }
    };
    final GetAssignedFacilities assignedFacilities = new GetAssignedFacilities(PerunEntity.SERVICE, serviceId, events);
    assignedFacilities.retrieveData();
    ls.addChangeHandler(new ChangeHandler() {

        public void onChange(ChangeEvent event) {
            if (ls.getSelectedIndex() > 0) {
                // store last selected facility id
                addDestButton.setEnabled(true);
                lastSelectedFacilityId = ls.getSelectedObject().getId();
            } else {
                addDestButton.setEnabled(false);
                lastSelectedFacilityId = 0;
            }
            refreshEvents.onFinished(null);
        }
    });
    // CLICK HANDLERS FOR BUTTONS
    addDestButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            session.getTabManager().addTabToCurrentTab(new AddFacilityDestinationTabItem(ls.getSelectedObject()));
        }
    });
    removeDestButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            // get
            final ArrayList<Destination> destsToRemove = callback.getTableSelectedList();
            UiElements.showDeleteConfirm(destsToRemove, new ClickHandler() {

                @Override
                public void onClick(ClickEvent clickEvent) {
                    // TODO - SHOULD HAVE ONLY ONE CALLBACK TO CORE
                    for (int i = 0; i < destsToRemove.size(); i++) {
                        if (i == destsToRemove.size() - 1) {
                            RemoveDestination request = new RemoveDestination(destsToRemove.get(i).getFacility().getId(), service.getId(), JsonCallbackEvents.disableButtonEvents(removeDestButton, refreshEvents));
                            request.removeDestination(destsToRemove.get(i).getDestination(), destsToRemove.get(i).getType());
                        } else {
                            RemoveDestination request = new RemoveDestination(destsToRemove.get(i).getFacility().getId(), service.getId(), JsonCallbackEvents.disableButtonEvents(removeDestButton));
                            request.removeDestination(destsToRemove.get(i).getDestination(), destsToRemove.get(i).getType());
                        }
                    }
                }
            });
        }
    });
    allowButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            final ArrayList<Destination> destForBlockUnblock = callback.getTableSelectedList();
            // TODO - SHOULD HAVE ONLY ONE CALLBACK TO CORE !!
            for (int i = 0; i < destForBlockUnblock.size(); i++) {
                if (i == destForBlockUnblock.size() - 1) {
                    BlockUnblockServiceOnDestination request = new BlockUnblockServiceOnDestination(JsonCallbackEvents.disableButtonEvents(allowButton, refreshEvents));
                    request.unblockServiceOnDestination(destForBlockUnblock.get(i).getService().getId(), destForBlockUnblock.get(i).getId());
                } else {
                    BlockUnblockServiceOnDestination request = new BlockUnblockServiceOnDestination(JsonCallbackEvents.disableButtonEvents(allowButton));
                    request.unblockServiceOnDestination(destForBlockUnblock.get(i).getService().getId(), destForBlockUnblock.get(i).getId());
                }
            }
        }
    });
    blockButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            final ArrayList<Destination> destForBlockUnblock = callback.getTableSelectedList();
            // TODO - SHOULD HAVE ONLY ONE CALLBACK TO CORE !!
            for (int i = 0; i < destForBlockUnblock.size(); i++) {
                if (i == destForBlockUnblock.size() - 1) {
                    BlockUnblockServiceOnDestination request = new BlockUnblockServiceOnDestination(JsonCallbackEvents.disableButtonEvents(blockButton, refreshEvents));
                    request.blockServiceOnDestination(destForBlockUnblock.get(i).getService().getId(), destForBlockUnblock.get(i).getId());
                } else {
                    BlockUnblockServiceOnDestination request = new BlockUnblockServiceOnDestination(JsonCallbackEvents.disableButtonEvents(blockButton));
                    request.blockServiceOnDestination(destForBlockUnblock.get(i).getService().getId(), destForBlockUnblock.get(i).getId());
                }
            }
        }
    });
    // filter box
    menu.addFilterWidget(new ExtendedSuggestBox(callback.getOracle()), new PerunSearchEvent() {

        public void searchFor(String text) {
            callback.filterTable(text);
        }
    }, ButtonTranslation.INSTANCE.filterDestinationByFacility());
    table.addStyleName("perun-table");
    ScrollPanel sp = new ScrollPanel(table);
    sp.addStyleName("perun-tableScrollPanel");
    vp.add(sp);
    session.getUiElements().resizePerunTable(sp, 350, this);
    blockButton.setEnabled(false);
    allowButton.setEnabled(false);
    removeDestButton.setEnabled(false);
    JsonUtils.addTableManagedButton(callback, table, blockButton);
    JsonUtils.addTableManagedButton(callback, table, allowButton);
    JsonUtils.addTableManagedButton(callback, table, removeDestButton);
    // add tabs to the main panel
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) RemoveDestination(cz.metacentrum.perun.webgui.json.servicesManager.RemoveDestination) BlockUnblockServiceOnDestination(cz.metacentrum.perun.webgui.json.servicesManager.BlockUnblockServiceOnDestination) Destination(cz.metacentrum.perun.webgui.model.Destination) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) GetAllRichDestinations(cz.metacentrum.perun.webgui.json.servicesManager.GetAllRichDestinations) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) GetAssignedFacilities(cz.metacentrum.perun.webgui.json.facilitiesManager.GetAssignedFacilities) AddFacilityDestinationTabItem(cz.metacentrum.perun.webgui.tabs.facilitiestabs.AddFacilityDestinationTabItem) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) BlockUnblockServiceOnDestination(cz.metacentrum.perun.webgui.json.servicesManager.BlockUnblockServiceOnDestination) Facility(cz.metacentrum.perun.webgui.model.Facility) PerunError(cz.metacentrum.perun.webgui.model.PerunError) GetRichDestinations(cz.metacentrum.perun.webgui.json.servicesManager.GetRichDestinations) RemoveDestination(cz.metacentrum.perun.webgui.json.servicesManager.RemoveDestination)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 ChangeEvent (com.google.gwt.event.dom.client.ChangeEvent)1 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 GetAssignedFacilities (cz.metacentrum.perun.webgui.json.facilitiesManager.GetAssignedFacilities)1 BlockUnblockServiceOnDestination (cz.metacentrum.perun.webgui.json.servicesManager.BlockUnblockServiceOnDestination)1 GetAllRichDestinations (cz.metacentrum.perun.webgui.json.servicesManager.GetAllRichDestinations)1 GetRichDestinations (cz.metacentrum.perun.webgui.json.servicesManager.GetRichDestinations)1 RemoveDestination (cz.metacentrum.perun.webgui.json.servicesManager.RemoveDestination)1 Destination (cz.metacentrum.perun.webgui.model.Destination)1 Facility (cz.metacentrum.perun.webgui.model.Facility)1 PerunError (cz.metacentrum.perun.webgui.model.PerunError)1 AddFacilityDestinationTabItem (cz.metacentrum.perun.webgui.tabs.facilitiestabs.AddFacilityDestinationTabItem)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ArrayList (java.util.ArrayList)1