|
| 1 | +package root.javafx.Controller; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.LinkedHashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import de.jensd.fx.glyphs.fontawesome.FontAwesomeIcon; |
| 11 | +import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView; |
| 12 | +import javafx.event.ActionEvent; |
| 13 | +import javafx.fxml.FXML; |
| 14 | +import javafx.fxml.FXMLLoader; |
| 15 | +import javafx.scene.control.Label; |
| 16 | +import javafx.scene.layout.AnchorPane; |
| 17 | +import javafx.scene.layout.StackPane; |
| 18 | +import lombok.AllArgsConstructor; |
| 19 | +import lombok.Data; |
| 20 | +import root.core.domain.MonitoringResult; |
| 21 | +import root.core.domain.enums.UsageUIType; |
| 22 | +import root.javafx.CustomView.MonitoringTableViewContainer; |
| 23 | +import root.javafx.DI.DependencyInjection; |
| 24 | +import root.utils.DateUtils; |
| 25 | + |
| 26 | +public class MonitoringTableViewPagingBox extends AnchorPane { |
| 27 | + |
| 28 | + @FXML |
| 29 | + private FontAwesomeIconView iconView; |
| 30 | + |
| 31 | + @FXML |
| 32 | + private Label aliasLabel; |
| 33 | + |
| 34 | + @FXML |
| 35 | + private Label monitoringTimeLabel; |
| 36 | + |
| 37 | + @FXML |
| 38 | + private StackPane tableViewSP; |
| 39 | + |
| 40 | + private Map<AliasInfo, MonitoringTableViewContainer> tableViewContainerMap = new LinkedHashMap<>(); |
| 41 | + |
| 42 | + public MonitoringTableViewPagingBox(String monitoringType) { |
| 43 | + try { |
| 44 | + FXMLLoader loader = DependencyInjection.getLoader("/fxml/MonitoringTableViewPagingBox.fxml"); |
| 45 | + loader.setController(this); |
| 46 | + loader.setRoot(this); |
| 47 | + loader.load(); |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + iconView.setIcon(monitoringType.equals("DB") ? FontAwesomeIcon.DATABASE : FontAwesomeIcon.SERVER); |
| 52 | + setMonitoringTimeLabel(new Date()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Alias별로 모니터링 TableView를 나열할 컨테이너를 생성한다. |
| 57 | + * |
| 58 | + * @param alias |
| 59 | + * @param type |
| 60 | + */ |
| 61 | + public void addMonitoringTableViewContainer(String alias, Class<? extends MonitoringResult> type) { |
| 62 | + if (tableViewContainerMap.get(findAliasInfoByAlias(alias)) == null) { |
| 63 | + MonitoringTableViewContainer container = new MonitoringTableViewContainer(); |
| 64 | + tableViewContainerMap.put(new AliasInfo(alias, findLastAliasInfoIndex() + 1), container); |
| 65 | + tableViewSP.getChildren().add(container); |
| 66 | + } |
| 67 | + |
| 68 | + MonitoringTableViewContainer container = tableViewContainerMap.get(findAliasInfoByAlias(alias)); |
| 69 | + container.addMonitoringTableView(type); |
| 70 | + container.setUsageUIType(type, UsageUIType.NUMERIC); // default |
| 71 | + tableViewContainerMap.put(findAliasInfoByAlias(alias), container); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 모니터링 결과 TableView의 사용량 컬럼의 UI 타입을 설정한다. |
| 76 | + * |
| 77 | + * @param alias |
| 78 | + * @param type |
| 79 | + * @param uiType |
| 80 | + */ |
| 81 | + public void setMonitoringTableViewUsageUIType(String alias, Class<? extends MonitoringResult> type, |
| 82 | + UsageUIType uiType) { |
| 83 | + tableViewContainerMap.get(findAliasInfoByAlias(alias)).setUsageUIType(type, uiType); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 모니터링 결과 TableView에 데이터셋을 설정한다. |
| 88 | + * |
| 89 | + * @param <T> |
| 90 | + * @param alias |
| 91 | + * @param type |
| 92 | + * @param dataList |
| 93 | + */ |
| 94 | + public <T extends MonitoringResult> void setMonitoringTableViewData(String alias, Class<T> type, List<T> dataList) { |
| 95 | + tableViewContainerMap.get(findAliasInfoByAlias(alias)).setTableData(type, dataList); |
| 96 | + showTableViewContainer(alias); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * 특정 Alias의 TableViewContainer를 맨 앞으로 가져오고 라벨을 변경한다. |
| 101 | + * |
| 102 | + * @param alias |
| 103 | + */ |
| 104 | + private void showTableViewContainer(String alias) { |
| 105 | + setAliasLabel(alias); |
| 106 | + tableViewContainerMap.get(findAliasInfoByAlias(alias)).toFront(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Alias Label의 텍스트를 설정한다. |
| 111 | + * |
| 112 | + * @param alias |
| 113 | + */ |
| 114 | + private void setAliasLabel(String alias) { |
| 115 | + aliasLabel.setText(alias); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Monitoring Time Label의 텍스트를 설정한다. |
| 120 | + * |
| 121 | + * @param time |
| 122 | + */ |
| 123 | + public void setMonitoringTimeLabel(Date time) { |
| 124 | + monitoringTimeLabel.setText(DateUtils.format(time, "yyyy-MM-dd HH:mm:ss")); |
| 125 | + } |
| 126 | + |
| 127 | + private AliasInfo findAliasInfoByAlias(String alias) { |
| 128 | + return tableViewContainerMap.keySet() |
| 129 | + .stream() |
| 130 | + .filter(info -> info.getAlias().equals(alias)) |
| 131 | + .findFirst() |
| 132 | + .orElse(null); |
| 133 | + } |
| 134 | + |
| 135 | + private AliasInfo findAliasInfoByIndex(int index) { |
| 136 | + return tableViewContainerMap.keySet() |
| 137 | + .stream() |
| 138 | + .filter(info -> info.getIndex() == index) |
| 139 | + .findFirst() |
| 140 | + .orElse(null); |
| 141 | + } |
| 142 | + |
| 143 | + private int findAliasInfoIndex(String alias) { |
| 144 | + return findAliasInfoByAlias(alias).getIndex(); |
| 145 | + } |
| 146 | + |
| 147 | + private int findFirstAliasInfoIndex() { |
| 148 | + return tableViewContainerMap.keySet() |
| 149 | + .stream() |
| 150 | + .sorted() |
| 151 | + .map(info -> info.getIndex()) |
| 152 | + .findFirst() |
| 153 | + .orElse(-1); |
| 154 | + } |
| 155 | + |
| 156 | + private int findLastAliasInfoIndex() { |
| 157 | + return tableViewContainerMap.keySet() |
| 158 | + .stream() |
| 159 | + .sorted(Collections.reverseOrder()) |
| 160 | + .map(info -> info.getIndex()) |
| 161 | + .findFirst() |
| 162 | + .orElse(-1); |
| 163 | + } |
| 164 | + |
| 165 | + private int findPrevAliasInfoIndex(int curIndex) { |
| 166 | + return tableViewContainerMap.keySet() |
| 167 | + .stream() |
| 168 | + .filter(info -> info.getIndex() < curIndex) |
| 169 | + .sorted(Collections.reverseOrder()) |
| 170 | + .map(info -> info.getIndex()) |
| 171 | + .findFirst() |
| 172 | + .orElse(-1); |
| 173 | + } |
| 174 | + |
| 175 | + private int findNextAliasInfoIndex(int curIndex) { |
| 176 | + return tableViewContainerMap.keySet() |
| 177 | + .stream() |
| 178 | + .filter(info -> info.getIndex() > curIndex) |
| 179 | + .sorted() |
| 180 | + .map(info -> info.getIndex()) |
| 181 | + .findFirst() |
| 182 | + .orElse(-1); |
| 183 | + } |
| 184 | + |
| 185 | + public void prevContainer(ActionEvent e) { |
| 186 | + int curIndex = findAliasInfoIndex(aliasLabel.getText()); |
| 187 | + int prevIndex = findPrevAliasInfoIndex(curIndex); |
| 188 | + if(prevIndex == -1) { |
| 189 | + prevIndex = findLastAliasInfoIndex(); |
| 190 | + } |
| 191 | + showTableViewContainer(findAliasInfoByIndex(prevIndex).getAlias()); |
| 192 | + } |
| 193 | + |
| 194 | + public void nextContainer(ActionEvent e) { |
| 195 | + int curIndex = findAliasInfoIndex(aliasLabel.getText()); |
| 196 | + int nextIndex = findNextAliasInfoIndex(curIndex); |
| 197 | + if(nextIndex == -1) { |
| 198 | + nextIndex = findFirstAliasInfoIndex(); |
| 199 | + } |
| 200 | + showTableViewContainer(findAliasInfoByIndex(nextIndex).getAlias()); |
| 201 | + } |
| 202 | + |
| 203 | + @AllArgsConstructor |
| 204 | + @Data |
| 205 | + private static class AliasInfo implements Comparable<AliasInfo> { |
| 206 | + String alias; |
| 207 | + int index; |
| 208 | + |
| 209 | + @Override |
| 210 | + public int compareTo(AliasInfo o) { |
| 211 | + return this.index - o.index; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments