Search in sources :

Example 1 with SetAttribute

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

the class ShellChangeTabItem method draw.

public Widget draw() {
    VerticalPanel vp = new VerticalPanel();
    final FlexTable ft = new FlexTable();
    ft.setWidth("350px");
    ft.setStyleName("inputFormFlexTable");
    ft.setHTML(0, 0, "Available shells:");
    ft.getFlexCellFormatter().setStyleName(0, 0, "itemName");
    final ListBox shells = new ListBox();
    shells.setWidth("200px");
    ft.setWidget(0, 1, shells);
    vp.add(ft);
    final CustomButton selectShellButton = TabMenu.getPredefinedButton(ButtonType.SAVE, "Save preferred shell");
    // callback for available shells
    GetAttributes attrs = new GetAttributes(new JsonCallbackEvents() {

        @Override
        public void onError(PerunError error) {
            shells.clear();
            shells.addItem("Error while loading");
        }

        @Override
        public void onFinished(JavaScriptObject jso) {
            shells.clear();
            ArrayList<Attribute> list = JsonUtils.jsoAsList(jso);
            if (list.isEmpty() || list == null) {
                shells.addItem("No shells available");
                return;
            }
            // fill shells
            for (Attribute a : list) {
                if (a.getFriendlyName().equalsIgnoreCase("shells")) {
                    for (int i = 0; i < a.getValueAsJsArray().length(); i++) {
                        // fill shell values
                        shells.addItem(a.getValueAsJsArray().get(i));
                    }
                    break;
                }
            }
            // set selected
            for (int i = 0; i < shells.getItemCount(); i++) {
                if (shells.getValue(i).equals(a.getValue())) {
                    shells.setSelectedIndex(i);
                    break;
                }
            }
            if (shells.getValue(shells.getSelectedIndex()).equals(a.getValue())) {
                selectShellButton.setEnabled(false);
            } else {
                selectShellButton.setEnabled(true);
            }
        }

        @Override
        public void onLoadingStart() {
            shells.clear();
            shells.addItem("Loading...");
        }
    });
    final TabItem tab = this;
    TabMenu menu = new TabMenu();
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    selectShellButton.setEnabled(false);
    selectShellButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            // OK click button
            // set new value
            a.setValue(shells.getValue(shells.getSelectedIndex()));
            // send request
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("user", userId);
            ids.put("facility", resource.getFacilityId());
            SetAttribute request = new SetAttribute(JsonCallbackEvents.disableButtonEvents(selectShellButton, new JsonCallbackEvents() {

                @Override
                public void onFinished(JavaScriptObject jso) {
                    // refresh only what's necessary
                    events.onFinished(jso);
                    // don't refresh underlaying tab
                    session.getTabManager().closeTab(tab, false);
                }
            }));
            request.setAttribute(ids, a);
        }
    });
    attrs.getResourceAttributes(resource.getId());
    attrs.retrieveData();
    shells.addChangeHandler(new ChangeHandler() {

        @Override
        public void onChange(ChangeEvent changeEvent) {
            if (shells.getValue(shells.getSelectedIndex()).equals(a.getValue())) {
                selectShellButton.setEnabled(false);
            } else {
                selectShellButton.setEnabled(true);
            }
        }
    });
    final CustomButton defaultButton = new CustomButton("Use default", "", SmallIcons.INSTANCE.lightningIcon());
    defaultButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Map<String, Integer> ids = new HashMap<String, Integer>();
            ids.put("user", userId);
            ids.put("facility", resource.getFacilityId());
            RemoveAttributes request = new RemoveAttributes(JsonCallbackEvents.disableButtonEvents(defaultButton, new JsonCallbackEvents() {

                @Override
                public void onFinished(JavaScriptObject jso) {
                    // refresh only what's necessary
                    events.onFinished(jso);
                    // don't refresh underlaying tab
                    session.getTabManager().closeTab(tab, false);
                }
            }));
            ArrayList<Attribute> list = new ArrayList<Attribute>();
            list.add(a);
            request.removeAttributes(ids, list);
        }
    });
    menu.addWidget(selectShellButton);
    menu.addWidget(defaultButton);
    menu.addWidget(TabMenu.getPredefinedButton(ButtonType.CANCEL, "", new ClickHandler() {

        @Override
        public void onClick(ClickEvent clickEvent) {
            session.getTabManager().closeTab(tab, false);
        }
    }));
    contentWidget.setWidget(vp);
    return getWidget();
}
Also used : GetAttributes(cz.metacentrum.perun.webgui.json.attributesManager.GetAttributes) JsonCallbackEvents(cz.metacentrum.perun.webgui.json.JsonCallbackEvents) SetAttribute(cz.metacentrum.perun.webgui.json.attributesManager.SetAttribute) Attribute(cz.metacentrum.perun.webgui.model.Attribute) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) ArrayList(java.util.ArrayList) SetAttribute(cz.metacentrum.perun.webgui.json.attributesManager.SetAttribute) TabMenu(cz.metacentrum.perun.webgui.widgets.TabMenu) TabItem(cz.metacentrum.perun.webgui.tabs.TabItem) ClickHandler(com.google.gwt.event.dom.client.ClickHandler) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton) PerunError(cz.metacentrum.perun.webgui.model.PerunError) HashMap(java.util.HashMap) Map(java.util.Map) RemoveAttributes(cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)

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 GetAttributes (cz.metacentrum.perun.webgui.json.attributesManager.GetAttributes)1 RemoveAttributes (cz.metacentrum.perun.webgui.json.attributesManager.RemoveAttributes)1 SetAttribute (cz.metacentrum.perun.webgui.json.attributesManager.SetAttribute)1 Attribute (cz.metacentrum.perun.webgui.model.Attribute)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 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1