Bladeren bron

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

# Conflicts:
#	bin/ui/view/GUI$2.class
#	bin/ui/view/GUI$3.class
#	bin/ui/view/GUI$4.class
#	bin/ui/view/GUI$5.class
#	bin/ui/view/GUI$6.class
#	bin/ui/view/GUI.class
#	bin/ui/view/MyCanvas.class
Teh-Hai Julian Zheng 8 jaren geleden
bovenliggende
commit
16b168907f
5 gewijzigde bestanden met toevoegingen van 100 en 63 verwijderingen
  1. BIN
      bin/classes/HolonElement.class
  2. BIN
      bin/ui/view/GUI$1.class
  3. 24 16
      src/classes/HolonElement.java
  4. 33 11
      src/ui/view/GUI.java
  5. 43 36
      src/ui/view/MyCanvas.java

BIN
bin/classes/HolonElement.class


BIN
bin/ui/view/GUI$1.class


+ 24 - 16
src/classes/HolonElement.java

@@ -14,17 +14,21 @@ public class HolonElement {
 	float totalEnergy;
 	/* Path of the image for the Obj. */
 	String image;
+	/* +: for Consumers and -: Producers */
+	String sign;
 
-	
-	
 	public HolonElement(String eleName, float energy, int amount) {
 		setEleName(eleName);
 		setAmount(amount);
 		setEnergy(energy);
 		setActive(true);
+		if (energy < 0) {
+			setSign("-");
+		} else {
+			setSign("+");
+		}
 	}
 
-
 	/**
 	 * @return the name
 	 */
@@ -33,7 +37,8 @@ public class HolonElement {
 	}
 
 	/**
-	 * @param name the name to set
+	 * @param name
+	 *            the name to set
 	 */
 	public void setEleName(String name) {
 		this.eleName = name;
@@ -47,7 +52,8 @@ public class HolonElement {
 	}
 
 	/**
-	 * @param amount the amount to set
+	 * @param amount
+	 *            the amount to set
 	 */
 	public void setAmount(int amount) {
 		this.amount = amount;
@@ -61,7 +67,8 @@ public class HolonElement {
 	}
 
 	/**
-	 * @param energy the energy to set
+	 * @param energy
+	 *            the energy to set
 	 */
 	public void setEnergy(float energy) {
 		this.energy = energy;
@@ -75,7 +82,8 @@ public class HolonElement {
 	}
 
 	/**
-	 * @param active the active to set
+	 * @param active
+	 *            the active to set
 	 */
 	public void setActive(boolean active) {
 		this.active = active;
@@ -88,21 +96,14 @@ public class HolonElement {
 		return image;
 	}
 
-
-
-
-
 	/**
-	 * @param image the image to set
+	 * @param image
+	 *            the image to set
 	 */
 	public void setImage(String image) {
 		this.image = image;
 	}
 
-
-
-
-
 	/**
 	 * Multiply the amount of gadgets, given by the user, and the
 	 * consumption/production. If the switch isWorking is turned off for on
@@ -115,4 +116,11 @@ public class HolonElement {
 		return totalEnergy;
 	}
 
+	public void setSign(String s) {
+		this.sign = s;
+	}
+
+	public String getSign() {
+		return sign;
+	}
 }

+ 33 - 11
src/ui/view/GUI.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import java.io.File;
+import java.text.AttributedCharacterIterator;
 import java.util.ArrayList;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
@@ -23,13 +24,19 @@ import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.JEditorPane;
 import javax.swing.JFileChooser;
 import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableModel;
 
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.Cursor;
 import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Shape;
 import java.awt.Toolkit;
 import java.awt.BorderLayout;
 
@@ -42,6 +49,7 @@ import javax.swing.ImageIcon;
 
 import classes.Category;
 import classes.CpsObject;
+import classes.HolonElement;
 import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.HolonTransformer;
@@ -50,6 +58,7 @@ import Interfaces.CategoryListener;
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+import java.awt.image.ImageObserver;
 import java.awt.event.ActionEvent;
 
 public class GUI implements CategoryListener {
@@ -75,7 +84,8 @@ public class GUI implements CategoryListener {
 	private final JEditorPane dtrpnHereWillBe = new JEditorPane();
 	private final JSplitPane splitPane_2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
 
-	private JTable table;
+	private JTable table = new JTable();
+	private DefaultTableModel tableModel = new DefaultTableModel();
 	private final Model model;
 	private final Control controller;
 	private AddObjectPopUp addObjectPopUP;
@@ -137,11 +147,6 @@ public class GUI implements CategoryListener {
 
 		menuBar.add(mnHelp);
 
-		// Table
-		String[] columnNames = { "Gadget", "Typ", "Energy", "Quantity" };
-
-		Object[][] data = {};
-
 		canvas.setBackground(Color.WHITE);
 		canvas.setPreferredSize(new Dimension(10000, 10000));
 		JScrollPane canvasSP = new JScrollPane(canvas);
@@ -158,14 +163,15 @@ public class GUI implements CategoryListener {
 		scrollPane_2.setViewportView(dtrpnHereWillBe);
 
 		JScrollPane scrollPane = new JScrollPane();
-
-		table = new JTable(data, columnNames);
+		// Table Set up
+		Object[] columnNames = { "Gadget", "Typ", "Energy", "Quantity" };
+		tableModel.setColumnIdentifiers(columnNames);
+		table.setModel(tableModel);
 		scrollPane.setViewportView(table);
-
 		table.setFillsViewportHeight(true);
-		table.setModel(new DefaultTableModel(data, columnNames));
 		table.setCellSelectionEnabled(true);
 		table.setColumnSelectionAllowed(true);
+
 		frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
 
 		TreeCellRenderer customRenderer = new TreeCellRenderer() {
@@ -249,7 +255,6 @@ public class GUI implements CategoryListener {
 			public void mousePressed(MouseEvent e) {
 				try {
 					actualObjectClicked = tree.getPathForLocation(e.getX(), e.getY()).getLastPathComponent().toString();
-
 					for (Category cat : model.getCategories()) {
 						for (CpsObject cps : cat.getObjects()) {
 							if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
@@ -315,6 +320,23 @@ public class GUI implements CategoryListener {
 			}
 		});
 
+		canvas.addMouseListener(new MouseAdapter() {
+			@Override
+			public void mousePressed(MouseEvent e) {
+				if (tableModel.getRowCount() > 0) {
+					for (int i = tableModel.getRowCount() - 1; i > -1; i--) {
+						tableModel.removeRow(i);
+					}
+				}
+				if (canvas.dataSelected == null) {
+					for (HolonElement he : canvas.dataSelected) {
+						Object[] temp = { he.getEleName(), he.getSign(), he.getEnergy(), he.getAmount() };
+						tableModel.addRow(temp);
+					}
+				}
+			}
+		});
+
 		toolBar.add(btnAdd);
 
 		// Del Button

+ 43 - 36
src/ui/view/MyCanvas.java

@@ -49,10 +49,11 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	Graphics2D g2; // For Painting
 	private int cx;
 	private int cy;
+	ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
 
-	private boolean dragging = false;	//for dragging
-	private boolean drawEdge = false;	//for drawing edges
-	private boolean click = false; 		// for double click
+	private boolean dragging = false; // for dragging
+	private boolean drawEdge = false; // for drawing edges
+	private boolean click = false; // for double click
 	private CpsObject tempCps = null;
 	private Rectangle selectRect = new Rectangle();
 
@@ -70,7 +71,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		itemDelete.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
-				//Remove the selected Object object
+				// Remove the selected Object object
 				model.getObjectsOnCanvas().remove(tempCps);
 				for (CpsObject cps : model.getObjectsOnCanvas()) {
 					cps.getConnectedTo().remove(tempCps);
@@ -93,7 +94,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	 */
 	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
-		//Rendering
+		// Rendering
 		g2 = (Graphics2D) g;
 		RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 		g2.setRenderingHints(rh);
@@ -103,6 +104,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			g2.setColor(Color.GREEN);
 			g2.fillRect((int) selectRect.getX(), (int) selectRect.getY(), (int) selectRect.getWidth(),
 					(int) selectRect.getHeight());
+
 		}
 
 		// drawEdges
@@ -114,12 +116,10 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			for (CpsObject con : cps.getConnectedTo()) {
 				if (con.getID() != model.getSelectedObjectID() && cps.getID() != model.getSelectedObjectID())
-
 					g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
 							cps.getPosition().y + controller.getScaleDiv2(),
 							con.getPosition().x + controller.getScaleDiv2(),
 							con.getPosition().y + controller.getScaleDiv2());
-
 			}
 		}
 
@@ -128,12 +128,10 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 			for (CpsObject con : cps.getConnectedTo()) {
 				if (con.getID() == model.getSelectedObjectID())
-
 					g2.drawLine(cps.getPosition().x + controller.getScaleDiv2(),
 							cps.getPosition().y + controller.getScaleDiv2(),
 							con.getPosition().x + controller.getScaleDiv2(),
 							con.getPosition().y + controller.getScaleDiv2());
-
 			}
 		}
 
@@ -149,7 +147,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
-		//If double clicked on a Switch change the Image to on/off
+		// If double clicked on a Switch change the Image to on/off
 		if (doubleClick() && tempCps != null && tempCps.getClass() == HolonSwitch.class) {
 			System.out.println("trans double click");
 			if (tempCps.getImage().compareTo("/Images/switch-on.png") == 0) {
@@ -188,9 +186,18 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			cy = cps.getPosition().y;
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
 				tempCps = cps;
-				//If drawing an Edge (CTRL down)
+				// If drawing an Edge (CTRL down)
 				if (e.isControlDown())
-					drawEdge = true;		
+
+					drawEdge = true;
+				if (tempCps.getClass() == HolonObject.class) {
+					HolonObject tempObj = ((HolonObject) tempCps);
+					dataSelected = tempObj.getElements();
+				} else {
+					dataSelected = new ArrayList<>();
+				}
+
+				drawEdge = true;
 			}
 		}
 		// Object Selection Highlighting (selectRect)
@@ -205,8 +212,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			drawEdge = false;
 			drawDeleteEdge();
 		}
-		
-		//if Dragged reposition the Object
+
+		// if Dragged reposition the Object
 		if (dragging) {
 			x = e.getX();
 			y = e.getY();
@@ -229,22 +236,22 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 	@Override
 	public void mouseDragged(MouseEvent e) {
-		//If Edge is drawn
+		// If Edge is drawn
 		if (drawEdge) {
 			x = e.getX();
 			y = e.getY();
 			repaint();
 		} else {
 			try {
-				//Drag Position
+				// Drag Position
 				tempCps.setPosition(e.getX() - controller.getScaleDiv2(), e.getY() - controller.getScaleDiv2());
 				dragging = true;
-				//Highlighting Position
+				// Highlighting Position
 				selectRect.setLocation(tempCps.getPosition().x - (controller.getScale() / 20),
-						tempCps.getPosition().y - (controller.getScale() / 20)); 
-				//TipText Position and name
+						tempCps.getPosition().y - (controller.getScale() / 20));
+				// TipText Position and name
 				objectTT.setTipText(tempCps.getName());
-				objectTT.setLocation(tempCps.getPosition().x, tempCps.getPosition().y+controller.getScale());
+				objectTT.setLocation(tempCps.getPosition().x, tempCps.getPosition().y + controller.getScale());
 				repaint();
 			} catch (Exception e2) {
 			}
@@ -255,7 +262,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 	public void mouseMoved(MouseEvent e) {
 		x = e.getX();
 		y = e.getY();
-		//Everytghin for the tooltip :)
+		// Everytghin for the tooltip :)
 		boolean on = false;
 		for (CpsObject cps : model.getObjectsOnCanvas()) {
 
@@ -264,7 +271,7 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
 
 				objectTT.setTipText(cps.getName());
-				objectTT.setLocation(cx, cy+controller.getScale());
+				objectTT.setLocation(cx, cy + controller.getScale());
 				on = true;
 			}
 		}
@@ -310,26 +317,26 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 			}
 		}
 	}
-	
+
 	/**
 	 * Checks if a double click was made
 	 * 
 	 * @return true if doublecklick, false if not
 	 */
-	private boolean doubleClick(){
+	private boolean doubleClick() {
 		if (click) {
-	        click = false;
-	        return true;
-	    } else {
-	    	click = true;
-	        Timer t = new Timer("doubleclickTimer", false);
-	        t.schedule(new TimerTask() {
-	            @Override
-	            public void run() {
-	            	click = false;
-	            }
-	        }, 500);
-	    }
+			click = false;
+			return true;
+		} else {
+			click = true;
+			Timer t = new Timer("doubleclickTimer", false);
+			t.schedule(new TimerTask() {
+				@Override
+				public void run() {
+					click = false;
+				}
+			}, 500);
+		}
 		return false;
 	}
 }