Skip to content

Instantly share code, notes, and snippets.

@uvlad7
Last active March 18, 2019 17:09
Show Gist options
  • Save uvlad7/7dcf0458c5eaf9bf6a19bcf341a4e392 to your computer and use it in GitHub Desktop.
Save uvlad7/7dcf0458c5eaf9bf6a19bcf341a4e392 to your computer and use it in GitHub Desktop.
УП 4.1
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import java.io.FileInputStream;
import java.io.IOException;
public class Main extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
GridPane root = new GridPane();
//root.setGridLinesVisible(true);
root.setPrefWidth(800);
root.setPrefHeight(120);
ColumnConstraints columnConstraints1 = new ColumnConstraints();
columnConstraints1.setPercentWidth(60);
ColumnConstraints columnConstraints2 = new ColumnConstraints();
columnConstraints2.setPercentWidth(25);
ColumnConstraints columnConstraints3 = new ColumnConstraints();
columnConstraints3.setPercentWidth(15);
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setPercentHeight(100);
rowConstraints.setValignment(VPos.TOP);
root.getColumnConstraints().addAll(columnConstraints1, columnConstraints2, columnConstraints3);
root.getRowConstraints().addAll(rowConstraints);
TextArea textArea = new TextArea();
textArea.setWrapText(true);
root.add(textArea, 0, 0);
ComboBox<String> comboBox = new ComboBox<>(FXCollections.observableArrayList("Натуральное число", "Целое число", "Число с плавающей запятой", "Дата", "Время", "E-mail"));
comboBox.setPrefWidth(Double.MAX_VALUE);
TitledPane titledPane = new TitledPane("Type", comboBox);
titledPane.setCollapsible(false);
root.add(titledPane, 1, 0);
Circle circle = new Circle();
circle.setFill(Color.GREEN);
BorderPane pane = new BorderPane(circle);
pane.widthProperty().addListener(observable -> circle.setRadius(pane.getWidth() / 2.5));
root.add(pane, 2, 0);
textArea.textProperty().addListener((observableValue, s, t1) -> circle.setFill(Verifier.verify(t1, comboBox.getSelectionModel().getSelectedIndex()) ? Color.GREEN : Color.RED));
comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> circle.setFill(Verifier.verify(textArea.getText(), comboBox.getSelectionModel().getSelectedIndex()) ? Color.GREEN : Color.RED));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Type verification");
try (FileInputStream icon = new FileInputStream("src\\regexp.png")) {
stage.getIcons().add(new Image(icon));
} catch (IOException e) {
}
stage.sizeToScene();
stage.show();
}
}
import java.util.regex.Pattern;
public class Verifier {
private static Pattern naturalPattern = Pattern.compile("^[1-9]\\d*$");
private static Pattern intPattern = Pattern.compile("^(?:(?:[-‐−‒–—]?[1-9]\\d*)|0)$");
private static Pattern doublePattern = Pattern.compile("^(?:0|(?:[-‐−‒–—]?(?:(?:[1-9]\\d*(?:[.,]\\d*[1-9])?)|(?:0[,.]\\d*[1-9]))(?:[eе](?:[-‐−‒–—]?[1-9]\\d*))?))$");
private static Pattern datePattern = Pattern.compile("^(?:(?:(?:(?:(?!0000)\\d{4})-" +
"(?:(?:(?:(?:0[1-9])|(?:1[0-2]))[-‐−‒–—](?:(?:0[1-9])|(?:1\\d)|(?:2[0-8])))|(?:(?:(?:0[13-9])|(?:1[0-2]))[-‐−‒–—](?:(?:29)|(?:30)))|(?:(?:(?:0[13578])|(?:1[02]))[-‐−‒–—]31)))" +
"|(?:(?:(?:(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))00)|(?:\\d{2}(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))))[-‐−‒–—]02[-‐−‒–—]29)" +
"|(?:(?:(?:(?:(?:0[1-9])|(?:1\\d)|(?:2[0-8]))\\.(?:(?:0[1-9])|(?:1[0-2])))|(?:(?:(?:29)|(?:30))\\.(?:(?:0[13-9])|(?:1[0-2])))|(?:31\\.(?:(?:0[13578])|(?:1[02]))))" +
"\\.(?:(?!0000)\\d{4}))" +
"|(?:(?:(?:(?:(?:0?[1-9])|(?:1[0-2]))/(?:(?:0?[1-9])|(?:1\\d)|(?:2[0-8])))|(?:(?:(?:0?[13-9])|(?:1[0-2]))/(?:(?:29)|(?:30)))|(?:(?:(?:0?[13578])|(?:1[02]))/31))" +
"/(?:(?!0000)\\d{4}))" +
"|(?:(?:(?:29\\.02\\.)|(?:0?2/29/))(?:(?:(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))00)|(?:\\d{2}(?:(?:[13579][26])|(?:[2468][048])|(?:0[48]))))))" +
"(?: ?(?:(?:[BA]C)|(?:(?:до )?(?:(?:н\\. ?э\\.)|(?:нашей эры)))))?)$");
private static Pattern timePattern = Pattern.compile("^(?:(?:(?:[1-9]|(?:1[0-2]))[.:][0-5]\\d ?(?:(?:[ap]\\. ?m\\.)|(?:[AP]M)))" +
"|(?:(?:(?:[01]\\d)|(?:2[0-3]))[.:][0-5]\\d))$");
private static Pattern emailPattern = Pattern.compile("^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[a-z0-9](?:[-a-z0-9]{0,61}[a-z0-9])?\\.)+" +
"(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$");
public static boolean verify(String text, int index) {
if (text.length() == 0)
return true;
switch (index) {
case 0:
return naturalVerify(text);
case 1:
return intVerify(text);
case 2:
return doubleVerify(text);
case 3:
return dateVerify(text);
case 4:
return timeVerify(text);
case 5:
return emailVerify(text);
default:
return true;
}
}
private static boolean naturalVerify(String text) {
return naturalPattern.matcher(text).matches();
}
private static boolean intVerify(String text) {
return intPattern.matcher(text).matches();
}
private static boolean doubleVerify(String text) {
return doublePattern.matcher(text).matches();
}
private static boolean dateVerify(String text) {
return datePattern.matcher(text).matches();
}
private static boolean timeVerify(String text) {
return timePattern.matcher(text).matches();
}
private static boolean emailVerify(String text) {
return emailPattern.matcher(text).matches();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment