Search in sources :

Example 16 with FileChooser

use of com.kotcrab.vis.ui.widget.file.FileChooser in project Eidolons by IDemiurge.

the class FileChooserX method initFileChooser.

private static void initFileChooser(Stage stage, String folder, String format) {
    // FileChooser.setFavoritesPrefsName("com.your.package.here.filechooser");
    FileChooser fileChooser = map.get(stage);
    if (!folder.contains(PathFinder.getRootPath())) {
        folder = PathFinder.getRootPath() + folder;
    }
    Gdx.input.setInputProcessor(stage);
    if (fileChooser == null) {
        fileChooser = new FileChooser(FileChooser.Mode.OPEN);
        if (stage instanceof GenericGuiStage) {
            ((GenericGuiStage) stage).setFileChooser(fileChooser);
        }
        stage.addActor(fileChooser.fadeIn());
        map.put(stage, fileChooser);
        if (format != null)
            fileChooser.setFileFilter(new OrFileFilter(DirectoryFileFilter.DIRECTORY, new SuffixFileFilter("." + format)));
    } else {
        stage.addActor(fileChooser.fadeIn());
    }
    fileChooser.setDirectory(folder);
    fileChooser.setSize(GdxMaster.getWidth() * 3 / 4, GdxMaster.getHeight() * 3 / 5);
    GdxMaster.center(fileChooser);
    fileChooser.setSelectionMode(FileChooser.SelectionMode.FILES);
    FileChooser finalFileChooser = fileChooser;
    FileChooserAdapter adapter;
    fileChooser.clearListeners();
    fileChooser.setListener(adapter = new FileChooserAdapter() {

        @Override
        public void canceled() {
            super.canceled();
            WaitMaster.receiveInput(waitOperation, null);
            finalFileChooser.fadeOut();
        }

        @Override
        public void selected(Array<FileHandle> file) {
            if (file.size == 0)
                return;
            WaitMaster.receiveInput(waitOperation, file.get(0).file().getAbsolutePath());
            finalFileChooser.fadeOut();
        }
    });
    FileChooser finalFileChooser1 = fileChooser;
    fileChooser.addListener(new InputListener() {

        @Override
        public boolean keyDown(InputEvent event, int keycode) {
            if (keycode == Input.Keys.ENTER) {
                try {
                    Method selectionFinished = FileChooser.class.getDeclaredMethod("selectionFinished");
                    selectionFinished.setAccessible(true);
                    try {
                        selectionFinished.invoke(finalFileChooser1);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            if (keycode == Input.Keys.ESCAPE) {
                finalFileChooser1.fadeOut();
                adapter.canceled();
            }
            return true;
        }
    });
}
Also used : OrFileFilter(org.apache.commons.io.filefilter.OrFileFilter) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Array(com.badlogic.gdx.utils.Array) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) GenericGuiStage(libgdx.stage.GenericGuiStage) FileChooser(com.kotcrab.vis.ui.widget.file.FileChooser) FileChooserAdapter(com.kotcrab.vis.ui.widget.file.FileChooserAdapter) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent)

Aggregations

FileChooser (com.kotcrab.vis.ui.widget.file.FileChooser)16 FileHandle (com.badlogic.gdx.files.FileHandle)14 FileChooserAdapter (com.kotcrab.vis.ui.widget.file.FileChooserAdapter)9 IOException (java.io.IOException)6 FileChooserListener (com.kotcrab.vis.ui.widget.file.FileChooserListener)5 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)3 Array (com.badlogic.gdx.utils.Array)3 ClickListener (com.badlogic.gdx.scenes.scene2d.utils.ClickListener)2 FileTypeFilter (com.kotcrab.vis.ui.widget.file.FileTypeFilter)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)1 JsonReader (com.badlogic.gdx.utils.JsonReader)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 CustomList (com.bladecoder.engineeditor.ui.panels.CustomList)1 SingleFileChooserListener (com.kotcrab.vis.ui.widget.file.SingleFileChooserListener)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1