123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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;
- //TODO delete old class AlgorithmMenu and change this class to AlgorithmMenu;
- 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) {
- //Found a file
- File founded = fileChooser.getSelectedFile();
- //Get Class name:
- String name = founded.getName().substring(0, founded.getName().lastIndexOf("."));
- //get Package name
- 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;
- }
- }
- //GetPathName
- String path = founded.getParentFile().isDirectory() ? founded.getParentFile().getAbsolutePath() : "";
- File folder = founded.getParentFile();
- System.out.println(founded.getName() + ", Name:" + name + ", Package:" + packageName + ", Path:" + path);
- // Compile source file.
- 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) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }else {
- //Not
- }
-
-
- });
- 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.add(new JButton("Run"),BorderLayout.PAGE_END );
- 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"));
-
- //Remove all Index from old Folder
- 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("."))));
- }
- //System.out.println(Arrays.asList(files).stream().map(Object::toString).collect(Collectors.joining(", ")));
- }else {
- System.out.println("Falsch");
- }
-
- //
- }
- }
|