-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUiController.java
More file actions
62 lines (51 loc) · 1.66 KB
/
UiController.java
File metadata and controls
62 lines (51 loc) · 1.66 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
package observer;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.UUID;
/**
* Created by Beka on 27.04.17.
*/
public class UiController extends Application implements Initializable {
@FXML TextArea textArea;
private final Collection collection = new Collection();
private Observer observer = () -> {
textArea.clear();
for (String name : collection.getNames()) {
textArea.setText(String.format("%s\n%s", textArea.getText(), name));
}
};
@Override
public void initialize(URL location, ResourceBundle resources) {
collection.attach(observer);
}
public void onAdd(ActionEvent actionEvent) {
collection.add(UUID.randomUUID().toString());
}
public void onRemove(ActionEvent actionEvent) {
collection.remove(collection.getNames().get((int) (Math.random() * (collection.size() - 1))));
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/observer.fxml"));
primaryStage.setTitle("Observer pattern");
primaryStage.setScene(new Scene(root, 400, 375));
primaryStage.show();
}
@Override
public void stop() throws Exception {
collection.detach(observer);
super.stop();
}
}