Browse Source

delete Category und delete Object in kategory

dominik.rieder 7 years ago
parent
commit
8cc02c7e32

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


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


BIN
bin/ui/view/GUI.class


+ 71 - 11
src/ui/controller/CategoryController.java

@@ -64,10 +64,6 @@ public class CategoryController {
 		notifyCatListeners();
 	}
 	
-	public void deleteCategory(int idx) {
-		M.getCategories().remove(idx);
-	}
-
 	/**
 	 * Adds New Category into Model
 	 * 
@@ -78,6 +74,30 @@ public class CategoryController {
 
 		addCategory(new Category(name));
 	}
+	
+	/**
+	 * deletes category with given name
+	 * @param catName
+	 * @return true if successfull, otherwise false
+	 */
+	public boolean deleteCategory(String catName){
+		int position = getPositionInList(M.getCategories(), searchCatNode(catName));
+		if(position != -1){
+			deleteCategoryAt(position);
+			return true;
+		}
+		else{
+			return false;
+		}
+	}
+	
+	/**
+	 * deletes a Category at given position
+	 */
+	public void deleteCategoryAt(int idx) {
+		M.getCategories().remove(idx);
+		notifyCatListeners();
+	}
 
 	/**
 	 * Add Object into a Category
@@ -127,15 +147,31 @@ public class CategoryController {
 	public void addNewHolonSwitch(Category cat, String obj) {
 		addObject(cat, new HolonSwitch(obj));
 	}
-
+	
 	/**
-	 * 
+	 * deletes given Object in given Category
+	 * @param toDelete
+	 * @param deleteIn
 	 */
-	public void notifyCatListeners() {
-		for (CategoryListener l : M.getCategoryListeners()) {
-			l.onChange(M.getCategories());
+	public void deleteObjectInCat(String toDelete, String deleteIn) {
+		Category cat = searchCatNode(deleteIn);
+		print(cat.getObjects());
+		for(int i = 0; i < cat.getObjects().size(); i++){
+			if(cat.getObjects().get(i).getCompareName().equals(toDelete)){
+				cat.getObjects().remove(i);
+				cat.getObjects();
+				notifyCatListeners();
+			}
+		}
+		
+	}
+	
+	public void print(ArrayList<CpsObject> iterate){
+		for(CpsObject cps : iterate){
+			System.out.println(cps.getCompareName());
 		}
 	}
+	
 
 	/**
 	 * 
@@ -144,13 +180,22 @@ public class CategoryController {
 	public void addCatListener(CategoryListener catLis) {
 		M.getCategoryListeners().add(catLis);
 	}
+	
+	/**
+	 * notifies all listeners about changes in the Categories
+	 */
+	public void notifyCatListeners() {
+		for (CategoryListener l : M.getCategoryListeners()) {
+			l.onChange(M.getCategories());
+		}
+	}
 
 	/**
 	 * search for category
 	 * @param name
 	 * @return
 	 */
-	public Category searchNode(String name) {
+	public Category searchCatNode(String name) {
 		Category query = null;
 
 		for (Category cat : M.getCategories()) {
@@ -161,6 +206,21 @@ public class CategoryController {
 		}
 		return query;
 	}
+	
+	/**
+	 * gets the position of an object in the array
+	 * @param arrayList
+	 * @param toSearch
+	 * @return -1 if object is not in the array, otherwise the index
+	 */
+	public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
+		for(int i = 0;i < arrayList.size(); i++){
+			if(arrayList.get(i).getCompareName() == toSearch.getCompareName()){
+				return i;
+			}
+		}
+		return -1;
+	}
 
 	public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
 		for(ComparableObject obj : arrayList){
@@ -170,6 +230,6 @@ public class CategoryController {
 		}
 		return false;
 	}
-	
 
+	
 }

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

@@ -52,13 +52,21 @@ public class Control {
 	
 	public Category searchCategory(String name) {
 		
-		return categoryController.searchNode(name);
+		return categoryController.searchCatNode(name);
 	}
 	
 	public void initListener(CategoryListener catLis) {
 		categoryController.addCatListener(catLis);
 	}
 	
+	public void deleteCategory(String catName){
+		categoryController.deleteCategory(catName);
+	}
+	
+	public void deleteObjectInCat(String toDelete, String deleteIn){
+		categoryController.deleteObjectInCat(toDelete, deleteIn);
+	}
+	
 	
 	////////// etc
 	/**

+ 60 - 32
src/ui/view/GUI.java

@@ -89,7 +89,6 @@ public class GUI implements CategoryListener {
 	private final Control controller;
 
 	private final JPanel panel = new JPanel();
-	private final JTextField textField = new JTextField();
 	private final JComboBox comboBox = new JComboBox();
 
 	// Buttons
@@ -113,7 +112,6 @@ public class GUI implements CategoryListener {
 	 */
 	@SuppressWarnings("serial")
 	private void initialize() {
-		textField.setColumns(10);
 		frmCyberPhysical = new JFrame();
 		frmCyberPhysical.setTitle("Cyber Physical Systems Model");
 		frmCyberPhysical.setBounds(100, 100, 891, 467);
@@ -195,53 +193,52 @@ public class GUI implements CategoryListener {
 		btnAdd.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 
-				Category selectedNode;
 				Object nodeInfo = tree.getLastSelectedPathComponent();
-				String selected = comboBox.getSelectedItem().toString();
-				String text = textField.getText();
-				if(selected.toString() == "Category"){
-					String catName = JOptionPane.showInputDialog("Please enter the Name for Category ");
-					if(catName != ""){
-					controller.addNewCategory(catName);
-					}
-				}
-				else if (selected.equals("Object")) {
-					selectedNode = controller.searchCategory(nodeInfo.toString());
-					controller.addNewObject(selectedNode, textField.getText());
-
-				} else if (selected.equals("Transformer")) {
-					selectedNode = controller.searchCategory(nodeInfo.toString());
-					controller.addNewTransformer(selectedNode, textField.getText());
-
-				} else {
-					selectedNode = controller.searchCategory(nodeInfo.toString());
-					controller.addNewSwitch(selectedNode, textField.getText());
-
+				String selectedOption = comboBox.getSelectedItem().toString();
+				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
+				
+				switch(selectedOption){
+				
+					case "Category":
+						String catName = JOptionPane.showInputDialog("Please enter a Name for Category ");
+						if(catName != ""){
+						controller.addNewCategory(catName);
+						}
+						break;
+							
+					default :
+						addObjectAction(selectedOption, selectedNode);
+						break;
 				}
-
 			}
 		});
+		
 		toolBar.add(btnAdd);
 
 		// Del Button
 		btnDel.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 
-				Category selectedNode;
 				Object nodeInfo = tree.getLastSelectedPathComponent();
-				String selected = comboBox.getSelectedItem().toString();
+				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
+				String nodeName = selectedNode.getUserObject().toString();
+				int depthOfNode = selectedNode.getLevel();
 				
-				if(nodeInfo.toString().isEmpty()){
-					
+				switch(depthOfNode){
+				case 1:
+					controller.deleteCategory(nodeName);
+					break;
+				case 2:
+					DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode.getParent();
+					controller.deleteObjectInCat(nodeName,parent.getUserObject().toString());
+					break;
 				}
+				
 
 
 			}
 		});
 		toolBar.add(btnDel);
-		
-		
-		panel.add(textField);
 		frmCyberPhysical.getContentPane().add(splitPane);
 
 		mntmOpen.addActionListener(new java.awt.event.ActionListener() {
@@ -261,6 +258,38 @@ public class GUI implements CategoryListener {
 		});
 
 	}
+	
+	/*
+	 * adds a specific object type to selected Category 
+	 * also handles input windows and illegal inputs
+	 */
+	public void addObjectAction(String objType, DefaultMutableTreeNode selectedNode){
+		if(selectedNode == null){
+			JOptionPane.showMessageDialog(new JFrame(), "Please select a Category first before adding " + objType + ".");
+		}
+		//if selected node is a directory for Categories
+		else{
+			if(selectedNode.getLevel()==1){
+				String objname = JOptionPane.showInputDialog("Please enter a Name for the " + objType);
+				Category cat = controller.searchCategory(selectedNode.getUserObject().toString());
+				switch(objType){
+				
+				case "Object": 
+					controller.addNewObject( cat , objname);
+					break;
+				
+				case "Switch" :
+					controller.addNewSwitch(cat, objname);
+					
+				case "Transformer" :
+					controller.addNewTransformer(cat, objname);
+				}
+			}
+			else{
+				JOptionPane.showMessageDialog(new JFrame(), "Objects can not be added to Objects. Please select a Category.");
+				}
+		}
+	}
 
 	/**
 	 * reloads the Categories from Model
@@ -289,7 +318,6 @@ public class GUI implements CategoryListener {
 	public void onChange(ArrayList<Category> categories) {
 		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
 		updateCategories(categories);
-		textField.setText("");
 		model.reload();
 	}