-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUI.java
More file actions
101 lines (81 loc) · 2.99 KB
/
GUI.java
File metadata and controls
101 lines (81 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.FileInputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
/**
* Initializes and sets up the GUI, passes the controller to the parent Messenger class
*/
public class GUI extends Application{
/**
* stores controller (only accessible when start method runs)
*/
private static Controller controller;
/**
* stores Messenger id
*/
private static String id;
/**
* Necessary method that runs when GUI is initialized
* @param stage passed by JavaFX
*/
@Override
public void start(Stage stage){
FileInputStream fxmlStream = null;
try{
fxmlStream = new FileInputStream("messenger.fxml"); //load layout file
}catch(Exception e){
System.out.println("FXML file error");
}
FXMLLoader loader = new FXMLLoader();
AnchorPane root = null;
try{
root = (AnchorPane) loader.load(fxmlStream);
}catch(IOException e){
System.out.println("FXML loader error");
}
controller = (Controller) loader.getController(); //access and set instance controller
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle(id); //id from Messenger
stage.show(); //show GUI
}
/**
* Gracefully handle exit other than by command, called by JavaFX on application close
*/
@Override
public void stop(){
System.exit(0); //stop all threads including connection listener and RMI
}
/**
* For use by Messenger class, shares objects between messenger/controller
* @param mes owner, to be passed along to controller
* @param localID value for title bar
* @return object passed to owner to allow direct access to controller
*/
public static Controller getInstance(Messenger mes, String localID){ //starts the GUI and returns the controller
id = localID; //set instance variable
new Thread("gui"){ //create a thread as run() only ends when GUI quits
public void run(){
Application.launch(GUI.class); //start initialization process
}
}.start();
try{
Boolean init = false;
while(!init){ //while the GUI is not loaded
try{ //not very elegant but under a time crunch so good for now
controller.addText(""); //check if we get an error trying to add text
init = true;
}catch(NullPointerException e){
Thread.sleep(100); //wait a bit to avoid busywait
}
}
controller.setMessenger(mes); //pass Messenger class to controller
}catch(Exception e){
//we must catch this to avoid errors, but should deal with it later
}
return controller; //send to Messenger class
}
}