jp.xxxxxxxx.l3fish.twnyaan.MainApp.java Source code

Java tutorial

Introduction

Here is the source code for jp.xxxxxxxx.l3fish.twnyaan.MainApp.java

Source

/*
 * The MIT License
 *
 * Copyright 2016 sashimi.
 *
 * 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 jp.xxxxxxxx.l3fish.twnyaan;

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import jp.xxxxxxxx.l3fish.twnyaan.config.TwitterAPIKey;
import jp.xxxxxxxx.l3fish.twnyaan.controller.ContainerController;
import jp.xxxxxxxx.l3fish.twnyaan.repository.AccessTokenRepository;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import static javafx.application.Application.launch;

/**
 * JavaFX?????
 *
 * @author sashimi
 */
public class MainApp extends Application {

    private static MainApp INSTANCE;
    private Stage primaryStage;
    private ContainerController container;
    @Inject
    private AccessTokenRepository repository;

    public static MainApp getInstance() {
        return INSTANCE;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        INSTANCE = this;
        this.primaryStage = primaryStage;

        primaryStage.setTitle("TWNyaan");
        primaryStage.setOnCloseRequest((e) -> finishApplication());

        // ?
        container = new ContainerController();
        primaryStage.setScene(new Scene(container));
        changeContent(container);

        primaryStage.show();
    }

    @Override
    public void init() throws Exception {
        // DI
        Injector injector = Guice.createInjector();
        injector.injectMembers(this);

        // Twitter?ConsumerKey/ConsumerSecret?
        Twitter twitter = TwitterFactory.getSingleton();
        twitter.setOAuthConsumer(TwitterAPIKey.getConsumerKey(), TwitterAPIKey.getConsumerSecret());
        AccessToken accessToken = repository.load();
        if (accessToken != null) {
            twitter.setOAuthAccessToken(accessToken);
        }
    }

    /**
     * ?????
     *
     * @param controller ????
     */
    public void changeContent(Parent controller) {
        container.setContent(controller);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    /**
     * ????stage?????????????null??
     */
    private void finishApplication() {
        primaryStage = null;
        Platform.exit();
    }
}