Quellcode durchsuchen

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

Jessey Widhalm vor 8 Jahren
Ursprung
Commit
9d345f31bf

+ 1 - 1
src/interfaces/GraphListener.java

@@ -6,7 +6,7 @@ import classes.HolonObject;
 
 public interface GraphListener {
 	
-	public void repaintGraph();
+	public void repaintTree();
 	
 	public void addTrackedObject(ArrayList<HolonObject> hlList);
 

+ 35 - 2
src/ui/controller/MultiPurposeController.java

@@ -115,7 +115,7 @@ public class MultiPurposeController {
 	public HolonElement searchEleById(HolonObject object, int idEle) {
 		return object.searchElementById(idEle);
 	}
-	
+
 	/**
 	 * 
 	 * @param upperNode
@@ -123,7 +123,7 @@ public class MultiPurposeController {
 	 * @return
 	 */
 	public AbstractCpsObject searchIDUpperNode(CpsUpperNode upperNode, int id) {
-		
+
 		Integer idx;
 
 		if ((idx = upperNode.getNodesIdx().get(id)) == null || upperNode.getNodesIdx().size() < 1)
@@ -176,6 +176,39 @@ public class MultiPurposeController {
 		}
 	}
 
+	/**
+	 * Adjust Indices before porting them from one map to another
+	 * 
+	 * @param key
+	 *            the Key
+	 * @param <T>
+	 *            key type
+	 * @param map
+	 *            the Map
+	 */
+	public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
+
+		for (Entry<T, Integer> i : map.entrySet()) {
+			i.setValue(i.getValue() + x + 1);
+		}
+	}
+
+	/**
+	 * Returns the highest Id
+	 * @param map
+	 * @return
+	 */
+	public <T> int getHighestIdx(HashMap<T, Integer> map) {
+		int max = 0;
+
+		for (T i : map.keySet()) {
+			if (map.get(i) > max)
+				max = map.get(i);
+		}
+		
+		return max;
+	}
+
 	/**
 	 * Copies a HashMap into a new One.
 	 * 

+ 2 - 0
src/ui/controller/NodeController.java

@@ -118,6 +118,8 @@ public class NodeController {
 		// TODO Auto-generated method stub
 		// add all nodes into upperNode
 		(upperNode == null ? model.getObjectsOnCanvas() : upperNode.getNodes()).addAll(node.getNodes());
+		// change the indices accordingly the higher layer
+		mpC.adjustIdx(mpC.getHighestIdx((upperNode == null ? model.getCvsObjIdx() : upperNode.getNodesIdx())), node.getNodesIdx());
 		// add all indices of nodes into upperNode
 		(upperNode == null ? model.getCvsObjIdx() : upperNode.getNodesIdx()).putAll(node.getNodesIdx());
 		// add all Edges of node into upperNode

+ 1 - 0
src/ui/controller/UpdateController.java

@@ -201,6 +201,7 @@ public class UpdateController {
 	 */
 	public AbstractCpsObject getActualCps() {
 		int tempID = model.getSelectedObjectID();
+		System.out.println(model.getSelectedObjectID());
 		AbstractCpsObject tempCps = controller.searchByID(tempID);
 		return tempCps;
 	}

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

@@ -390,7 +390,7 @@ public class Model {
 
 	private void notifyGraphListeners() {
 		for (GraphListener gl : graphListeners) {
-			gl.repaintGraph();
+			gl.repaintTree();
 		}
 
 	}
@@ -707,7 +707,7 @@ public class Model {
 	public void addObjectsToGraphListeners() {
 		for (GraphListener gl : graphListeners) {
 			gl.addTrackedObject(trackingObj);
-			gl.repaintGraph();
+			gl.repaintTree();
 		}
 	}
 

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

@@ -737,6 +737,7 @@ public class GUI<E> implements CategoryListener {
 		model.getTableHolonElement().addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				HolonObject obj = (HolonObject) updCon.getActualCps();
+				System.out.println(obj);
 				yValueElements = e.getY();
 				HolonElement ele = null;
 				// Search for current clicked HolonElement
@@ -1524,8 +1525,8 @@ public class GUI<E> implements CategoryListener {
 			public void stateChanged(ChangeEvent e) {
 				int i = model.getCurIteration();
 				controller.calculateStateForTimeStep(i);
-
 				unitGraph.repaint();
+				statSplitPane.repaintGraphs();
 			}
 		});
 		splitPane.setRightComponent(splitPane1);
@@ -1835,7 +1836,8 @@ public class GUI<E> implements CategoryListener {
 			unc.addMouseListener(new MouseAdapter() {
 				@Override
 				public void mousePressed(MouseEvent e) {
-					temp = ((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport().getComponent(0)).tempCps;
+					temp = ((UpperNodeCanvas) ((JScrollPane) tabbedPane.getSelectedComponent()).getViewport()
+							.getComponent(0)).tempCps;
 					if (doubleClick() && temp instanceof CpsUpperNode) {
 						openNewUpperNodeTab();
 					}

+ 0 - 1
src/ui/view/MyCanvas.java

@@ -472,7 +472,6 @@ public class MyCanvas extends JPanel implements MouseListener, MouseMotionListen
 		dataSelected = null;
 		edgeHighlight = null;
 		controller.setSelecteEdge(null);
-		// System.out.println(model.getEdgesOnCanvas().size());
 		// Object Selection
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
 			cx = cps.getPosition().x;

+ 13 - 12
src/ui/view/StatisticGraph.java

@@ -44,7 +44,7 @@ public class StatisticGraph extends JPanel {
 	GeneralPath path = new GeneralPath();
 
 	// Data
-	private ArrayList<TrackedDataSet> objects = new ArrayList<>();
+	public ArrayList<TrackedDataSet> objects = new ArrayList<>();
 
 	/**
 	 * Constructor.
@@ -57,7 +57,7 @@ public class StatisticGraph extends JPanel {
 	public StatisticGraph(final Model model, Control control) {
 		this.controller = control;
 		this.model = model;
-
+		
 		this.setBackground(Color.WHITE);
 	}
 
@@ -94,6 +94,8 @@ public class StatisticGraph extends JPanel {
 
 			// Calculate the Maximum
 			calcMaximum();
+			((StatisticGraphPanel)this.getParent()).setMaximumLabel(maximum);
+			((StatisticGraphPanel)this.getParent()).makeLegendPanel();
 
 			// Calculate values for each set and add them
 			addValues();
@@ -181,7 +183,7 @@ public class StatisticGraph extends JPanel {
 			case TrackedDataSet.CONSUMPTION:
 				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
 					if (h.getEnergy() < 0) {
-						val += h.getEnergy();
+						val += h.getEnergy()*h.getAmount();
 					}
 				}
 				val *= -1;
@@ -189,7 +191,7 @@ public class StatisticGraph extends JPanel {
 			case TrackedDataSet.PRODUCTION:
 				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
 					if (h.getEnergy() > 0) {
-						val += h.getEnergy();
+						val += h.getEnergy()*h.getAmount();
 					}
 				}
 
@@ -220,16 +222,16 @@ public class StatisticGraph extends JPanel {
 			switch (set.getProperty()) {
 			case TrackedDataSet.CONSUMPTION:
 				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
-					if (h.getEnergy() < 0) {
-						val += Math.abs(h.getEnergyAt()[model.getCurIteration()]);
+					if (h.getEnergy() < 0 && h.getActive()) {
+						val += Math.abs(h.getEnergyAt()[model.getCurIteration()])*h.getAmount();
 					}
 					set.setValAt(val, model.getCurIteration());
 				}
 				break;
 			case TrackedDataSet.PRODUCTION:
 				for (HolonElement h : ((HolonObject) set.getCpsObject()).getElements()) {
-					if (h.getEnergy() > 0) {
-						val += Math.abs(h.getEnergyAt()[model.getCurIteration()]);
+					if (h.getEnergy() > 0 && h.getActive()) {
+						val += Math.abs(h.getEnergyAt()[model.getCurIteration()])*h.getAmount();
 					}
 					set.setValAt(val, model.getCurIteration());
 				}
@@ -272,9 +274,8 @@ public class StatisticGraph extends JPanel {
 	private void createPathFloats(TrackedDataSet set) {
 		boolean init = true;
 		path.moveTo(0, 0);
-		for (int i = 0; i < model.getCurIteration() - 1; i++) {
-			controller.addTextToConsole(path.getCurrentPoint().getX() + ", " + path.getCurrentPoint().getY());
-			if (init /* && set.getValues()[i] != -1 */) {
+		for (int i = 0; i < model.getCurIteration(); i++) {
+			if (init && set.getValues()[i] != -1 ) {
 				path.moveTo(i * this.getWidth() / model.getIterations() - 1, convertToCanvasY(set.getValues()[i]));
 				init = false;
 			}
@@ -297,7 +298,7 @@ public class StatisticGraph extends JPanel {
 	 */
 	private void createPathBooleans(TrackedDataSet set) {
 		boolean init = true;
-		for (int i = 0; i < model.getCurIteration() - 1; i++) {
+		for (int i = 0; i < model.getCurIteration(); i++) {
 			if (init && set.getValues()[i] != -1) {
 				path.moveTo(i * this.getWidth() / model.getIterations() - 1,
 						convertToCanvasY((float) (set.getValues()[i] * maximum)));

+ 71 - 22
src/ui/view/StatisticGraphPanel.java

@@ -5,16 +5,22 @@ import javax.swing.JPanel;
 import classes.TrackedDataSet;
 import ui.controller.Control;
 import ui.model.Model;
+
 import javax.swing.JLabel;
-import javax.swing.BoxLayout;
 import javax.swing.JButton;
 import javax.swing.SwingConstants;
+import javax.swing.Timer;
+
 import java.awt.BorderLayout;
-import javax.swing.border.LineBorder;
 import java.awt.Color;
 import java.awt.Dimension;
+import java.awt.FlowLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+import java.util.Hashtable;
 
 public class StatisticGraphPanel extends JPanel {
 
@@ -33,7 +39,10 @@ public class StatisticGraphPanel extends JPanel {
 
 	// Variables
 	String graphName;
-	private final JPanel Legendpanel = new JPanel();
+	private final JPanel legendPanel = new JPanel();
+	private JPanel that;
+	private Hashtable<String, StatisticGraphPanel> graphHashtable;
+	private JPanel tempP; // for makeLegend
 
 	/**
 	 * Constructor.
@@ -43,46 +52,58 @@ public class StatisticGraphPanel extends JPanel {
 	 * @param cont
 	 *            the Controller
 	 */
-	public StatisticGraphPanel(Model mod, Control cont, String name) {
+	public StatisticGraphPanel(Model mod, Control cont, String name, Hashtable<String, StatisticGraphPanel> gHt) {
 		super();
-		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
 		this.model = mod;
 		this.controller = cont;
 		this.sGraph = new StatisticGraph(mod, cont);
 		this.graphName = name;
+		this.graphHashtable = gHt;
 		setLayout(new BorderLayout(0, 0));
 
 		// ******************** Component Propertys ***************//
-		//Graph Name
+		// Graph Name
 		graphNameLabel = new JLabel(graphName);
 		graphNameLabel.setHorizontalTextPosition(JLabel.CENTER);
 
-		//Panel on top (Name and Close Button)
+		// Panel on top (Name and Close Button)
 		topPanel.setLayout(new BorderLayout(0, 0));
 		topPanel.add(graphNameLabel, BorderLayout.CENTER);
 		topPanel.add(closeButton, BorderLayout.EAST);
 		topPanel.setBorder(null);
-		
-		//Maximum Label
+
+		// Maximum Label
 		maximumLabel.setVerticalAlignment(SwingConstants.TOP);
-		maximumLabel.setPreferredSize(new Dimension(30,10));
+		maximumLabel.setMinimumSize(new Dimension(30, 10));
+
+		// Legend Panel
+		makeLegendPanel();
+
 		// ******************** Component Listener ****************//
-		
-		JPanel that = this;
+
+		that = this;
 		closeButton.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				JPanel parent = (JPanel) that.getParent();
+				for (int i = 0; i < parent.getComponentCount(); i++) {
+					if (parent.getComponent(i).equals(that)) {
+						parent.remove(parent.getComponent(i + 1));
+						break;
+					}
+				}
+				graphHashtable.remove(graphName);
 				parent.remove(that);
-				parent.revalidate();
+				parent.updateUI();
 			}
 		});
-		
+
 		// ******************** add everything ********************//
 		this.add(sGraph);
 		this.add(topPanel, BorderLayout.NORTH);
 		this.add(maximumLabel, BorderLayout.WEST);
-		this.add(Legendpanel, BorderLayout.SOUTH);
+		this.add(legendPanel, BorderLayout.SOUTH);
+
 	}
 
 	/**
@@ -95,17 +116,45 @@ public class StatisticGraphPanel extends JPanel {
 	}
 
 	/**
-	 * Repaint the Graph.
+	 * Set the Maximum Label
+	 *
+	 * @param max
+	 */
+	public void setMaximumLabel(double max) {
+		maximumLabel.setText(Double.toString(max));
+	}
+
+	/**
+	 * Get the name of the Graph.
 	 * 
-	 * public void repaint() { sGraph.repaint(); }
+	 * @return the name of the Graph
 	 */
+	public String getGraphName() {
+		return this.graphName;
+	}
 
 	/**
-	 * Set the Maximum Label
-	 *
-	 * @param max
+	 * Make the LegendPane.
 	 */
-	public void setMaximumLabel(int max) {
-		maximumLabel.setText(Integer.toString(max));
+	public void makeLegendPanel() {
+		legendPanel.removeAll();
+		for (TrackedDataSet set : sGraph.objects) {
+			JLabel b = new JLabel(set.getCpsObject().getName());
+			b.setBackground(Color.CYAN);
+			b.setOpaque(true);
+			tempP = new JPanel();
+			tempP.add(b);
+			b.addMouseListener(new MouseAdapter() {
+				@Override
+				public void mousePressed(MouseEvent e) {
+					if (e.BUTTON3 == e.getButton()) {
+						legendPanel.remove(tempP);
+						that.updateUI();
+					}
+				}
+			});
+			legendPanel.add(tempP);
+		}
+		updateUI();
 	}
 }

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

@@ -218,7 +218,7 @@ public class StatisticPane extends JPanel implements GraphListener{
     }
 
 	@Override
-	public void repaintGraph() {
+	public void repaintTree() {
 		// TODO Auto-generated method stub
 		statGraph1.repaint();
 	}

+ 4 - 5
src/ui/view/UpperNodeCanvas.java

@@ -624,13 +624,12 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 					controller.addTextToConsole("" + cps.getID(), Color.RED, 12, true, false, true);
 					dragging = true;
 					if (e.isControlDown() && tempCps != null) {
-						System.out.println(tempCps.getName() + " " + tempCps.getID());
 						if (model.getSelectedCpsObjects().contains(tempCps)) {
 							controller.deleteSelectedObject(tempCps);
 						} else {
 							controller.addSelectedObject(tempCps);
 						}
-					} else {
+					} else if (e.getButton() != MouseEvent.BUTTON3) {
 						controller.setSelectedObjectID(tempCps.getID());
 						model.getSelectedCpsObjects().clear();
 						controller.addSelectedObject(tempCps);
@@ -713,9 +712,9 @@ public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotio
 	@Override
 	public void mouseReleased(MouseEvent e) {
 		dragging = false;
-
-		updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
-		updCon.refreshTableProperties(model.getPropertyTable());
+		// updCon.refreshTableHolonElement(model.getMultiTable(),
+		// model.getSingleTable());
+		// updCon.refreshTableProperties(model.getPropertyTable());
 		if (model.getSelectedCpsObjects().size() > 1) {
 			model.getTableHolonElement().setModel(model.getMultiTable());
 		} else if (model.getSelectedCpsObjects().size() == 1) {

+ 44 - 16
src/ui/view/splitPane.java

@@ -24,6 +24,7 @@ import javax.swing.tree.TreeNode;
 import DataSets.GraphDataSet;
 import DataSets.PropertyDataSet;
 import classes.HolonObject;
+import classes.TrackedDataSet;
 import interfaces.GraphListener;
 import ui.controller.Control;
 
@@ -52,6 +53,12 @@ import java.awt.FlowLayout;
 import java.awt.BorderLayout;
 
 public class splitPane extends JSplitPane implements GraphListener {
+	//Property Integers
+	public static  final int CONSUMPTION = 0;
+	public static final int PRODUCTION = 1;
+	public static final int ACTIVATED_ELEMENTS = 2;
+	public static final int ON_OFF = 3;
+	
 	private JTextField graphNrTxtField;
 	private JTextField redField;
 	private JTextField greenField;
@@ -61,6 +68,8 @@ public class splitPane extends JSplitPane implements GraphListener {
 	private DefaultMutableTreeNode objectsNode;
 	private DefaultMutableTreeNode wholeHolon;
 	private Hashtable<String, GraphDataSet> objectHashtable;
+	private Hashtable<String, StatisticGraphPanel> graphHashtable;
+	private Hashtable<String, Integer> propValTable;
 	private JPanel colorPanel;
 	private PropertyDataSet currentProperty = new PropertyDataSet();
 	private JComboBox colorComboBox;
@@ -70,10 +79,14 @@ public class splitPane extends JSplitPane implements GraphListener {
 	JLabel showObjectlbl;
 	JLabel showPropertylbl;
 	public splitPane(Control cont) {
-		//this.rightComponent
 		this.controller = cont;
 		objectHashtable = new Hashtable<String, GraphDataSet>();
-
+		graphHashtable = new Hashtable<String, StatisticGraphPanel>();
+		propValTable = new Hashtable<String, Integer>();
+		propValTable.put("total Production", PRODUCTION);
+		propValTable.put("total Consumption", CONSUMPTION);
+		propValTable.put("number of activated Elements", ACTIVATED_ELEMENTS);
+		
 		JScrollPane dataPane = new JScrollPane(); 
 		setLeftComponent(dataPane);
 		JPanel panel = new JPanel();
@@ -342,25 +355,34 @@ public class splitPane extends JSplitPane implements GraphListener {
 		JButton btnAdd = new JButton("Add");
 		btnAdd.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
-				/*
 				 DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)objectTree.getLastSelectedPathComponent();
 				 if(selectedNode == null){
 					 return;
 				 }else{
 					 if(selectedNode.getLevel() == 3){
-
+						 StatisticGraphPanel tmp = null;
+						 if(!graphHashtable.containsKey(graphNrTxtField.getText()) && graphNrTxtField.getText().length() > 0){
+							tmp = new StatisticGraphPanel(controller.getModel(), controller, graphNrTxtField.getText(), 
+									graphHashtable);
+						 	tmp.setPreferredSize(new Dimension(280,150));
+						 	tmp.setMaximumSize(new Dimension(1000,150));
+						 	tmp.setMinimumSize(new Dimension(100,45));
+						 	tmp.setBorder(new LineBorder(new Color(0, 0, 0), 1));
+						 	graphPanel.add(tmp);
+						 	graphPanel.add(Box.createRigidArea(new Dimension(50,50)));
+						 	graphPanel.revalidate();
+						 	graphPanel.updateUI();
+						 	graphHashtable.put(graphNrTxtField.getText(), tmp);
+						 }
+						 if(tmp != null){
+							 String object = ((DefaultMutableTreeNode)selectedNode.getParent()).toString();
+							 String property = selectedNode.toString();
+							 GraphDataSet dataSet = objectHashtable.get(object);
+							 TrackedDataSet tds = new TrackedDataSet(dataSet.getObject(), propValTable.get(property), currentProperty.getColor());
+							 tmp.addObjec(tds);
+							 }
 					 }
 				 }
-				 */
-				 StatisticGraph tmp = new StatisticGraph(controller.getModel(), controller);
-				 tmp.setPreferredSize(new Dimension(280,120));
-				 tmp.setMaximumSize(new Dimension(1000,120));
-				 tmp.setMinimumSize(new Dimension(100,45));
-				 tmp.setBorder(new LineBorder(new Color(0, 0, 0), 2));
-				 graphPanel.add(tmp);
-				 graphPanel.add(Box.createRigidArea(new Dimension(50,50)));
-				 graphPanel.revalidate();
-				 graphPanel.updateUI();
 			}
 		});
 		
@@ -536,10 +558,10 @@ public class splitPane extends JSplitPane implements GraphListener {
 		graphPanel.revalidate();
 		graphPanel.updateUI();
 		graphScrollPane.setViewportView(graphPanel);
-		repaintGraph();
+		repaintTree();
 	}
 	@Override
-	public void repaintGraph() {
+	public void repaintTree() {
 		treeModel.reload();
 	}
 	@Override
@@ -598,6 +620,12 @@ public class splitPane extends JSplitPane implements GraphListener {
 		colorPanel.setBackground(Color.WHITE);
 	}
 	
+	public void repaintGraphs(){
+		for(StatisticGraphPanel sg: graphHashtable.values()){
+			sg.repaint();
+		}
+	}
+	
 	private static void addPopup(Component component, final JPopupMenu popup) {
 		component.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {