1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package login; 33 34 import java.io.InputStream; 35 import java.util.logging.Level; 36 import java.util.logging.Logger; 37 import javafx.application.Application; 38 import javafx.fxml.FXMLLoader; 39 import javafx.fxml.JavaFXBuilderFactory; 40 import javafx.scene.Node; 41 import javafx.scene.Scene; 42 import javafx.scene.layout.AnchorPane; 43 import javafx.stage.Stage; 44 import login.model.User; 45 import login.security.Authenticator; 46 47 /** 48 * Main Application. This class handles navigation and user session. 49 */ 50 public class Main extends Application { 51 52 private Stage stage; 53 private User loggedUser; 54 private final double MINIMUM_WINDOW_WIDTH = 390.0; 55 private final double MINIMUM_WINDOW_HEIGHT = 500.0; 56 57 /** 58 * @param args the command line arguments 59 */ 60 public static void main(String[] args) { 61 Application.launch(Main.class, (java.lang.String[])null); 62 } 63 64 @Override 65 public void start(Stage primaryStage) { 66 try { 67 stage = primaryStage; 68 stage.setMinWidth(MINIMUM_WINDOW_WIDTH); 69 stage.setMinHeight(MINIMUM_WINDOW_HEIGHT); 70 stage.setTitle("Login Sample"); 71 gotoLogin(); 72 primaryStage.show(); 73 } catch (Exception ex) { 74 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 75 } 76 } 77 78 public User getLoggedUser() { 79 return loggedUser; 80 } 81 82 public boolean userLogging(String userId, String password){ 83 System.out.println("got user id " + userId + " password " + password); 84 if (Authenticator.validate(userId, password)) { 85 System.out.println("OK"); 86 loggedUser = User.of(userId); 87 gotoProfile(); 88 return true; 89 } else { 90 System.out.println("NOT OK"); 91 return false; 92 } 93 } 94 95 void userLogout(){ 96 loggedUser = null; 97 gotoLogin(); 98 } 99 100 private void gotoProfile() { 101 try { 102 ProfileController profile = (ProfileController) replaceSceneContent("Profile.fxml"); 103 profile.setApp(this); 104 } catch (Exception ex) { 105 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 106 } 107 } 108 109 private void gotoLogin() { 110 try { 111 LoginController login = (LoginController) replaceSceneContent("Login.fxml"); 112 login.setApp(this); 113 } catch (Exception ex) { 114 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 115 } 116 } 117 118 private Node replaceSceneContent(String fxml) throws Exception { 119 FXMLLoader loader = new FXMLLoader(); 120 InputStream in = Main.class.getResourceAsStream(fxml); 121 loader.setBuilderFactory(new JavaFXBuilderFactory()); 122 loader.setLocation(Main.class.getResource(fxml)); 123 AnchorPane page; 124 try { 125 page = (AnchorPane) loader.load(in); 126 } finally { 127 in.close(); 128 } 129 130 // Store the stage width and height in case the user has resized the window 131 double stageWidth = stage.getWidth(); 132 if (!Double.isNaN(stageWidth)) { 133 stageWidth -= (stage.getWidth() - stage.getScene().getWidth()); 134 } 135 136 double stageHeight = stage.getHeight(); 137 if (!Double.isNaN(stageHeight)) { 138 stageHeight -= (stage.getHeight() - stage.getScene().getHeight()); 139 } 140 141 Scene scene = new Scene(page); 142 if (!Double.isNaN(stageWidth)) { 143 page.setPrefWidth(stageWidth); 144 } 145 if (!Double.isNaN(stageHeight)) { 146 page.setPrefHeight(stageHeight); 147 } 148 149 stage.setScene(scene); 150 stage.sizeToScene(); 151 return (Node) loader.getController(); 152 } 153 }