Преглед изворни кода

Edit of HolonObjects in Categories. There is just one problem that has to be fixed (old build path stays equal) --> no preview bild in categories-tree.

Edgardo Palza пре 8 година
родитељ
комит
73a8a58ba8
2 измењених фајлова са 63 додато и 16 уклоњено
  1. 40 11
      src/ui/view/AddObjectPopUp.java
  2. 23 5
      src/ui/view/GUI.java

+ 40 - 11
src/ui/view/AddObjectPopUp.java

@@ -35,6 +35,7 @@ import javax.swing.SwingConstants;
 import javax.swing.border.EmptyBorder;
 import javax.swing.filechooser.FileNameExtensionFilter;
 
+import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
 import ui.controller.Control;
@@ -55,13 +56,14 @@ public class AddObjectPopUp extends JDialog {
 	private String filePath = " ";
 	private String givenCategory;
 	private JLabel lblImagePreview;
+	private CpsObject toEdit;
 
 	/**
 	 * Launch the application.
 	 */
 	public static void main(String[] args) {
 		try {
-			AddObjectPopUp dialog = new AddObjectPopUp();
+			AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
 			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 			dialog.setVisible(true);
 		} catch (Exception e) {
@@ -72,7 +74,8 @@ public class AddObjectPopUp extends JDialog {
 	/**
 	 * Create the dialog.
 	 */
-	public AddObjectPopUp() {
+	public AddObjectPopUp(boolean edit, CpsObject obj, String cat) {
+		toEdit = obj;
 		this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
 				.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
 		setBounds(100, 100, 450, 342);
@@ -108,6 +111,9 @@ public class AddObjectPopUp extends JDialog {
 					objectName.setBackground(Color.WHITE);
 				}
 			});
+			if (edit) {
+				objectName.setText(obj.getName());
+			}
 			objectName.setBounds(98, 18, 172, 20);
 			contentPanel.add(objectName);
 			objectName.setColumns(10);
@@ -143,6 +149,15 @@ public class AddObjectPopUp extends JDialog {
 					sourcePath.setBackground(Color.WHITE);
 				}
 			});
+			if (edit) {
+				sourcePath.setText(obj.getImage());
+				if (sourcePath.getText().startsWith("/Images/")) {
+					sourcePath
+							.setText(System.getProperty("user.home") + "/praktikum-holons/res/" + sourcePath.getText());
+				}
+				filePath = sourcePath.getText();
+				selectedFile = new File(sourcePath.getText());
+			}
 			sourcePath.setBounds(148, 77, 271, 20);
 			contentPanel.add(sourcePath);
 			sourcePath.setColumns(10);
@@ -178,6 +193,11 @@ public class AddObjectPopUp extends JDialog {
 				scrollPane.setViewportView(list);
 			}
 		}
+		if (edit) {
+			for (HolonElement e : ((HolonObject) obj).getElements()) {
+				addElement(e);
+			}
+		}
 		{
 			JButton btnNewButton = new JButton("Delete Element");
 			btnNewButton.addActionListener(new ActionListener() {
@@ -197,7 +217,6 @@ public class AddObjectPopUp extends JDialog {
 			lblImagePreview.setBounds(295, 3, 50, 50);
 			contentPanel.add(lblImagePreview);
 		}
-
 		{
 			JPanel buttonPane = new JPanel();
 			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
@@ -214,16 +233,23 @@ public class AddObjectPopUp extends JDialog {
 								// HolonObject(objectName.getText());
 								// theObject.setElements(hElements);
 								// theObject.setImage(imagePath);
-								controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
-										hElements, imagePath);
+								if (edit) {
+									controller.delObjectCategory(givenCategory, toEdit.getName());
+									controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
+											hElements, imagePath);
+								} else {
+									controller.addObject(controller.searchCategory(givenCategory), objectName.getText(),
+											hElements, imagePath);
+								}
 								// controller.addObjectCategory(controller.searchCategory(givenCategory),
 								// theObject);
 								//
 								// System.out.println(theObject.getImage());
 								System.out.println(givenCategory);
 								dispose();
-							} else
+							} else {
 								sourcePath.setBackground(new Color(255, 50, 50));
+							}
 						} else {
 							objectName.setBackground(new Color(255, 50, 50));
 							if (!sourcePath.getText().equals(filePath))
@@ -279,11 +305,10 @@ public class AddObjectPopUp extends JDialog {
 		try {
 			File source = new File(filePath);
 			File dest = new File(System.getProperty("user.home") + "/HolonGUI/Images/");
-		
-			 dest.mkdirs();
-			 dest = new File(dest, selectedFile.getName());
-			 imagePath = "" + dest;
-			
+			dest.mkdirs();
+			dest = new File(dest, selectedFile.getName());
+			imagePath = "" + dest;
+
 			inStream = new FileInputStream(source);
 			outStream = new FileOutputStream(dest);
 			byte[] buffer = new byte[1024];
@@ -303,6 +328,10 @@ public class AddObjectPopUp extends JDialog {
 		}
 	}
 
+	public void editInformation(HolonObject obj) {
+		objectName.setText(obj.getName());
+	}
+
 	public static String getJarContainingFolder(Class aclass) throws Exception {
 		CodeSource codeSource = aclass.getProtectionDomain().getCodeSource();
 

+ 23 - 5
src/ui/view/GUI.java

@@ -85,6 +85,7 @@ public class GUI<E> implements CategoryListener {
 	private final JScrollPane scrollPane_2 = new JScrollPane();
 	private JPopupMenu popmenuEdit = new JPopupMenu();
 	private JMenuItem editItem = new JMenuItem("Edit Object");
+	private String catOfObjToBeEdited;
 
 	private final JLabel maxGraph = new JLabel("100%");
 	private final JLabel medGraph = new JLabel("50%");
@@ -386,7 +387,6 @@ public class GUI<E> implements CategoryListener {
 						} else {
 							holonEleNamesDisplayed = ele.getEleName() + " ";
 						}
-
 					}
 				} else if (ele != null) {
 					selectedElements.clear();
@@ -615,8 +615,17 @@ public class GUI<E> implements CategoryListener {
 						// }
 					}
 					if (SwingUtilities.isRightMouseButton(e)) {
-						editItem.setEnabled(true);
-						popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
+						for (Category cat : model.getCategories()) {
+							for (CpsObject cps : cat.getObjects()) {
+								if (actualObjectClicked.compareTo(cps.getObjName()) == 0
+										&& !(cps instanceof HolonSwitch)) {
+									editItem.setEnabled(true);
+									popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
+									catOfObjToBeEdited = selectedNode.getParent().toString();
+									tempCps = cps;
+								}
+							}
+						}
 					} else {
 						for (Category cat : model.getCategories()) {
 							for (CpsObject cps : cat.getObjects()) {
@@ -644,7 +653,16 @@ public class GUI<E> implements CategoryListener {
 				}
 			}
 		});
-
+		editItem.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				// Remove the selected Object object
+				addObjectPopUP = new AddObjectPopUp(true, tempCps, catOfObjToBeEdited);
+				addObjectPopUP.setCategory(catOfObjToBeEdited);
+				addObjectPopUP.setController(controller);
+				addObjectPopUP.setVisible(true);
+			}
+		});
 		scrollPane_1.setViewportView(tree);
 
 		scrollPane_1.setColumnHeaderView(panel);
@@ -677,7 +695,7 @@ public class GUI<E> implements CategoryListener {
 								"Please select a Category first before adding " + selectedOption + ".");
 					}
 					if (selectedNode.getLevel() == 1) {
-						addObjectPopUP = new AddObjectPopUp();
+						addObjectPopUP = new AddObjectPopUp(false, null, null);
 						addObjectPopUP.setVisible(true);
 						addObjectPopUP.setController(controller);
 						addObjectPopUP.setCategory(selectedNode.toString());