<commit_before>package cz.metacentrum.perun.webgui.tabs.userstabs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import cz.metacentrum.perun.webgui.client.PerunWebSession;
import cz.metacentrum.perun.webgui.client.localization.ButtonTranslation;
import cz.metacentrum.perun.webgui.client.resources.ButtonType;
import cz.metacentrum.perun.webgui.client.resources.SmallIcons;
import cz.metacentrum.perun.webgui.client.resources.Utils;
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
import cz.metacentrum.perun.webgui.json.JsonUtils;
import cz.metacentrum.perun.webgui.json.usersManager.UpdateNameTitles;
import cz.metacentrum.perun.webgui.json.usersManager.UpdateUser;
import cz.metacentrum.perun.webgui.model.User;
import cz.metacentrum.perun.webgui.tabs.TabItem;
import cz.metacentrum.perun.webgui.widgets.CustomButton;
import cz.metacentrum.perun.webgui.widgets.ExtendedTextBox;
import cz.metacentrum.perun.webgui.widgets.TabMenu;

/**
 * !! USE ONLY AS INNER TAB !!!
 * Edit user details tab
 *
 * @author <NAME> <<EMAIL>>
 */
public class EditUserDetailsTabItem implements TabItem {

	/**
	 * Perun web session
	 */
	private PerunWebSession session = PerunWebSession.getInstance();

	/**
	 * Content widget - should be simple panel
	 */
	private SimplePanel contentWidget = new SimplePanel();

	/**
	 * Title widget
	 */
	private Label titleWidget = new Label("Edit: ");

	/**
	 * Data
	 */
	private User user;
	private ButtonTranslation buttonTranslation = ButtonTranslation.INSTANCE;
	private JsonCallbackEvents events;

	/**
	 * Creates a tab instance
	 *
	 * @param u
	 * @param event
	 */
	public EditUserDetailsTabItem(User u, JsonCallbackEvents event){
		this.user = u;
		this.events = event;
	}

	public boolean isPrepared(){
		return (user != null);
	}

	public Widget draw() {

		titleWidget = new Label("Edit user");

		VerticalPanel vp = new VerticalPanel();

		final TextBox beforeName = new TextBox();
		final TextBox afterName = new TextBox();
		final TextBox firstName = new TextBox();
		final TextBox middleName = new TextBox();
		final ExtendedTextBox lastName = new ExtendedTextBox();

		final ExtendedTextBox.TextBoxValidator validator = new ExtendedTextBox.TextBoxValidator() {
			@Override
			public boolean validateTextBox() {

				if (lastName.getTextBox().getText().trim().isEmpty()) {
					lastName.setError("Last name can't be empty");
					return false;
				} else {
					lastName.setOk();
					return true;
				}

			}
		};
		lastName.setValidator(validator);

		// prepares layout
		FlexTable layout = new FlexTable();
		layout.setStyleName("inputFormFlexTable");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// set values from user
		beforeName.setText(user.getTitleBefore());
		afterName.setText(user.getTitleAfter());
		firstName.setText(user.getFirstName());
		lastName.getTextBox().setText(user.getLastName());
		middleName.setText(user.getMiddleName());

		// service users can have only first and last name (first is fixed as "(Service)"
		if (user.isServiceUser()) {
			beforeName.setEnabled(false);
			afterName.setEnabled(false);
			firstName.setEnabled(false);
			middleName.setEnabled(false);
		}

		// close tab events
		final TabItem tab = this;

		TabMenu menu = new TabMenu();

		// send button
		final CustomButton saveButton = TabMenu.getPredefinedButton(ButtonType.SAVE, buttonTranslation.saveResourceDetails());

		if (session.isPerunAdmin()) {

			saveButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					if (validator.validateTextBox()) {
						User u = JsonUtils.clone(user).cast();
						u.setFirstName(firstName.getText().trim());
						u.setMiddleName(middleName.getText().trim());
						u.setLastName(lastName.getTextBox().getText().trim());
						u.setTitleBefore(beforeName.getText().trim());
						u.setTitleAfter(afterName.getText().trim());
						UpdateUser request = new UpdateUser(JsonCallbackEvents.closeTabDisableButtonEvents(saveButton, tab, events));
						request.updateUser(u);
					}
				}
			});

		} else {

			saveButton.addClickHandler(new ClickHandler() {<commit_msg>GUI: Allow to set empty last name to user<commit_after>package cz.metacentrum.perun.webgui.tabs.userstabs;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import cz.metacentrum.perun.webgui.client.PerunWebSession;
import cz.metacentrum.perun.webgui.client.localization.ButtonTranslation;
import cz.metacentrum.perun.webgui.client.resources.ButtonType;
import cz.metacentrum.perun.webgui.client.resources.SmallIcons;
import cz.metacentrum.perun.webgui.json.JsonCallbackEvents;
import cz.metacentrum.perun.webgui.json.JsonUtils;
import cz.metacentrum.perun.webgui.json.usersManager.UpdateNameTitles;
import cz.metacentrum.perun.webgui.json.usersManager.UpdateUser;
import cz.metacentrum.perun.webgui.model.User;
import cz.metacentrum.perun.webgui.tabs.TabItem;
import cz.metacentrum.perun.webgui.widgets.CustomButton;
import cz.metacentrum.perun.webgui.widgets.TabMenu;

/**
 * !! USE ONLY AS INNER TAB !!!
 * Edit user details tab
 *
 * @author <NAME> <<EMAIL>>
 */
public class EditUserDetailsTabItem implements TabItem {

	/**
	 * Perun web session
	 */
	private PerunWebSession session = PerunWebSession.getInstance();

	/**
	 * Content widget - should be simple panel
	 */
	private SimplePanel contentWidget = new SimplePanel();

	/**
	 * Title widget
	 */
	private Label titleWidget = new Label("Edit: ");

	/**
	 * Data
	 */
	private User user;
	private ButtonTranslation buttonTranslation = ButtonTranslation.INSTANCE;
	private JsonCallbackEvents events;

	/**
	 * Creates a tab instance
	 *
	 * @param u
	 * @param event
	 */
	public EditUserDetailsTabItem(User u, JsonCallbackEvents event){
		this.user = u;
		this.events = event;
	}

	public boolean isPrepared(){
		return (user != null);
	}

	public Widget draw() {

		titleWidget = new Label("Edit user");

		VerticalPanel vp = new VerticalPanel();

		final TextBox beforeName = new TextBox();
		final TextBox afterName = new TextBox();
		final TextBox firstName = new TextBox();
		final TextBox middleName = new TextBox();
		final TextBox lastName = new TextBox();

		// prepares layout
		FlexTable layout = new FlexTable();
		layout.setStyleName("inputFormFlexTable");
		FlexCellFormatter cellFormatter = layout.getFlexCellFormatter();

		// set values from user
		beforeName.setText(user.getTitleBefore());
		afterName.setText(user.getTitleAfter());
		firstName.setText(user.getFirstName());
		lastName.setText(user.getLastName());
		middleName.setText(user.getMiddleName());

		// service users can have only first and last name (first is fixed as "(Service)"
		if (user.isServiceUser()) {
			beforeName.setEnabled(false);
			afterName.setEnabled(false);
			firstName.setEnabled(false);
			middleName.setEnabled(false);
		}

		// close tab events
		final TabItem tab = this;

		TabMenu menu = new TabMenu();

		// send button
		final CustomButton saveButton = TabMenu.getPredefinedButton(ButtonType.SAVE, buttonTranslation.saveResourceDetails());

		if (session.isPerunAdmin()) {

			saveButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					User u = JsonUtils.clone(user).cast();
					u.setFirstName(firstName.getText().trim());
					u.setMiddleName(middleName.getText().trim());
					u.setLastName(lastName.getText().trim());
					u.setTitleBefore(beforeName.getText().trim());
					u.setTitleAfter(afterName.getText().trim());
					UpdateUser request = new UpdateUser(JsonCallbackEvents.closeTabDisableButtonEvents(saveButton, tab, events));
					request.updateUser(u);
				}
			});

		} else {

			saveButton.addClickHandler(new ClickHandler() {
{'fraction_non_alphanumeric': 0.12116356464932825, 'fraction_numerical': 0.0, 'mean_word_length': 4.519727891156463, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '835923', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2424, 'n_words': 499}