Browse Source

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 years ago
parent
commit
2e89daec80
4 changed files with 84 additions and 54 deletions
  1. 3 1
      src/classes/HolonObject.java
  2. 10 1
      src/ui/view/AddElementPopUp.java
  3. 6 2
      src/ui/view/GUI.java
  4. 65 50
      src/ui/view/UnitGraph.java

+ 3 - 1
src/classes/HolonObject.java

@@ -61,7 +61,9 @@ public class HolonObject extends CpsObject {
 	public float getCurrentEnergy() {
 		float temp = 0;
 		for (HolonElement e : getElements()) {
-			temp = temp + e.getTotalEnergy();
+			if (e.getActive()) {
+				temp = temp + e.getTotalEnergy();
+			}
 		}
 		currentEnergy = temp;
 		return currentEnergy;

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

@@ -148,8 +148,17 @@ public class AddElementPopUp extends JDialog {
 						} else {
 							// JOptionPane.showMessageDialog(new JFrame(),
 							// "Please enter a Name");
+
+							if (elementName.getText().length() == 0) {
+								JLabel errorString = new JLabel("No name");
+								errorString.setBounds(200, 8, 200, 20);
+								contentPanel.add(errorString);
+							} else if (repeated) {
+								JLabel errorString = new JLabel("Name already given");
+								errorString.setBounds(240, 8, 100, 20);
+								contentPanel.add(errorString);
+							}
 							elementName.setBackground(new Color(255, 50, 50));
-							// Jlabel repeatedString = new JLabel();
 						}
 					}
 				});

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

@@ -170,6 +170,7 @@ public class GUI<E> implements CategoryListener {
 	private int xTHIS;
 	private int yBTHIS;
 	private int xBTHIS;
+	private CpsObject temp = null;
 
 	/**
 	 * Create the application.
@@ -661,7 +662,10 @@ public class GUI<E> implements CategoryListener {
 		canvas.addMouseListener(new MouseAdapter() {
 			@Override
 			public void mousePressed(MouseEvent e) {
-				CpsObject temp = getActualCps();
+				if(temp == null || temp.getID() != model.getSelectedObjectID()){
+					unitGraph.empty();
+				}				
+				temp = getActualCps();
 				// Update of the Information about the HolonElements - only for
 				if (temp instanceof HolonObject) {
 					refreshTableHolonElement();
@@ -729,7 +733,7 @@ public class GUI<E> implements CategoryListener {
 					tableModelProperties.setCellEditable(2, 1, true);
 				} else if (getActualCps() == null) {
 					deleteRows();
-				}
+				} 
 			}
 
 		});

+ 65 - 50
src/ui/view/UnitGraph.java

@@ -81,10 +81,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 		graphCurve.reset();
 
-		if (arrayOfValue != null) {
-			fillArrayofValue();
-		}
-
 		// Draw the Vertical Lines
 		g2.setColor(new Color(240, 240, 240));
 		for (int i = 0; i < model.getIterations(); i++) {
@@ -97,22 +93,28 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					(i) * this.getHeight() / (model.getIterations() - 1));
 		}
 
-		// Draw the Lines
-		g2.setColor(Color.BLACK);
-		for (int i = 0; i < pointList.size() - 1; i++) {
-			c = buildCurve(pointList.get(i), pointList.get(i + 1));
-			graphCurve.append(c, true);
-		}
-		g2.draw(graphCurve);
+		if (arrayOfValue != null) {
+			//array fillen
+			fillArrayofValue();
 
-		// Draw the Points
-		g2.setColor(Color.BLUE);
-		for (int i = 0; i < pointList.size() - 0; i++) {
-			g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
-					(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
-					(int) recSize.getY());
+			// Draw the Lines
+			g2.setColor(Color.BLACK);
+			for (int i = 0; i < pointList.size() - 1; i++) {
+				c = buildCurve(pointList.get(i), pointList.get(i + 1));
+				graphCurve.append(c, true);
+			}
+			g2.draw(graphCurve);
+
+			// Draw the Points
+			g2.setColor(Color.BLUE);
+			for (int i = 0; i < pointList.size() - 0; i++) {
+				g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2),
+						(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2), (int) recSize.getX(),
+						(int) recSize.getY());
+			}
 		}
 		// Iteration Line
+		g2.setColor(Color.BLUE);
 		g2.drawLine((model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), 0,
 				(model.getCurIteration()) * this.getWidth() / (model.getIterations() - 1), this.getHeight());
 		// Iteration Value
@@ -158,9 +160,10 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				x = tempP.getX();
 			}
 			tempP.setLocation(x, y);
+			repaint();
 		}
 
-		repaint();
+		
 	}
 
 	@Override
@@ -193,39 +196,41 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 
 		// Click on Point
 		tempP = null;
-		for (Point p : pointList) {
-			if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
-					&& x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
-				if (e.getButton() == MouseEvent.BUTTON3) {
-					tempP = p;
-					deletePoint = true;
-				} else {
-					pointDrag = true;
-					tempP = p;
+		if (pointList != null) {
+			for (Point p : pointList) {
+				if (x >= p.getX() - recSize.getX() / 2 && y >= p.getY() - recSize.getY() / 2
+						&& x <= p.getX() + recSize.getX() / 2 && y <= p.getY() * scaleY + recSize.getY() / 2) {
+					if (e.getButton() == MouseEvent.BUTTON3) {
+						tempP = p;
+						deletePoint = true;
+					} else {
+						pointDrag = true;
+						tempP = p;
+					}
 				}
 			}
-		}
-		if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
-				&& e.getX() != this.getWidth() / scaleX) {
-			for (int i = 0; i < pointList.size(); i++) {
-				if (x < pointList.get(i).getX() && !added) {
-					if (e.getY() <= 0) {
-						pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
-					} else {
-						pointList.add(i, new Point((int) (x), (int) y));
+			if (!pointDrag && e.getButton() != MouseEvent.BUTTON3 && e.getX() != 0
+					&& e.getX() != this.getWidth() / scaleX) {
+				for (int i = 0; i < pointList.size(); i++) {
+					if (x < pointList.get(i).getX() && !added) {
+						if (e.getY() <= 0) {
+							pointList.add(i, new Point((int) (x), (int) (0 / scaleY)));
+						} else {
+							pointList.add(i, new Point((int) (x), (int) y));
+						}
+						added = true;
+						pointDrag = true;
+						tempP = pointList.get(i);
 					}
-					added = true;
-					pointDrag = true;
-					tempP = pointList.get(i);
 				}
 			}
-		}
-		if (deletePoint && tempP.getX() != 0
-				&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
-			pointList.remove(tempP);
-		}
+			if (deletePoint && tempP.getX() != 0
+					&& (tempP.getX() != this.getWidth() / scaleX || tempP != pointList.getLast())) {
+				pointList.remove(tempP);
+			}
 
-		repaint();
+			repaint();
+		}
 	}
 
 	@Override
@@ -273,7 +278,17 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	}
 
 	/*
-	 * Resets the Graph
+	 * Emptys the Graph
+	 */
+	public void empty() {
+		pointList = null;
+		tempElement = null;
+		arrayOfValue = null;
+		repaint();
+	}
+
+	/*
+	 * Resets the Points for the Element
 	 */
 	public void reset() {
 		pointList.removeAll(pointList);
@@ -390,14 +405,14 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				// Teil der Kurve aussuchen
 				if (cl.getX1() <= xVal * scaleX && cl.getX2() > xVal * scaleX) {
 					c = cl;
-					//Kurve Links von "unten"
+					// Kurve Links von "unten"
 					if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
 						for (int j = (int) (height - 1); j >= 0; j--) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
 						}
-					} else {//Kurve Links von "oben"
+					} else {// Kurve Links von "oben"
 						for (int j = 0; j < height; j++) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
@@ -406,14 +421,14 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 					}
 				} else {
 					c = cr;
-					//Kurve Links von "unten"
+					// Kurve Links von "unten"
 					if (pointList.get(i).getY() >= pointList.get(i + 1).getY()) {
 						for (int j = 0; j < height; j++) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);
 							}
 						}
-					} else {//Kurve Links von "oben"
+					} else {// Kurve Links von "oben"
 						for (int j = (int) (height - 1); j >= 0; j--) {
 							if (c.contains(xVal * scaleX, j * scaleY)) {
 								return (float) (j);