/src/main/java/lk/vivoxalabs/scenemanager/tools/FileLoader.java

https://github.com/Oshan96/CustomStage · Java · 191 lines · 90 code · 24 blank · 77 comment · 7 complexity · fcee1bf5ec4aef5a7b099ad26fa37e73 MD5 · raw file

  1. package lk.vivoxalabs.scenemanager.tools;
  2. import javafx.fxml.FXMLLoader;
  3. import lk.vivoxalabs.scenemanager.SceneManager;
  4. import java.io.File;
  5. import java.io.FilenameFilter;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * This class is used to load all the files in the given package as to the given extension.
  12. *
  13. * @author oshan
  14. * @version 1.0
  15. */
  16. public class FileLoader {
  17. private SceneManager manager;
  18. private String dir="", ext;
  19. /**
  20. * <p>
  21. * IMPORTANT : <b>
  22. * This DOES NOT GUARANTEE to be executed the way expected and should be avoided unless in testing.
  23. * Use another constructor instead.
  24. * </b>
  25. * Calling this constructor will create a FileLoader object which can load the type of files of the given extension (ext)
  26. * from the same package of the class the constructor was called.
  27. * Ex : If the constructor was called from "Foo.class", this will create a FileLoader which is able to load the specific
  28. * files from the same directory "Foo.class" file is located.
  29. * </p>
  30. *
  31. * <p>
  32. * <b>NOTE :</b> This constructor is specifically created to be used with SceneManager. If used separately, this will not
  33. * work properly and will be unable to load the files. (Since this uses getStackTrace()[3] argument and if called directly,
  34. * it should probably be changed to getStackTrace()[1]).
  35. * </p>
  36. *
  37. * @param ext extension of the type of files needed to be loaded
  38. */
  39. public FileLoader(String ext) {
  40. this.ext = ext;
  41. try {
  42. // Class clazz = sun.reflect.Reflection.getCallerClass(2);
  43. Class clazz = Class.forName(new Throwable().getStackTrace()[3].getClassName());
  44. dir = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath()).toString().replace("%20"," ");
  45. } catch (ClassNotFoundException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. /**
  50. * <p>
  51. * This is same as {@link #FileLoader(String ext)}. The only difference is that this constructor will initialize the
  52. * SceneManager property which can be used later (through the given SceneManager object)
  53. *</p>
  54. *
  55. * @param ext extension of the type of files needed to be loaded
  56. * @param manager SceneManager object which is going to access this FileLoader object
  57. */
  58. public FileLoader(String ext, SceneManager manager){
  59. this(ext);
  60. this.manager=manager;
  61. }
  62. /**
  63. * <p>
  64. * This is used to make the FileLoader eligible to load the given type of files from the directory of the URL provides.
  65. * Ex : {@code new FileLoader(getClass().getResource("/path/to/file/myfile.fxml")),"fxml")}
  66. * Will make the FileLoader eligible to load file with ".fxml" extension from the "/path/to/file" directory.
  67. * </p>
  68. *
  69. * @param url URL of a class to determine the package which needs to be lookup for files to load
  70. * @param ext extension of the type of files needed to be loaded
  71. */
  72. public FileLoader(URL url, String ext){
  73. String[] content = url.toString().replace("file:/","").replace("%20"," ").split("/");
  74. for(int i=0; i<content.length-1;i++){
  75. dir+=content[i];
  76. if(i<content.length-2){
  77. dir+=File.separator;
  78. }
  79. }
  80. this.ext = ext;
  81. }
  82. /**
  83. * <p>
  84. * This is same as {@link #FileLoader(URL, String)}. The only difference is that this constructor will initialize the
  85. * SceneManager property which can be used later (through the given SceneManager object)
  86. * </p>
  87. *
  88. * @param url URL of a class to determine the package which needs to be lookup for files to load
  89. * @param ext extension of the type of files needed to be loaded
  90. * @param manager SceneManager object which is going to access this FileLoader object
  91. */
  92. public FileLoader(URL url, String ext, SceneManager manager){
  93. this(url, ext);
  94. this.manager = manager;
  95. }
  96. /**
  97. * <p>
  98. * This is used to make the FileLoader eligible to load the given type of files from the directory given in String format.
  99. * </p>
  100. *
  101. * @param dir directory path
  102. * @param ext extension of the type of files needed to be loaded
  103. */
  104. public FileLoader(String dir, String ext) {
  105. this.dir=dir;
  106. this.ext=ext;
  107. }
  108. /**
  109. * See {@link #FileLoader(String, String, SceneManager)}
  110. *
  111. * @param dir directory path
  112. * @param ext extension of the type of files needed to be loaded
  113. * @param manager SceneManager object which is going to access this FileLoader object
  114. */
  115. public FileLoader(String dir, String ext, SceneManager manager){
  116. this(dir, ext);
  117. this.manager=manager;
  118. }
  119. /**
  120. * Collects the set of files with the given extension and inside the given directory for the FileLoader.
  121. *
  122. * @return the loaded available files.
  123. */
  124. public List<File> collect(){
  125. List<File> files = new ArrayList<>();
  126. String list[];
  127. File folder = new File(dir);
  128. if(!folder.isDirectory()){
  129. System.out.println("Not a directory");
  130. return files;
  131. }
  132. if(ext.equalsIgnoreCase("fxml")) {
  133. list = folder.list(new FXMLExtensionFilter());
  134. }else{
  135. list = folder.list(new ExtensionFilter());
  136. }
  137. for(String name : list){
  138. files.add(new File(dir + File.separator + name));
  139. }
  140. return files;
  141. }
  142. private class FXMLExtensionFilter implements FilenameFilter {
  143. @Override
  144. public boolean accept(File dir, String name) {
  145. if(name.endsWith(ext)) {
  146. try {
  147. if(manager!=null) {
  148. FXMLLoader loader = new FXMLLoader(new File(dir.toString().concat(File.separator).concat(name)).toURI().toURL());
  149. manager.addScene(name.replace("." + ext, ""), loader.load(), loader.getController());
  150. }
  151. } catch (MalformedURLException e) {
  152. e.printStackTrace();
  153. }catch (Exception e){
  154. // e.printStackTrace();
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. }
  161. private class ExtensionFilter implements FilenameFilter{
  162. @Override
  163. public boolean accept(File dir, String name) {
  164. return name.endsWith(ext);
  165. }
  166. }
  167. }