<commit_before>package org.sagebionetworks.web.client.widget.entity;

import org.gwtbootstrap3.client.ui.Icon;
import org.gwtbootstrap3.client.ui.Tooltip;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.html.Span;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PlaceChanger;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.place.Synapse;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.widget.LoadingSpinner;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class EntityBadgeViewImpl extends Composite implements EntityBadgeView {
	SynapseJSNIUtils synapseJSNIUtils;
	Widget modifiedByWidget;
	Presenter presenter;
	public interface Binder extends UiBinder<Widget, EntityBadgeViewImpl> {	}
	
	@UiField
	FocusPanel iconContainer;
	@UiField
	Icon icon;
	@UiField
	FlowPanel entityContainer;
	@UiField
	TextBox idField;
	@UiField
	SimplePanel modifiedByField;
	@UiField
	Label modifiedOnField;
	
	@UiField
	Tooltip annotationsField;
	@UiField
	Label sizeField;
	@UiField
	TextBox md5Field;
	@UiField
	Icon publicIcon;
	@UiField
	Icon privateIcon;
	@UiField
	Icon sharingSetIcon;
	@UiField
	Icon wikiIcon;
	@UiField
	Icon annotationsIcon;
	@UiField
	Tooltip errorField;
	@UiField
	Icon errorIcon;
	@UiField
	Span fileDownloadButtonContainer;
	@UiField
	Icon discussionIcon;
	@UiField
	Tooltip discussion;
	@UiField
	Icon unLinkIcon;
	Callback onAttachCallback;
	Anchor entityAnchor;
	PlaceChanger placeChanger;
	@Inject
	public EntityBadgeViewImpl(final Binder uiBinder,
			final SynapseJSNIUtils synapseJSNIUtils,
			PortalGinInjector ginInjector,
			GlobalApplicationState globalAppState) {
		this.synapseJSNIUtils = synapseJSNIUtils;
		initWidget(uiBinder.createAndBindUi(this));
		this.placeChanger = globalAppState.getPlaceChanger();
		idField.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				idField.selectAll();
			}
		});
		md5Field.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				md5Field.selectAll();
			}
		});
		unLinkIcon.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.onUnlink();
			}
		});
	}

	@Override
	public void setPresenter(Presenter p) {
		this.presenter = p;
	}
	
	@Override
	public void setOnAttachCallback(Callback onAttachCallback) {
		this.onAttachCallback = onAttachCallback;
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		if (onAttachCallback != null) {
			onAttachCallback.invoke();
		}
	}
	
	@Override
	public void setEntity(final EntityHeader entityHeader) {
		clear();
		if(entityHeader == null)  throw new IllegalArgumentException("Entity is required");
		
		if(entityHeader != null) {
			entityAnchor = new Anchor();
			entityAnchor.setText(entityHeader.getName());
			entityAnchor.addStyleName("link");
			entityAnchor.setHref("#!Synapse:" + entityHeader.getId());
			entityAnchor.addClickHandler(event -> {
				event.preventDefault();
				placeChanger.goTo(new Synapse(entityHeader.getId()));
			});
			
			ClickHandler clickHandler = new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					entityAnchor.fireEvent(event);
				}
			};
			iconContainer.setWidget(icon);
			iconContainer.addClickHandler(clickHandler);
			entityContainer.add(entityAnchor);
			idField.setText(entityHeader.getId());
		} 		
	}
	@Override
	public void setIcon(IconType iconType) {
		icon.setType(iconType);
	}
	
	@Override
	public void showLoadError(String principalId) {
		clear();
		entityContainer.add(new HTML(DisplayConstants.ERROR_LOADING));		
	}
	
	public void clear() {
		iconContainer.clear();
		entityContainer.clear();
	}
	
	@Override
	public void addClickHandler(final ClickHandler handler) {
		entityAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				event.preventDefault();
				handler.onClick(event);
			}
		});
	}
	
	@Override
	public void setModifiedByWidget(Widget w) {
		modifiedByField.setWidget(w);
		this.modifiedByWidget = w;
	}
	
	@Override
	public void setModifiedOn(String modifiedOnString) {
		modifiedOnField.setText(modifiedOnString);
	}
	
	@Override
	public void setModifiedByWidgetVisible(boolean visible) {
		modifiedByWidget.setVisible(visible);
	}


	@Override
	public String getFriendlySize(Long contentSize, boolean abbreviatedUnits) {
		return DisplayUtils.getFriendlySize(contentSize, abbreviatedUnits);
	}
	
	@Override
	public void setAnnotations(String html) {<commit_msg>set custom click handler in entity badge (do not add additional click handlers)<commit_after>package org.sagebionetworks.web.client.widget.entity;

import org.gwtbootstrap3.client.ui.Icon;
import org.gwtbootstrap3.client.ui.Tooltip;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.html.Span;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PlaceChanger;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.place.Synapse;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.widget.LoadingSpinner;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class EntityBadgeViewImpl extends Composite implements EntityBadgeView {
	SynapseJSNIUtils synapseJSNIUtils;
	Widget modifiedByWidget;
	Presenter presenter;
	public interface Binder extends UiBinder<Widget, EntityBadgeViewImpl> {	}
	
	@UiField
	FocusPanel iconContainer;
	@UiField
	Icon icon;
	@UiField
	FlowPanel entityContainer;
	@UiField
	TextBox idField;
	@UiField
	SimplePanel modifiedByField;
	@UiField
	Label modifiedOnField;
	
	@UiField
	Tooltip annotationsField;
	@UiField
	Label sizeField;
	@UiField
	TextBox md5Field;
	@UiField
	Icon publicIcon;
	@UiField
	Icon privateIcon;
	@UiField
	Icon sharingSetIcon;
	@UiField
	Icon wikiIcon;
	@UiField
	Icon annotationsIcon;
	@UiField
	Tooltip errorField;
	@UiField
	Icon errorIcon;
	@UiField
	Span fileDownloadButtonContainer;
	@UiField
	Icon discussionIcon;
	@UiField
	Tooltip discussion;
	@UiField
	Icon unLinkIcon;
	Callback onAttachCallback;
	Anchor entityAnchor;
	PlaceChanger placeChanger;
	ClickHandler customClickHandler;
	@Inject
	public EntityBadgeViewImpl(final Binder uiBinder,
			final SynapseJSNIUtils synapseJSNIUtils,
			PortalGinInjector ginInjector,
			GlobalApplicationState globalAppState) {
		this.synapseJSNIUtils = synapseJSNIUtils;
		initWidget(uiBinder.createAndBindUi(this));
		this.placeChanger = globalAppState.getPlaceChanger();
		idField.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				idField.selectAll();
			}
		});
		md5Field.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				md5Field.selectAll();
			}
		});
		unLinkIcon.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.onUnlink();
			}
		});
	}

	@Override
	public void setPresenter(Presenter p) {
		this.presenter = p;
	}
	
	@Override
	public void setOnAttachCallback(Callback onAttachCallback) {
		this.onAttachCallback = onAttachCallback;
	}

	@Override
	protected void onAttach() {
		super.onAttach();
		if (onAttachCallback != null) {
			onAttachCallback.invoke();
		}
	}
	
	@Override
	public void setEntity(final EntityHeader entityHeader) {
		clear();
		if(entityHeader == null)  throw new IllegalArgumentException("Entity is required");
		
		if(entityHeader != null) {
			entityAnchor = new Anchor();
			entityAnchor.setText(entityHeader.getName());
			entityAnchor.addStyleName("link");
			entityAnchor.setHref("#!Synapse:" + entityHeader.getId());
			entityAnchor.addClickHandler(event -> {
				event.preventDefault();
				if (customClickHandler != null) {
					customClickHandler.onClick(event);
				} else {
					placeChanger.goTo(new Synapse(entityHeader.getId()));	
				}
			});
			
			ClickHandler clickHandler = new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					entityAnchor.fireEvent(event);
				}
			};
			iconContainer.setWidget(icon);
			iconContainer.addClickHandler(clickHandler);
			entityContainer.add(entityAnchor);
			idField.setText(entityHeader.getId());
		} 		
	}
	@Override
	public void setIcon(IconType iconType) {
		icon.setType(iconType);
	}
	
	@Override
	public void showLoadError(String principalId) {
		clear();
		entityContainer.add(new HTML(DisplayConstants.ERROR_LOADING));		
	}
	
	public void clear() {
		iconContainer.clear();
		entityContainer.clear();
	}
	
	@Override
	public void addClickHandler(final ClickHandler handler) {
		customClickHandler = handler;
	}
	
	@Override
	public void setModifiedByWidget(Widget w) {
		modifiedByField.setWidget(w);
		this.modifiedByWidget = w;
	}
	
	@Override
	public void setModifiedOn(String modifiedOnString) {
		modifiedOnField.setText(modifiedOnString);
	}
	
	@Override
	public void setModifiedByWidgetVisible(boolean visible) {
		modifiedByWidget.setVisible(visible);
	}


	@Override
	public String getFriendlySize(Long contentSize, boolean abbreviatedUnits) {
		return DisplayUtils.getFriendlySize(contentSize, abbreviatedUnits);
	}
	
	@Override
	public void setAnnotations(String html) {
{'fraction_non_alphanumeric': 0.09992823824901327, 'fraction_numerical': 0.0012558306422676714, 'mean_word_length': 5.166482300884955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '21062', 'n_tokens_mistral': 3931, 'n_tokens_neox': 3623, 'n_words': 650}