Kaynağa Gözat

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 yıl önce
ebeveyn
işleme
fb69c26cde

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

@@ -18,7 +18,10 @@ import javax.swing.JComboBox;
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.ImageIcon;
 
+import classes.CpsObject;
 import classes.HolonElement;
+import classes.HolonObject;
+import ui.model.Model;
 
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
@@ -32,12 +35,14 @@ public class AddElementPopUp extends JDialog {
 	private JTextField providedEnergy;
 	private JTextField amount;
 	private HolonElement hl;
+	private CpsObject tempCps;
 
 	/**
 	 * Launch the application.
 	 */
 	public static void main(String[] args) {
 		try {
+
 			AddElementPopUp dialog = new AddElementPopUp();
 			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 			dialog.setVisible(true);
@@ -120,7 +125,14 @@ public class AddElementPopUp extends JDialog {
 				JButton okButton = new JButton("OK");
 				okButton.addActionListener(new ActionListener() {
 					public void actionPerformed(ActionEvent arg0) {
-						if (elementName.getText().length() != 0) {
+						boolean repeated = false;
+						for (HolonElement e : ((HolonObject) tempCps).getElements()) {
+							if (elementName.getText().equals(e.getEleName())) {
+								repeated = true;
+								break;
+							}
+						}
+						if (elementName.getText().length() != 0 && !repeated) {
 							try {
 								float energy = Float.parseFloat(providedEnergy.getText().toString());
 								int elementAmount = Integer.parseInt(amount.getText().toString());
@@ -137,6 +149,7 @@ public class AddElementPopUp extends JDialog {
 							// JOptionPane.showMessageDialog(new JFrame(),
 							// "Please enter a Name");
 							elementName.setBackground(new Color(255, 50, 50));
+							// Jlabel repeatedString = new JLabel();
 						}
 					}
 				});
@@ -157,6 +170,10 @@ public class AddElementPopUp extends JDialog {
 		}
 	}
 
+	public void setActualCps(CpsObject cps) {
+		this.tempCps = cps;
+	}
+
 	public HolonElement getElement() {
 		return hl;
 	}

+ 160 - 86
src/ui/view/GUI.java

@@ -41,6 +41,7 @@ import javax.swing.JTabbedPane;
 import javax.swing.JTable;
 import javax.swing.JToolBar;
 import javax.swing.JTree;
+import javax.swing.SwingUtilities;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.table.DefaultTableModel;
@@ -58,6 +59,7 @@ import classes.HolonSwitch;
 import classes.HolonTransformer;
 import ui.controller.Control;
 import ui.model.Model;
+import ui.view.PropertyTable;;
 
 public class GUI<E> implements CategoryListener {
 
@@ -78,6 +80,8 @@ public class GUI<E> implements CategoryListener {
 	private final JScrollPane scrollPane_1 = new JScrollPane();
 	private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
 	private final JScrollPane scrollPane_2 = new JScrollPane();
+	private JPopupMenu popmenuEdit = new JPopupMenu();
+	private JMenuItem editItem = new JMenuItem("Edit Object");
 
 	private final JLabel maxGraph = new JLabel("100%");
 	private final JLabel medGraph = new JLabel("50%");
@@ -99,7 +103,7 @@ public class GUI<E> implements CategoryListener {
 	// HolonObject with consumption/production, name and amount.
 
 	private JTable tableHolonElement = new JTable();
-	private DefaultTableModel tableModelHolonElement = new DefaultTableModel();
+	private PropertyTable tableModelHolonElement = new PropertyTable();
 	private final JPanel scrollElements = new JPanel();
 	JScrollPane tableHolonElementScrollPane = new JScrollPane();
 
@@ -164,6 +168,8 @@ public class GUI<E> implements CategoryListener {
 	private final JComboBox comboBoxAlgo = new JComboBox();
 	private int yTHIS;
 	private int xTHIS;
+	private int yBTHIS;
+	private int xBTHIS;
 
 	/**
 	 * Create the application.
@@ -257,18 +263,26 @@ 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" };
+		Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElement.setColumnIdentifiers(columnNames);
 		tableHolonElement.setBorder(null);
 		tableHolonElement.setModel(tableModelHolonElement);
 		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" };
@@ -278,62 +292,51 @@ 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();
 				if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
 						&& tempCpsObject.getID() != 0) {
 					addElementPopUp = new AddElementPopUp();
+					addElementPopUp.setActualCps(getActualCps());
 					addElementPopUp.setVisible(true);
-					controller.addElementCanvasObject(tempCpsObject.getID(), addElementPopUp.getElement().getEleName(),
-							addElementPopUp.getElement().getAmount(), addElementPopUp.getElement().getEnergy());
+					HolonElement ele = addElementPopUp.getElement();
+					controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
+							ele.getEnergy());
 					refreshTableHolonElement();
 					refreshTableProperties();
 				}
 			}
 		});
-		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) {
@@ -347,7 +350,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();
@@ -359,13 +364,70 @@ 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();
 				}
+
+				yBTHIS = e.getY();
+				xBTHIS = e.getX();
+
+			}
+		});
+		/*
+		 * If the HolonElement Table enters to editing instance, than is the
+		 * propertyChangeListener triggered
+		 */
+		tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
+			@Override
+			public void propertyChange(PropertyChangeEvent evt) {
+				try {
+					int yMouse = yTHIS;
+					int yBMouse = yBTHIS;
+					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueY = (int) Math.floor(yMouse / 16);
+					int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueBY = (int) Math.floor(yBMouse / 16);
+					if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
+						if (selectedValueBX == 3) {
+							HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
+							String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
+									.toString();
+							Boolean bTemp = Boolean.parseBoolean(newBStuff);
+							eleBTemp.setActive(bTemp);
+						} else {
+							HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
+							String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
+									.toString();
+							if (selectedValueX == 0) {
+								eleTemp.setEleName(newStuff);
+							} else if (selectedValueX == 1) {
+								Float ftemp = Float.parseFloat(newStuff);
+								eleTemp.setEnergy(ftemp);
+							} else if (selectedValueX == 2) {
+								Integer iTemp = Integer.parseInt(newStuff);
+								eleTemp.setAmount(iTemp);
+							}
+						}
+						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) {
@@ -385,32 +447,20 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
-		tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
-			@Override
-			public void propertyChange(PropertyChangeEvent evt) {
-				try {
-					int yMouse = yTHIS;
-					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 3));
-					int selectedValueY = (int) Math.floor(yMouse / 16);
-					if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
-						HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
-						String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX).toString();
-						if (selectedValueX == 0) {
-							eleTemp.setEleName(newStuff);
-						} else if (selectedValueX == 1) {
-							Float ftemp = Float.parseFloat(newStuff);
-							eleTemp.setEnergy(ftemp);
-						} else if (selectedValueX == 2) {
-							Integer iTemp = Integer.parseInt(newStuff);
-							eleTemp.setAmount(iTemp);
-						}
-						tableModelHolonElement.fireTableDataChanged();
-					}
-				} catch (Exception e) {
-
-				}
+		/***********************
+		 * 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));
 
@@ -488,6 +538,13 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
+		popmenuEdit.add(editItem);
+		editItem.setEnabled(false);
+		editItem.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+			}
+		});
 		tree.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				try {
@@ -500,28 +557,37 @@ public class GUI<E> implements CategoryListener {
 						CpsObject selected = controller.searchObjInCat(selectedNode.toString(),
 								selectedNode.getParent().toString());
 						deleteRows();
-//						if (selected instanceof HolonObject && selected != null) {
-//							selected = (HolonObject) selected;
-//							fillElementTable(((HolonObject) selected).getElements());
-//						}
+						// if (selected instanceof HolonObject && selected !=
+						// null) {
+						// selected = (HolonObject) selected;
+						// fillElementTable(((HolonObject)
+						// selected).getElements());
+						// }
 					}
-					for (Category cat : model.getCategories()) {
-						for (CpsObject cps : cat.getObjects()) {
-							if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
-								File checkPath = new File(cps.getImage());
-								if (checkPath.exists()) {
-									img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
-											controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
-								} else {
-									img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-											.getScaledInstance(controller.getScale(), controller.getScale(),
-													java.awt.Image.SCALE_SMOOTH);
+					if (SwingUtilities.isRightMouseButton(e)) {
+						editItem.setEnabled(true);
+						System.out.println("HERE");
+						popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
+					} else {
+						for (Category cat : model.getCategories()) {
+							for (CpsObject cps : cat.getObjects()) {
+								if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
+									File checkPath = new File(cps.getImage());
+									if (checkPath.exists()) {
+										img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
+												controller.getScale(), controller.getScale(),
+												java.awt.Image.SCALE_SMOOTH);
+									} else {
+										img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
+												.getScaledInstance(controller.getScale(), controller.getScale(),
+														java.awt.Image.SCALE_SMOOTH);
+									}
+									tempCps = cps;
+									dragging = true;
+									Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
+											"Image");
+									frmCyberPhysical.setCursor(cursor);
 								}
-								tempCps = cps;
-								dragging = true;
-								Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
-										"Image");
-								frmCyberPhysical.setCursor(cursor);
 							}
 						}
 					}
@@ -901,23 +967,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--) {
@@ -926,15 +993,22 @@ 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() };
+			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
 			tableModelHolonElement.addRow(temp);
 		}
 	}
 
 	/**
-	 * Update the information about properties of the selected CpsObject
+	 * Update the information concerning properties of the selected CpsObject
 	 */
 	public void refreshTableProperties() {
 		CpsObject tempCps = getActualCps();

+ 19 - 0
src/ui/view/PropertyTable.java

@@ -0,0 +1,19 @@
+package ui.view;
+
+import java.util.Vector;
+
+import javax.swing.table.DefaultTableModel;
+
+public class PropertyTable extends DefaultTableModel {
+
+	@Override
+	public Class<?> getColumnClass(int columnIndex) {
+		Class clazz = String.class;
+		switch (columnIndex) {
+		case 3:
+			clazz = Boolean.class;
+			break;
+		}
+		return clazz;
+	}
+}