Selaa lähdekoodia

Merge branch 'master' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons

dominik.rieder 8 vuotta sitten
vanhempi
commit
527e8491f9

+ 56 - 39
src/ui/controller/Control.java

@@ -53,6 +53,7 @@ public class Control {
 	private final HolonCanvasController holonCanvasController;
 	private String autosaveDir = "";
 	private String categoryDir = "";
+	private int rand;
 
 	/**
 	 * Constructor.
@@ -87,6 +88,7 @@ public class Control {
 		// deleteDirectory(dest);
 		autoSave.mkdirs();
 		category.mkdirs();
+		createAutoRandom();
 		try {
 			autoSave();
 		} catch (IOException e) {
@@ -95,6 +97,17 @@ public class Control {
 		}
 	}
 
+	/**
+	 * Generate random number, so that every instance of the program has unique
+	 * save files
+	 */
+	private void createAutoRandom() {
+		rand = (int) (Math.random() * 1000);
+		while (new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
+			rand = (int) Math.random() * 1000;
+		}
+	}
+
 	/**
 	 * Delete a Directory.
 	 * 
@@ -108,10 +121,11 @@ public class Control {
 				if (files[i].isDirectory()) {
 					deleteDirectory(files[i]);
 				} else {
-					files[i].delete();
+					if(files[i].getName().contains(""+rand))
+							files[i].delete();
 				}
 			}
-			path.delete();
+			//path.delete();
 		}
 	}
 
@@ -167,7 +181,8 @@ public class Control {
 
 	/**
 	 * init default category and objects.
-	 * @throws IOException 
+	 * 
+	 * @throws IOException
 	 */
 	public void resetCategorys() throws IOException {
 		categoryController.initCategories();
@@ -179,7 +194,7 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            name of the new Category
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addCategory(String cat) throws IOException {
 		categoryController.addNewCategory(cat);
@@ -197,7 +212,7 @@ public class Control {
 	 *            Array of Elements
 	 * @param img
 	 *            the image Path
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) throws IOException {
 		categoryController.addNewHolonObject(cat, obj, ele, img);
@@ -223,7 +238,7 @@ public class Control {
 	 *            Category
 	 * @param obj
 	 *            New Object Name
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void addSwitch(Category cat, String obj) throws IOException {
 		categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
@@ -235,7 +250,7 @@ public class Control {
 	 * 
 	 * @param cat
 	 *            the Category
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void deleteCategory(String cat) throws IOException {
 		categoryController.deleteCategory(cat);
@@ -249,7 +264,7 @@ public class Control {
 	 *            the Category
 	 * @param obj
 	 *            the Object
-	 * @throws IOException 
+	 * @throws IOException
 	 */
 	public void delObjectCategory(String cat, String obj) throws IOException {
 		categoryController.deleteObject(cat, obj);
@@ -501,20 +516,21 @@ public class Control {
 	 *            the Path
 	 * @throws IOException
 	 *             exception
-	 * @throws ArchiveException 
+	 * @throws ArchiveException
 	 */
 	public void loadFile(String path) throws IOException, ArchiveException, ZipException {
 		loadController.readSave(path);
 		saveCategory();
 		autoSave();
 	}
-	
+
 	/**
 	 * Reads the Json File from Autosave
+	 * 
 	 * @param path
 	 * @throws IOException
 	 */
-	public void loadAutoSave(String path) throws IOException  {
+	public void loadAutoSave(String path) throws IOException {
 		loadController.readJson(path);
 	}
 
@@ -533,7 +549,7 @@ public class Control {
 	 * current Timestep.
 	 */
 	public void calculateStateForCurrentTimeStep() {
-		//simulationManager.reset();
+		// simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(model.getCurIteration());
 	}
 
@@ -544,15 +560,15 @@ public class Control {
 	 *            current Iteration
 	 */
 	public void calculateStateForTimeStep(int x) {
-		//simulationManager.reset();
+		// simulationManager.reset();
 		simulationManager.calculateStateForTimeStep(x);
 	}
-	
+
 	/**
-	 * resets the whole State of the simulation 
-	 * including a reset of all Edges to the default "is working" state
+	 * resets the whole State of the simulation including a reset of all Edges
+	 * to the default "is working" state
 	 */
-	public void resetSimulation(){
+	public void resetSimulation() {
 		setIsSimRunning(false);
 		simulationManager.resetSimulation();
 	}
@@ -575,12 +591,13 @@ public class Control {
 	 */
 	public void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
-		saveController.writeAutosave(autosaveDir + autoSaveController.getAutoSaveNr());
+		saveController.writeAutosave(autosaveDir + rand + autoSaveController.getAutoSaveNr());
 		if (autoSaveController.allowed()) {
-			new File(autosaveDir + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves())).delete();
+			new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr() - globalController.getNumbersOfSaves()))
+					.delete();
 		}
 	}
-	
+
 	public void saveCategory() throws IOException {
 		saveController.writeCategory(categoryDir + "Category.json");
 	}
@@ -592,10 +609,10 @@ public class Control {
 	 */
 	public String getUndoSave() {
 		autoSaveController.decreaseAutoSaveNr();
-		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.increaseAutoSaveNr();
 		}
-		return autosaveDir + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
 	}
 
 	/**
@@ -605,14 +622,12 @@ public class Control {
 	 */
 	public String getRedoSave() {
 		autoSaveController.increaseAutoSaveNr();
-		if (!new File(autosaveDir + (autoSaveController.getAutoSaveNr())).exists()) {
+		if (!new File(autosaveDir + rand + (autoSaveController.getAutoSaveNr())).exists()) {
 			autoSaveController.decreaseAutoSaveNr();
 		}
-		return autosaveDir + (autoSaveController.getAutoSaveNr());
+		return autosaveDir + rand + (autoSaveController.getAutoSaveNr());
 	}
 
-
-
 	/**
 	 * Getter for Model.
 	 * 
@@ -883,11 +898,12 @@ public class Control {
 	public void copy(CpsUpperNode upperNode) {
 		clipboardController.copy(upperNode);
 	}
-	
-	public void paste(CpsUpperNode upperNode, Point point) throws JsonParseException, UnsupportedFlavorException, IOException {
+
+	public void paste(CpsUpperNode upperNode, Point point)
+			throws JsonParseException, UnsupportedFlavorException, IOException {
 		clipboardController.paste(upperNode, point);
 	}
-	
+
 	public void cut(CpsUpperNode upperNode) {
 		clipboardController.cut(upperNode);
 	}
@@ -905,7 +921,7 @@ public class Control {
 	}
 
 	public int getTotalElements(ArrayList<AbstractCpsObject> arrayList) {
-		return holonCanvasController.getTotalElements(arrayList) ;
+		return holonCanvasController.getTotalElements(arrayList);
 	}
 
 	public int getTotalProducers(ArrayList<AbstractCpsObject> arrayList) {
@@ -915,6 +931,7 @@ public class Control {
 	public int getActiveElements(ArrayList<AbstractCpsObject> arrayList) {
 		return holonCanvasController.getActiveElements(arrayList);
 	}
+
 	/**
 	 * Set the Background Image;
 	 * 
@@ -928,25 +945,25 @@ public class Control {
 	 *            Image custom height
 	 */
 	public void setBackgroundImage(String imagePath, int mode, int width, int height) {
-	canvasController.setBackgroundImage(imagePath, mode, width, height);
+		canvasController.setBackgroundImage(imagePath, mode, width, height);
 	}
-	
-	public void setFlexiblePane(FlexiblePane fp){
+
+	public void setFlexiblePane(FlexiblePane fp) {
 		simulationManager.setFlexiblePane(fp);
 	}
-	
-	public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT){
+
+	public void setGraphTable(Hashtable<String, StatisticGraphPanel> gT) {
 		model.setGraphTable(gT);
 	}
-	
-	public Hashtable<String, StatisticGraphPanel> getGraphTable(){
+
+	public Hashtable<String, StatisticGraphPanel> getGraphTable() {
 		return model.getGraphTable();
 	}
-	
+
 	/**
 	 * Sets if the Simulation is running
 	 */
-	public void setIsSimRunning(boolean isRunning){
+	public void setIsSimRunning(boolean isRunning) {
 		globalController.setIsSimRunning(isRunning);
 	}
 

+ 109 - 0
src/ui/view/CanvasResizePopUp.java

@@ -0,0 +1,109 @@
+package ui.view;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Image;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JDialog;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JTextField;
+
+import ui.controller.Control;
+import ui.model.Model;
+
+public class CanvasResizePopUp extends JDialog {
+
+	private JPanel mainPanel = new JPanel();
+	private JTextField tFieldWidht = new JTextField();
+	private JTextField tFieldHeight = new JTextField();
+	private JLabel lblWidth = new JLabel("Width:");
+	private JLabel lblHeight = new JLabel("Height:");
+
+	private JPanel buttonPanel = new JPanel();
+	private final JButton btnOk = new JButton("OK");
+	private final JButton btnCancel = new JButton("Cancel");
+
+	JTabbedPane tabbedPane;
+	JTabbedPane tabbedPane2;
+	Model model;
+	Control controller;
+	MyCanvas canvas;
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public CanvasResizePopUp(Model model, Control controller, MyCanvas canvas, JTabbedPane tabbedPane,
+			JTabbedPane tabbedPane2) {
+		super((java.awt.Frame) null, true);
+		this.tabbedPane = tabbedPane;
+		this.tabbedPane2 = tabbedPane2;
+		this.model = model;
+		this.controller = controller;
+		this.canvas = canvas;
+
+		// properties and stuff
+		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
+				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
+		this.setTitle("Set the Size of the View");
+		setBounds(200, 100, 200, 100);
+
+		// MainPanel
+		tFieldWidht.setText("" + model.getCanvasX());
+		tFieldHeight.setText("" + model.getCanvasY());
+		mainPanel.add(lblWidth);
+		mainPanel.add(tFieldHeight);
+		mainPanel.add(lblHeight);
+		mainPanel.add(tFieldWidht);
+		mainPanel.setBackground(Color.WHITE);
+
+		// Button Panel
+		btnOk.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				controller.setCanvasX(Integer.parseInt(tFieldWidht.getText()));
+				controller.setCanvasY(Integer.parseInt(tFieldHeight.getText()));
+				canvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
+				for (int i = 4; i < tabbedPane.getTabCount(); i++) {
+					if (tabbedPane.getComponentAt(i) != null) {
+						UpperNodeCanvas unc = ((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i))
+								.getViewport().getComponent(0));
+						unc.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
+						unc.repaint();
+					}
+				}
+				if (tabbedPane2 != null && tabbedPane2.getSelectedIndex() >= 4) {
+					UpperNodeCanvas unc = ((UpperNodeCanvas) ((JScrollPane) tabbedPane2.getSelectedComponent())
+							.getViewport().getComponent(0));
+					unc.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
+					unc.repaint();
+				}
+				canvas.repaint();
+				dispose();
+			}
+		});
+		btnCancel.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				dispose();
+			}
+		});
+		buttonPanel.add(btnOk);
+		buttonPanel.add(btnCancel);
+		buttonPanel.setBackground(Color.WHITE);
+
+		// Add to ContentPane
+		getContentPane().add(mainPanel, BorderLayout.CENTER);
+		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
+	}
+
+}

+ 2 - 11
src/ui/view/GUI.java

@@ -704,16 +704,8 @@ public class GUI<E> implements CategoryListener {
 		mntmCanvasSize.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				JPanel myPanel = new JPanel();
-				JTextField field1 = new JTextField("" + canvas.getWidth() + "");
-				JTextField field2 = new JTextField("" + canvas.getHeight() + "");
-				myPanel.add(field1);
-				myPanel.add(field2);
-				JOptionPane.showMessageDialog(null, myPanel);
-				controller.setCanvasX(Integer.parseInt(field1.getText()));
-				controller.setCanvasY(Integer.parseInt(field2.getText()));
-				canvas.setPreferredSize(new Dimension(model.getCanvasX(), model.getCanvasY()));
-				canvas.repaint();
+				CanvasResizePopUp popUp = new CanvasResizePopUp(model, controller, canvas, tabbedPane, tabbedPane2);
+				popUp.setVisible(true);
 			}
 		});
 
@@ -1986,7 +1978,6 @@ public class GUI<E> implements CategoryListener {
 	 *            path to save-folder
 	 */
 	public void setUpAutoSave(File dest) {
-		controller.deleteDirectory(dest);
 		dest.mkdirs();
 		try {
 			controller.autoSave();

+ 1 - 1
src/ui/view/MyCanvas.java

@@ -457,7 +457,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			}
 		}
 
-		// Test SubNet Coloring
+		//SubNet Coloring
 		int i = 0;
 		for (SubNet s : controller.getSimManager().getSubNets()) {
 

+ 24 - 8
src/ui/view/TimePanel.java

@@ -1,20 +1,24 @@
 package ui.view;
 
 import java.awt.BorderLayout;
+import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Image;
 import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
-
+import javax.swing.AbstractAction;
+import javax.swing.Box;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSlider;
+import javax.swing.KeyStroke;
 import javax.swing.Timer;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
@@ -36,6 +40,11 @@ public class TimePanel extends JPanel {
 	private Control controller;
 
 	JSlider timeSlider = new JSlider() {
+		/**
+		 * 
+		 */
+		private static final long serialVersionUID = 1L;
+
 		{
 			// Make the slider jump to mouse position on left click
 			MouseListener[] listeners = getMouseListeners();
@@ -216,21 +225,17 @@ public class TimePanel extends JPanel {
 			}
 		});
 
-		// timeBtnPanel.add(simSpeedText);
 		timeBtnPanel.add(playBtn);
+		timeBtnPanel.add(Box.createRigidArea(new Dimension(10,0)));
 		timeBtnPanel.add(timeResetBtn);
-		/*
-		 * the backwards button isnt used anymore
-		 */
-		// timeBtnPanel.add(timeBackwardBtn);
-		timeBtnPanel.add(timeForwardBtn);
+		timeBtnPanel.add(Box.createRigidArea(new Dimension(10,0)));
+		timeBtnPanel.add(timeForwardBtn);	
 
 		// Speed Panel
 		speedPanel.add(simSpeedLabel);
 		speedPanel.add(speedSlider);
 		speedSlider.setPaintTicks(true);
 		speedSlider.setPaintLabels(true);
-		speedSlider.setToolTipText("Speed: 1000");
 		speedSlider.setMaximum(5000);
 		speedSlider.setMinimum(500);
 		speedSlider.setValue(1000);
@@ -251,6 +256,17 @@ public class TimePanel extends JPanel {
 
 		this.add(btnAndSpeedPanel, BorderLayout.WEST);
 		this.add(timeSlider);
+		
+		//Left arrow Key
+		timeSlider.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "LEFT_ARROW");
+		timeSlider.getActionMap().put("LEFT_ARROW", new AbstractAction() {
+			private static final long serialVersionUID = 1L;
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				timeSlider.setValue(timeSlider.getValue()-1);
+				controller.resetSimulation();
+			}
+		});
 	}
 
 	/**

+ 2 - 2
src/ui/view/UnitGraph.java

@@ -250,8 +250,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 				y = this.getHeight() / scaleY;
 			}
 			// x
-			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x
-					|| pointList.get(i - 1).getX() > x) {
+			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() < x+3
+					|| pointList.get(i - 1).getX() > x-3 || pointList.getFirst().getX() > x-3 || pointList.getLast().getX() < x+3) {
 				x = tempP.getX();
 			}
 			tempP.setLocation(x, y);