Procházet zdrojové kódy

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

Conflicts:
	src/classes/CpsEdge.java
	src/classes/CpsObject.java
Edgardo Palza před 8 roky
rodič
revize
a3b7de2f7b

+ 129 - 0
src/API/CpsAPI.java

@@ -0,0 +1,129 @@
+package API;
+
+import java.awt.Color;
+import java.util.ArrayList;
+
+import classes.CpsEdge;
+import classes.CpsObject;
+import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.subNet;
+import ui.controller.Control;
+import ui.controller.SimulationManager;
+
+public class CpsAPI {
+	private Control controller;
+	private SimulationManager simManager;
+	
+	public CpsAPI(Control cont){
+		this.controller = cont;
+		this.simManager = controller.getSimManager();
+	}
+	
+	/**
+	 * a SubNet contains all Components
+	 * @return all SubNets on Canvas
+	 */
+	public ArrayList<subNet> getSubNets(){
+		simManager.searchForSubNets();
+		return simManager.getSubNets();
+	}
+	
+	/**
+	 * 
+	 * @return all Objects on Canvas in no specific order
+	 */
+	public ArrayList<CpsObject> getAllObjOnCanvas(){
+
+		return controller.getModel().getObjectsOnCanvas();
+	}
+	
+	/**
+	 * 
+	 * @return all Edges on Canvas
+	 */
+	public ArrayList<CpsEdge> getAllEdges(){
+		ArrayList<CpsEdge> result = new ArrayList<CpsEdge>();
+		for(subNet sN: getSubNets()){
+			result.addAll(sN.getEdges());
+		}
+		return result;
+	}
+	
+	/**
+	 * 
+	 * @return all Switches on Canvas
+	 */
+	public ArrayList<HolonSwitch> getAllSwitches(){
+		ArrayList<HolonSwitch> result = new ArrayList<HolonSwitch>();
+		for(subNet sN: getSubNets()){
+			result.addAll(sN.getSwitches());
+		}
+		return result;
+	}
+	
+	public ArrayList<HolonObject> getAllHolonObjects(){
+		ArrayList<HolonObject> result = new ArrayList<HolonObject>();
+		for(subNet sN: getSubNets()){
+			result.addAll(sN.getObjects());
+		}
+		return result;
+	}
+	/**
+	 * prints a given text on the console
+	 * @param text
+	 */
+	public void consolePrint(String text){
+		controller.addTextToConsole(text);
+	}
+	
+	/**
+	 * prints a given text on the console with more details
+	 * @param text: the text that will be printed
+	 * @param color: the color the text will have
+	 * @param p: font size
+	 * @param bold: true or false
+	 * @param italic: true or false
+	 * @param nl: new line after text
+	 */
+	public void consolePrint(String text, Color color, int p, boolean bold, boolean italic, boolean nl){
+		controller.addTextToConsole(text, color, p, bold, italic, nl);
+	}
+	
+	/**
+	 * changes the bordercolor of given object to given color
+	 * @param toChange
+	 * @param color
+	 */
+	public void setBorderColorForObj(CpsObject toChange, Color color){
+		toChange.setBorderColor(color);
+	}
+	
+	/**
+	 * changes the borderColor for all objects in List to given color
+	 * @param objects
+	 * @param color
+	 */
+	public void setBorderColorForMultObj(ArrayList<CpsObject> objects, Color color){
+		for(CpsObject cps: objects){
+			setBorderColorForObj(cps, color);
+		}
+	}
+	
+	/**
+	 * resets the bordercolor of given object to default (white)
+	 * @param toReset
+	 */
+	public void resetBorderColor(CpsObject toReset){
+		toReset.setBorderColor(Color.WHITE);
+	}
+	
+	/**
+	 * resets the bordercolor for all objects on canvas
+	 */
+	public void resetBorderColorForAll(){
+		setBorderColorForMultObj(getAllObjOnCanvas(), Color.WHITE);
+	}
+	
+	
+}

+ 8 - 0
src/API/CpsAlgorithm.java

@@ -0,0 +1,8 @@
+package API;
+
+public interface CpsAlgorithm {
+	CpsAPI API = null;
+	
+	public void setAPI(CpsAPI api);
+	public void runAlgorithm();
+}

+ 10 - 0
src/classes/CpsEdge.java

@@ -108,6 +108,7 @@ public class CpsEdge {
 		 * isWorking = false; state = false; }
 		 **/
 	}
+<<<<<<< HEAD
 
 	/**
 	 * Calculates the state of the edge (see description of isWorking)
@@ -116,6 +117,15 @@ public class CpsEdge {
 	 */
 	public void calculateState(boolean simMode) {
 		if (flow > maxCapacity && maxCapacity != -1) {
+=======
+	
+	/**
+	 * calculates wether isWorking or not
+	 * @param simMode
+	 */
+	public void calculateState(boolean simMode){
+		if(flow > maxCapacity && maxCapacity != -1){
+>>>>>>> 0f397a62123e10fe88ef0d9ee68b3db6c0201ec8
 			isWorking = false;
 		} else {
 			if (!simMode && flow <= maxCapacity) {

+ 21 - 0
src/classes/CpsObject.java

@@ -233,8 +233,20 @@ public abstract class CpsObject {
 	}
 
 	/**
+<<<<<<< HEAD
 	 * Set the colorof the border
 	 * 
+=======
+	 * Set the Border Color of this CpsObject
+	 * 
+	 * @param the BorderColor
+	 */
+	public void setBorderColor(Color c) {
+		this.BorderColor = c;
+	}
+
+	/**
+>>>>>>> 0f397a62123e10fe88ef0d9ee68b3db6c0201ec8
 	 * @param Color
 	 *            the Color to set
 	 */
@@ -242,27 +254,36 @@ public abstract class CpsObject {
 		this.BorderColor = color;
 	}
 
+<<<<<<< HEAD
 	/**
 	 * For internal purpose (energy flow)
 	 * 
 	 * @param tag
 	 */
+=======
+>>>>>>> 0f397a62123e10fe88ef0d9ee68b3db6c0201ec8
 	public void addTag(int tag) {
 		this.tags.add(tag);
 	}
 
+<<<<<<< HEAD
 	/**
 	 * Get the actual tags
 	 * 
 	 * @return ArrayList
 	 */
+=======
+>>>>>>> 0f397a62123e10fe88ef0d9ee68b3db6c0201ec8
 	public ArrayList<Integer> getTag() {
 		return tags;
 	}
 
+<<<<<<< HEAD
 	/**
 	 * Reste the tags to Null
 	 */
+=======
+>>>>>>> 0f397a62123e10fe88ef0d9ee68b3db6c0201ec8
 	public void resetTags() {
 		this.tags = new ArrayList<Integer>();
 	}

+ 2 - 0
src/classes/HolonObject.java

@@ -91,6 +91,8 @@ public class HolonObject extends CpsObject {
 	}
 
 	/**
+	 * doesnt take into account which timestep is watched,
+	 * calculates the max values
 	 * @return the currentEnergy
 	 */
 	public float getCurrentEnergy() {

+ 1 - 1
src/ui/controller/Control.java

@@ -94,7 +94,7 @@ public class Control {
 	}
 
 	public void addSwitch(Category cat, String obj) {
-		categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-off.png");
+		categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
 	}
 
 	public void deleteCategory(String cat) {

+ 1 - 1
src/ui/controller/ObjectController.java

@@ -118,7 +118,7 @@ public class ObjectController {
 	}
 
 	/**
-	 * adds a selectedObject
+	 * 	s a selectedObject
 	 * 
 	 * @param Cpsobject
 	 */

+ 4 - 0
src/ui/controller/SimulationManager.java

@@ -387,5 +387,9 @@ public class SimulationManager {
 		copyObjects(model.getObjectsOnCanvas());
 	}
 	
+	public ArrayList<subNet> getSubNets(){
+		return subNets;
+	}
+	
 	
 }

+ 2 - 0
src/ui/view/CategoryPanel.java

@@ -3,3 +3,5 @@ package ui.view;
 public class CategoryPanel {
 
 }
+
+//hier müsste das Panel eigentlich sein

+ 34 - 40
src/ui/view/GUI.java

@@ -373,7 +373,7 @@ public class GUI<E> implements CategoryListener {
 				unitGraph.empty();
 			}
 		});
-		
+
 		String cntrlFDown = "controlF";
 		inputMap.put(KeyStroke.getKeyStroke("control F"), cntrlFDown);
 		actionMap.put(cntrlFDown, new AbstractAction() {
@@ -386,9 +386,6 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
-		
-		
-
 		String cntrlCDown = "controlC";
 		inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
 		actionMap.put(cntrlCDown, new AbstractAction() {
@@ -888,12 +885,11 @@ public class GUI<E> implements CategoryListener {
 							if (value.toString().compareTo(cps.getObjName()) == 0) {
 								File checkPath = new File(cps.getImage());
 								if (checkPath.exists()) {
-									imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
-											50, 50, java.awt.Image.SCALE_SMOOTH);
+									imgR = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
+											java.awt.Image.SCALE_SMOOTH);
 								} else {
 									imgR = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-											.getScaledInstance(50, 50,
-													java.awt.Image.SCALE_SMOOTH);
+											.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
 								}
 								if (imgR != null) {
 									label.setIcon(new ImageIcon(imgR));
@@ -995,13 +991,11 @@ public class GUI<E> implements CategoryListener {
 								if (actualObjectClicked.compareTo(cps.getObjName()) == 0) {
 									File checkPath = new File(cps.getImage());
 									if (checkPath.exists()) {
-										img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
-												50, 50,
+										img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(50, 50,
 												java.awt.Image.SCALE_SMOOTH);
 									} else {
 										img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-												.getScaledInstance(50, 50,
-														java.awt.Image.SCALE_SMOOTH);
+												.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
 									}
 									tempCps = cps;
 									dragging = true;
@@ -1034,7 +1028,7 @@ public class GUI<E> implements CategoryListener {
 
 		panel.add(toolBar);
 		toolBar.add(comboBox);
-		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
+		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Switch" }));
 		// Add Button
 		btnAdd.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
@@ -1042,32 +1036,36 @@ public class GUI<E> implements CategoryListener {
 				Object nodeInfo = tree.getLastSelectedPathComponent();
 				String selectedOption = comboBox.getSelectedItem().toString();
 				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
+				try {
 
-				switch (selectedOption) {
+					switch (selectedOption) {
 
-				case "Category":
-					String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
-					if (catName.length() != 0) {
-						controller.addCategory(catName);
-					}
-					break;
-				case "Object":
-					if (selectedNode == null) {
-						JOptionPane.showMessageDialog(new JFrame(),
-								"Please select a Category first before adding " + selectedOption + ".");
-					}
-					if (selectedNode.getLevel() == 1) {
-						CpsObject tmp = new HolonObject("");
-						addObjectPopUP = new AddObjectPopUp(false, tmp, null);
-						addObjectPopUP.setVisible(true);
-						addObjectPopUP.setController(controller);
-						addObjectPopUP.setCategory(selectedNode.toString());
-					}
-					break;
+					case "Category":
+						String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
+						if (catName.length() != 0) {
+							controller.addCategory(catName);
+						}
+						break;
+					case "Object":
+						if (selectedNode == null) {
+							JOptionPane.showMessageDialog(new JFrame(),
+									"Please select a Category first before adding " + selectedOption + ".");
+						}
+						if (selectedNode.getLevel() == 1) {
+							CpsObject tmp = new HolonObject("");
+							addObjectPopUP = new AddObjectPopUp(false, tmp, null);
+							addObjectPopUP.setVisible(true);
+							addObjectPopUP.setController(controller);
+							addObjectPopUP.setCategory(selectedNode.toString());
+						}
+						break;
 
-				default:
-					addObjectAction(selectedOption, selectedNode);
-					break;
+					default:
+						addObjectAction(selectedOption, selectedNode);
+						break;
+					}
+				} catch (Exception e) {
+					// TODO: handle exception
 				}
 				tree.repaint();
 			}
@@ -1502,10 +1500,6 @@ public class GUI<E> implements CategoryListener {
 					case "Switch":
 						controller.addSwitch(cat, objname);
 						break;
-
-					case "Transformer":
-						controller.addTransformer(cat, objname);
-						break;
 					}
 				}
 			} else {

+ 6 - 3
src/ui/view/MyCanvas.java

@@ -156,8 +156,7 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
 
-		// drawEdges
-		// g2.setColor(Color.BLACK);
+		// drawEdges that is being dragged
 		if (drawEdge) {
 			g2.setColor(Color.BLACK);
 			g2.setStroke(new BasicStroke(2));
@@ -230,7 +229,11 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 			}
 		} else if (edgeHighlight != null) {
 			g2.setColor(Color.BLUE);
-			g2.setStroke(new BasicStroke(2));
+			if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
+				g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
+			} else {
+				g2.setStroke(new BasicStroke(2));
+			}
 			g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
 					edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
 					edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),

+ 13 - 5
src/ui/view/SimulationMenu.java

@@ -51,7 +51,7 @@ public class SimulationMenu extends JMenuBar {
 	private JComboBox algoCombo = new JComboBox<>();
 	private JButton algoFolderButton = new JButton("Algorithm:");
 	private HashMap<String, File> algosHash = new HashMap<>();
-	
+
 	Model model;
 	Control controller;
 
@@ -81,9 +81,16 @@ public class SimulationMenu extends JMenuBar {
 					for (int i = 0; i < files.length; i++) {
 						if (files[i].toString().endsWith(".java") || files[i].toString().endsWith(".class")) {
 							String name = files[i].toString();
-							int tmpA = name.lastIndexOf('/');
+							int tmpS = name.lastIndexOf('/');
+							int tmpBS = name.lastIndexOf('\\');
+							int tmpA;
+							if (tmpS > tmpBS) {
+								tmpA = tmpS;
+							} else {
+								tmpA = tmpBS;
+							}
 							int tmpB = name.lastIndexOf('.');
-							name = name.substring(tmpA+1, tmpB);
+							name = name.substring(tmpA + 1, tmpB);
 							algosHash.put(name, files[i]);
 							algoCombo.addItem(name);
 						}
@@ -107,6 +114,7 @@ public class SimulationMenu extends JMenuBar {
 			@Override
 			public void propertyChange(PropertyChangeEvent evt) {
 				controller.setIsSimulation(simButton.isSelected());
+				controller.calculateStateForCurrentTimeStep();
 			}
 		});
 		GridBagConstraints gbc_simButton = new GridBagConstraints();
@@ -155,8 +163,8 @@ public class SimulationMenu extends JMenuBar {
 		gbc_algoCombo.gridy = 0;
 		menuPanel.add(algoCombo, gbc_algoCombo);
 		algoCombo.addItem("choose folder");
-		
-		//Add Panel to SimulationMenu
+
+		// Add Panel to SimulationMenu
 		this.add(menuPanel);
 	}
 }

+ 32 - 8
src/ui/view/UnitGraph.java

@@ -19,8 +19,6 @@ import java.awt.Point;
 
 import javax.swing.JPanel;
 
-import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
-
 import classes.HolonElement;
 import ui.controller.Control;
 import ui.model.Model;
@@ -33,12 +31,17 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private static final long serialVersionUID = 1L;
 	private float MAXIMUM = 0;
 
+	// Information shown when a Point is Dragged
+	private String dragInformation = "";
+
+	// Points
 	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;
+	// Scale for the Graph
 	private double scaleX;
 	private double scaleY;
 
@@ -52,7 +55,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	private boolean isSwitch = false;
 
 	private ArrayList<HolonElement> tempElements = new ArrayList<>();
-	private HolonSwitch tempSwitch;
 	private Model model;
 	private Control controller;
 	private Line2D.Double line = null;
@@ -105,6 +107,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		if (isElement) {
 			// array fillen
 			fillArrayofValue();
+
 			if (arrayOfFloats != null) {
 				// Draw the Lines
 				g2.setStroke(new BasicStroke(2));
@@ -130,6 +133,12 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 							this.getHeight() - 10);
 				}
 			}
+			// drag Information
+			if (tempP != null) {
+				dragInformation = "" + convertToValueY(getYValueAt((int) tempP.getX()));
+				g2.drawString(dragInformation, (int) (tempP.getX() * scaleX) + 10, (int) (tempP.getY() * scaleY) + 10);
+			}
+
 			/*
 			 * // Actual Iteration Point Visualization g2.setColor(Color.RED);
 			 * if (arrayOfValue != null) { for (int i = 0; i <
@@ -169,7 +178,21 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 							this.getHeight() - 10);
 				}
 			}
+			if (tempP != null) {
+				try {
+					int i;
+					for (i = 0; (i * this.getWidth() / (model.getIterations() - 1) < getMousePosition().getX()); i++) {
+
+					}
+					dragInformation = "" + i;
+					g2.drawString(dragInformation, (int) (getMousePosition().getX()) + 10,
+							(int) (getMousePosition().getY()) + 10);
+				} catch (Exception e) {
+					// TODO: handle exception
+				}
+			}
 		}
+
 		// Iteration Line
 		g2.setColor(Color.BLUE);
 		g2.setStroke(new BasicStroke(1));
@@ -212,6 +235,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 				x = tempP.getX();
 			}
 			tempP.setLocation(x, y);
+
 			repaint();
 		}
 	}
@@ -334,7 +358,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		boolean deletePoint = false;
 
 		double x = e.getX() / scaleX;
-		double y = e.getY() / scaleY;
+		e.getY();
 
 		// Halbe Iterations Distanz
 		double dist = (width / (model.getIterations() - 1)) / 2;
@@ -407,6 +431,8 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 			pointDrag = false;
 			tempP = null;
 		}
+		dragInformation = "";
+		repaint();
 	}
 
 	public void componentResized(ComponentEvent e) {
@@ -443,12 +469,11 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	}
 
 	/*
-	 * Emptys the Graph
+	 * Empty the Graph
 	 */
 	public void empty() {
 		pointList = null;
 		tempElements = null;
-		tempSwitch = null;
 		arrayOfFloats = null;
 		arrayOfBooleans = null;
 		isSwitch = false;
@@ -501,7 +526,7 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 		isSwitch = false;
 		isElement = true;
 		MAXIMUM = selectedElement.get(selectedElement.size() - 1).getEnergy();
-  		// First time clicked on the Element
+		// First time clicked on the Element
 		if (pointList.isEmpty()) {
 			pointList.addFirst(new Point(0, 0));
 			pointList.addLast(new Point((int) (this.getWidth() / scaleX), 0));
@@ -517,7 +542,6 @@ class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, Co
 	 */
 	public void repaintWithNewSwitch(HolonSwitch s) {
 		arrayOfBooleans = s.getActiveAt();
-		tempSwitch = s;
 		pointList = s.getGraphPoints();
 		isSwitch = true;
 		isElement = false;