Ver código fonte

Ordnung reingebracht, Objekte in Kategorien hinzufügen

Teh-Hai Julian Zheng 8 anos atrás
pai
commit
b157cbe4de

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


+ 1 - 0
src/Interfaces/CategoryListener.java

@@ -1,5 +1,6 @@
 package Interfaces;
 
+import java.awt.event.ActionListener;
 import java.util.ArrayList;
 import java.util.Locale.Category;
 

+ 85 - 41
src/ui/controller/CategoryController.java

@@ -2,108 +2,152 @@ package ui.controller;
 
 import java.util.ArrayList;
 
+import Interfaces.CategoryListener;
 import ui.model.*;
 import ui.view.*;
 
-public class CategoryController{
+public class CategoryController {
 
 	private IdCounter ID;
 	private Model M;
 
-	
-	
-	public CategoryController(Model model, IdCounter id){
+	public CategoryController(Model model, IdCounter id) {
 		this.ID = id;
 		this.M = model;
 		initCategories();
 	}
-	
-	
-	
+
 	/**
 	 * init default category and objects
 	 */
-	public void initCategories(){
+	public void initCategories() {
 		Category energy = new Category("Energy");
 		Category building = new Category("Building");
 		Category component = new Category("Component");
-		
+
 		HolonObject powerp = new HolonObject("Power Plant");
 		HolonObject house = new HolonObject("House");
 		HolonTransformer transformer = new HolonTransformer("Transformer");
 		HolonSwitch sw = new HolonSwitch("Switch");
-		
+
 		addObject(energy, powerp);
 		addObject(building, house);
 		addObject(component, transformer);
 		addObject(component, sw);
-		
+
 		addCategory(energy);
 		addCategory(building);
 		addCategory(component);
-		
+
 	}
-	
-	
+
 	/**
 	 * Adds Category into Model
-	 * @param toAdd neue Kategorie
+	 * 
+	 * @param toAdd
+	 *            neue Kategorie
 	 */
-	public void addCategory(Category toAdd){
-	M.addCategory(toAdd);
+	public void addCategory(Category toAdd) {
+		M.getCategories().add(toAdd);
+		notifyCatListeners();
 	}
-	
+
 	/**
 	 * Adds New Category into Model
-	 * @param name Bezeichnung der neuen Kategorie
+	 * 
+	 * @param name
+	 *            Bezeichnung der neuen Kategorie
 	 */
-	public void addNewCategory(String name){
-		
+	public void addNewCategory(String name) {
+
 		addCategory(new Category(name));
 	}
-	
+
 	/**
 	 * Add Object into a Category
-	 * @param cat Category
-	 * @param obj Object
+	 * 
+	 * @param cat
+	 *            Category
+	 * @param obj
+	 *            Object
 	 */
-	public void addObject(Category cat,CpsObject obj){
+	public void addObject(Category cat, CpsObject obj) {
 		cat.getObjects().add(obj);
+		notifyCatListeners();
 	}
-	
+
 	/**
 	 * Add new Holon Object
-	 * @param cat Category
-	 * @param obj New Object Name
+	 * 
+	 * @param cat
+	 *            Category
+	 * @param obj
+	 *            New Object Name
 	 */
-	public void addNewHolonObject(Category cat, String obj){
+	public void addNewHolonObject(Category cat, String obj) {
 		addObject(cat, new HolonObject(obj));
 	}
-	
+
 	/**
 	 * Add new Holon Transformer
-	 * @param cat Category
-	 * @param obj New Object Name
+	 * 
+	 * @param cat
+	 *            Category
+	 * @param obj
+	 *            New Object Name
 	 */
-	public void addNewHolonTransformer(Category cat, String obj){
+	public void addNewHolonTransformer(Category cat, String obj) {
 		addObject(cat, new HolonTransformer(obj));
 	}
-	
+
 	/**
 	 * Add new Holon Switch
-	 * @param cat Category
-	 * @param obj New Object Name
+	 * 
+	 * @param cat
+	 *            Category
+	 * @param obj
+	 *            New Object Name
 	 */
-	public void addNewHolonSwitch(Category cat, String obj){
+	public void addNewHolonSwitch(Category cat, String obj) {
 		addObject(cat, new HolonSwitch(obj));
 	}
 
+	/**
+	 * 
+	 */
+	public void notifyCatListeners() {
+		for (CategoryListener l : M.getCategoryListeners()) {
+			l.onChange(M.getCategories());
+		}
+	}
 
-	public void deleteCategory(int idx){
-	M.getCategories().remove(idx);
+	/**
+	 * 
+	 * @param catLis
+	 */
+	public void addCatListener(CategoryListener catLis) {
+		M.getCategoryListeners().add(catLis);
 	}
 
+	/**
+	 * search for category
+	 * @param name
+	 * @return
+	 */
+	public Category searchNode(String name) {
+		Category query = null;
+
+		for (Category cat : M.getCategories()) {
+			if (cat.getName().equals(name)) {
+				query = cat;
+				break;
+			}
+		}
+		return query;
+	}
+
+	public void deleteCategory(int idx) {
+		M.getCategories().remove(idx);
+	}
 
-	
-	
 }

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

@@ -4,10 +4,16 @@ import ui.model.Category;
 import ui.model.IdCounter;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+
+import Interfaces.CategoryListener;
 import ui.model.Model;
 import ui.view.GUI;
 
 public class Control {
+	
+	public enum command {
+		CATEGORY, OBJECT
+	}
 
 	
 	private IdCounter iD;
@@ -30,10 +36,25 @@ public class Control {
 	}
 	
 	public void addNewObject(Category cat, String name){
-		
+		categoryController.addNewHolonObject(cat, name);
 	}
 	
+	public void addNewTransformer(Category cat, String name){
+		categoryController.addNewHolonTransformer(cat, name);
+	}
 	
+	public void addNewSwitch(Category cat, String name){
+		categoryController.addNewHolonSwitch(cat, name);
+	}
+	
+	public Category searchCategory(String name) {
+		
+		return categoryController.searchNode(name);
+	}
+	
+	public void initListener(CategoryListener catLis) {
+		categoryController.addCatListener(catLis);
+	}
 	
 	
 	

+ 19 - 26
src/ui/model/Model.java

@@ -17,40 +17,17 @@ public class Model {
 	
 	private ArrayList<Category> categories;
 	private ArrayList<CpsObject> objectsOnCanvas;
-	private List<CategoryListener> categoryListeners = new LinkedList<>();
+	private List<CategoryListener> categoryListeners;
+	
 	
 	
 	public Model(){
 		setCategories(new ArrayList<Category>());
 		setObjectsOnCanvas(new ArrayList<CpsObject>());
+		setCategoryListeners(new LinkedList<>());
 	}
 	
-	/**
-	 * 
-	 * @param toAdd
-	 */
-	public void addCategory(Category toAdd){
-		categories.add(toAdd);
-		notifyCatListeners();
-	}
-	
-	/**
-	 * 
-	 * @param catLis
-	 */
-	public void addCatListener(CategoryListener catLis){
-		categoryListeners.add(catLis);
-	}
 
-	/**
-	 * 
-	 */
-	private void notifyCatListeners() {
-		for(CategoryListener l : categoryListeners){
-			l.onChange(this.categories);
-		}
-		
-	}
 
 	/**
 	 * @return the categories
@@ -83,6 +60,22 @@ public class Model {
 		this.objectsOnCanvas = objectsOnCanvas;
 	}
 
+
+	/**
+	 * @return the categoryListeners
+	 */
+	public List<CategoryListener> getCategoryListeners() {
+		return categoryListeners;
+	}
+
+
+	/**
+	 * @param categoryListeners the categoryListeners to set
+	 */
+	public void setCategoryListeners(List<CategoryListener> categoryListeners) {
+		this.categoryListeners = categoryListeners;
+	}
+
 	
 	
 	

+ 72 - 16
src/ui/view/GUI.java

@@ -75,19 +75,25 @@ public class GUI 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 final MyCanvas canvas = new MyCanvas();
+
 	private final JTree tree = new JTree();
 	private final JEditorPane dtrpnHereWillBe = new JEditorPane();
 	private final JSplitPane splitPane_2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
+
 	private JTable table;
-	private final JTable table_2 = new JTable();
-	private Model model;
+	private JTable table_2 = new JTable();
+	private final Model model;
 	private final Control controller;
 
 	private final JPanel panel = new JPanel();
 	private final JTextField textField = new JTextField();
 	private final JComboBox comboBox = new JComboBox();
+
+	// Buttons
 	private final JButton btnAdd = new JButton("Add");
+	private final JButton btnDel = new JButton("Del");
 
 	private final JToolBar toolBar = new JToolBar();
 
@@ -97,7 +103,7 @@ public class GUI implements CategoryListener {
 	public GUI(Control control) {
 		this.controller = control;
 		this.model = control.getModel();
-		model.addCatListener(this);
+		control.initListener(this);
 		initialize();
 	}
 
@@ -183,15 +189,58 @@ public class GUI implements CategoryListener {
 		panel.add(toolBar);
 		toolBar.add(comboBox);
 		comboBox.setModel(new DefaultComboBoxModel(new String[] { "Category", "Object", "Transformer", "Switch" }));
+
+		// Add Button
 		btnAdd.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
-				if (comboBox.getSelectedItem().toString() == "Category") {
+
+				Category selectedNode;
+				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 (comboBox.getSelectedItem().toString() == "Object") {
+					
+				} else if (text.isEmpty() || nodeInfo.toString().isEmpty()) {
+					// throw EmptyField or NoSelectedNode Exception
+
+				} 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());
+
 				}
+
 			}
 		});
 		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();
+				
+				if(nodeInfo.toString().isEmpty()){
+					
+				}
+
+
+			}
+		});
+		toolBar.add(btnDel);
+		
+		
 		panel.add(textField);
 		frmCyberPhysical.getContentPane().add(splitPane);
 
@@ -213,26 +262,19 @@ public class GUI implements CategoryListener {
 
 	}
 
-	public void onChange(ArrayList<Category> cats) {
-		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
-		refreshCategories(cats);
-		textField.setText("Test");
-		model.reload();
-	}
-
 	/**
 	 * reloads the Categories from Model
 	 */
-	public void refreshCategories(final ArrayList<Category> cats) {
+	public void updateCategories(final ArrayList<Category> categories) {
 		tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode("Categories") {
 			{
 				DefaultMutableTreeNode node_1;
-				for (Category c : cats) {
+				for (Category c : categories) {
 					node_1 = new DefaultMutableTreeNode(c.getName());
 
 					// kann eventuell umgeändert werden
-					for (CpsObject hol : c.getObjects()) {
-						node_1.add(new DefaultMutableTreeNode(hol.getObjName()));
+					for (CpsObject obj : c.getObjects()) {
+						node_1.add(new DefaultMutableTreeNode(obj.getObjName()));
 					}
 					add(node_1);
 				}
@@ -241,6 +283,20 @@ public class GUI implements CategoryListener {
 		}));
 	}
 
+	/**
+	 * 
+	 */
+	public void onChange(ArrayList<Category> categories) {
+		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
+		updateCategories(categories);
+		textField.setText("");
+		model.reload();
+	}
+
+	/**
+	 * 
+	 * @return
+	 */
 	public JFrame getFrmCyberPhysical() {
 		return frmCyberPhysical;
 	}

+ 2 - 2
src/ui/view/Main.java

@@ -29,10 +29,10 @@ public class Main {
 					Model 		M = new Model();
 					Control 	C = new Control(M, ID);
 					GUI 		V = new GUI(C);
-					M.addCatListener(V);
+					
 
 
-					V.refreshCategories(M.getCategories());
+					V.updateCategories(M.getCategories());
 					V.getFrmCyberPhysical().setVisible(true);
 
 				} catch (Exception e) {