-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheditUIController.java
More file actions
190 lines (182 loc) · 7.14 KB
/
editUIController.java
File metadata and controls
190 lines (182 loc) · 7.14 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package RouteMapMaker;
import java.net.URL;
import java.util.ArrayList;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
public class editUIController implements Initializable{
private Line line;
private Train train;
private Stage stage;
private ObservableList<String> olB = FXCollections.observableArrayList();
private ObservableList<String> olC = FXCollections.observableArrayList();
@FXML ToggleButton Insert;
@FXML ToggleButton Delete;
@FXML ToggleButton InsertAll;
@FXML ToggleButton DeleteAll;
@FXML Button Close;
@FXML ListView listB;
@FXML ListView listC;
@FXML Label infoLabel;
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
infoLabel.setText("停車駅は駅一覧の中から\n"
+ "上から順に追加してください。\n\n"
+ "挿入:挿入位置を右枠で選択し\n"
+ "挿入ボタンを押してから左枠\n"
+ "から挿入する駅を選択する。\n"
+ "hint:反応しない場合は\n"
+ "別の駅を選択してみてください\n\n"
+ "削除:削除ボタンを押してから\n"
+ "右枠で削除する駅を選択する。");
listB.setItems(olB);
listC.setItems(olC);
ToggleGroup group = new ToggleGroup();
Insert.setToggleGroup(group);
Delete.setToggleGroup(group);
InsertAll.setToggleGroup(group);
DeleteAll.setToggleGroup(group);
group.selectedToggleProperty().addListener((ObservableValue<? extends Toggle> ov, Toggle old_toggle,
Toggle new_toggle) ->{
if(group.getSelectedToggle() == InsertAll){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("選択路線駅全追加の確認");
alert.setContentText("全ての駅を停車駅として追加してよろしいですか?");
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.OK){
//全削除してから再度追加だと既存駅の属性が失われるので足りない分を追加する。
int count = 0;
for(int i = 0; i < line.getStations().size(); i++){
if(train.getStops().size()==count||!train.getStops().get(count).getSta().getName().equals(line.getStations().get(i).getName())){
//駅が存在しない
train.getStops().add(count, new TrainStop(line.getStations().get(i)));
}
count++;
}
olC.clear();
for(int i = 0; i < train.getStops().size(); i++){
olC.add(train.getStops().get(i).getSta().getName());
}
olC.add("<最後に追加>");
listC.getSelectionModel().select(0);
}
InsertAll.setSelected(false);
}
if(group.getSelectedToggle() == DeleteAll){
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("選択駅全消去の確認");
alert.setContentText("全ての停車駅を削除してよろしいですか?");
Optional<ButtonType> result = alert.showAndWait();
if(result.get() == ButtonType.OK){
//全消去処理
train.getStops().clear();
olC.clear();
olC.add("<最後に追加>");
listC.getSelectionModel().select(train.getStops().size());
}
DeleteAll.setSelected(false);
}
});
listB.setOnMouseClicked((MouseEvent) ->{
if(group.getSelectedToggle() == Insert){
int indexB = listB.getSelectionModel().getSelectedIndex();
int indexC = listC.getSelectionModel().getSelectedIndex();
if(indexC == -1){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setContentText("停車駅を追加する位置を選んでください。");
alert.showAndWait();
}else if(indexB != -1){
//追加して大丈夫か検査する。
int pre;//前の停車駅の路線でのindex
int next;//後の停車駅の路線でのindex
try{
pre = line.getStations().indexOf(train.getStops().get(indexC - 1).getSta());
}catch(IndexOutOfBoundsException e){
pre = -1;//系統の先頭に追加要求があった場合。
}
try{
next = line.getStations().lastIndexOf(train.getStops().get(indexC).getSta());
}catch(IndexOutOfBoundsException e){
next = line.getStations().size();//系統の最後に追加要求があった場合
}
if(pre < indexB && indexB < next){
boolean adjon = false;//連続して同じ駅が登録されていると都合が悪い。
try{
if(line.getStations().get(indexB) == train.getStops().get(indexC -1).getSta()) adjon = true;
if(line.getStations().get(indexB) == train.getStops().get(indexC).getSta()) adjon = true;
}catch(Exception e){
//indexエラーはここでは無視していいので何もしない
}
if(adjon){
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setContentText("同じ駅を隣接して追加することはできません。");
alert.showAndWait();
}else{//順番検査と隣接検査をクリアしたら追加する。
train.getStops().add(indexC, new TrainStop(line.getStations().get(indexB)));
}
}else{
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setContentText("停車駅は駅一覧の上から順である必要があります。");
alert.showAndWait();
}
olC.clear();
for(int i = 0; i < train.getStops().size(); i++){
olC.add(train.getStops().get(i).getSta().getName());
}
olC.add("<最後に追加>");
listC.getSelectionModel().select(indexC + 1);
}
}
});
listC.setOnMouseClicked((MouseEvent) ->{
//動作に不具合は見られないけどIndexOutOfBoundsExceptionが出てくる
if(group.getSelectedToggle() == Delete){
int indexC = listC.getSelectionModel().getSelectedIndex();
if(indexC != -1 && indexC < train.getStops().size()){
train.getStops().remove(indexC);
olC.clear();
for(int i = 0; i < train.getStops().size(); i++){
olC.add(train.getStops().get(i).getSta().getName());
}
olC.add("<最後に追加>");
listC.getSelectionModel().select(train.getStops().size());
}
}
});
Close.setOnAction((ActionEvent) -> {
stage.close();
});
}
public void setObjects(Line line, Train train, Stage stage){
this.line = line;
this.train = train;
this.stage = stage;
//listBの初期設定
olB.clear();
for(int i = 0; i < line.getStations().size(); i++){
olB.add(line.getStations().get(i).getName());
}
//listCの初期設定
olC.clear();
for(int i = 0; i < train.getStops().size(); i++){
olC.add(train.getStops().get(i).getSta().getName());
}
olC.add("<最後に追加>");
listC.getSelectionModel().select(train.getStops().size());
}
}