-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselectFontController.java
More file actions
87 lines (81 loc) · 2.67 KB
/
selectFontController.java
File metadata and controls
87 lines (81 loc) · 2.67 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
package RouteMapMaker;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class selectFontController implements Initializable{
@FXML Button saveBT;
@FXML Button cancelBT;
@FXML Button defaultFont;
@FXML Label sampleText;
@FXML ListView<String> FontList;
private Stage stage;//このstageを保持する
private int fontIndex;//今どれが選択されているか
private int defaultIndex;//デフォルトのフォントのindexを保持する
private boolean saved;//戻る時に変更を反映するかしないか
private ObservableList<String> fn = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
for(int i = 0; i < Font.getFamilies().size(); i++){//デフォルトフォントを探索
if(Font.getFamilies().get(i).equals("System")) defaultIndex = i;
}
for(int i = 0; i < Font.getFamilies().size(); i++){
fn.add(Font.getFamilies().get(i));
}
FontList.setItems(fn);
FontList.setCellFactory((ListView<String> l) -> new FontFormatCell());
FontList.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) -> {
fontIndex = FontList.getSelectionModel().getSelectedIndex();
sampleText.setFont(Font.font(Font.getFamilies().get(fontIndex)));
});
defaultFont.setOnAction((ActionEvent) ->{
fontIndex = defaultIndex;
FontList.getSelectionModel().select(fontIndex);
sampleText.setFont(Font.font(Font.getFamilies().get(fontIndex)));
});
cancelBT.setOnAction((ActionEvent) ->{
saved = false;
stage.close();
});
saveBT.setOnAction((ActionEvent) ->{
saved = true;
stage.close();
});
}
public void setObject(Stage stage, String currentFont){
this.stage = stage;
if(currentFont == null){//nullのときはSystemを指定します。
fontIndex = defaultIndex;
}else{
boolean found = false;
for(int i = 0; i < Font.getFamilies().size(); i++){
if(Font.getFamilies().get(i).equals(currentFont)){
fontIndex = i;
found = true;
}
}
if(! found){
fontIndex = defaultIndex;
}
}
FontList.getSelectionModel().select(fontIndex);
}
public boolean shouldSave(){
return saved;
}
public String getFontName(){
if(saved){
return Font.getFamilies().get(fontIndex);
}else{
throw new IllegalArgumentException("shouldSaveがfalseです。フォント名は渡せません");
}
}
}