All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kotcrab.vis.ui.widget.file.internal.DirsSuggestionPopup Maven / Gradle / Ivy

/*
 * Copyright 2014-2016 See AUTHORS file.
 *
 * 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 com.kotcrab.vis.ui.widget.file.internal;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.Array;
import com.kotcrab.vis.ui.widget.MenuItem;
import com.kotcrab.vis.ui.widget.VisTextField;
import com.kotcrab.vis.ui.widget.file.FileChooser;

/** @author Kotcrab */
public class DirsSuggestionPopup extends AbstractSuggestionPopup {
	private final VisTextField pathField;

	public DirsSuggestionPopup (FileChooser chooser, VisTextField pathField) {
		super(chooser);
		this.pathField = pathField;
	}

	public void pathFieldKeyTyped (Stage stage, float width) {
		if (pathField.getText().length() == 0) {
			remove();
			return;
		}

		int suggestions = createDirSuggestions(width);
		if (suggestions == 0) {
			remove();
			return;
		}

		showMenu(stage, pathField);
	}

	private int createDirSuggestions (float width) {
		clearChildren();
		int suggestions = 0;

		FileHandle enteredDir = Gdx.files.absolute(pathField.getText());
		String partialPath = "";
		if (enteredDir.exists() == false) {
			partialPath = enteredDir.name();
			enteredDir = enteredDir.parent();
		}

		for (final FileHandle file : enteredDir.list(chooser.getFileFilter())) {
			if (file.exists() == false || file.isDirectory() == false) continue;
			if (file.name().startsWith(partialPath) == false || file.name().equals(partialPath)) continue;

			MenuItem item = createMenuItem(file.path());
			item.getLabel().setEllipsis(true);
			item.getLabelCell().width(width);
			addItem(item);

			item.addListener(new ChangeListener() {
				@Override
				public void changed (ChangeEvent event, Actor actor) {
					chooser.setDirectory(file, FileChooser.HistoryPolicy.ADD);
				}
			});

			suggestions++;
			if (suggestions == MAX_SUGGESTIONS) {
				break;
			}
		}

		return suggestions;
	}

	public void showRecentDirectories (Stage stage, Array recentDirectories, float width) {
		int suggestions = createRecentDirSuggestions(recentDirectories, width);
		if (suggestions == 0) {
			remove();
			return;
		}

		showMenu(stage, pathField);
	}

	private int createRecentDirSuggestions (Array files, float width) {
		clearChildren();
		int suggestions = 0;
		for (final FileHandle file : files) {
			if (file.exists() == false) continue;

			MenuItem item = createMenuItem(file.path());
			item.getLabel().setEllipsis(true);
			item.getLabelCell().width(width);
			addItem(item);

			item.addListener(new ChangeListener() {
				@Override
				public void changed (ChangeEvent event, Actor actor) {
					chooser.setDirectory(file, FileChooser.HistoryPolicy.ADD);
				}
			});

			suggestions++;
			if (suggestions == MAX_SUGGESTIONS) {
				break;
			}
		}

		return suggestions;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy