Skip to content

Commit c417903

Browse files
committed
Refactoring: Processing Usage UI type configured value is NULL and Create RoundingDigits Enum class
1 parent 25970fa commit c417903

File tree

4 files changed

+87
-32
lines changed

4 files changed

+87
-32
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package root.core.domain.enums;
2+
3+
import java.util.Collections;
4+
import java.util.Map;
5+
import java.util.Optional;
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
import lombok.Getter;
11+
12+
@Getter
13+
public enum RoundingDigits {
14+
15+
ONE(1), TWO(2), THREE(3), FOUR(4), FIVE(5);
16+
17+
private int digits;
18+
19+
private RoundingDigits(int digits) {
20+
this.digits = digits;
21+
}
22+
23+
private static final Map<Integer, RoundingDigits> roundingDigitsMap = Collections.unmodifiableMap(
24+
Stream.of(values()).collect(Collectors.toMap(RoundingDigits::getDigits, Function.identity())));
25+
26+
public static RoundingDigits find(final int digits) {
27+
return Optional.ofNullable(roundingDigitsMap.get(digits)).orElse(TWO);
28+
}
29+
30+
public static RoundingDigits find(final String digits) {
31+
try {
32+
return find(Integer.parseInt(digits));
33+
} catch (NumberFormatException e) {
34+
return TWO;
35+
}
36+
}
37+
}

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

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javafx.util.StringConverter;
4545
import root.core.domain.JdbcConnectionInfo;
4646
import root.core.domain.JschConnectionInfo;
47+
import root.core.domain.enums.RoundingDigits;
4748
import root.core.domain.enums.UsageUIType;
4849
import root.core.repository.constracts.PropertyRepository;
4950
import root.core.repository.implement.PropertyRepositoryImpl;
@@ -94,7 +95,7 @@ public class SettingMenuController implements Initializable {
9495
JFXComboBox<FileSize> fileSizeCB;
9596

9697
@FXML
97-
JFXComboBox<Integer> roundingDigitsCB;
98+
JFXComboBox<RoundingDigits> roundingDigitsCB;
9899

99100
@FXML
100101
JFXComboBox<UsageUIType> usageUICB;
@@ -129,10 +130,10 @@ public void initialize(URL location, ResourceBundle resources) {
129130
setVisible(noMonitoringConfigAP, true);
130131
}
131132

132-
/* 실행 설정 탭 - 조회결과 단위 설정 콤보박스 */
133-
// 조회결과 단위 설정 콤보박스 아이템 설정
133+
/* 실행 설정 탭 - 조회결과 단위 콤보박스 */
134+
// 조회결과 단위 콤보박스 아이템 설정
134135
fileSizeCB.getItems().addAll(FileSize.values());
135-
136+
136137
// 아이템 변경 리스너
137138
fileSizeCB.valueProperty().addListener((options, oldValue, newValue) -> {
138139
Map<String, Object> map = new HashMap<>();
@@ -141,27 +142,49 @@ public void initialize(URL location, ResourceBundle resources) {
141142
});
142143

143144
// 초기값 - 설정된 값 없다면 기본값 GB
144-
FileSize fileSize = FileSize.of(propRepo.getCommonResource("unit.filesize"));
145-
fileSizeCB.getSelectionModel().select(fileSize == null ? FileSize.GB : fileSize);
145+
FileSize fileSize = FileSize.find(propRepo.getCommonResource("unit.filesize"));
146+
fileSizeCB.getSelectionModel().select(fileSize);
146147

147-
/* 실행 설정 탭 - 반올림 자릿수 설정 콤보박스 */
148-
// 반올림 자릿수 설정 콤보박스 아이템 설정
149-
roundingDigitsCB.getItems().addAll(List.of(1, 2, 3, 4, 5));
148+
/* 실행 설정 탭 - 반올림 자릿수 콤보박스 */
149+
// 반올림 자릿수 콤보박스 아이템 설정
150+
roundingDigitsCB.getItems().addAll(RoundingDigits.values());
150151

151152
// 아이템 변경 리스너
152153
roundingDigitsCB.valueProperty().addListener((options, oldValue, newValue) -> {
153154
Map<String, Object> map = new HashMap<>();
154-
map.put("unit.rounding", newValue);
155+
map.put("unit.rounding", newValue.getDigits());
155156
propRepo.saveCommonConfig(map);
156157
});
157158

159+
// Converter
160+
roundingDigitsCB.setConverter(new StringConverter<RoundingDigits>() {
161+
@Override
162+
public String toString(RoundingDigits digits) {
163+
return String.valueOf(digits.getDigits());
164+
}
165+
166+
@Override
167+
public RoundingDigits fromString(String digits) {
168+
return RoundingDigits.find(digits);
169+
}
170+
});
171+
158172
// 초기값 - 설정된 값 없다면 기본값 2
159-
String roundingDigits = propRepo.getCommonResource("unit.rounding");
160-
roundingDigitsCB.getSelectionModel().select(roundingDigits == null ? 2 : Integer.valueOf(roundingDigits));
173+
RoundingDigits roundingDigits = RoundingDigits.find(propRepo.getCommonResource("unit.rounding"));
174+
roundingDigitsCB.getSelectionModel().select(roundingDigits);
161175

176+
/* 실행 설정 탭 - 사용량 표시방법 콤보박스 */
177+
// 사용량 표시방법 콤보박스 아이템 설정
178+
usageUICB.getItems().addAll(UsageUIType.values());
162179

163-
// Set usage UI type comboBox items and Set setting value;
164-
String usageUICode = propRepo.getCommonResource("usage-ui-type");
180+
// 아이템 변경 리스너
181+
usageUICB.valueProperty().addListener((options, oldValue, newValue) -> {
182+
Map<String, Object> map = new HashMap<>();
183+
map.put("usage-ui-type", newValue.getCode());
184+
propRepo.saveCommonConfig(map);
185+
});
186+
187+
// Converter
165188
usageUICB.setConverter(new StringConverter<UsageUIType>() {
166189
@Override
167190
public String toString(UsageUIType uiType) {
@@ -170,16 +193,13 @@ public String toString(UsageUIType uiType) {
170193

171194
@Override
172195
public UsageUIType fromString(String string) {
173-
return usageUICB.getItems().stream().filter(ui -> ui.getName().equals(string)).findFirst().orElse(null);
196+
return UsageUIType.find(string);
174197
}
175198
});
176-
this.usageUICB.getItems().addAll(UsageUIType.values());
177-
this.usageUICB.getSelectionModel().select(UsageUIType.find(usageUICode));
178-
usageUICB.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
179-
Map<String, Object> map = new HashMap<>();
180-
map.put("usage-ui-type", newValue.getCode());
181-
propRepo.saveCommonConfig(map);
182-
});
199+
200+
// 초기값 - 설정된 값 없다면 기본값 GRAPHIC_BAR
201+
String usageUICode = propRepo.getCommonResource("usage-ui-type");
202+
usageUICB.getSelectionModel().select(UsageUIType.find(usageUICode));
183203
}
184204

185205
/**

src/main/java/root/utils/UnitUtils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Collections;
44
import java.util.Map;
5+
import java.util.Optional;
6+
import java.util.function.Function;
57
import java.util.stream.Collectors;
68
import java.util.stream.Stream;
79

@@ -16,20 +18,16 @@ public enum FileSize {
1618
private String unit;
1719
private int order;
1820

19-
private static final Map<String, String> FILESIZE_MAP = Collections
20-
.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(FileSize::getUnit, FileSize::name)));
21+
private static final Map<String, FileSize> fileSizeMap = Collections
22+
.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(FileSize::getUnit, Function.identity())));
2123

2224
FileSize(String unit, int order) {
2325
this.unit = unit;
2426
this.order = order;
2527
}
2628

27-
public static FileSize of(final String unit) {
28-
try {
29-
return FileSize.valueOf(FILESIZE_MAP.get(unit));
30-
} catch (NullPointerException e) {
31-
return null;
32-
}
29+
public static FileSize find(final String unit) {
30+
return Optional.ofNullable(fileSizeMap.get(unit)).orElse(GB);
3331
}
3432
}
3533

src/main/resources/fxml/SettingMenu.fxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
<RowConstraints maxHeight="-Infinity" minHeight="-Infinity" prefHeight="40.0" vgrow="SOMETIMES" />
291291
</rowConstraints>
292292
<children>
293-
<Label style="-fx-font-size: 13px;" stylesheets="@../css/javaFx.css" text="조회결과 단위 설정">
293+
<Label style="-fx-font-size: 13px;" stylesheets="@../css/javaFx.css" text="조회결과 단위">
294294
<graphic>
295295
<FontAwesomeIconView glyphName="CHEVRON_CIRCLE_RIGHT" size="13" />
296296
</graphic>
@@ -302,7 +302,7 @@
302302
<String fx:value="bold" />
303303
</styleClass>
304304
</Label>
305-
<Label style="-fx-font-size: 13px;" stylesheets="@../css/javaFx.css" text="반올림 자릿수 설정" GridPane.columnIndex="2">
305+
<Label style="-fx-font-size: 13px;" stylesheets="@../css/javaFx.css" text="반올림 자릿수" GridPane.columnIndex="2">
306306
<graphic>
307307
<FontAwesomeIconView glyphName="CHEVRON_CIRCLE_RIGHT" size="13" />
308308
</graphic>

0 commit comments

Comments
 (0)