Skip to content

Commit 19b7f1f

Browse files
committed
Refactoring: Componentize the UI that was created in programming method
1 parent 2a69d4c commit 19b7f1f

File tree

5 files changed

+252
-156
lines changed

5 files changed

+252
-156
lines changed

src/main/java/root/javafx/Controller/SettingMenuController.java

Lines changed: 26 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,17 @@
2020
import javafx.event.ActionEvent;
2121
import javafx.fxml.FXML;
2222
import javafx.fxml.Initializable;
23-
import javafx.geometry.Pos;
2423
import javafx.scene.Node;
25-
import javafx.scene.control.Alert;
2624
import javafx.scene.control.Alert.AlertType;
2725
import javafx.scene.control.ButtonBar.ButtonData;
2826
import javafx.scene.control.ButtonType;
29-
import javafx.scene.control.Label;
3027
import javafx.scene.control.Separator;
3128
import javafx.scene.control.SplitPane;
3229
import javafx.scene.control.TextField;
3330
import javafx.scene.control.TextInputDialog;
3431
import javafx.scene.image.Image;
3532
import javafx.scene.layout.AnchorPane;
36-
import javafx.scene.layout.FlowPane;
37-
import javafx.scene.layout.HBox;
3833
import javafx.scene.layout.VBox;
39-
import javafx.scene.paint.Paint;
40-
import javafx.scene.text.TextAlignment;
4134
import javafx.stage.FileChooser;
4235
import javafx.stage.FileChooser.ExtensionFilter;
4336
import javafx.stage.Stage;
@@ -52,6 +45,7 @@
5245
import root.core.service.implement.FilePropertyService;
5346
import root.javafx.CustomView.ConnectionInfoVBox;
5447
import root.javafx.CustomView.DBConnInfoControl;
48+
import root.javafx.CustomView.MonitoringYNVBox;
5549
import root.javafx.CustomView.ServerConnInfoControl;
5650
import root.javafx.CustomView.dialogUI.CustomTextInputDialog;
5751
import root.utils.AlertUtils;
@@ -263,9 +257,7 @@ public void openFileChooser(ActionEvent e) {
263257
// 파일 선택창 열고, 선택된 파일 반환받음
264258
File selectedFile = fileChooser.showOpenDialog((Stage) rootSplitPane.getScene().getWindow());
265259

266-
if (selectedFile == null) {
267-
// NOTHING
268-
} else {
260+
if (selectedFile != null) {
269261
if (selectedFile.isFile() && selectedFile.exists()) {
270262
// 올바른 파일
271263
String filePath = selectedFile.getAbsolutePath();
@@ -283,8 +275,6 @@ public void openFileChooser(ActionEvent e) {
283275
* @param filePath
284276
*/
285277
private void loadSelectedConfigFile(String absoluteFilePath) {
286-
boolean loadResult = false;
287-
288278
try {
289279
// 1. 절대경로를 상대경로로 변환한다.
290280
int startIdx = absoluteFilePath.lastIndexOf("\\config");
@@ -296,6 +286,7 @@ private void loadSelectedConfigFile(String absoluteFilePath) {
296286
// 3. 프로퍼티파일에 작성된 내용에 따라 동적 요소를 생성한다.
297287
createSettingDynamicElements();
298288

289+
// TODO move this logic to PropertyService
299290
// 4. remember.properties 파일에 최근 사용된 설정파일 경로를 저장한다.
300291
PropertiesConfiguration rememberConfig = propRepo.getConfiguration("rememberConfig");
301292
rememberConfig.setProperty("filepath.config.lastuse", filePath.replace("\\", "/"));
@@ -304,25 +295,12 @@ private void loadSelectedConfigFile(String absoluteFilePath) {
304295
// 5. fileChooserText의 텍스트를 현재 선택된 파일경로로 변경한다.
305296
fileChooserText.setText(filePath);
306297

307-
loadResult = true;
308-
} catch (Exception e1) {
309-
e1.printStackTrace();
310-
} finally {
311-
312-
// 6. 파일 load가 완료되었다는 메시지를 띄운다.
313-
if (loadResult) {
314-
Alert successAlert = new Alert(AlertType.INFORMATION);
315-
successAlert.setHeaderText("설정파일 불러오기");
316-
successAlert.setContentText("설정파일을 정상적으로 불러왔습니다.");
317-
successAlert.getDialogPane().setStyle("-fx-font-family: NanumGothic;");
318-
successAlert.show();
319-
} else {
320-
Alert failAlert = new Alert(AlertType.ERROR);
321-
failAlert.setHeaderText("설정파일 불러오기");
322-
failAlert.setContentText("설정파일 불러오기에 실패했습니다. 설정파일을 확인해주세요.");
323-
failAlert.getDialogPane().setStyle("-fx-font-family: NanumGothic;");
324-
failAlert.show();
325-
}
298+
} catch (Exception e) {
299+
e.printStackTrace();
300+
// 6. 파일 load가 실패 시, Alert 메시지를 띄운다.
301+
String headerText = "설정파일 불러오기";
302+
String contentText = "설정파일 불러오기에 실패했습니다. 설정파일을 확인해주세요.";
303+
AlertUtils.showAlert(AlertType.ERROR, headerText, contentText);
326304
}
327305
}
328306

@@ -382,6 +360,7 @@ public void saveConnInfoSettings(ActionEvent e) {
382360
* @param e
383361
*/
384362
public void saveMonitoringSettings(ActionEvent e) {
363+
// TODO move this logic to PropertyService
385364
PropertiesConfiguration config = propRepo.getConfiguration("monitoringConfig");
386365
String presetName = monitoringPresetComboBox.getSelectionModel().getSelectedItem();
387366
String monitoringFilePath = monitoringPresetMap.get(presetName);
@@ -392,13 +371,12 @@ public void saveMonitoringSettings(ActionEvent e) {
392371
config.setProperty(thisToggle.getId(), thisToggle.isSelected());
393372
}
394373
propRepo.save(monitoringFilePath, config);
374+
395375
loadMonitoringConfigFile(monitoringFilePath);
396376

397-
Alert failAlert = new Alert(AlertType.INFORMATION);
398-
failAlert.setHeaderText("설정 저장");
399-
failAlert.setContentText("모니터링여부 설정이 저장되었습니다.");
400-
failAlert.getDialogPane().setStyle("-fx-font-family: NanumGothic;");
401-
failAlert.show();
377+
String headerText = "설정 저장";
378+
String contentText = "모니터링여부 설정이 저장되었습니다.";
379+
AlertUtils.showAlert(AlertType.INFORMATION, headerText, contentText);
402380
}
403381
}
404382

@@ -407,130 +385,22 @@ public void saveMonitoringSettings(ActionEvent e) {
407385
*
408386
* @param rootVBox
409387
* @param monitoringElements
410-
* @param elementContents
388+
* @param monitoringAlias
411389
*/
412-
private void createMonitoringElements(VBox rootVBox, String[] monitoringElements, String[] elementContents) {
413-
for (String mName : monitoringElements) {
414-
// String headerToggleId = mName.replaceAll("\\s", "") + "TotalToggleBtn";
415-
String headerToggleId = mName.replaceAll("\\s", "") + "TotalToggleBtn";
416-
417-
// Header
418-
VBox eachWrapVBox = new VBox();
419-
eachWrapVBox.setFillWidth(true);
420-
421-
HBox headerHBox = new HBox();
422-
headerHBox.setFillHeight(true);
423-
headerHBox.setPrefHeight(40);
424-
425-
Label headerLabel = new Label();
426-
headerLabel.setText(mName);
427-
headerLabel.setTextAlignment(TextAlignment.LEFT);
428-
headerLabel.setAlignment(Pos.CENTER_LEFT);
429-
headerLabel.setPrefWidth(200);
430-
headerLabel.setPrefHeight(40);
431-
headerLabel.getStylesheets().add(System.getProperty("resourceBaseDir") + "/css/javaFx.css");
432-
headerLabel.getStyleClass().add("basic-font");
433-
headerLabel.setStyle("-fx-font-size: 13px");
434-
435-
JFXToggleButton headerToggleBtn = new JFXToggleButton();
436-
headerToggleBtn.setId(headerToggleId);
437-
headerToggleBtn.setSize(6);
438-
headerToggleBtn.setToggleColor(Paint.valueOf("#0132ac"));
439-
headerToggleBtn.setToggleLineColor(Paint.valueOf("#6e93ea"));
440-
headerToggleBtn.setAlignment(Pos.CENTER);
441-
headerToggleBtn.setSelected(propRepo.isMonitoringContent(headerToggleId));
442-
headerToggleBtn.setOnAction((ActionEvent e) -> {
443-
boolean isSelected = ((JFXToggleButton) e.getSource()).isSelected();
444-
for (Node n : eachWrapVBox.lookupAll("JFXToggleButton")) {
445-
((JFXToggleButton) n).setSelected(isSelected);
446-
}
447-
});
390+
private void createMonitoringElements(VBox rootVBox, String[] monitoringElements, String[] monitoringAlias) {
448391

449-
headerHBox.getChildren().addAll(headerToggleBtn, headerLabel);
450-
451-
// Content
452-
FlowPane contentFlowPane = new FlowPane();
453-
contentFlowPane.prefWidthProperty().bind(rootVBox.widthProperty());
454-
contentFlowPane.minWidthProperty().bind(rootVBox.minWidthProperty());
455-
456-
for (String s : elementContents) {
457-
String contentToggleId = mName.replaceAll("\\s", "") + s + "ToggleBtn";
458-
459-
HBox contentHBox = new HBox();
460-
Label contentLabel = new Label();
461-
contentLabel.setText(s);
462-
contentLabel.setTextAlignment(TextAlignment.LEFT);
463-
contentLabel.setAlignment(Pos.CENTER_LEFT);
464-
contentLabel.setMinWidth(80);
465-
contentLabel.setMaxWidth(80);
466-
contentLabel.setPrefHeight(40);
467-
contentLabel.getStylesheets().add(System.getProperty("resourceBaseDir") + "/css/javaFx.css");
468-
contentLabel.getStyleClass().add("basic-font");
469-
470-
JFXToggleButton contentToggleBtn = new JFXToggleButton();
471-
contentToggleBtn.setId(contentToggleId);
472-
contentToggleBtn.setSize(4);
473-
contentToggleBtn.setMinWidth(40);
474-
contentToggleBtn.setMaxWidth(40);
475-
contentToggleBtn.setPrefHeight(40);
476-
contentToggleBtn.setAlignment(Pos.CENTER);
477-
contentToggleBtn.setSelected(propRepo.isMonitoringContent(contentToggleId));
478-
contentToggleBtn.setOnAction((ActionEvent e) -> {
479-
boolean isSelected = ((JFXToggleButton) e.getSource()).isSelected();
480-
481-
/*
482-
* 1. 하위요소가 선택되었을 때, 1.1. 부모요소가 선택되었는지 확인 1.1.1. 선택됨 - break; 1.1.2. 선택안됨 -
483-
* isSelected = true 2. 하위요소가 선택되지 않았을 때, 2.1. 부모요소가 선택되었는지 확인 2.1.1. 선택안됨 -
484-
* break; 2.1.2. 선택됨 2.1.2.1. 모든 하위요소 선택여부 확인 2.1.2.1.1. 모든 하위요소 선택되지않음 - 부모요소
485-
* isSelected = false;
486-
*/
487-
if (isSelected == true) {
488-
for (Node n : eachWrapVBox.lookupAll("JFXToggleButton")) {
489-
JFXToggleButton thisToggle = (JFXToggleButton) n;
490-
if (thisToggle.getId().equals(headerToggleId)) { // 부모 Toggle
491-
if (thisToggle.isSelected() == false) {
492-
thisToggle.setSelected(true);
493-
break;
494-
}
495-
}
496-
}
497-
} else {
498-
boolean isNotAllSelected = false;
499-
boolean isParentSelected = true;
500-
for (Node n : eachWrapVBox.lookupAll("JFXToggleButton")) {
501-
JFXToggleButton thisToggle = (JFXToggleButton) n;
502-
if (thisToggle.getId().equals(headerToggleId) == false) { // 자식 Toggle
503-
if (thisToggle.isSelected() == true) {
504-
isNotAllSelected = true;
505-
break;
506-
}
507-
} else {
508-
if (thisToggle.isSelected() == false) {
509-
isParentSelected = false;
510-
break;
511-
}
512-
}
513-
}
514-
515-
if (isNotAllSelected == false && isParentSelected == true) {
516-
for (Node n : eachWrapVBox.lookupAll("JFXToggleButton")) {
517-
JFXToggleButton thisToggle = (JFXToggleButton) n;
518-
if (thisToggle.getId().equals(headerToggleId)) { // 부모 Toggle
519-
thisToggle.setSelected(false);
520-
break;
521-
}
522-
}
523-
}
524-
}
525-
});
526-
527-
contentHBox.getChildren().addAll(contentToggleBtn, contentLabel);
528-
contentFlowPane.getChildren().addAll(contentHBox);
529-
}
392+
for (String monitoringElement : monitoringElements) {
530393

531-
eachWrapVBox.getChildren().addAll(headerHBox, contentFlowPane);
394+
MonitoringYNVBox monitoringYNVBox = new MonitoringYNVBox();
395+
// TODO 초기값 설정
396+
monitoringYNVBox.addParentToggle(monitoringElement, 6, false);
397+
398+
for (String alias : monitoringAlias) {
399+
// TODO 초기값 설정
400+
monitoringYNVBox.addChildToggle(alias, 4, false);
401+
}
532402

533-
rootVBox.getChildren().addAll(eachWrapVBox);
403+
rootVBox.getChildren().addAll(monitoringYNVBox);
534404
}
535405

536406
rootVBox.getChildren().add(new Separator());
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package root.javafx.CustomView;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import javafx.event.ActionEvent;
8+
import javafx.event.EventHandler;
9+
import javafx.fxml.FXML;
10+
import javafx.fxml.FXMLLoader;
11+
import javafx.scene.layout.FlowPane;
12+
import javafx.scene.layout.HBox;
13+
import javafx.scene.layout.VBox;
14+
import javafx.scene.paint.Paint;
15+
import root.javafx.DI.DependencyInjection;
16+
17+
public class MonitoringYNVBox extends VBox {
18+
19+
private static final Paint PARENT_TOGGLE_COLOR = Paint.valueOf("#0132ac");
20+
private static final Paint PARENT_TOGGLE_LINE_COLOR = Paint.valueOf("#6e93ea");
21+
private static final Paint CHILD_TOGGLE_COLOR = Paint.valueOf("#009688");
22+
private static final Paint CHILD_TOGGLE_LINE_COLOR = Paint.valueOf("#77c2bb");
23+
24+
@FXML
25+
HBox parentHBox;
26+
27+
@FXML
28+
FlowPane childFlowPane;
29+
30+
ToggleHBox parentToggle;
31+
32+
List<ToggleHBox> childToggleList = new ArrayList<>();
33+
34+
public MonitoringYNVBox() {
35+
try {
36+
FXMLLoader loader = DependencyInjection.getLoader("/fxml/MonitoringYNVBox.fxml");
37+
loader.setController(this);
38+
loader.setRoot(this);
39+
loader.load();
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
/**
46+
* 부모 토글 버튼을 추가한다.
47+
*
48+
* @param text 토글 우글 라벨 텍스트
49+
* @param size 토글 크기
50+
* @param isSelected 트글 선택 여부
51+
*/
52+
public void addParentToggle(String text, int size, boolean isSelected) {
53+
parentToggle = new ToggleHBox();
54+
parentToggle.setLabelText(text);
55+
parentToggle.setToggle(size, PARENT_TOGGLE_COLOR, PARENT_TOGGLE_LINE_COLOR);
56+
parentToggle.setToggleSelected(isSelected);
57+
parentToggle.setToggleAction(new EventHandler<ActionEvent>() {
58+
59+
@Override
60+
public void handle(ActionEvent e) {
61+
for (ToggleHBox t : childToggleList) {
62+
t.setToggleSelected(parentToggle.isToggleSelected());
63+
}
64+
}
65+
});
66+
67+
parentHBox.getChildren().add(parentToggle);
68+
}
69+
70+
/**
71+
* 자식 토글 버튼을 추가한다.
72+
*
73+
* @param text 토글 우측 라벨 텍스트
74+
* @param size 토글 크기
75+
* @param isSelected 토글 선택 여부
76+
*/
77+
public void addChildToggle(String text, int size, boolean isSelected) {
78+
addChildToggle(text, size, CHILD_TOGGLE_COLOR, CHILD_TOGGLE_LINE_COLOR, isSelected);
79+
}
80+
81+
/**
82+
* 자식 토글 버튼을 추가한다.
83+
*
84+
* @param text 토글 우측 라벨 텍스트
85+
* @param size 토글 크기
86+
* @param color 토글 색
87+
* @param lineColor 토글 라인 색
88+
* @param isSelected 토글 선택 여부
89+
*/
90+
public void addChildToggle(String text, int size, Paint color, Paint lineColor, boolean isSelected) {
91+
ToggleHBox toggleHBox = new ToggleHBox();
92+
toggleHBox.setLabelText(text);
93+
toggleHBox.setToggle(size, color, lineColor);
94+
toggleHBox.setToggleSelected(isSelected);
95+
toggleHBox.setToggleAction(new EventHandler<ActionEvent>() {
96+
97+
@Override
98+
public void handle(ActionEvent e) {
99+
100+
if (toggleHBox.isToggleSelected()) {
101+
parentToggle.setToggleSelected(true);
102+
} else {
103+
boolean isAllNotSelected = true;
104+
for (ToggleHBox t : childToggleList) {
105+
if (t.isToggleSelected()) {
106+
isAllNotSelected = false;
107+
break;
108+
}
109+
}
110+
parentToggle.setToggleSelected(!isAllNotSelected);
111+
}
112+
}
113+
});
114+
115+
childFlowPane.getChildren().add(toggleHBox);
116+
childToggleList.add(toggleHBox);
117+
}
118+
}

0 commit comments

Comments
 (0)