pl.mcpg.nbtjeditor.Start.java Source code

Java tutorial

Introduction

Here is the source code for pl.mcpg.nbtjeditor.Start.java

Source

/*
 * Copyright (c) 2017 Pawe Cholewa
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 */

package pl.mcpg.nbtjeditor;

import java.io.File;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import static pl.mcpg.nbtjeditor.gui.Dialogs.displayError;
import pl.mcpg.nbtjeditor.gui.MainController;

public class Start extends Application {
    public static final String APP_TITLE/*prefix*/ = "NBT JEditor";
    private static File fileToOpen;

    @Override
    public void start(Stage stage) {
        Parent parent = null;
        FXMLLoader loader = new FXMLLoader();
        try {
            parent = loader.load(getClass().getResourceAsStream("/main.fxml"));
        } catch (Exception e) {
            displayError("Couldn't load main FXML file! Application will\nnow close.", e);
            e.printStackTrace();
            System.exit(1);
        }
        stage.setMinWidth(150);
        stage.setMinHeight(150);
        stage.setTitle(APP_TITLE);
        stage.setScene(new Scene(parent, 640, 480));
        ((MainController) loader.getController()).setStage(stage);
        stage.show();
        if (fileToOpen != null && fileToOpen.exists()) {
            ((MainController) loader.getController()).open(null);
        }
    }

    public static void main(String[] args) {
        // if there are any arguments, parse them
        if (args.length > 0) {
            Options options = new Options();

            Option help = new Option("h", "help", false, "Displays this help message");

            Option openfile = new Option("f", "file", true, "Opens requested file at startup");
            openfile.setArgName("filename");
            openfile.setType(String.class);
            options.addOption(openfile);

            options.addOption(help);
            options.addOption(openfile);

            CommandLineParser parser = new DefaultParser();
            CommandLine commandLine = null;
            try {
                commandLine = parser.parse(options, args);
            } catch (ParseException e) {
                displayError("Couldn't parse command line arguments!", e);
            }
            if (commandLine != null) {
                // display help message
                if (commandLine.hasOption("h")) {
                    HelpFormatter helpFormatter = new HelpFormatter();
                    helpFormatter.printHelp("nbt-jeditor", "\nCommand line arguments for NBT JEditor", options,
                            "\nIssues? Report them at GitHub: https://github.com/Mcpg/nbt-jeditor/issues", true);
                    System.exit(0);
                }
                if (commandLine.hasOption("f")) {
                    // find the file and if it doesn't exist display message and set it to null
                    String value = commandLine.getOptionValue("f");
                    if (value != null && !value.isEmpty()) {
                        fileToOpen = new File(value);
                        if (!fileToOpen.exists()) {
                            displayError("Requested file '" + value + "' doesn't exist.");
                            fileToOpen = null;
                        }
                    }
                }
            }
        }
        launch(args);
    }

}