forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathUiPart.java
87 lines (75 loc) · 2.56 KB
/
UiPart.java
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
package seedu.address.ui;
import java.io.IOException;
import java.net.URL;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import seedu.address.MainApp;
import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.events.BaseEvent;
/**
* Represents a distinct part of the UI. e.g. Windows, dialogs, panels, status bars, etc.
* It contains a scene graph with a root node of type {@code T}.
*/
public abstract class UiPart<T> {
/** Resource folder where FXML files are stored. */
public static final String FXML_FILE_FOLDER = "/view/";
private FXMLLoader fxmlLoader;
/**
* Constructs a UiPart with the specified FXML file URL.
* The FXML file must not specify the {@code fx:controller} attribute.
*/
public UiPart(URL fxmlFileUrl) {
assert fxmlFileUrl != null;
fxmlLoader = new FXMLLoader(fxmlFileUrl);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
throw new AssertionError(e);
}
}
/**
* Constructs a UiPart using the specified FXML file within {@link #FXML_FILE_FOLDER}.
* @see #UiPart(URL)
*/
public UiPart(String fxmlFileName) {
this(fxmlFileName != null ? MainApp.class.getResource(FXML_FILE_FOLDER + fxmlFileName) : null);
}
/**
* Returns the root object of the scene graph of this UiPart.
*/
public T getRoot() {
return fxmlLoader.getRoot();
}
/**
* Raises the event via {@link EventsCenter#post(BaseEvent)}
* @param event
*/
protected void raise(BaseEvent event) {
EventsCenter.getInstance().post(event);
}
/**
* Registers the object as an event handler at the {@link EventsCenter}
* @param handler usually {@code this}
*/
protected void registerAsAnEventHandler(Object handler) {
EventsCenter.getInstance().registerHandler(handler);
}
/**
* Creates a modal dialog.
* @param title Title of the dialog.
* @param parentStage The owner stage of the dialog.
* @param scene The scene that will contain the dialog.
* @return the created dialog, not yet made visible.
*/
protected Stage createDialogStage(String title, Stage parentStage, Scene scene) {
Stage dialogStage = new Stage();
dialogStage.setTitle(title);
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(parentStage);
dialogStage.setScene(scene);
return dialogStage;
}
}