Browse Source

nochmal keine doppelten kategorien möglich + pop up window für categorienamen eingabe

dominik.rieder 8 years ago
parent
commit
df475db684

+ 1 - 1
bin/.gitignore

@@ -1,4 +1,4 @@
-/ui/
 /Interfaces/
 /exceptions/
 /tests/
+/ui/

BIN
bin/tests/Tests1.class


BIN
bin/tests/praktikumHolonsTestMinimal.class


BIN
bin/ui/model/Model.class


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


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


BIN
bin/ui/view/GUI.class


+ 6 - 0
src/Interfaces/ComparableObject.java

@@ -0,0 +1,6 @@
+package Interfaces;
+
+public interface ComparableObject {
+	public String getCompareName();
+	public int getID();
+}

+ 24 - 2
src/ui/controller/CategoryController.java

@@ -1,8 +1,12 @@
 package ui.controller;
 
 import java.util.ArrayList;
+import java.util.LinkedList;
+
+import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
 
 import Interfaces.CategoryListener;
+import Interfaces.ComparableObject;
 import ui.model.*;
 import ui.view.*;
 
@@ -43,14 +47,26 @@ public class CategoryController {
 
 	/**
 	 * Adds Category into Model
+	 * if a Category with the same name already exists
+	 * Add Category_+1
 	 * 
 	 * @param toAdd
 	 *            neue Kategorie
 	 */
 	public void addCategory(Category toAdd) {
+		int number = 0;
+		String name = toAdd.getName();
+		while(containsInList( M.getCategories(),toAdd)){
+			number++;
+			toAdd.setName(name + "_" + number);
+		};
 		M.getCategories().add(toAdd);
 		notifyCatListeners();
 	}
+	
+	public void deleteCategory(int idx) {
+		M.getCategories().remove(idx);
+	}
 
 	/**
 	 * Adds New Category into Model
@@ -146,8 +162,14 @@ public class CategoryController {
 		return query;
 	}
 
-	public void deleteCategory(int idx) {
-		M.getCategories().remove(idx);
+	public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
+		for(ComparableObject obj : arrayList){
+			if(obj.getCompareName().equals(toSearch.getCompareName())){
+				return true;
+			}
+		}
+		return false;
 	}
+	
 
 }

+ 3 - 3
src/ui/controller/Control.java

@@ -1,9 +1,12 @@
 package ui.controller;
 
 import ui.model.Category;
+import ui.model.CpsObject;
 import ui.model.IdCounter;
+
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.util.LinkedList;
 
 import Interfaces.CategoryListener;
 import ui.model.Model;
@@ -57,9 +60,6 @@ public class Control {
 	}
 	
 	
-	
-	
-	
 	////////// etc
 	/**
 	 * Getter for Model

+ 15 - 1
src/ui/model/Category.java

@@ -2,14 +2,18 @@ package ui.model;
 
 import java.util.ArrayList;
 
-public class Category {
+import Interfaces.ComparableObject;
+
+public class Category implements ComparableObject{
 	
+	private int id;
 	private ArrayList<CpsObject> objects;
 	private String name;
 	
 	public Category(String name){
 		this.setObjects(new ArrayList<CpsObject>());
 		this.setName(name);
+		id = -1;
 	}
 
 	/**
@@ -39,6 +43,16 @@ public class Category {
 	public void setName(String name) {
 		this.name = name;
 	}
+
+	@Override
+	public String getCompareName() {
+		return getName();
+	}
+
+	@Override
+	public int getID() {
+		return id;
+	}
 	
 //	public void addObject(HolonObject toAdd){
 //		objects.add(toAdd);

+ 8 - 1
src/ui/model/CpsObject.java

@@ -2,7 +2,9 @@ package ui.model;
 
 import java.util.ArrayList;
 
-public class CpsObject {
+import Interfaces.ComparableObject;
+
+public class CpsObject implements ComparableObject {
 	/* Type of the Object */
 	String objName;
 	/* Name given by the user */
@@ -96,4 +98,9 @@ public class CpsObject {
 		this.energyOut = energyOut;
 	}
 
+	@Override
+	public String getCompareName() {
+		return objName;
+	}
+
 }

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

@@ -24,7 +24,7 @@ public class Model {
 	public Model(){
 		setCategories(new ArrayList<Category>());
 		setObjectsOnCanvas(new ArrayList<CpsObject>());
-		setCategoryListeners(new LinkedList<>());
+		setCategoryListeners(new LinkedList<CategoryListener>());
 	}
 	
 
@@ -70,10 +70,10 @@ public class Model {
 
 
 	/**
-	 * @param categoryListeners the categoryListeners to set
+	 * @param linkedList the categoryListeners to set
 	 */
-	public void setCategoryListeners(List<CategoryListener> categoryListeners) {
-		this.categoryListeners = categoryListeners;
+	public void setCategoryListeners(LinkedList<CategoryListener> linkedList) {
+		this.categoryListeners = linkedList;
 	}
 
 	

+ 21 - 0
src/ui/model/tests.java

@@ -0,0 +1,21 @@
+package ui.model;
+
+import java.util.ArrayList;
+
+import ui.controller.CategoryController;
+
+public class tests {
+	
+	public static void main(String[] args){
+		CategoryController cc = new CategoryController(new Model(), new IdCounter());
+		ArrayList<Category> cats = new ArrayList<Category>();
+		cats.add(new Category("sup"));
+		boolean value;
+		value = cc.containsInList(cats, new Category("sup"));
+		if(value){
+			System.out.println("true");
+		}else{
+			System.out.println("false");
+		}
+	}
+}

+ 8 - 8
src/ui/view/GUI.java

@@ -8,6 +8,7 @@ import javax.swing.JFrame;
 import javax.swing.JMenuBar;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.JTree;
@@ -198,14 +199,13 @@ public class GUI implements CategoryListener {
 				Object nodeInfo = tree.getLastSelectedPathComponent();
 				String selected = comboBox.getSelectedItem().toString();
 				String text = textField.getText();
-
-				if (selected.equals("Category") && !text.isEmpty()) {
-					controller.addNewCategory(textField.getText());
-					
-				} else if (text.isEmpty() || nodeInfo.toString().isEmpty()) {
-					// throw EmptyField or NoSelectedNode Exception
-
-				} else if (selected.equals("Object")) {
+				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());