Search in sources :

Example 1 with UpdateService

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

the class EditServiceDetailsTabItem method draw.

public Widget draw() {
    titleWidget = new Label("Edit service");
    VerticalPanel vp = new VerticalPanel();
    final ExtendedTextBox serviceName = new ExtendedTextBox();
    final ExtendedTextBox serviceDescription = new ExtendedTextBox();
    final ExtendedTextBox scriptPath = new ExtendedTextBox();
    final CheckBox enabled = new CheckBox();
    final ExtendedTextBox delay = new ExtendedTextBox();
    final ExtendedTextBox recurrence = new ExtendedTextBox();
    serviceName.getTextBox().setText(service.getName());
    serviceDescription.getTextBox().setText(service.getDescription());
    scriptPath.getTextBox().setText(service.getScriptPath());
    enabled.setValue(service.isEnabled());
    delay.getTextBox().setText(String.valueOf(service.getDelay()));
    recurrence.getTextBox().setText(String.valueOf(service.getRecurrence()));
    final ExtendedTextBox.TextBoxValidator validator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (serviceName.getTextBox().getText().trim().isEmpty()) {
                serviceName.setError("Name can't be empty");
                return false;
            } else if (!serviceName.getTextBox().getText().trim().matches(Utils.SERVICE_NAME_MATCHER)) {
                serviceName.setError("Name can contain only letters, numbers and underscore.");
                return false;
            } else {
                serviceName.setOk();
                // fill script path on service name change
                scriptPath.getTextBox().setValue("./" + serviceName.getTextBox().getText().trim().toLowerCase().replaceAll(Utils.SERVICE_NAME_TO_SCRIP_PATH_MATCHER, "_"));
                return true;
            }
        }
    };
    serviceName.setValidator(validator);
    enabled.setText("Enabled / Disabled");
    final ExtendedTextBox.TextBoxValidator delayValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (!JsonUtils.checkParseInt(delay.getTextBox().getText().trim())) {
                delay.setError("Delay must be a number (time in minutes) !");
                return false;
            } else {
                delay.setOk();
                return true;
            }
        }
    };
    delay.setValidator(delayValidator);
    final ExtendedTextBox.TextBoxValidator recurrenceValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (!JsonUtils.checkParseInt(delay.getTextBox().getText().trim())) {
                recurrence.setError("Recurrence must be a number!");
                return false;
            } else {
                recurrence.setOk();
                return true;
            }
        }
    };
    recurrence.setValidator(recurrenceValidator);
    final ExtendedTextBox.TextBoxValidator scriptValidator = new ExtendedTextBox.TextBoxValidator() {

        @Override
        public boolean validateTextBox() {
            if (scriptPath.getTextBox().getText().trim().isEmpty()) {
                scriptPath.setError("Script path can't be empty !");
                return false;
            } else {
                scriptPath.setOk();
                return true;
            }
        }
    };
    scriptPath.setValidator(scriptValidator);
    // 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 saveButton = TabMenu.getPredefinedButton(ButtonType.SAVE, buttonTranslation.saveServiceDetails());
    saveButton.addClickHandler(new ClickHandler() {

        public void onClick(ClickEvent event) {
            if (validator.validateTextBox() && delayValidator.validateTextBox() && scriptValidator.validateTextBox() && recurrenceValidator.validateTextBox()) {
                Service serv = JsonUtils.clone(service).cast();
                serv.setName(serviceName.getTextBox().getText().trim());
                String desc = serviceDescription.getTextBox().getText().trim();
                if (desc.isEmpty())
                    desc = null;
                serv.setDescription(desc);
                serv.setDelay(Integer.parseInt(delay.getTextBox().getText().trim()));
                serv.setRecurrence(Integer.parseInt(recurrence.getTextBox().getText().trim()));
                serv.setEnabled(enabled.getValue());
                serv.setScriptPath(scriptPath.getTextBox().getText().trim());
                UpdateService request = new UpdateService(JsonCallbackEvents.closeTabDisableButtonEvents(saveButton, tab, true, events));
                request.updateService(serv);
            }
        }
    });
    // cancel button
    final CustomButton cancelButton = TabMenu.getPredefinedButton(ButtonType.CANCEL, "");
    cancelButton.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent clickEvent) {
            session.getTabManager().closeTab(tab, isRefreshParentOnClose());
        }
    });
    // Add some standard form options
    // fill form
    layout.setHTML(0, 0, "Name:");
    layout.setHTML(1, 0, "Description:");
    layout.setHTML(2, 0, "Status:");
    layout.setHTML(3, 0, "Delay:");
    layout.setHTML(4, 0, "Recurrence:");
    layout.setHTML(5, 0, "Script path:");
    layout.setWidget(0, 1, serviceName);
    layout.setWidget(1, 1, serviceDescription);
    layout.setWidget(2, 1, enabled);
    layout.setWidget(3, 1, delay);
    layout.setWidget(4, 1, recurrence);
    layout.setWidget(5, 1, scriptPath);
    for (int i = 0; i < layout.getRowCount(); i++) {
        cellFormatter.addStyleName(i, 0, "itemName");
    }
    menu.addWidget(saveButton);
    menu.addWidget(cancelButton);
    vp.add(layout);
    vp.add(menu);
    vp.setCellHorizontalAlignment(menu, HasHorizontalAlignment.ALIGN_RIGHT);
    this.contentWidget.setWidget(vp);
    return getWidget();
}
Also used : FlexCellFormatter(com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter) ClickEvent(com.google.gwt.event.dom.client.ClickEvent) Label(com.google.gwt.user.client.ui.Label) FlexTable(com.google.gwt.user.client.ui.FlexTable) Service(cz.metacentrum.perun.webgui.model.Service) UpdateService(cz.metacentrum.perun.webgui.json.servicesManager.UpdateService) ExtendedTextBox(cz.metacentrum.perun.webgui.widgets.ExtendedTextBox) 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) UpdateService(cz.metacentrum.perun.webgui.json.servicesManager.UpdateService) CheckBox(com.google.gwt.user.client.ui.CheckBox) CustomButton(cz.metacentrum.perun.webgui.widgets.CustomButton)

Aggregations

ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 CheckBox (com.google.gwt.user.client.ui.CheckBox)1 FlexTable (com.google.gwt.user.client.ui.FlexTable)1 FlexCellFormatter (com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter)1 Label (com.google.gwt.user.client.ui.Label)1 VerticalPanel (com.google.gwt.user.client.ui.VerticalPanel)1 UpdateService (cz.metacentrum.perun.webgui.json.servicesManager.UpdateService)1 Service (cz.metacentrum.perun.webgui.model.Service)1 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)1 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)1 ExtendedTextBox (cz.metacentrum.perun.webgui.widgets.ExtendedTextBox)1 TabMenu (cz.metacentrum.perun.webgui.widgets.TabMenu)1