|
@@ -0,0 +1,182 @@
|
|
|
+package ui.view;
|
|
|
+
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.awt.Font;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLClassLoader;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Scanner;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JFileChooser;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JMenu;
|
|
|
+import javax.swing.JMenuBar;
|
|
|
+import javax.swing.JMenuItem;
|
|
|
+import javax.swing.JPanel;
|
|
|
+import javax.swing.JTextArea;
|
|
|
+import javax.swing.JTextPane;
|
|
|
+import javax.swing.text.SimpleAttributeSet;
|
|
|
+import javax.swing.text.StyleConstants;
|
|
|
+import javax.swing.text.StyledDocument;
|
|
|
+import javax.tools.JavaCompiler;
|
|
|
+import javax.tools.ToolProvider;
|
|
|
+
|
|
|
+import api.Algorithm;
|
|
|
+import ui.controller.Control;
|
|
|
+
|
|
|
+
|
|
|
+public class AlgoWindow extends JFrame{
|
|
|
+ private File root;
|
|
|
+ private Algorithm actual;
|
|
|
+ private Control control;
|
|
|
+ private JPanel content = new JPanel();
|
|
|
+ AlgoWindow(JFrame parentFrame, Control control){
|
|
|
+ this.control = control;
|
|
|
+ this.setTitle("Algorithm");
|
|
|
+ this.setVisible(true);
|
|
|
+ this.setContentPane(content);
|
|
|
+ this.setIconImage(Util.loadImage("/Images/Holeg.png", 30, 30));
|
|
|
+ initMenuBar();
|
|
|
+ initDefaultContentPanel();
|
|
|
+ this.pack();
|
|
|
+ this.setLocationRelativeTo(parentFrame);
|
|
|
+ }
|
|
|
+ private void initMenuBar() {
|
|
|
+ JMenuBar menuBar = new JMenuBar();
|
|
|
+ JMenu menuSelect = new JMenu("File");
|
|
|
+ JMenuItem menuItemFolder= new JMenuItem("Open Folder..");
|
|
|
+
|
|
|
+
|
|
|
+ JMenuItem menuItemAlgorithm= new JMenuItem("Open .java-File..");
|
|
|
+ menuItemAlgorithm.addActionListener(actionEvent -> {
|
|
|
+ System.out.println("Test");
|
|
|
+ JFileChooser fileChooser = new JFileChooser();
|
|
|
+ fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
|
|
+ fileChooser.setAcceptAllFileFilterUsed(false);
|
|
|
+ int result = fileChooser.showOpenDialog(this);
|
|
|
+ if(result == JFileChooser.APPROVE_OPTION) {
|
|
|
+
|
|
|
+ File founded = fileChooser.getSelectedFile();
|
|
|
+
|
|
|
+ String name = founded.getName().substring(0, founded.getName().lastIndexOf("."));
|
|
|
+
|
|
|
+ String packageName = "";
|
|
|
+ Scanner in = null;
|
|
|
+ try {
|
|
|
+ in = new Scanner(founded);
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ System.out.println("File not Found");
|
|
|
+ }
|
|
|
+ while(in.hasNext()) {
|
|
|
+ String line = in.nextLine().trim();
|
|
|
+ if(line.startsWith("package")) {
|
|
|
+ packageName = line.substring(8, line.length()-1);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String path = founded.getParentFile().isDirectory() ? founded.getParentFile().getAbsolutePath() : "";
|
|
|
+ File folder = founded.getParentFile();
|
|
|
+ System.out.println(founded.getName() + ", Name:" + name + ", Package:" + packageName + ", Path:" + path);
|
|
|
+
|
|
|
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
+ if (ToolProvider.getSystemJavaCompiler() == null) {
|
|
|
+ System.out.println("MissingCompiler");
|
|
|
+ }else {
|
|
|
+ compiler.run(null, null, null, founded.getAbsolutePath());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { folder.toURI().toURL() });
|
|
|
+ Class<?> cls = Class.forName(packageName.isEmpty()?name:packageName + "." +name, true, classLoader);
|
|
|
+ Object object = cls.newInstance();
|
|
|
+ if(!(object instanceof Algorithm)) {
|
|
|
+ System.out.println("Class does not implement CpsAlgorithm!");
|
|
|
+ }else {
|
|
|
+ actual = (Algorithm)object;
|
|
|
+ actual.setController(control);
|
|
|
+ this.setContentPane(actual.getAlgorithmPanel());
|
|
|
+ this.pack();
|
|
|
+ }
|
|
|
+ } catch (MalformedURLException e) {
|
|
|
+ System.out.println("URLClassLoader error");
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ System.out.println("ClassNotFound error" + e.getMessage());
|
|
|
+ } catch (InstantiationException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+ menuSelect.add(menuItemAlgorithm);
|
|
|
+ menuItemFolder.addActionListener(actionEvent->openFolder(menuSelect));
|
|
|
+ menuSelect.add(menuItemFolder);
|
|
|
+ menuSelect.addSeparator();
|
|
|
+ JMenu menuHelp = new JMenu("Help");
|
|
|
+ menuBar.add(menuSelect);
|
|
|
+ menuBar.add(menuHelp);
|
|
|
+ this.setJMenuBar(menuBar);
|
|
|
+ }
|
|
|
+ private void initDefaultContentPanel() {
|
|
|
+ content.setLayout(new BorderLayout());
|
|
|
+ JTextPane textPane = new JTextPane();
|
|
|
+ SimpleAttributeSet attribs = new SimpleAttributeSet();
|
|
|
+ StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_JUSTIFIED);
|
|
|
+ textPane.setParagraphAttributes(attribs, true);
|
|
|
+ textPane.setText("No algorithm is loaded."
|
|
|
+ +"\n OPTION[1]:"
|
|
|
+ +"\n• Select '.java'-file via the file browser. (File\u2192Open .jave-File..)"
|
|
|
+ +"\n"
|
|
|
+ +"\n OPTION[2]: CURRENTLY NOT WORKING"
|
|
|
+ +"\n• First select the folder where the algorithm '.java'-file is located. (File\u2192Open Folder..)"
|
|
|
+ +"\n• Second load the algorithm. (File\u2192'YourAlgo')");
|
|
|
+ textPane.setFont(new Font("Serif", Font.PLAIN, 16));
|
|
|
+
|
|
|
+
|
|
|
+ content.add(textPane, BorderLayout.CENTER);
|
|
|
+
|
|
|
+ content.setPreferredSize(new Dimension(500, 500));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void openFolder(JMenu menuSelect) {
|
|
|
+ System.out.println("OpenFolder");
|
|
|
+ JFileChooser fileChooser = new JFileChooser();
|
|
|
+ fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
|
|
|
+ fileChooser.setAcceptAllFileFilterUsed(false);
|
|
|
+ int result = fileChooser.showOpenDialog(this);
|
|
|
+ if(result == JFileChooser.APPROVE_OPTION) {
|
|
|
+ System.out.println("Richtig" + menuSelect.getItemCount());
|
|
|
+ System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
|
|
|
+ root = fileChooser.getSelectedFile();
|
|
|
+ File[] files = fileChooser.getSelectedFile().listFiles(file -> file.getName().endsWith(".java"));
|
|
|
+
|
|
|
+
|
|
|
+ for(int i= menuSelect.getItemCount()-1; i> 2; i--) {
|
|
|
+ menuSelect.remove(i);
|
|
|
+ }
|
|
|
+ for(File file: files) {
|
|
|
+ menuSelect.add(new JMenuItem(file.getName().substring(0, file.getName().lastIndexOf("."))));
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ System.out.println("Falsch");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|