123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package ui.view;
- import api.CpsAlgorithm;
- import ui.controller.Control;
- import ui.model.Model;
- import javax.swing.*;
- import javax.tools.JavaCompiler;
- import javax.tools.ToolProvider;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.HashMap;
- /**
- * This Class represents the Menu, where you can select the Algorithms.
- *
- * @author Gruppe14
- */
- public class AlgorithmMenu extends JMenu {
- private static final long serialVersionUID = 1L;
- JMenuItem algoFolderButton = new JMenuItem("Select Algorithm Folder");
- // root Directory
- File root = null;
- Model model;
- Control controller;
- private JMenu mnSelectAlgorithm = new JMenu("Select Algorithm");
- private HashMap<String, File> algosHash = new HashMap<>();
- private JMenuItem noneItem = new JMenuItem("none");
- /**
- * Constructor.
- *
- * @param mod
- * the Model
- * @param cont
- * the Controller
- */
- public AlgorithmMenu(Model mod, Control cont, GUI parentGui) {
- super();
- // Init Stuff
- this.model = mod;
- this.controller = cont;
- this.setText("Algorithm");
- // algoFolderButton Action
- algoFolderButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- menuSetFolderActionPerformed(evt);
- }
- private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- fileChooser.setAcceptAllFileFilterUsed(false);
- if (fileChooser.showOpenDialog(parentGui.getFrmCyberPhysical()) == JFileChooser.APPROVE_OPTION) {
- // empty everything and reset the selected algorithm
- controller.setAlgorithm(null);
- mnSelectAlgorithm.removeAll();
- mnSelectAlgorithm.add(noneItem);
- noneItem.doClick();
- File[] files = fileChooser.getSelectedFile().listFiles();
- // Set Root Folder Path
- root = new File(fileChooser.getCurrentDirectory().getPath());
- for (int i = 0; i < files.length; i++) {
- if (files[i].toString()
- .endsWith(".java") /*
- * || files[i].toString().
- * endsWith(".class")
- */) {
- String name = files[i].getName();
- int tmpB = name.lastIndexOf('.');
- name = name.substring(0, tmpB);
- algosHash.put(name, files[i]);
- JMenuItem tempItem = new JMenuItem(name);
- tempItem.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
- mnSelectAlgorithm.getItem(i).setForeground(null);
- }
- tempItem.setForeground(Color.BLUE);
- setAlgorithm(algosHash.get(tempItem.getText()), tempItem.getText());
- }
- });
- mnSelectAlgorithm.add(tempItem);
- }
- }
- }
- }
- });
- // Add to Panel
- this.add(algoFolderButton);
- this.add(mnSelectAlgorithm);
- mnSelectAlgorithm.add(noneItem);
- noneItem.setForeground(Color.BLUE);
- noneItem.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
- mnSelectAlgorithm.getItem(i).setForeground(null);
- }
- noneItem.setForeground(Color.BLUE);
- controller.setAlgorithm(null);
- }
- });
- }
- public void setAlgorithm(File file, String name) {
- boolean missingCompiler = false;
- boolean instantiationError = false;
- try {
- BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
- String line = br.readLine();
- // Package Name
- String packageName = "";
- while (line != null) {
- line = line.trim();
- if (!line.isEmpty()) {
- if (line.length() >= 7 && line.substring(0, 7).equals("package")) {
- packageName = line.substring(8, line.length() - 1);
- }
- }
- if (packageName.isEmpty()) {
- line = br.readLine();
- } else {
- line = null;
- }
- }
- // Compile source file.
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- if (ToolProvider.getSystemJavaCompiler() == null) {
- missingCompiler = true;
- }
- compiler.run(null, null, null, file.getPath());
- // Load and instantiate compiled class.
- URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
- instantiationError = true;
- Class<?> cls;
- if (packageName.isEmpty()) {
- cls = Class.forName(name, true, classLoader);
- } else {
- cls = Class.forName(packageName + "." + name, true, classLoader);
- }
- Object t = cls.newInstance();
- if (t instanceof CpsAlgorithm) {
- controller.setAlgorithm(t);
- } else {
- JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
- JOptionPane.ERROR_MESSAGE);
- noneItem.doClick();
- }
- } catch (Exception e) {
- if (missingCompiler) {
- JOptionPane.showMessageDialog(null, "Missing Compiiler! Please install the JDK!", "Error!",
- JOptionPane.ERROR_MESSAGE);
- } else if (instantiationError) {
- JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
- JOptionPane.ERROR_MESSAGE);
- noneItem.doClick();
- } else {
- JOptionPane.showMessageDialog(null, e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
- }
- }
- }
- }
|