|
| 1 | +package sample; |
| 2 | + |
| 3 | +import javafx.event.ActionEvent; |
| 4 | +import javafx.fxml.FXML; |
| 5 | +import javafx.fxml.Initializable; |
| 6 | +import javafx.scene.control.CheckBox; |
| 7 | +import javafx.scene.control.Label; |
| 8 | +import javafx.scene.control.TextArea; |
| 9 | +import javafx.scene.control.TextField; |
| 10 | +import javafx.scene.layout.TilePane; |
| 11 | +import sample.util.FileUtil; |
| 12 | +import sample.util.Utils; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URL; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.ResourceBundle; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author DeMon |
| 24 | + * Created on 2021/5/27. |
| 25 | + |
| 26 | + * Desc: |
| 27 | + */ |
| 28 | +public class ChannelController implements Initializable { |
| 29 | + @FXML |
| 30 | + private TextField tfChannelKey; |
| 31 | + @FXML |
| 32 | + private TextArea tfChannel; |
| 33 | + @FXML |
| 34 | + private TilePane tilePane; |
| 35 | + @FXML |
| 36 | + public Label tvConsole; |
| 37 | + |
| 38 | + private File channelFile; |
| 39 | + private File selectedFile; |
| 40 | + |
| 41 | + private List<String> selectedList = new ArrayList<>(); |
| 42 | + |
| 43 | + private List<CheckBox> checkBoxList = new ArrayList<>(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(URL location, ResourceBundle resources) { |
| 47 | + try { |
| 48 | + selectedFile = new File(System.getProperty("user.dir") + "/channel_selected.txt"); |
| 49 | + if (!selectedFile.exists()) { |
| 50 | + selectedFile.createNewFile(); |
| 51 | + } else { |
| 52 | + String selects = FileUtil.readText(selectedFile.getPath()); |
| 53 | + if (!Utils.isEmpty(selects)) { |
| 54 | + selectedList.addAll(Arrays.asList(selects.split(";"))); |
| 55 | + } |
| 56 | + } |
| 57 | + channelFile = new File(System.getProperty("user.dir") + "/channel.txt"); |
| 58 | + if (!channelFile.exists()) { |
| 59 | + channelFile.createNewFile(); |
| 60 | + } else { |
| 61 | + String channels = FileUtil.readText(channelFile.getPath()); |
| 62 | + if (!Utils.isEmpty(channels) && channels.contains("_")) { |
| 63 | + String[] array = channels.split("_"); |
| 64 | + tfChannelKey.setText(array[0]); |
| 65 | + tfChannel.setText(array[1]); |
| 66 | + updateChannels(array[1]); |
| 67 | + } |
| 68 | + } |
| 69 | + } catch (IOException e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private void updateChannels(String channels) { |
| 76 | + tilePane.getChildren().clear(); |
| 77 | + checkBoxList.clear(); |
| 78 | + String[] channelArray = channels.split(";"); |
| 79 | + for (String channel : channelArray) { |
| 80 | + CheckBox checkBox = new CheckBox(channel); |
| 81 | + checkBox.setSelected(selectedList.contains(channel)); |
| 82 | + checkBoxList.add(checkBox); |
| 83 | + checkBox.selectedProperty().addListener((observable, oldValue, newValue) -> { |
| 84 | + System.out.println(channel + ":" + newValue); |
| 85 | + if (newValue) { |
| 86 | + selectedList.add(channel); |
| 87 | + } else { |
| 88 | + selectedList.remove(channel); |
| 89 | + } |
| 90 | + }); |
| 91 | + tilePane.getChildren().add(checkBox); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @FXML |
| 97 | + private void doRefresh(ActionEvent actionEvent) { |
| 98 | + String channelKey = tfChannelKey.getText(); |
| 99 | + if (Utils.isEmpty(channelKey)) { |
| 100 | + tvConsole.setText("渠道前缀标识不能为空!"); |
| 101 | + return; |
| 102 | + } |
| 103 | + String channels = tfChannel.getText(); |
| 104 | + if (Utils.isEmpty(channels)) { |
| 105 | + tvConsole.setText("渠道配置不能为空!"); |
| 106 | + return; |
| 107 | + } |
| 108 | + FileUtil.writeTxt(channelFile.getPath(), channelKey + "_" + channels); |
| 109 | + selectedList.clear(); |
| 110 | + FileUtil.writeTxt(selectedFile.getPath(), ""); |
| 111 | + updateChannels(channels); |
| 112 | + tvConsole.setText("渠道配置已更新,请重新选择并保存!"); |
| 113 | + } |
| 114 | + |
| 115 | + @FXML |
| 116 | + private void doAll(ActionEvent actionEvent) { |
| 117 | + for (CheckBox box : checkBoxList) { |
| 118 | + box.setSelected(true); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @FXML |
| 123 | + private void doAllNo(ActionEvent actionEvent) { |
| 124 | + for (CheckBox box : checkBoxList) { |
| 125 | + box.setSelected(false); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @FXML |
| 130 | + private void doSave(ActionEvent actionEvent) { |
| 131 | + if (selectedList.isEmpty()) { |
| 132 | + tvConsole.setText("请至少选择一个渠道!"); |
| 133 | + return; |
| 134 | + } |
| 135 | + StringBuilder sb = new StringBuilder(); |
| 136 | + for (String s : selectedList) { |
| 137 | + sb.append(s); |
| 138 | + sb.append(";"); |
| 139 | + } |
| 140 | + System.out.println(sb); |
| 141 | + FileUtil.writeTxt(selectedFile.getPath(), sb.toString()); |
| 142 | + tvConsole.setText("渠道选择配置保存成功!"); |
| 143 | + } |
| 144 | +} |
0 commit comments