|
| 1 | +package com.github.reugn.devtools.controllers; |
| 2 | + |
| 3 | +import com.github.reugn.devtools.services.AsciiService; |
| 4 | +import com.github.reugn.devtools.utils.Elements; |
| 5 | +import com.github.reugn.devtools.utils.Logger; |
| 6 | +import javafx.event.ActionEvent; |
| 7 | +import javafx.fxml.FXML; |
| 8 | +import javafx.fxml.Initializable; |
| 9 | +import javafx.geometry.Insets; |
| 10 | +import javafx.scene.control.*; |
| 11 | +import javafx.scene.input.KeyEvent; |
| 12 | +import javafx.scene.layout.Border; |
| 13 | +import javafx.scene.layout.HBox; |
| 14 | +import javafx.scene.text.Font; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | + |
| 17 | +import java.net.URL; |
| 18 | +import java.util.ResourceBundle; |
| 19 | + |
| 20 | +public class AsciiController implements Initializable, Logger { |
| 21 | + |
| 22 | + @FXML |
| 23 | + private Label asciiLabel; |
| 24 | + @FXML |
| 25 | + private TextField asciiField; |
| 26 | + @FXML |
| 27 | + private Button asciiCalculateButton; |
| 28 | + @FXML |
| 29 | + private Button asciiClearButton; |
| 30 | + @FXML |
| 31 | + private Label asciiMessage; |
| 32 | + @FXML |
| 33 | + private TextArea asciiResult; |
| 34 | + @FXML |
| 35 | + private Label fontSizeLabel; |
| 36 | + @FXML |
| 37 | + private Label fontNameLabel; |
| 38 | + @FXML |
| 39 | + private TextField asciiCharField; |
| 40 | + @FXML |
| 41 | + private TextField fontSizeField; |
| 42 | + @FXML |
| 43 | + private ComboBox<String> fontNameComboBox; |
| 44 | + @FXML |
| 45 | + private ComboBox<String> fontStyleComboBox; |
| 46 | + @FXML |
| 47 | + private Label asciiCharLabel; |
| 48 | + @FXML |
| 49 | + private Label fontStyleLabel; |
| 50 | + |
| 51 | + @Override |
| 52 | + public void initialize(URL url, ResourceBundle resourceBundle) { |
| 53 | + |
| 54 | + HBox.setMargin(fontSizeLabel, new Insets(15, 5, 10, 0)); |
| 55 | + HBox.setMargin(fontNameLabel, new Insets(15, 5, 10, 0)); |
| 56 | + HBox.setMargin(asciiCharField, new Insets(10, 5, 10, 0)); |
| 57 | + HBox.setMargin(fontSizeField, new Insets(10, 5, 10, 0)); |
| 58 | + |
| 59 | + HBox.setMargin(fontNameComboBox, new Insets(10, 5, 10, 0)); |
| 60 | + HBox.setMargin(fontStyleComboBox, new Insets(10, 5, 10, 0)); |
| 61 | + HBox.setMargin(asciiCharLabel, new Insets(15, 5, 10, 0)); |
| 62 | + HBox.setMargin(fontStyleLabel, new Insets(15, 5, 10, 0)); |
| 63 | + |
| 64 | + HBox.setMargin(asciiLabel, new Insets(15, 5, 10, 0)); |
| 65 | + HBox.setMargin(asciiField, new Insets(10, 5, 10, 0)); |
| 66 | + HBox.setMargin(asciiCalculateButton, new Insets(10, 5, 10, 0)); |
| 67 | + HBox.setMargin(asciiClearButton, new Insets(10, 5, 10, 0)); |
| 68 | + HBox.setMargin(asciiMessage, new Insets(10, 5, 10, 0)); |
| 69 | + |
| 70 | + asciiResult.setPrefRowCount(128); |
| 71 | + asciiResult.setFont(Font.font(java.awt.Font.MONOSPACED, 12)); |
| 72 | + |
| 73 | + asciiCharField.setText("*"); |
| 74 | + fontSizeField.setText("14"); |
| 75 | + fontNameComboBox.getItems().setAll(javafx.scene.text.Font.getFamilies()); |
| 76 | + fontNameComboBox.setValue("Arial"); |
| 77 | + fontStyleComboBox.getItems().setAll("PLAIN", "BOLD", "ITALIC"); |
| 78 | + fontStyleComboBox.setValue("PLAIN"); |
| 79 | + } |
| 80 | + |
| 81 | + @FXML |
| 82 | + private void handleKeyMatch(final KeyEvent keyEvent) { |
| 83 | + doConvert(); |
| 84 | + } |
| 85 | + |
| 86 | + @FXML |
| 87 | + private void handleConvert(final ActionEvent actionEvent) { |
| 88 | + doConvert(); |
| 89 | + } |
| 90 | + |
| 91 | + private void doConvert() { |
| 92 | + resetBorders(); |
| 93 | + asciiResult.setText(""); |
| 94 | + if (validate()) { |
| 95 | + int height = Integer.parseInt(fontSizeField.getText()); |
| 96 | + int style = AsciiService.getStyleByName(fontStyleComboBox.getSelectionModel().getSelectedItem()); |
| 97 | + String converted = AsciiService.convert(asciiField.getText(), asciiCharField.getText(), |
| 98 | + height, style, fontNameComboBox.getSelectionModel().getSelectedItem()); |
| 99 | + asciiResult.setText(converted); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @FXML |
| 104 | + private void handleClear(final ActionEvent actionEvent) { |
| 105 | + asciiField.setText(""); |
| 106 | + asciiResult.setText(""); |
| 107 | + } |
| 108 | + |
| 109 | + private boolean validate() { |
| 110 | + if (asciiCharField.getText().length() != 1) { |
| 111 | + asciiCharField.setBorder(Elements.alertBorder); |
| 112 | + return false; |
| 113 | + } else if (!StringUtils.isNumeric(fontSizeField.getText()) || |
| 114 | + Integer.parseInt(fontSizeField.getText()) > 128) { |
| 115 | + fontSizeField.setBorder(Elements.alertBorder); |
| 116 | + return false; |
| 117 | + } else return !asciiField.getText().isEmpty(); |
| 118 | + } |
| 119 | + |
| 120 | + private void resetBorders() { |
| 121 | + asciiCharField.setBorder(Border.EMPTY); |
| 122 | + fontSizeField.setBorder(Border.EMPTY); |
| 123 | + } |
| 124 | +} |
0 commit comments