blob: 538b2b8375c5a20f2b4d8722b067ae00835eaa51 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 demo;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import demo.model.User;
import demo.security.Authenticator;
/**
* Main Application. This class handles navigation and user session.
*/
public class Main extends Application {
private Stage stage;
private User loggedUser;
private final double MINIMUM_WINDOW_WIDTH = 390.0;
private final double MINIMUM_WINDOW_HEIGHT = 500.0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(Main.class, (java.lang.String[])null);
}
@Override
public void start(Stage primaryStage) {
try {
stage = primaryStage;
stage.setTitle("FXML Login Sample");
stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
gotoLogin();
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public User getLoggedUser() {
return loggedUser;
}
public boolean userLogging(String userId, String password){
if (Authenticator.validate(userId, password)) {
loggedUser = User.of(userId);
gotoProfile();
return true;
} else {
return false;
}
}
void userLogout(){
loggedUser = null;
gotoLogin();
}
private void gotoProfile() {
try {
ProfileController profile = (ProfileController) replaceSceneContent("profile.fxml");
profile.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void gotoLogin() {
try {
LoginController login = (LoginController) replaceSceneContent("login.fxml");
login.setApp(this);
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private Initializable replaceSceneContent(String fxml) throws Exception {
FXMLLoader loader = new FXMLLoader();
InputStream in = Main.class.getResourceAsStream(fxml);
loader.setBuilderFactory(new JavaFXBuilderFactory());
loader.setLocation(Main.class.getResource(fxml));
AnchorPane page;
try {
page = (AnchorPane) loader.load(in);
} finally {
in.close();
}
Scene scene = new Scene(page, 800, 600);
stage.setScene(scene);
stage.sizeToScene();
return (Initializable) loader.getController();
}
}