package ui.view; import algorithms.geneticAlgorithm.holegGA.GenAlgoInit; import algorithms.geneticAlgorithm.holegGA.GenAlgoWindow; 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; GenAlgoWindow genAlgoWindow; JMenuItem geneticAlgoBtn = new JMenuItem("Build Network"); //opens the window for gen algo JMenuItem evaluateAlgoBtn = new JMenuItem("Evaluate Algorithm"); //evaluates the gen algo 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 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); } } } } }); geneticAlgoBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { GenAlgoInit algoInit = new GenAlgoInit(controller, model); algoInit.start(); } }); evaluateAlgoBtn.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { GenAlgoInit algoInit = new GenAlgoInit(controller, model); algoInit.evaluate(); } }); // Add to Panel this.add(geneticAlgoBtn); this.add(evaluateAlgoBtn); 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); } } } }