Преглед на файлове

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

Teh-Hai Julian Zheng преди 8 години
родител
ревизия
c6850eac69

+ 3 - 11
src/classes/CpsEdge.java

@@ -5,7 +5,6 @@ public class CpsEdge {
 	float maxCapacity;
 	float flow;
 	boolean isWorking;
-	boolean infinite;
 	
 	CpsObject A;
 	CpsObject B;
@@ -57,10 +56,10 @@ public class CpsEdge {
 	 */
 	public void setFlow(float flow) {
 		this.flow = flow;
-		if(flow > maxCapacity){
-			isWorking = false;
-		}else{
+		if(flow <= maxCapacity || flow == -1){
 			isWorking = true;
+		}else{
+			isWorking = false;
 		}
 	}
 	
@@ -99,12 +98,5 @@ public class CpsEdge {
 		return isWorking;
 	}
 	
-	public void setInfinite(boolean inf){
-		infinite = inf;
-	}
-	
-	public boolean getInfinite(){
-		return infinite;
-	}
 
 }

+ 29 - 15
src/ui/controller/AutoSaveController.java

@@ -4,30 +4,44 @@ import ui.model.Model;
 
 public class AutoSaveController {
 	private Model MODEL;
+	private int max = Integer.MAX_VALUE;
 	private int numberOfSaves = 20;
-	private int autoSaveNr, tmp = 0;
+	private int currentSave;
+	private int count = 0;
+	private boolean isAllowed = false;
+
 	public AutoSaveController(Model model) {
 		this.MODEL = model;
 	}
-	
-	public void increaseAutoSaveNr(){
-		 autoSaveNr = MODEL.getAutoSaveNr()+1;
-		 tmp = MODEL.getAutoSaveNr();
-		if(autoSaveNr > numberOfSaves){
-			autoSaveNr = 0;
+
+	public void increaseAutoSaveNr() {
+		currentSave = MODEL.getAutoSaveNr() + 1;
+		if (count < currentSave) {
+			count = currentSave;
+		}
+		if (numberOfSaves == count) {
+			isAllowed = true;
 		}
-		MODEL.setAutoSaveNr(autoSaveNr);
+		if (currentSave > max) {
+			currentSave = 0;
+		}
+
+		MODEL.setAutoSaveNr(currentSave);
 	}
-	
+
 	public void decreaseAutoSaveNr() {
-		 autoSaveNr = MODEL.getAutoSaveNr()-1;
-		if(autoSaveNr < 0){
-			autoSaveNr = numberOfSaves;
+		currentSave = MODEL.getAutoSaveNr() - 1;
+		if (currentSave < 0) {
+			currentSave = max;
 		}
-		MODEL.setAutoSaveNr(autoSaveNr);
+		MODEL.setAutoSaveNr(currentSave);
 	}
-	
-	public int getAutoSaveNr(){
+
+	public int getAutoSaveNr() {
 		return MODEL.getAutoSaveNr();
 	}
+
+	public boolean allowed() {
+		return isAllowed;
+	}
 }

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

@@ -219,6 +219,9 @@ public class Control {
 	private void autoSave() throws IOException {
 		autoSaveController.increaseAutoSaveNr();
 		storeController.writeCanvasFile(autoPath + autoSaveController.getAutoSaveNr());
+		if(autoSaveController.allowed()){
+			new File(autoPath + (autoSaveController.getAutoSaveNr()-globalController.getNumbersOfSaves())).delete();
+		}
 	}
 
 	public String getUndoSave() {

+ 18 - 0
src/ui/controller/GlobalController.java

@@ -45,5 +45,23 @@ public class GlobalController {
 	public void setCurIteration(int cur_it) {
 		MODEL.setCurIteration(cur_it);
 	}
+	
+	/**
+	 * Returns numberOfSaves
+	 * 
+	 * @return numberOfSaves
+	 */
+	public int getNumbersOfSaves(){
+		return MODEL.getNumberOfSaves();
+	}
+	
+	/**
+	 * sets the max number of autosaves
+	 * 
+	 * @param numberOfSaves, the max number of autosaves
+	 */
+	public void setNumberOfSaves(int numberOfSaves){
+		MODEL.setNumberOfSaves(numberOfSaves);;
+	}
 
 }

+ 17 - 0
src/ui/model/Model.java

@@ -29,7 +29,10 @@ public class Model {
 	private ArrayList<CpsObject> clipboardObjects = new ArrayList<CpsObject>();
 
 	private int selectedID = 0;
+	// number of the current autosave
 	private int autoSaveNr = -1;
+	// number of max simultaneous autosaves 
+	private int numberOfSaves = 35;
 	// eventuell wenn Canvasgröße gewählt werden kann
 	private int HEIGHT;
 	private int WIDTH;
@@ -332,6 +335,20 @@ public class Model {
 		return autoSaveNr;
 	}
 
+	/**
+	 * @return the numberOfSaves
+	 */
+	public int getNumberOfSaves() {
+		return numberOfSaves;
+	}
+
+	/**
+	 * @param numberOfSaves the numberOfSaves to set
+	 */
+	public void setNumberOfSaves(int numberOfSaves) {
+		this.numberOfSaves = numberOfSaves;
+	}
+
 	public void setClipboradObjects(ArrayList<CpsObject> c){
 		this.clipboardObjects = c;
 	}

+ 31 - 6
src/ui/view/EditEdgesPopUp.java

@@ -32,6 +32,7 @@ public class EditEdgesPopUp extends JDialog{
 	private JRadioButton rdbtnChangeForAll_1;
 	private Control controller;
 	private MyCanvas canvas;
+	private JLabel lblenterinfiniteFor;
 	
 	/**
 	 * Launch the application.
@@ -51,7 +52,7 @@ public class EditEdgesPopUp extends JDialog{
 		super((java.awt.Frame) null, true);
 		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
 		this.setTitle("Edit Capacities of Edges");
-		setBounds(100, 100, 400, 220);
+		setBounds(100, 100, 438, 220);
 		getContentPane().setLayout(new BorderLayout());
 		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
 		getContentPane().add(contentPanel, BorderLayout.CENTER);
@@ -92,8 +93,8 @@ public class EditEdgesPopUp extends JDialog{
 		JButton btnOk_1 = new JButton("OK");
 		btnOk_1.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
-				try {
-					capacity = Float.parseFloat(capacityField.getText().toString());
+				if(capacityField.getText().equals("infinite")){
+					capacity = -1;
 					if(rdbtnChangeForAll.isSelected()){
 						changeForExisting(capacity);
 						dispose();
@@ -107,9 +108,29 @@ public class EditEdgesPopUp extends JDialog{
 						JOptionPane.showMessageDialog(new JFrame(),
 								"Please select one of the options");
 					}
-				} catch (NumberFormatException e1) {
-					JOptionPane.showMessageDialog(new JFrame(),
-							"Please enter a number in the Field for Maximum Capacity");
+				}else{
+					try {
+						capacity = Float.parseFloat(capacityField.getText().toString());
+						if(capacity < 0){
+							throw new NumberFormatException();
+						}
+						if(rdbtnChangeForAll.isSelected()){
+							changeForExisting(capacity);
+							dispose();
+						}else if (rdbtnChangeForNew.isSelected()){
+							changeForNew(capacity);
+							dispose();
+						}else if(rdbtnChangeForAll_1.isSelected()){
+							changeForExAndNew(capacity);
+							dispose();
+						}else{
+							JOptionPane.showMessageDialog(new JFrame(),
+									"Please select one of the options");
+						}
+					} catch (NumberFormatException e1) {
+						JOptionPane.showMessageDialog(new JFrame(),
+							"Please enter a number greater or equal 0 in the Field for Maximum Capacity");
+					}
 				}
 			}
 		});
@@ -120,6 +141,10 @@ public class EditEdgesPopUp extends JDialog{
 		bG.add(rdbtnChangeForAll_1);
 		bG.add(rdbtnChangeForNew);
 		bG.add(rdbtnChangeForAll);
+		
+		lblenterinfiniteFor = new JLabel("(enter \"infinite\" for infinite Capacity)");
+		lblenterinfiniteFor.setBounds(237, 11, 175, 14);
+		contentPanel.add(lblenterinfiniteFor);
 	}
 	
 	public void setCanvas(MyCanvas can){

+ 19 - 1
src/ui/view/GUI.java

@@ -72,7 +72,6 @@ import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.HolonTransformer;
-import sun.nio.ch.SelChImpl;
 import ui.controller.Control;
 import ui.model.Model;;
 
@@ -197,6 +196,7 @@ public class GUI<E> implements CategoryListener {
 	private final JMenuItem mntmRedo = new JMenuItem("Redo");
 	private final JMenuItem mntmEditEdges = new JMenuItem("Edit Edges");
 	private final JMenuItem mntmFindReplace = new JMenuItem("Find/ Replace");
+	private final JMenuItem mntmEditShowedInformation = new JMenuItem("Edit showed Information");
 
 	/**
 	 * Create the application.
@@ -242,6 +242,7 @@ public class GUI<E> implements CategoryListener {
 							files[i].delete();
 						}
 					}
+					path.delete();
 				}
 			}
 		});
@@ -275,6 +276,7 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
+					unitGraph.empty();
 				} catch (IOException f) {
 					// TODO Auto-generated catch block
 					f.printStackTrace();
@@ -305,6 +307,7 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
+					unitGraph.empty();
 				} catch (IOException f) {
 					// TODO Auto-generated catch block
 					f.printStackTrace();
@@ -413,6 +416,19 @@ public class GUI<E> implements CategoryListener {
 		});
 		
 		mnNewMenu_1.add(mntmFindReplace);
+		mntmEditShowedInformation.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				try {
+					ShowedInformationPopUp dialog = new ShowedInformationPopUp(canvas);
+					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+					dialog.setVisible(true);
+				} catch (Exception e1) {
+					e1.printStackTrace();
+				}
+			}
+		});
+		
+		mnNewMenu_1.add(mntmEditShowedInformation);
 
 		menuBar.add(mnNewMenu_2);
 		mntmEditEdges.addActionListener(new ActionListener() {
@@ -1286,6 +1302,7 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
+					unitGraph.empty();
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
@@ -1321,6 +1338,7 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
+					unitGraph.empty();
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();

+ 54 - 13
src/ui/view/MyCanvas.java

@@ -54,7 +54,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 
 	ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
 	ArrayList<CpsObject> TempSelected = new ArrayList<CpsObject>();
-
+	
+	private boolean[] showedInformation = new boolean[3];
 	private boolean dragging = false; // for dragging
 	private boolean dragged = false; // if an object/objects was/were dragged
 	private boolean drawEdge = false; // for drawing edges
@@ -77,6 +78,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		this.controller = control;
 		this.model = model;
 
+		showedInformation[0] = true;
+		showedInformation[1] = false;
 		edgeCapacity = 10000;
 		popmenu.add(itemDelete);
 		itemDelete.setEnabled(false);
@@ -102,6 +105,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	 * 
 	 */
 	public void paintComponent(Graphics g) {
+		String maxCap;
 		super.paintComponent(g);
 		// Rendering
 		g2 = (Graphics2D) g;
@@ -120,9 +124,11 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		for (CpsEdge con : model.getEdgesOnCanvas()) {
 			if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
 					&& con != edgeHighlight) {
-				if (con.getFlow() <= con.getCapacity()) {
+				if (con.getFlow() <= con.getCapacity() || con.getCapacity() == -1) {
 					g2.setColor(Color.GREEN);
-					g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
+					if(con.getCapacity() != -1){
+						g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
+					}
 				} else {
 					g2.setColor(Color.RED);
 					g2.setStroke(new BasicStroke(2));
@@ -131,9 +137,17 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 						con.getA().getPosition().y + controller.getScaleDiv2(),
 						con.getB().getPosition().x + controller.getScaleDiv2(),
 						con.getB().getPosition().y + controller.getScaleDiv2());
-				g2.drawString(con.getFlow() + "/" + con.getCapacity(),
-						(con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
-						(con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
+				
+				if(con.getCapacity() == -1){
+					maxCap = Character.toString('\u221e');
+				}else{
+					maxCap = String.valueOf(con.getCapacity());
+				}
+				if(showedInformation[0]){
+					g2.drawString(con.getFlow() + "/" + maxCap,
+							(con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
+							(con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
+				}
 			}
 		}
 
@@ -152,9 +166,17 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 							con.getA().getPosition().y + controller.getScaleDiv2(),
 							con.getB().getPosition().x + controller.getScaleDiv2(),
 							con.getB().getPosition().y + controller.getScaleDiv2());
-					g2.drawString(con.getFlow() + "/" + con.getCapacity(),
-							(con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
-							(con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
+					
+					if(con.getCapacity() == -1){
+						maxCap = Character.toString('\u221e');
+					}else{
+						maxCap = String.valueOf(con.getCapacity());
+					}
+					if(showedInformation[0]){
+						g2.drawString(con.getFlow() + "/" + maxCap,
+								(con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
+								(con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
+					}
 				}
 			}
 		} else if (edgeHighlight != null) {
@@ -164,11 +186,19 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
 					edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
 					edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
-			g2.drawString(edgeHighlight.getFlow() + "/" + edgeHighlight.getCapacity(),
-					(edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
+			
+			if(edgeHighlight.getCapacity() == -1){
+				maxCap = Character.toString('\u221e');
+			}else{
+				maxCap = String.valueOf(edgeHighlight.getCapacity());
+			}
+			if(showedInformation[0]){
+				g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
+						(edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
 							+ controller.getScaleDiv2(),
-					(edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
-							+ controller.getScaleDiv2());
+							(edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
+								+ controller.getScaleDiv2());
+			}
 		}
 
 		// Objects
@@ -629,4 +659,15 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	public void setEdgeCapacity(float cap) {
 		edgeCapacity = cap;
 	}
+
+	public void setShowedInformation(boolean connection, boolean object) {
+		showedInformation[0] = connection;
+		showedInformation[1] = object;
+	}
+	
+	public boolean[] getShowedInformation(){
+		return showedInformation;
+	}
+	
+	
 }

+ 65 - 0
src/ui/view/ShowedInformationPopUp.java

@@ -0,0 +1,65 @@
+package ui.view;
+
+import java.awt.BorderLayout;
+
+import javax.swing.JDialog;
+import javax.swing.JPanel;
+import javax.swing.border.EmptyBorder;
+import javax.swing.JCheckBox;
+import javax.swing.JButton;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+public class ShowedInformationPopUp extends JDialog{
+	private final JPanel contentPanel = new JPanel();
+	private final JButton btnOk = new JButton("OK");
+	private MyCanvas canvas;
+	private JCheckBox objectEnergyCheckbox;
+	private JCheckBox connectionCheckbox;
+	
+	public ShowedInformationPopUp(MyCanvas canvas){
+		super((java.awt.Frame) null, true);
+		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
+		this.setTitle("Edit Showed Informations");
+		setBounds(100, 100, 277, 169);
+		getContentPane().setLayout(new BorderLayout());
+		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
+		getContentPane().add(contentPanel, BorderLayout.CENTER);
+		contentPanel.setLayout(null);
+		this.canvas = canvas;
+		
+		objectEnergyCheckbox = new JCheckBox("Show Total Energy of Objects");
+		objectEnergyCheckbox.setBounds(19, 19, 181, 23);
+		contentPanel.add(objectEnergyCheckbox);
+		
+		connectionCheckbox = new JCheckBox("Show Connection Properties");
+		connectionCheckbox.setBounds(19, 57, 181, 23);
+		contentPanel.add(connectionCheckbox);
+		
+		objectEnergyCheckbox.setSelected(canvas.getShowedInformation()[1]);
+		connectionCheckbox.setSelected(canvas.getShowedInformation()[0]);
+		btnOk.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				setInformation(connectionCheckbox.isSelected(), objectEnergyCheckbox.isSelected());
+				dispose();
+			}
+		});
+		btnOk.setBounds(169, 98, 82, 23);
+		contentPanel.add(btnOk);
+		
+		JButton btnCancel = new JButton("Cancel");
+		btnCancel.setActionCommand("Cancel");
+		btnCancel.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				dispose();
+			}
+		});
+		btnCancel.setBounds(70, 98, 89, 23);
+		contentPanel.add(btnCancel);
+	}
+	
+	private void setInformation(boolean connection, boolean object){
+		canvas.setShowedInformation(connection, object);
+		canvas.repaint();
+	}
+}