ImportPopUp.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.awt.Container;
  3. import java.io.File;
  4. import javax.swing.JFileChooser;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ClassImportException;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.JavaFileFilter;
  8. /**
  9. * Import PopUp which allows selection of a java File which should be compiled and be a subclass of T. Throws ClassImportExceptions on failures, and returns null if importing was aborted.
  10. *
  11. * @author Andreas T. Meyer-Berg
  12. * @param <T> Interface/Class which should be superclass of imported
  13. */
  14. public class ImportPopUp<T> {
  15. private Container panel;
  16. private Class<T> classType;
  17. /**
  18. * Creates ImportPopUp
  19. * @param panel position relative to this panel
  20. * @param classType superclass/interface of the imported class
  21. */
  22. public ImportPopUp (Container panel, Class<T> classType) {
  23. this.panel = panel;
  24. this.classType = classType;
  25. }
  26. /**
  27. * Creates PopUp to import an java File, returns the class of this file
  28. *
  29. * @return Class of imported File, which is subclass of T, null on abort
  30. * @throws ClassImportException on Errors
  31. */
  32. public Class<? extends T> showPopUp() throws ClassImportException{
  33. JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
  34. fc.setFileFilter(new JavaFileFilter());
  35. int returnVal = fc.showOpenDialog(panel);
  36. if (returnVal == JFileChooser.APPROVE_OPTION) {
  37. /**
  38. * selected File
  39. */
  40. File file = fc.getSelectedFile();
  41. String[] parts = file.getName().split("[.]");
  42. if(parts.length <= 1)
  43. throw new ClassImportException("Invalid fileName: "+file.getName()+" missing Name.java Format");
  44. /**
  45. * Check the file type ending
  46. */
  47. String ending = parts[parts.length-1].toLowerCase();
  48. if(ending.equals("java")){
  49. // Compile source file.
  50. /**
  51. * Imported Java Class
  52. */
  53. Class<?> imported = null;
  54. imported = ImportController.importJavaClass(file);
  55. if(imported == null)
  56. throw new ClassImportException("Importing javaClass failed");
  57. try {
  58. /**
  59. * If valid subclass
  60. */
  61. return imported.asSubclass(classType);
  62. } catch (ClassCastException e) {
  63. throw new ClassImportException("Class does not implement "+classType.getSimpleName());
  64. }
  65. }else{
  66. /**
  67. * Invalid file name
  68. */
  69. throw new ClassImportException("Invalid fileEnding: \""+ending+"\" instead of \"java\"");
  70. }
  71. } else {
  72. //Importing Cancelled by user
  73. return null;
  74. }
  75. }
  76. }