ソースを参照

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

Jessey Widhalm 8 年 前
コミット
0175b255f6
4 ファイル変更152 行追加87 行削除
  1. 2 0
      src/classes/CpsEdge.java
  2. 92 59
      src/ui/view/GUI.java
  3. 10 10
      src/ui/view/MyCanvas.java
  4. 48 18
      src/ui/view/UnitGraph.java

+ 2 - 0
src/classes/CpsEdge.java

@@ -11,6 +11,8 @@ public class CpsEdge {
 	public CpsEdge(CpsObject A, CpsObject B){
 		setA(A);
 		setB(B);
+		this.A.AddConnection(this);
+		this.B.AddConnection(this);
 		this.maxCapacity = 100;
 	}
 	

+ 92 - 59
src/ui/view/GUI.java

@@ -260,10 +260,9 @@ public class GUI<E> implements CategoryListener {
 
 		scrollPane_2.setViewportView(dtrpnHereWillBe);
 
-		/*
-		 * Right Container Set up
-		 */
-
+		/********************
+		 * RIGHT CONTAINER (INFORMATION)
+		 **********************/
 		// Set up of the HolonElements section
 		Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElement.setColumnIdentifiers(columnNames);
@@ -272,6 +271,15 @@ public class GUI<E> implements CategoryListener {
 		tableHolonElement.setFillsViewportHeight(true);
 		tableHolonElement.setCellSelectionEnabled(true);
 		tableHolonElement.setColumnSelectionAllowed(true);
+		tableHolonElementScrollPane.setViewportView(tableHolonElement);
+		scrollElements.setLayout(new BorderLayout(0, 0));
+		scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
+		scrollElements.add(tableHolonElementScrollPane);
+		panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
+		toolBarHolonEl.add(btnAddHolEL);
+		toolBarHolonEl.add(btnDelHolEL);
+		toolBarHolonEl.setFloatable(false);
+		panel_HolonEl.add(toolBarHolonEl);
 
 		// Set up of the Properties section
 		Object[] colNames = { "Field", "Information" };
@@ -281,42 +289,32 @@ public class GUI<E> implements CategoryListener {
 		tableProperties.setFillsViewportHeight(true);
 		tableProperties.setCellSelectionEnabled(true);
 		tableProperties.setColumnSelectionAllowed(true);
-		// Set up of the Graph section
+		scrollProperties.setViewportView(tableProperties);
 
+		// Set up of the Graph section
 		Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
 		tableModelGraph.setColumnIdentifiers(tempText);
 		tableGraph.setModel(tableModelGraph);
 		tableGraph.setFillsViewportHeight(true);
 		tableGraph.setCellSelectionEnabled(true);
 		tableGraph.setColumnSelectionAllowed(true);
-
-		/*
-		 * End of right container setup
-		 */
-		scrollProperties.setViewportView(tableProperties);
-		tableHolonElementScrollPane.setViewportView(tableHolonElement);
 		scrollGraph.setViewportView(unitGraph);
 		graphLabel.setLayout(new BorderLayout(0, 10));
 		graphLabel.add(maxGraph, BorderLayout.NORTH);
 		graphLabel.add(medGraph, BorderLayout.CENTER);
 		graphLabel.add(minGraph, BorderLayout.SOUTH);
 		toolBarGraph.add(elementGraph);
-		// comboBoxGraph.setModel(new DefaultComboBoxModel(new String[] { "Day",
-		// "Month", "Year" }));
-		// toolBarGraph.add(comboBoxGraph);
 		toolBarGraph.add(resetGraphBtn);
 		toolBarGraph.setFloatable(false);
 		scrollGraph.setRowHeaderView(graphLabel);
 		scrollGraph.setColumnHeaderView(toolBarGraph);
-		scrollElements.setLayout(new BorderLayout(0, 0));
-		scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
-		scrollElements.add(tableHolonElementScrollPane);
-
-		panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
-		toolBarHolonEl.setFloatable(false);
 
-		panel_HolonEl.add(toolBarHolonEl);
-		toolBarHolonEl.add(btnAddHolEL);
+		/***********************
+		 * HolonElement Table Actions
+		 **********************/
+		/*
+		 * Add HolonElement to given HolonObject
+		 */
 		btnAddHolEL.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				CpsObject tempCpsObject = getActualCps();
@@ -331,13 +329,9 @@ public class GUI<E> implements CategoryListener {
 				}
 			}
 		});
-		resetGraphBtn.addActionListener(new ActionListener() {
-
-			public void actionPerformed(ActionEvent arg0) {
-				unitGraph.reset();
-			}
-		});
-		toolBarHolonEl.add(btnDelHolEL);
+		/*
+		 * Delete the choosen HolonElement of the selected HolonObject
+		 */
 		btnDelHolEL.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				if (getActualCps().getClass() == HolonObject.class) {
@@ -351,7 +345,9 @@ public class GUI<E> implements CategoryListener {
 				}
 			}
 		});
-
+		/*
+		 * Communication between HolonElement Table and displayed Graph
+		 */
 		tableHolonElement.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				HolonObject obj = (HolonObject) getActualCps();
@@ -363,6 +359,8 @@ public class GUI<E> implements CategoryListener {
 				} else {
 					elementGraph.setText("None ");
 				}
+				// if any HolonElement is double-clicked --> Edit instance of
+				// selected HolonElement
 				if (e.getClickCount() == 2) {
 					yTHIS = e.getY();
 					xTHIS = e.getX();
@@ -373,26 +371,10 @@ public class GUI<E> implements CategoryListener {
 
 			}
 		});
-
-		tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
-			@Override
-			public void propertyChange(PropertyChangeEvent evt) {
-				try {
-					Object temp;
-					if (getActualCps() != null) {
-						temp = tableModelProperties.getValueAt(0, 1);
-						getActualCps().setName(temp.toString());
-					} else {
-						temp = tableModelProperties.getValueAt(2, 1);
-						Float ftemp = Float.parseFloat(temp.toString());
-						model.getSelectedEdge().setCapacity(ftemp);
-					}
-				} catch (Exception e) {
-
-				}
-			}
-		});
-
+		/*
+		 * If the HolonElement Table enters to editing instance, than is the
+		 * propertyChangeListener triggered
+		 */
 		tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
 			@Override
 			public void propertyChange(PropertyChangeEvent evt) {
@@ -423,8 +405,36 @@ public class GUI<E> implements CategoryListener {
 								Integer iTemp = Integer.parseInt(newStuff);
 								eleTemp.setAmount(iTemp);
 							}
-							tableModelHolonElement.fireTableDataChanged();
 						}
+						refreshTableProperties();
+						tableModelHolonElement.fireTableDataChanged();
+					}
+				} catch (Exception e) {
+
+				}
+			}
+		});
+
+		/***********************
+		 * HolonElement Properties Actions
+		 **********************/
+		/*
+		 * If any value at the Properties Table enters to editing instance, than
+		 * is PropertyChangeListener triggered (For HolonObject, the only value
+		 * to be edited is the name and for CpsEdge the Max.flow)
+		 */
+		tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
+			@Override
+			public void propertyChange(PropertyChangeEvent evt) {
+				try {
+					Object temp;
+					if (getActualCps() != null) {
+						temp = tableModelProperties.getValueAt(0, 1);
+						getActualCps().setName(temp.toString());
+					} else {
+						temp = tableModelProperties.getValueAt(2, 1);
+						Float ftemp = Float.parseFloat(temp.toString());
+						model.getSelectedEdge().setCapacity(ftemp);
 					}
 				} catch (Exception e) {
 
@@ -432,6 +442,21 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
+		/***********************
+		 * HolonElement Graph Actions
+		 **********************/
+		/*
+		 * Reset the graph of the selected HolonElement
+		 */
+		resetGraphBtn.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				unitGraph.reset();
+			}
+		});
+		/*****************************
+		 * RIGHT CONTAINER DONE
+		 *****************************/
+
 		frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
 
 		TreeCellRenderer customRenderer = new TreeCellRenderer() {
@@ -923,23 +948,24 @@ public class GUI<E> implements CategoryListener {
 		}
 	}
 
-	/*
-	 * Refresh the Table displaying the HolonElements
+	/**
+	 * Update the HolonElement Table, that means erase all rows and add the new
+	 * rows with new data
 	 */
 	public void refreshTableHolonElement() {
 		// Update of the Information about the HolonElements - only for
 		// HolonObjects
+
 		deleteRows();
 		if (getActualCps() != null) {
 			fillElementTable(((HolonObject) getActualCps()).getElements());
-		} /**
-			 * hinzugef�gt damit man auch nach dem objekt platziert wurde
-			 * elemente von Objekten in Kategorien ansehen kann
-			 */
+		}
 		tableModelHolonElement.fireTableDataChanged();
-
 	}
 
+	/**
+	 * Erase all information of the HolonElement Model
+	 */
 	public void deleteRows() {
 		if (tableModelHolonElement.getRowCount() > 0) {
 			for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
@@ -948,6 +974,13 @@ public class GUI<E> implements CategoryListener {
 		}
 	}
 
+	/**
+	 * Add the Information of the given ArrayList in the HolonElement Model as
+	 * Name,Energy and Amount
+	 * 
+	 * @param elements
+	 *            ArrayList to be displayed
+	 */
 	public void fillElementTable(ArrayList<HolonElement> elements) {
 		for (HolonElement he : elements) {
 			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
@@ -956,7 +989,7 @@ public class GUI<E> implements CategoryListener {
 	}
 
 	/**
-	 * Update the information about properties of the selected CpsObject
+	 * Update the information concerning properties of the selected CpsObject
 	 */
 	public void refreshTableProperties() {
 		CpsObject tempCps = getActualCps();

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

@@ -364,8 +364,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				}
 				if (newEdge) {
 					e = new CpsEdge(cps, tempCps);
-					cps.AddConnection(e);
-					tempCps.AddConnection(e);
+//					cps.AddConnection(e);
+//					tempCps.AddConnection(e);
 					controller.AddEdgeOnCanvas(e);
 				}
 			}
@@ -390,16 +390,16 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				k = p.getB();
 
 				e = new CpsEdge(n, tempCps);
-				n.AddConnection(e);
-				tempCps.AddConnection(e);
+//				n.AddConnection(e);
+//				tempCps.AddConnection(e);
 
 				e1 = new CpsEdge(n, r);
-				n.AddConnection(e1);
-				r.AddConnection(e1);
+//				n.AddConnection(e1);
+//				r.AddConnection(e1);
 
 				e2 = new CpsEdge(n, k);
-				n.AddConnection(e2);
-				k.AddConnection(e2);
+//				n.AddConnection(e2);
+//				k.AddConnection(e2);
 
 				p.getA().getConnections().remove(p);
 				p.getB().getConnections().remove(p);
@@ -421,8 +421,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 			e = new CpsEdge(n, tempCps);
 
-			n.AddConnection(e);
-			tempCps.AddConnection(e);
+//			n.AddConnection(e);
+//			tempCps.AddConnection(e);
 			controller.AddEdgeOnCanvas(e);
 			System.out.println("node ID: " + n.getID());
 		}

+ 48 - 18
src/ui/view/UnitGraph.java

@@ -32,6 +32,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private Point recSize = new Point(8, 8); // Point Size
 	private Graphics2D g2;
 	private CubicCurve2D c = new CubicCurve2D.Double();
+	private CubicCurve2D cr = new CubicCurve2D.Double();
+	private CubicCurve2D cl = new CubicCurve2D.Double();
 	private LinkedList<Point> pointList;
 	private double scaleX;
 	private double scaleY;
@@ -80,9 +82,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		graphCurve.reset();
 
 		if (arrayOfValue != null) {
-			for (int i = 0; i < arrayOfValue.length; i++) {
-				arrayOfValue[i] = convertToValueY(getYValueAt((int) (i * width / (model.getIterations() - 1))));
-			}
+			fillArrayofValue();
 		}
 
 		// Draw the Vertical Lines
@@ -128,14 +128,15 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			}
 		}
 		// Actual Iteration Point Visualization
-		/*
-		 * g2.setColor(Color.RED); if (arrayOfValue != null) { for (int i = 0; i
-		 * < arrayOfValue.length; i++) { g2.fillOval((int) (i * width /
-		 * (model.getIterations() - 1) * scaleX - recSize.getX() / 2), (int)
-		 * (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() /
-		 * 2), (int) recSize.getX(), (int) recSize.getY()); } }
-		 */
 
+		g2.setColor(Color.RED);
+		if (arrayOfValue != null) {
+			for (int i = 0; i < arrayOfValue.length; i++) {
+				g2.fillOval((int) (i * width / (model.getIterations() - 1) * scaleX - recSize.getX() / 2),
+						(int) (convertToCanvasY((int) arrayOfValue[i]) * scaleY - recSize.getY() / 2),
+						(int) recSize.getX(), (int) recSize.getY());
+			}
+		}
 	}
 
 	@Override
@@ -152,7 +153,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				y = this.getHeight() / scaleY;
 			}
 			// x
-			if (tempP.getX() == 0 || tempP.getX() == this.getWidth() / scaleX || pointList.get(i + 1).getX() <= x
+			if (tempP == pointList.getFirst() || tempP == pointList.getLast() || pointList.get(i + 1).getX() <= x
 					|| pointList.get(i - 1).getX() >= x) {
 				x = tempP.getX();
 			}
@@ -289,7 +290,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @return the converted number
 	 */
 	public double convertToCanvasY(float d) {
-		System.out.println("" + (height + (d * (height / MAXIMUM))));
 		return (height - (d * (height / MAXIMUM)));
 	}
 
@@ -301,7 +301,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 * @return the converted number
 	 */
 	public float convertToValueY(double d) {
-		return Math.round(((float) ((height - (height * (d / height))) / (height / MAXIMUM)) * 10)) / 10;
+		return (float) Math.round(((height - (height * (d / height))) / (height / MAXIMUM)) * 10) / 10;
 	}
 
 	/**
@@ -347,30 +347,59 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		return c;
 	}
 
+	public void fillArrayofValue() {
+		for (int i = 0; i < arrayOfValue.length; i++) {
+			arrayOfValue[i] = convertToValueY(getYValueAt_2((int) (i * this.getWidth() / (model.getIterations() - 1))));
+		}
+	}
+	
 	/**
 	 * 
 	 * @param xVal,
 	 *            the x value for the y value
 	 * @return y, the value at x
 	 */
-	public int getYValueAt(int xVal) {
+	public float getYValueAt(int xVal) {
 		for (int i = 0; i < pointList.size() - 1; i++) {
 			// get the Points
-			if (xVal < pointList.get(i + 1).getX()) {
+			if (xVal <= pointList.get(i + 1).getX()) {
 				// Curve erstellen
 				Line2D l1 = new Line2D.Double(pointList.get(i).getX(), pointList.get(i).getY(),
 						pointList.get(i + 1).getX(), pointList.get(i + 1).getY());
 				Line2D l2 = new Line2D.Double(xVal, 0, xVal, height);
 
-				return (int) getIntersectionPoint(l1, l2).getY();
+				return (float) getIntersectionPoint(l1, l2).getY();
+			}
+		}
+		return 0;
+	}
+	
+	/**
+	 * 
+	 * @param xVal,
+	 *            the x value for the y value
+	 * @return y, the value at x
+	 */
+	public float getYValueAt_2(int xVal) {
+		for (int i = 0; i < pointList.size() - 1; i++) {
+			// get the Points
+			if (xVal <= pointList.get(i + 1).getX()) {
+				// Curve erstellen
+				c = buildCurve(pointList.get(i), pointList.get(i + 1));
+				for (int j = 0; j < height; j++) {
+					if (c.contains(xVal, j)) {
+						return j;
+					}
+				}
 			}
 		}
 		return 0;
 	}
 
 	public Point getIntersectionPoint(Line2D l1, Line2D l2) {
-		if (!l1.intersectsLine(l2))
+		if (!l1.intersectsLine(l2)) {
 			return null;
+		}
 		double px = l1.getX1(), py = l1.getY1(), rx = l1.getX2() - px, ry = l1.getY2() - py;
 		double qx = l2.getX1(), qy = l2.getY1(), sx = l2.getX2() - qx, sy = l2.getY2() - qy;
 
@@ -379,8 +408,9 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			return null;
 		} else {
 			double z = (sx * (qy - py) + sy * (px - qx)) / det;
-			if (z < 0 || z > 1)
+			if (z < 0 || z > 1) {
 				return new Point(0, 0); // intersection at end point!
+			}
 			return new Point((int) (px + z * rx), (int) (py + z * ry));
 		}
 	} // end intersection line-line