فهرست منبع

Added more States to HolonObjects + Edges can be editet now => Option menu

dominik.rieder 7 سال پیش
والد
کامیت
62c692d3bd

+ 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;
 	}
 }

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

@@ -36,18 +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()){
-						System.out.println(production + consumption);
-						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);
 				}
 			}
 		}
@@ -66,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;
 	}

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

@@ -111,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));

+ 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 - 0
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);
 

+ 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;
+	}
 }