<commit_before>/*******************************************************************************
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package pl.kotcrab.vis.ui.widget;

import pl.kotcrab.vis.ui.VisUI;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

public class PopupMenu extends Table {
	private PopupMenuStyle style;
	private boolean autoRemove;

	private InputListener autoRemoveListener = new InputListener() {
		@Override
		public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
			if (autoRemove) {
				remove();
				return true;
			}

			return false;
		}
	};

	public PopupMenu () {
		this(false, "default");
	}

	public PopupMenu (boolean autoRemove) {
		this(autoRemove, "default");
	}

	public PopupMenu (String styleName) {
		this(false, styleName);
	}

	public PopupMenu (boolean autoRemove, String styleName) {
		this.autoRemove = autoRemove;
		style = VisUI.skin.get(styleName, PopupMenuStyle.class);
	}

	public void addItem (MenuItem item) {
		add(item).fillX().row();
		pack();
	}

	@Override
	public void draw (Batch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
		if (style.border != null) style.border.draw(batch, getX(), getY(), getWidth(), getHeight());
	}

	public void displayMenu (Stage stage, float x, float y) {
		setPosition(x, y - getHeight());
		if (stage.getHeight() - getY() > stage.getHeight()) setY(getY() + getHeight());
		stage.addActor(this);
	}

	public boolean isAutoRemove () {
		return autoRemove;
	}

	public void setAutoRemove (boolean autoRemove) {
		this.autoRemove = autoRemove;
	}

	@Override
	protected void setStage (Stage stage) {
		super.setStage(stage);
		if (getStage() != null) getStage().addListener(autoRemoveListener);
	}

	@Override
	public boolean remove () {
		if (getStage() != null) getStage().removeListener(autoRemoveListener);
		return super.remove();
	}

	static public class PopupMenuStyle {
		public Drawable border;
<commit_msg>Fixed autoremove in poupmenu<commit_after>/*******************************************************************************
 * Copyright 2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package pl.kotcrab.vis.ui.widget;

import pl.kotcrab.vis.ui.VisUI;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;

public class PopupMenu extends Table {
	private PopupMenuStyle style;
	
	private Rectangle boundingRectangle;
	private boolean autoRemove;

	private InputListener autoRemoveListener = new InputListener() {
		@Override
		public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
			if (contains(x, y) == false && autoRemove) {
				remove();
				return true;
			}

			return false;
		}
	};

	public PopupMenu () {
		this(false, "default");
	}

	public PopupMenu (boolean autoRemove) {
		this(autoRemove, "default");
	}

	public PopupMenu (String styleName) {
		this(false, styleName);
	}

	public PopupMenu (boolean autoRemove, String styleName) {
		this.autoRemove = autoRemove;
		style = VisUI.skin.get(styleName, PopupMenuStyle.class);
	}

	public void addItem (MenuItem item) {
		add(item).fillX().row();
		pack();
	}

	@Override
	public void draw (Batch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
		if (style.border != null) style.border.draw(batch, getX(), getY(), getWidth(), getHeight());
	}

	public void displayMenu (Stage stage, float x, float y) {
		setPosition(x, y - getHeight());
		if (stage.getHeight() - getY() > stage.getHeight()) setY(getY() + getHeight());
		stage.addActor(this);
	}

	public boolean isAutoRemove () {
		return autoRemove;
	}

	public void setAutoRemove (boolean autoRemove) {
		this.autoRemove = autoRemove;
	}

	private boolean contains (float x, float y) {
		return boundingRectangle.contains(x, y);
	}
	
	@Override
	protected void setStage (Stage stage) {
		super.setStage(stage);
		if (getStage() != null) getStage().addListener(autoRemoveListener);
	}

	@Override
	public boolean remove () {
		if (getStage() != null) getStage().removeListener(autoRemoveListener);
		return super.remove();
	}

	@Override
	public void validate () {
		super.validate();
		boundingRectangle = new Rectangle(getX(), getY(), getWidth(), getHeight());
	}

	static public class PopupMenuStyle {
		public Drawable border;

{'fraction_non_alphanumeric': 0.16169858451290592, 'fraction_numerical': 0.00466278101582015, 'mean_word_length': 4.634146341463414, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6262723', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1778, 'n_words': 524}