Search in sources :

Example 1 with MoveGroup

use of cz.metacentrum.perun.webgui.json.groupsManager.MoveGroup in project perun by CESNET.

the class MoveGroupsTabItem method draw.

public Widget draw() {
    VerticalPanel vp = new VerticalPanel();
    int voId = (vo != null) ? vo.getId() : group.getVoId();
    List<Group> backupGroups = new ArrayList<>();
    backupGroups.addAll(groups);
    // remove any subgroups of parent groups in a list of MOVED groups
    for (Group bg : backupGroups) {
        Iterator<? extends Group> iterator = groups.iterator();
        while (iterator.hasNext()) {
            // check if is subgroup
            Group moveGroup = iterator.next();
            if (moveGroup.getName().startsWith(bg.getName() + ":")) {
                iterator.remove();
            }
        }
    }
    // textboxes which set the class data when updated
    final ListBoxWithObjects<Group> vosGroups = new ListBoxWithObjects<Group>();
    // prepares layout
    FlexTable layout = new FlexTable();
    layout.setStyleName("inputFormFlexTable");
    FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();
    // close tab events
    final TabItem tab = this;
    TabMenu menu = new TabMenu();
    // send button
    final CustomButton moveButton = TabMenu.getPredefinedButton(ButtonType.MOVE, buttonTranslation.moveGroup());
    moveButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            final Group destinationGroup = vosGroups.getSelectedObject();
            final MoveGroup request = new MoveGroup();
            final JsonCallbackEvents nextEvent = JsonCallbackEvents.disableButtonEvents(moveButton, new JsonCallbackEvents() {

                int groupsCounter = 0;

                @Override
                public void onFinished(JavaScriptObject jso) {
                    if (groups.size() - 1 == groupsCounter) {
                        // there is another group to move
                        request.setEvents(JsonCallbackEvents.closeTabDisableButtonEvents(moveButton, tab, true));
                        request.moveGroup(groups.get(groupsCounter), destinationGroup);
                    } else if (groups.size() - 1 > groupsCounter) {
                        request.moveGroup(groups.get(groupsCounter), destinationGroup);
                    }
                // done
                }

                @Override
                public void onError(PerunError error) {
                    // close tab if failed
                    moveButton.setProcessing(false);
                    session.getTabManager().closeTab(tab);
                }

                @Override
                public void onLoadingStart() {
                    groupsCounter++;
                }
            });
            if (groups.size() == 1) {
                // single group
                request.setEvents(JsonCallbackEvents.closeTabDisableButtonEvents(moveButton, tab, true));
            } else {
                // iterate over more groups
                request.setEvents(nextEvent);
            }
            request.moveGroup(groups.get(0), destinationGroup);
        }
    });
    final GetAllGroups groupsCall = new GetAllGroups(voId, new JsonCallbackEvents() {

        public void onFinished(JavaScriptObject jso) {
            vosGroups.clear();
            ArrayList<Group> retGroups = JsonUtils.jsoAsList(jso);
            retGroups = new TableSorter<Group>().sortByName(retGroups);
            // skip groups which are moving and their sub-groups and direct parents (since they would stayed at the same place)
            Iterator<Group> iterator = retGroups.iterator();
            while (iterator.hasNext()) {
                Group retGroup = iterator.next();
                for (Group g : groups) {
                    if (g.getId() == retGroup.getId() || retGroup.getName().startsWith(g.getName() + ":") || retGroup.getName().equals(g.getName()) || g.getParentGroupId() == retGroup.getId()) {
                        iterator.remove();
                        break;
                    }
                }
            }
            for (Group g : retGroups) {
                if (!g.isCoreGroup()) {
                    // SKIP CORE GROUPS !!
                    vosGroups.addItem(g);
                }
            }
            if (vosGroups.getAllObjects().isEmpty()) {
                for (Group g : groups) {
                    // at lease one of moving groups is a sub group, so we can offer move to top-level
                    if (g.getName().contains(":")) {
                        vosGroups.addNotSelectedOption();
                        moveButton.setEnabled(true);
                        return;
                    }
                }
                // can't move group anywhere
                vosGroups.addItem("No possible destination group was found or group is already top-level.");
            } else {
                vosGroups.addNotSelectedOption();
                moveButton.setEnabled(true);
            }
        }

        public void onLoadingStart() {
            vosGroups.clear();
            vosGroups.addItem("Loading...");
            moveButton.setEnabled(false);
        }

        public void onError(PerunError error) {
            vosGroups.clear();
            vosGroups.addItem("Error while loading");
        }
    });
    groupsCall.retrieveData();
    // cancel button
    final CustomButton cancelButton = TabMenu.getPredefinedButton(ButtonType.CANCEL, "");
    cancelButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent clickEvent) {
            session.getTabManager().closeTab(tab, isRefreshParentOnClose());
        }
    });
    layout.setHTML(0, 0, "Following groups will be moved (including all their sub-groups):");
    layout.getFlexCellFormatter().setColSpan(0, 0, 2);
    String items = "<ul>";
    for (Group g : groups) {
        items = items.concat("<li>" + g.getName() + "</li>");
    }
    items = items.concat("</ul>");
    ScrollPanel sp = new ScrollPanel();
    sp.setStyleName("border");
    sp.setSize("100%", "100px");
    sp.add(new HTML(items));
    layout.setWidget(1, 0, sp);
    layout.getFlexCellFormatter().setColSpan(1, 0, 2);
    // Add some standard form options
    layout.setHTML(2, 0, "Destination&nbsp;group:");
    layout.setWidget(2, 1, vosGroups);
    for (int i = 2; i < layout.getRowCount(); i++) {
        cellFormatter.addStyleName(i, 0, "itemName");
    }
    layout.setHTML(3, 0, "Group(s) will be moved (including all their sub-groups) under destination group! If no destination group is selected, group will be moved to top-level.<p>We strongly recommend to move groups one by one.");
    layout.getFlexCellFormatter().setStyleName(3, 0, "inputFormInlineComment");
    layout.getFlexCellFormatter().setColSpan(3, 0, 2);
    vp.setWidth("400px");
    menu.addWidget(moveButton);
    menu.addWidget(cancelButton);
    vp.add(layout);
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : MoveGroup(cz.metacentrum.perun.webgui.json.groupsManager.MoveGroup) JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) MoveGroup(cz.metacentrum.perun.webgui.json.groupsManager.MoveGroup) Group(cz.metacentrum.perun.webgui.model.Group) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) HTML(com.google.gwt.user.client.ui.HTML) ListBoxWithObjects(cz.metacentrum.perun.webgui.widgets.ListBoxWithObjects) GetAllGroups(cz.metacentrum.perun.webgui.json.groupsManager.GetAllGroups) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) Iterator(java.util.Iterator) FlexCellFormatter(com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter) FlexTable(com.google.gwt.user.client.ui.FlexTable) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) VerticalPanel(com.google.gwt.user.client.ui.VerticalPanel) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) PerunError(cz.metacentrum.perun.webgui.model.PerunError) ScrollPanel(com.google.gwt.user.client.ui.ScrollPanel)

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 FlexTable (com.google.gwt.user.client.ui.FlexTable)1 FlexCellFormatter (com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter)1 HTML (com.google.gwt.user.client.ui.HTML)1 ScrollPanel (com.google.gwt.user.client.ui.ScrollPanel)1 VerticalPanel (com.google.gwt.user.client.ui.VerticalPanel)1 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)1 GetAllGroups (cz.metacentrum.perun.webgui.json.groupsManager.GetAllGroups)1 MoveGroup (cz.metacentrum.perun.webgui.json.groupsManager.MoveGroup)1 Group (cz.metacentrum.perun.webgui.model.Group)1 PerunError (cz.metacentrum.perun.webgui.model.PerunError)1 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ListBoxWithObjects (cz.metacentrum.perun.webgui.widgets.ListBoxWithObjects)1 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1