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();
}
Aggregations