PageRenderTime 80ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/util/CustomUtil.java

https://gitlab.com/CreaTIKZ-groupe4/CreaTIKZ
Java | 131 lines | 75 code | 13 blank | 43 comment | 9 complexity | 655b6415047177d7c8efd2ac5141f03d MD5 | raw file
  1. package util;
  2. import javafx.stage.DirectoryChooser;
  3. import javafx.stage.Stage;
  4. import model.services.Server;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardCopyOption;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. /**
  13. * Created by J on 26/03/2016.
  14. */
  15. public class CustomUtil {
  16. public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  17. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  18. /**
  19. * Selects a directory
  20. * @param initDirectoryPath the given directory
  21. * @param primaryStage JavaFx's primary stage
  22. */
  23. public static File selectDirectory(String initDirectoryPath, Stage primaryStage) {
  24. File defaultDirectory = new File(initDirectoryPath);
  25. DirectoryChooser rootDirectoryChooser = new DirectoryChooser();
  26. rootDirectoryChooser.setInitialDirectory(defaultDirectory);
  27. rootDirectoryChooser.setTitle("Select folder");
  28. File selectedDirectory = rootDirectoryChooser.showDialog(primaryStage);
  29. return selectedDirectory;
  30. }
  31. /**
  32. * Creates the user's directory based on his id
  33. */
  34. public static void createUserRep() throws IOException {
  35. if (Server.getActiveUser() != null) {
  36. Files.createDirectories(Paths.get(Server.getActiveUser().getPersonalDir()));
  37. }
  38. }
  39. /**
  40. * Creates the default directory for the Creatikz projects
  41. */
  42. public static void createDefaultRep() throws IOException {
  43. Files.createDirectories(Paths.get(System.getProperty("user.home") + "/CreaTIKZ/Default"));
  44. Files.createDirectories(Paths.get(Server.getActiveUser().getPersonalDir()));
  45. }
  46. /**
  47. * Finds the current working directory
  48. * @return the current user's working directory
  49. */
  50. public static String getWorkingDirPath() {
  51. return Server.getActiveUser() != null ? Server.getActiveUser().getPersonalDir()
  52. : System.getProperty("user.home") + "/CreaTIKZ/Default";
  53. }
  54. /**
  55. * Does an email verification
  56. * @param email The email to check
  57. * @return the boolean state for the validity of the email
  58. */
  59. public static Boolean isValidEmail(String email) {
  60. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher(email);
  61. return matcher.find();
  62. }
  63. /**
  64. * Deletes a given file or folder
  65. * @param source the file/folder to delete
  66. */
  67. public static void deleteFolderOrFile(File source) {
  68. try {
  69. if (source.isDirectory()) {
  70. for (File file : source.listFiles()) {
  71. deleteFolderOrFile(file);
  72. }
  73. }
  74. Files.deleteIfExists(Paths.get(source.getPath()));
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. /**
  80. * Copies a given file to a given destination
  81. * @param source The file to copy
  82. * @param destination The destination where to Copy the file
  83. *
  84. */
  85. public static void recursiveCopy(File source, File destination) throws IOException {
  86. Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING,
  87. StandardCopyOption.COPY_ATTRIBUTES);
  88. if (source.isDirectory()) {
  89. for (File file : source.listFiles()) {
  90. recursiveCopy(file, destination.toPath().resolve(file.getName()).toFile());
  91. }
  92. }
  93. }
  94. /**
  95. * Gets the minimum of an array
  96. * @param values
  97. * @return
  98. */
  99. public static Double getMin(Double... values){
  100. double min = Double.MAX_VALUE;
  101. for (Double value : values) {
  102. min = Math.min(min, value);
  103. }
  104. return min;
  105. }
  106. /**
  107. * Gets the maximum value of an array
  108. * @param values
  109. * @return
  110. */
  111. public static Double getMax(Double... values){
  112. double max = 0;
  113. for (Double value : values) {
  114. max = Math.max(max, value);
  115. }
  116. return max;
  117. }
  118. }