Explorar el Código

Listeners for View + Add Category

dominik.rieder hace 8 años
padre
commit
8b910fc115

+ 1 - 0
bin/.gitignore

@@ -1 +1,2 @@
 /ui/
+/Interfaces/

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


+ 8 - 0
src/Interfaces/CategoryListener.java

@@ -0,0 +1,8 @@
+package Interfaces;
+
+import java.util.ArrayList;
+import java.util.Locale.Category;
+
+public interface CategoryListener {
+	public void onChange(ArrayList<ui.model.Category> categories);
+}

+ 4 - 5
src/ui/controller/CategoryControl.java

@@ -9,13 +9,12 @@ public class CategoryControl{
 
 	private IdCounter ID;
 	private Model M;
-	private GUI V;
+
 	
 	
-	public CategoryControl(Model model, GUI view, IdCounter id){
+	public CategoryControl(Model model, IdCounter id){
 		this.ID = id;
 		this.M = model;
-		this.V = view;
 		initCategories();
 	}
 	
@@ -51,7 +50,7 @@ public class CategoryControl{
 	 * @param toAdd neue Kategorie
 	 */
 	public void addCategory(Category toAdd){
-	M.getCategories().add(toAdd);
+	M.addCategory(toAdd);
 	}
 	
 	/**
@@ -60,7 +59,7 @@ public class CategoryControl{
 	 */
 	public void addNewCategory(String name){
 		
-		addCategory(new Category(name));
+		M.addCategory(new Category(name));
 	}
 	
 	/**

+ 14 - 4
src/ui/controller/Control.java

@@ -1,6 +1,8 @@
 package ui.controller;
 
 import ui.model.IdCounter;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import ui.model.Model;
 import ui.view.GUI;
 
@@ -9,13 +11,21 @@ public class Control {
 	
 	private IdCounter id;
 	private Model model;
-	private GUI view;
+	private ActionListener actionListener;
+	private final CategoryControl catControler;
 	
-	public Control(Model model, GUI view, IdCounter id){
+	public Control(Model model, IdCounter id){
 		this.model = model;
-		this.view = view;
 		this.id = id;
-		CategoryControl cc = new CategoryControl(model, view, id);
+		this.catControler = new CategoryControl(model, id);
+	}
+	
+	public void addCategory(String catName){
+		catControler.addNewCategory(catName);
+	}
+
+	public Model getModel() {
+		return model;
 	}
 	
 	

+ 27 - 6
src/ui/model/Model.java

@@ -1,6 +1,10 @@
 package ui.model;
 
+import Interfaces.CategoryListener;
+
 import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
 
 import ui.controller.*;
 
@@ -10,21 +14,38 @@ public class Model {
 	private int HEIGHT;
 	private int WIDTH;
 	
-	private ArrayList<Category> Categories;
-	private ArrayList<CpsObject> ObjectsOnCanvas;
+	
+	private ArrayList<Category> categories;
+	private ArrayList<CpsObject> objectsOnCanvas;
+	private List<CategoryListener> categoryListeners = new LinkedList<>();
 	
 	
 	public Model(){
 		setCategories(new ArrayList<Category>());
 		setObjectsOnCanvas(new ArrayList<CpsObject>());
 	}
+	
+	public void addCategory(Category toAdd){
+		categories.add(toAdd);
+		notifyCatListeners();
+	}
+	
+	public void addCatListener(CategoryListener catLis){
+		categoryListeners.add(catLis);
+	}
 
+	private void notifyCatListeners() {
+		for(CategoryListener l : categoryListeners){
+			l.onChange(this.categories);
+		}
+		
+	}
 
 	/**
 	 * @return the categories
 	 */
 	public ArrayList<Category> getCategories() {
-		return Categories;
+		return categories;
 	}
 
 
@@ -32,7 +53,7 @@ public class Model {
 	 * @param categories the categories to set
 	 */
 	public void setCategories(ArrayList<Category> categories) {
-		Categories = categories;
+		this.categories = categories;
 	}
 
 
@@ -40,7 +61,7 @@ public class Model {
 	 * @return the objectsOnCanvas
 	 */
 	public ArrayList<CpsObject> getObjectsOnCanvas() {
-		return ObjectsOnCanvas;
+		return objectsOnCanvas;
 	}
 
 
@@ -48,7 +69,7 @@ public class Model {
 	 * @param objectsOnCanvas the objectsOnCanvas to set
 	 */
 	public void setObjectsOnCanvas(ArrayList<CpsObject> objectsOnCanvas) {
-		ObjectsOnCanvas = objectsOnCanvas;
+		this.objectsOnCanvas = objectsOnCanvas;
 	}
 
 	

+ 45 - 6
src/ui/view/GUI.java

@@ -2,6 +2,7 @@ package ui.view;
 
 import java.awt.EventQueue;
 import java.io.File;
+import java.util.ArrayList;
 
 import javax.swing.JFrame;
 import javax.swing.JMenuBar;
@@ -10,7 +11,6 @@ import javax.swing.JMenuItem;
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
 import javax.swing.JTree;
-
 import javax.swing.BoxLayout;
 import javax.swing.JSplitPane;
 import javax.swing.JScrollPane;
@@ -19,17 +19,25 @@ import javax.swing.JTable;
 import javax.swing.tree.DefaultTreeModel;
 
 
+
+
+
+import ui.controller.Control;
 import ui.model.*;
 
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.JEditorPane;
 import javax.swing.JFileChooser;
 import javax.swing.table.DefaultTableModel;
+
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.Window.Type;
+
 import javax.swing.JInternalFrame;
+
 import java.awt.BorderLayout;
+
 import javax.swing.JPanel;
 import javax.swing.JTextField;
 import javax.swing.JToolBar;
@@ -37,16 +45,23 @@ import javax.swing.JButton;
 import javax.swing.JList;
 import javax.swing.ListSelectionModel;
 import javax.swing.AbstractListModel;
+
 import java.awt.Choice;
+
 import javax.swing.JComboBox;
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.GroupLayout;
 import javax.swing.GroupLayout.Alignment;
+
+import Interfaces.CategoryListener;
+
 import java.awt.GridBagLayout;
 import java.awt.GridBagConstraints;
 import java.awt.Font;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
 
-public class GUI {
+public class GUI implements CategoryListener {
 
 	private JFrame frmCyberPhysical;
 
@@ -72,18 +87,27 @@ public class GUI {
 	private JTable table;
 	private final JTable table_2 = new JTable();
 	private Model model;
+	private final Control control;
+	public Control getControl() {
+		return control;
+	}
+
 	private final JPanel panel = new JPanel();
 	private final JTextField textField = new JTextField();
 	private final JComboBox comboBox = new JComboBox();
 	private final JButton btnAdd = new JButton("Add");
+
+
 	private final JToolBar toolBar = new JToolBar();
 	
 
 	/**
 	 * Create the application.
 	 */
-	public GUI(Model model) {
-		this.model = model;
+	public GUI(Control control) {
+		this.control = control;
+		this.model = control.getModel();
+		model.addCatListener(this);
 		initialize();
 	}
 
@@ -187,6 +211,15 @@ public class GUI {
 		panel.add(toolBar);
 		toolBar.add(comboBox);
 		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Category", "Object"}));
+		btnAdd.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				if(comboBox.getSelectedItem().toString() == "Category"){
+					control.addCategory(textField.getText());
+				}
+				else if(comboBox.getSelectedItem().toString()== "Object"){
+				}
+			}
+		});
 		toolBar.add(btnAdd);
 		panel.add(textField);
 		frmCyberPhysical.getContentPane().add(splitPane);
@@ -208,15 +241,21 @@ public class GUI {
 		
 	}
 	
+	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(){
+	public void refreshCategories(final ArrayList<Category> cats){
 		tree.setModel(new DefaultTreeModel(
 				new DefaultMutableTreeNode("Categories") {
 					{
 						DefaultMutableTreeNode node_1;
-						for (Category c : model.getCategories()) {
+						for (Category c : cats) {
 							node_1 = new DefaultMutableTreeNode(c.getName());
 							
 							//kann eventuell umgeändert werden

+ 6 - 3
src/ui/view/Main.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import java.awt.EventQueue;
+import java.util.ArrayList;
 
 import javax.swing.UIManager;
 import javax.swing.UnsupportedLookAndFeelException;
@@ -26,10 +27,12 @@ public class Main {
 				try {
 					IdCounter 	ID = new IdCounter();
 					Model 		M = new Model();
-					GUI 		V = new GUI(M);
-					Control 	C = new Control(M, V, ID);
+					Control 	C = new Control(M, ID);
+					GUI 		V = new GUI(C);
+					M.addCatListener(V);
 
-					V.refreshCategories();
+
+					V.refreshCategories(M.getCategories());
 					V.getFrmCyberPhysical().setVisible(true);
 
 				} catch (Exception e) {