Browse Source

Clean Up 5!

Tom Troppmann 5 years ago
parent
commit
a359024dba

+ 2 - 3
src/exampleAlgorithms/RandomSwitch.java

@@ -7,11 +7,9 @@ import java.util.Hashtable;
 import javax.swing.BorderFactory;
 import javax.swing.BoxLayout;
 import javax.swing.JButton;
-import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSlider;
-import javax.swing.JTextField;
 
 import api.Algorithm;
 import classes.HolonSwitch;
@@ -31,6 +29,7 @@ public class RandomSwitch implements Algorithm {
 //        newFrame.pack();
 //        newFrame.setVisible(true);
 //    }
+	
 	public RandomSwitch(){
 		content.setLayout(new BorderLayout());
 		content.add(createParameterPanel(), BorderLayout.CENTER);
@@ -61,7 +60,6 @@ public class RandomSwitch implements Algorithm {
 		return flipChance;
 	}
 	private void run() {
-		System.out.println("run");
 		for (HolonSwitch s : control.getModel().getSwitches()) {
 			// Set to Manual Mode
 			s.setManualMode(true);
@@ -72,6 +70,7 @@ public class RandomSwitch implements Algorithm {
 			} 
 		}
 		control.calculateStateForCurrentTimeStep();
+		control.updateCanvas();
 	}
 	@Override
 	public JPanel getAlgorithmPanel() {

+ 3 - 1
src/ui/controller/Control.java

@@ -1001,7 +1001,9 @@ public class Control {
 	public void updateOutliner() {
 		gui.updateOutliners(simulationManager.getActualDecorState());
 	}
-	
+	public void updateCanvas() {
+		gui.repaintCanvas();
+	}
 	public GUI getGui() {
 		return gui;
 	}

+ 2 - 0
src/ui/view/AlgoWindow.java

@@ -92,6 +92,7 @@ public class AlgoWindow extends JFrame{
 	 */
 	private void openJavaFile() {
 		JFileChooser fileChooser = new JFileChooser();
+		fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")+"/src/"));
 		fileChooser.setFileFilter(new FileNameExtensionFilter("JAVA Source Files", "java"));
 		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 		fileChooser.setAcceptAllFileFilterUsed(false);
@@ -104,6 +105,7 @@ public class AlgoWindow extends JFrame{
 	
 	private void openFolder(JMenu menuSelect) {
 		JFileChooser fileChooser = new JFileChooser();
+		fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")+"/src/"));
 		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 		fileChooser.setAcceptAllFileFilterUsed(false);
 		int result = fileChooser.showOpenDialog(this);

+ 0 - 189
src/ui/view/AlgorithmMenu.java

@@ -1,189 +0,0 @@
-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);
-			}
-		}
-	}
-}

+ 4 - 7
src/ui/view/GUI.java

@@ -69,7 +69,6 @@ public class GUI implements CategoryListener {
 	private final JMenu mnNewMenuOptions = new JMenu("Options");
 	private final JMenu mnNewMenuView = new JMenu("View");
 	private final JMenu menuWindow = new JMenu("Window");
-	private final AlgorithmMenu algorithmMenu;
 	
 	/** Help Menu containing helpful Informations and the AboutUs Popup */
 	private final JMenu mnHelp = new JMenu("Help");
@@ -326,7 +325,6 @@ public class GUI implements CategoryListener {
 
 		control.initListener(this);
 		model.setTableProperties(tableProperties);
-		algorithmMenu = new AlgorithmMenu(model, control, this);
 		initialize();
 		updateCategories(model.getCategories());
 		updCon = new UpdateController(model, controller);
@@ -985,7 +983,6 @@ public class GUI implements CategoryListener {
 
 		splitPane_1.setLeftComponent(lblHolonBodySize);
 
-		menuBar.add(algorithmMenu);
 
 		/**
 		 * add Help Menu and its items
@@ -2433,7 +2430,6 @@ public class GUI implements CategoryListener {
 		splitGraphHolonEl.setTopComponent(scrollElements);
 		splitGraphHolonEl.setBottomComponent(scrollGraph);
 		canvasSP.setViewportView(canvas);
-		algorithmMenu.setBackground(new Color(240, 240, 240));
 
 		tabbedPaneOriginal.setBorder(null);
 		scrollProperties.setBorder(null);
@@ -2504,7 +2500,6 @@ public class GUI implements CategoryListener {
 		//Algo
 		JMenuItem openMenu =  new JMenuItem("Open Algorithm Panel", new ImageIcon(Util.loadImage("/Button_Images/iconAlgo.png").getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
 		openMenu.addActionListener(actionEvent -> {
-			System.out.println("sdfsdf");
 			new AlgoWindow(frmCyberPhysical, controller);
 		});
 		openMenu.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
@@ -2719,8 +2714,6 @@ public class GUI implements CategoryListener {
 		saveBeforeNew = tempArray[28];
 		eraseCategory = tempArray[29];
 		selectObjBeforeErase = tempArray[30];
-		// SimMenu
-		algorithmMenu.algoFolderButton.setText(Languages.getLanguage()[85]);
 		// TimePanel
 		timePanel.playBtn.setToolTipText(Languages.getLanguage()[89]);
 		timePanel.timeResetBtn.setToolTipText(Languages.getLanguage()[90]);
@@ -3153,4 +3146,8 @@ public class GUI implements CategoryListener {
 			}
 		}
 	}
+	
+	public void repaintCanvas() {
+		tabbedPaneInnerOriginal.repaint();
+	}
 }