Pārlūkot izejas kodu

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 gadi atpakaļ
vecāks
revīzija
b9823609e3

+ 10 - 0
src/classes/CpsEdge.java

@@ -19,6 +19,16 @@ public class CpsEdge {
 		isWorking = true;
 	}
 	
+	public CpsEdge(CpsObject A, CpsObject B, float maxCap){
+		setA(A);
+		setB(B);
+		this.A.AddConnection(this);
+		this.B.AddConnection(this);
+		this.maxCapacity = maxCap;
+		flow = 0;
+		isWorking = true;
+	}
+	
 	
 	/**
 	 * @return the capacity

+ 25 - 8
src/classes/HolonObject.java

@@ -16,9 +16,9 @@ public class HolonObject extends CpsObject {
 	private float currentEnergy;
 
 	/**
-	 * true if supplied and false if not supplied
+	 * 0 = no energy, 1 = not supplied, 2 = supplied, 3 producer
 	 */
-	boolean supplied = false;
+	int state = 0;
 
 	/**
 	 * Constructor Set by default the name of the object equals to the category
@@ -28,7 +28,7 @@ public class HolonObject extends CpsObject {
 		super(ObjName);
 		setElements(new ArrayList<HolonElement>());
 		setEleIdx(new HashMap<String,Integer>());
-
+		setState();
 	}
 
 	public HolonObject(String ObjName, String obj) {
@@ -36,12 +36,28 @@ public class HolonObject extends CpsObject {
 		super.setName(obj);
 		setElements(new ArrayList<HolonElement>());
 		setEleIdx(new HashMap<String,Integer>());
+		setState();
 	}
 
 	public HolonObject(CpsObject obj) {
 		super(obj);
 		setEleIdx(MultiPurposeController.copyHashMap(((HolonObject) obj).getEleIdx()));
 		setElements(copyElements(((HolonObject)obj).getElements()));
+		setState();
+	}
+	
+	/**
+	 * sets the State, wether object is a producer, zero Energy, supplied or not supplied
+	 */
+	public void setState(){
+		if(getCurrentEnergy() > 0){
+			setState(3);
+		}
+		else{
+			if(getCurrentEnergy() == 0){
+				setState(0);
+			}
+		}
 	}
 
 	/**
@@ -87,6 +103,7 @@ public class HolonObject extends CpsObject {
 		currentEnergy = temp;
 		return currentEnergy;
 	}
+	
 
 	/**
 	 * @param currentEnergy
@@ -149,19 +166,19 @@ public class HolonObject extends CpsObject {
 		}
 		return newArr;
 	}
-	
+
 	/*
 	 * returns supplied
 	 */
-	public boolean getSupplied() {
-		return this.supplied;
+	public int getState() {
+		return this.state;
 	}
 
 	/**
 	 * @param supplied
 	 *            boolean if the Object is fully supplied
 	 */
-	public void setSupplied(boolean sup) {
-		this.supplied = sup;
+	public void setState(int st) {
+		this.state = st;
 	}
 }

+ 6 - 5
src/ui/controller/AutoSaveController.java

@@ -5,25 +5,26 @@ import ui.model.Model;
 public class AutoSaveController {
 	private Model MODEL;
 	private int numberOfSaves = 20;
-	private int autoSaveNr = 0;
+	private int autoSaveNr, tmp = 0;
 	public AutoSaveController(Model model) {
 		this.MODEL = model;
 	}
 	
 	public void increaseAutoSaveNr(){
 		 autoSaveNr = MODEL.getAutoSaveNr()+1;
+		 tmp = MODEL.getAutoSaveNr();
 		if(autoSaveNr > numberOfSaves){
-			autoSaveNr = 1;
+			autoSaveNr = 0;
 		}
-		MODEL.setAutoSAveNr(autoSaveNr);
+		MODEL.setAutoSaveNr(autoSaveNr);
 	}
 	
 	public void decreaseAutoSaveNr() {
 		 autoSaveNr = MODEL.getAutoSaveNr()-1;
-		if(autoSaveNr <= 0){
+		if(autoSaveNr < 0){
 			autoSaveNr = numberOfSaves;
 		}
-		MODEL.setAutoSAveNr(autoSaveNr);
+		MODEL.setAutoSaveNr(autoSaveNr);
 	}
 	
 	public int getAutoSaveNr(){

+ 18 - 5
src/ui/controller/Control.java

@@ -72,6 +72,12 @@ public class Control {
 
 	public void addObject(Category cat, String obj, ArrayList<HolonElement> ele, String img) {
 		categoryController.addNewHolonObject(cat, obj, ele, img);
+		try {
+			autoSave();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 	}
 
 	public void addTransformer(Category cat, String obj) {
@@ -106,11 +112,6 @@ public class Control {
 		}
 	}
 
-	private void autoSave() throws IOException {
-		autoSaveController.increaseAutoSaveNr();
-		storeController.writeSaveFile(autoPath+autoSaveController.getAutoSaveNr());
-	}
-
 	public void removeEdgesOnCanvas(CpsEdge edge) {
 		canvasController.removeEdgesOnCanvas(edge);
 		try {
@@ -206,13 +207,25 @@ public class Control {
 		simulationManager.setCanvas(can);
 	}
 	
+
+	private void autoSave() throws IOException {
+		autoSaveController.increaseAutoSaveNr();
+		storeController.writeSaveFile(autoPath+autoSaveController.getAutoSaveNr());
+	}
+	
 	public String getUndoSave(){
 		autoSaveController.decreaseAutoSaveNr();
+		if(!new File(autoPath + (autoSaveController.getAutoSaveNr())).exists()){
+			autoSaveController.increaseAutoSaveNr();
+		}
 		return autoPath+(autoSaveController.getAutoSaveNr());
 	}
 	
 	public String getRedoSave(){
 		autoSaveController.increaseAutoSaveNr();
+		if(!new File(autoPath+(autoSaveController.getAutoSaveNr())).exists()){
+			autoSaveController.decreaseAutoSaveNr();
+		}
 		return autoPath+(autoSaveController.getAutoSaveNr());
 	}
 	

+ 18 - 9
src/ui/controller/SimulationManager.java

@@ -36,17 +36,21 @@ public class SimulationManager {
 				e.setFlow(production);
 			}
 			for(HolonObject hl: singleSubNet.getObjects()){
-				for(int i = 0; i < hl.getConnections().size(); i++){
-					CpsEdge edge = hl.getConnectedTo().get(i);
-					if(edge.getState()){
-						if((production + consumption) >= 0 ){
-							hl.setSupplied(true);
-						}else{
-							hl.setSupplied(false);
+				if(!(hl.getState() == 0) && !(hl.getState() == 3)){
+					for(int i = 0; i < hl.getConnections().size(); i++){
+						CpsEdge edge = hl.getConnectedTo().get(i);
+						if(edge.getState()){
+							// 0 = no energy, 1 = not supplied, 2 = supplied
+							if((production + consumption) >= 0){
+								hl.setState(2);
+							}
+							if((production + consumption) < 0){
+								hl.setState(1);
+							}
+							break;
 						}
-						break;
+						hl.setState(1);
 					}
-					hl.setSupplied(false);
 				}
 			}
 		}
@@ -65,13 +69,18 @@ public class SimulationManager {
 			if(type.equals("prod")){
 				if(hl.getCurrentEnergyAtTimeStep(x) > 0){
 					energy = energy + hl.getCurrentEnergyAtTimeStep(x);
+					hl.setState(3);
 				}
 			}
 			if(type.equals("cons")){
 				if(hl.getCurrentEnergyAtTimeStep(x) < 0){
 					energy = energy + hl.getCurrentEnergyAtTimeStep(x);
+					hl.setState(1);
 				}
 			}
+			if(hl.getCurrentEnergyAtTimeStep(x) == 0){
+				hl.setState(0);
+			}
 		}
 		return energy;
 	}

+ 2 - 2
src/ui/model/Model.java

@@ -27,7 +27,7 @@ public class Model {
 	private CpsEdge selectedEdge;
 
 	private int selectedID = 0;
-	private int autoSaveNr = 0;
+	private int autoSaveNr = -1;
 	// eventuell wenn Canvasgröße gewählt werden kann
 	private int HEIGHT;
 	private int WIDTH;
@@ -319,7 +319,7 @@ public class Model {
 		this.cvsObjIdx = cvsObjIdx;
 	}
 	
-	public void setAutoSAveNr(int autoSaveNr){
+	public void setAutoSaveNr(int autoSaveNr){
 		this.autoSaveNr = autoSaveNr;
 	}
 	

+ 1 - 9
src/ui/view/AddElementPopUp.java

@@ -101,11 +101,6 @@ public class AddElementPopUp extends JDialog {
 		contentPanel.add(elementName);
 		elementName.setColumns(10);
 
-		final JComboBox sign = new JComboBox();
-		sign.setModel(new DefaultComboBoxModel(new String[] { "+", "-" }));
-		sign.setBounds(240, 46, 60, 20);
-		contentPanel.add(sign);
-
 		providedEnergy = new JTextField();
 		providedEnergy.setBounds(130, 46, 110, 20);
 		contentPanel.add(providedEnergy);
@@ -116,7 +111,7 @@ public class AddElementPopUp extends JDialog {
 		amount.setBounds(130, 81, 110, 20);
 		contentPanel.add(amount);
 		amount.setColumns(10);
-		amount.setText("0");
+		amount.setText("1");
 		{
 			JPanel buttonPane = new JPanel();
 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -136,9 +131,6 @@ public class AddElementPopUp extends JDialog {
 							try {
 								float energy = Float.parseFloat(providedEnergy.getText().toString());
 								int elementAmount = Integer.parseInt(amount.getText().toString());
-								if (sign.getSelectedItem().toString().equals("-")) {
-									energy = energy * -1;
-								}
 								hl = new HolonElement(elementName.getText().toString(), elementAmount, energy);
 								dispose();
 							} catch (NumberFormatException e) {

+ 4 - 2
src/ui/view/AddObjectPopUp.java

@@ -57,6 +57,7 @@ public class AddObjectPopUp extends JDialog {
 	private String givenCategory;
 	private JLabel lblImagePreview;
 	private CpsObject toEdit;
+	private boolean editState;
 	private boolean imageChanged = false;
 
 	/**
@@ -77,6 +78,7 @@ public class AddObjectPopUp extends JDialog {
 	 */
 	public AddObjectPopUp(boolean edit, CpsObject obj, String cat) {
 		toEdit = obj;
+		editState = edit;
 		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
 				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
 		setBounds(100, 100, 450, 342);
@@ -178,7 +180,7 @@ public class AddObjectPopUp extends JDialog {
 			btnAddDefaultElement.addActionListener(new ActionListener() {
 				public void actionPerformed(ActionEvent arg0) {
 					addElement = new AddElementPopUp();
-					addElement.setActualCps(obj);
+					addElement.setActualCps(toEdit);
 					addElement.setVisible(true);
 					HolonElement hl = addElement.getElement();
 					addElement(hl);
@@ -244,7 +246,7 @@ public class AddObjectPopUp extends JDialog {
 								// HolonObject(objectName.getText());
 								// theObject.setElements(hElements);
 								// theObject.setImage(imagePath);
-								if (edit) {
+								if (editState) {
 									controller.delObjectCategory(givenCategory, toEdit.getName());
 									controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
 											hElements, imagePath);

+ 147 - 0
src/ui/view/EditEdgesPopUp.java

@@ -0,0 +1,147 @@
+package ui.view;
+
+import javax.swing.ButtonGroup;
+import javax.swing.JDialog;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
+import javax.swing.JLabel;
+
+import java.awt.BorderLayout;
+import java.awt.Font;
+
+import javax.swing.JTextField;
+import javax.swing.JButton;
+import javax.swing.border.EmptyBorder;
+
+import classes.CpsEdge;
+import ui.controller.Control;
+
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+public class EditEdgesPopUp extends JDialog{
+	private final JPanel contentPanel = new JPanel();
+	private JTextField textField;
+	private final JButton btnOk = new JButton("OK");
+	private JTextField capacityField;
+	private float capacity;
+	private JRadioButton rdbtnChangeForAll;
+	private JRadioButton rdbtnChangeForNew;
+	private JRadioButton rdbtnChangeForAll_1;
+	private Control controller;
+	private MyCanvas canvas;
+	
+	/**
+	 * Launch the application.
+	 */
+	public static void main(String[] args) {
+		try {
+
+			EditEdgesPopUp dialog = new EditEdgesPopUp();
+			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+			dialog.setVisible(true);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+	
+	public EditEdgesPopUp() {
+		super((java.awt.Frame) null, true);
+		setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
+		this.setTitle("Edit Capacities of Edges");
+		setBounds(100, 100, 400, 220);
+		getContentPane().setLayout(new BorderLayout());
+		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
+		getContentPane().add(contentPanel, BorderLayout.CENTER);
+		contentPanel.setLayout(null);
+		
+		JLabel lblMaximumCapacity = new JLabel("Maximum Capacity:");
+		lblMaximumCapacity.setFont(new Font("Tahoma", Font.PLAIN, 11));
+		lblMaximumCapacity.setBounds(10, 11, 98, 14);
+		contentPanel.add(lblMaximumCapacity);
+		
+		capacityField = new JTextField();
+		capacityField.setBounds(107, 8, 120, 20);
+		contentPanel.add(capacityField);
+		capacityField.setColumns(10);
+		
+		rdbtnChangeForAll = new JRadioButton("Change for all existing Edges only");
+		rdbtnChangeForAll.setBounds(10, 39, 265, 23);
+		contentPanel.add(rdbtnChangeForAll);
+		
+		rdbtnChangeForNew = new JRadioButton("Change for new created Edges only");
+		rdbtnChangeForNew.setBounds(10, 65, 265, 23);
+		contentPanel.add(rdbtnChangeForNew);
+		
+		rdbtnChangeForAll_1 = new JRadioButton("Change for all existing and new created Edges");
+		rdbtnChangeForAll_1.setBounds(10, 95, 296, 23);
+		contentPanel.add(rdbtnChangeForAll_1);
+		
+		JButton btnCancel = new JButton("Cancel");
+		btnCancel.setActionCommand("Cancel");
+		btnCancel.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				dispose();
+			}
+		});
+		btnCancel.setBounds(285, 147, 89, 23);
+		contentPanel.add(btnCancel);
+		
+		JButton btnOk_1 = new JButton("OK");
+		btnOk_1.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				try {
+					capacity = Float.parseFloat(capacityField.getText().toString());
+					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 in the Field for Maximum Capacity");
+				}
+			}
+		});
+		btnOk_1.setBounds(186, 147, 89, 23);
+		contentPanel.add(btnOk_1);
+		this.setTitle("Edit Edge Capacities");
+		ButtonGroup bG = new ButtonGroup();
+		bG.add(rdbtnChangeForAll_1);
+		bG.add(rdbtnChangeForNew);
+		bG.add(rdbtnChangeForAll);
+	}
+	
+	public void setCanvas(MyCanvas can){
+		canvas = can;
+	}
+	
+	public void setController(Control cont){
+		controller = cont;
+	}
+	
+	public void changeForNew(float cap){
+		System.out.println("Imhere");
+		canvas.setEdgeCapacity(cap);
+	}
+	public void changeForExisting(float cap){
+		for(CpsEdge edge : controller.getModel().getEdgesOnCanvas()){
+			edge.setCapacity(cap);
+		}
+		canvas.repaint();
+	}
+	public void changeForExAndNew(float cap){
+		changeForNew(cap);
+		changeForExisting(cap);
+	}
+}

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

@@ -180,6 +180,7 @@ public class GUI<E> implements CategoryListener {
 	private final JButton btnTest = new JButton("test");
 	private final JMenuItem mntmUndo = new JMenuItem("Undo");
 	private final JMenuItem mntmRedo = new JMenuItem("Redo");
+	private final JMenuItem mntmEditEdges = new JMenuItem("Edit Edges");
 
 	/**
 	 * Create the application.
@@ -249,6 +250,16 @@ public class GUI<E> implements CategoryListener {
 		mnNewMenu_1.add(mntmRedo);
 
 		menuBar.add(mnNewMenu_2);
+		mntmEditEdges.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent e) {
+				EditEdgesPopUp edgePopUp = new EditEdgesPopUp();
+				edgePopUp.setCanvas(canvas);
+				edgePopUp.setController(controller);
+				edgePopUp.setVisible(true);
+			}
+		});
+		
+		mnNewMenu_2.add(mntmEditEdges);
 
 		menuBar.add(mnNewMenu_3);
 
@@ -1009,9 +1020,6 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
-					unitGraph.empty();
-					tree.repaint();
-
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
@@ -1047,9 +1055,6 @@ public class GUI<E> implements CategoryListener {
 							unitGraph.fillArrayofBooleans();
 						}
 					}
-					unitGraph.empty();
-					tree.repaint();
-
 				} catch (IOException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();

+ 23 - 8
src/ui/view/MyCanvas.java

@@ -64,12 +64,16 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 	private JPopupMenu popmenu = new JPopupMenu();
 	private JMenuItem itemDelete = new JMenuItem("Delete Object");
 	private JToolTip objectTT = new JToolTip();
+	
+	//contains the value of the Capacity for new created Edges
+	private float edgeCapacity;
 
 	public MyCanvas(final Model model, Control control) {
 		this.add(objectTT);
 		this.controller = control;
 		this.model = model;
-
+		
+		edgeCapacity = 100;
 		popmenu.add(itemDelete);
 		itemDelete.setEnabled(false);
 		itemDelete.addActionListener(new ActionListener() {
@@ -181,11 +185,18 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
 							(int) selectRect.getHeight());
 				} else if (cps instanceof HolonObject) {
-					if (((HolonObject) cps).getSupplied()) {
+					if(((HolonObject) cps).getState() == 3){
+						g2.setColor(Color.lightGray);
+					}
+					if(((HolonObject) cps).getState() == 2) {
 						g2.setColor(Color.GREEN);
-					} else {
+					} 
+					if(((HolonObject) cps).getState() == 1){
 						g2.setColor(Color.ORANGE);
 					}
+					if(((HolonObject) cps).getState() == 0){
+						g2.setColor(Color.WHITE);
+					}
 					g2.fillRect(cps.getPosition().x - (controller.getScale() / 20),
 							cps.getPosition().y - (controller.getScale() / 20),
 							controller.getScale() + ((controller.getScale() / 20) * 2),
@@ -413,7 +424,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 					}
 				}
 				if (newEdge) {
-					e = new CpsEdge(cps, tempCps);
+					e = new CpsEdge(cps, tempCps, edgeCapacity);
 					controller.AddEdgeOnCanvas(e);
 				}
 			}
@@ -437,11 +448,11 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 				r = p.getA();
 				k = p.getB();
 
-				e = new CpsEdge(n, tempCps);
+				e = new CpsEdge(n, tempCps, edgeCapacity);
 
-				e1 = new CpsEdge(n, r);
+				e1 = new CpsEdge(n, r, edgeCapacity);
 
-				e2 = new CpsEdge(n, k);
+				e2 = new CpsEdge(n, k, edgeCapacity);
 
 				p.getA().getConnections().remove(p);
 				p.getB().getConnections().remove(p);
@@ -461,7 +472,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
 			controller.addObjectCanvas(n);
 
-			e = new CpsEdge(n, tempCps);
+			e = new CpsEdge(n, tempCps, edgeCapacity);
 
 			controller.AddEdgeOnCanvas(e);
 			System.out.println("node ID: " + n.getID());
@@ -537,4 +548,8 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		}
 		return false;
 	}
+	
+	public void setEdgeCapacity(float cap){
+		edgeCapacity = cap;
+	}
 }