Quellcode durchsuchen

Control und View changes

dominik.rieder vor 8 Jahren
Ursprung
Commit
69df6e32bf

+ 31 - 0
Model.java

@@ -0,0 +1,31 @@
+package ui.model;
+
+import java.util.ArrayList;
+
+import ui.controller.*;
+
+public class Model {
+	private ArrayList<Category> Categories;
+	private ArrayList<HolonObject> ObjectsOnCanvas;
+	
+	public Model(){
+		Categories = new ArrayList<Category>();
+		ObjectsOnCanvas = new ArrayList<HolonObject>();
+	}
+	
+	public void addCategory(Category toAdd){
+		Categories.add(toAdd);
+	}
+	
+	public void deleteCategory(int idx){
+		Categories.remove(idx);
+	}
+	
+	public void addObject(HolonObject toAdd){
+		ObjectsOnCanvas.add(toAdd);
+	}
+	
+	public void deleteObject(int idx){
+		ObjectsOnCanvas.remove(idx);
+	}
+}

+ 1 - 2
bin/.gitignore

@@ -1,3 +1,2 @@
-/exceptions/
-/tests/
 /ui/
+/tests/

BIN
bin/exceptions/newException1.class


BIN
bin/tests/Tests1.class


BIN
bin/ui/controller/IdCounter.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


BIN
bin/ui/view/MyCanvas.class


+ 7 - 7
src/tests/Tests1.java

@@ -12,12 +12,12 @@ public class Tests1 {
 	 * Create some CPS object and Gadgets
 	 */
 	public static void main(String[] args) {
-		HolonObject b1 = new HolonObject();
-		HolonObject b2 = new HolonObject();
-		HolonSwitch s1 = new HolonSwitch();
-		HolonTransformer t1 = new HolonTransformer();
-		b1.setPos(20, 30);
-		Position p = b1.getPos();
-		System.out.println(p.x + "," + p.y);
+//		HolonObject b1 = new HolonObject();
+//		HolonObject b2 = new HolonObject();
+//		HolonSwitch s1 = new HolonSwitch();
+//		HolonTransformer t1 = new HolonTransformer();
+//		b1.setPos(20, 30);
+//		Position p = b1.getPos();
+//		System.out.println(p.x + "," + p.y);
 	}
 }

+ 67 - 0
src/ui/controller/CategoryControl.java

@@ -0,0 +1,67 @@
+package ui.controller;
+
+import java.util.ArrayList;
+
+import com.sun.glass.ui.View;
+
+import ui.model.*;
+import ui.view.*;
+
+public class CategoryControl{
+
+	private IdCounter id;
+	private Model model;
+	private GUI view;
+	
+	
+	public CategoryControl(Model model, GUI view, IdCounter id){
+		this.id = id;
+		this.model = model;
+		this.view = view;
+		initCategories();
+	}
+	
+	
+	
+	/**
+	 * initialisiert alle Standart Kategorien und Objekte
+	 */
+	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");
+		HolonObject transformer = new HolonObject("Transformer");
+		HolonObject sw = new HolonObject("Switch");
+		
+		energy.addObject(powerp);
+		building.addObject(house);
+		component.addObject(transformer);
+		component.addObject(sw);
+		
+		model.addCategory(energy);
+		model.addCategory(building);
+		model.addCategory(component);
+		
+		
+	}
+	
+	
+	/**
+	 * läd die Kategorien und Objekte in die View
+	 */
+	public void loadCategories(){
+		//ArrayList<Category> category = m.getCategories();
+		
+
+//		for (Category c : category) {
+//			
+//		}
+	}
+	
+	
+	
+	
+}

+ 23 - 0
src/ui/controller/Control.java

@@ -0,0 +1,23 @@
+package ui.controller;
+
+import ui.model.IdCounter;
+import ui.model.Model;
+import ui.view.GUI;
+
+public class Control {
+
+	
+	private IdCounter id;
+	private Model model;
+	private GUI view;
+	
+	public Control(Model model, GUI view, IdCounter id){
+		this.model = model;
+		this.view = view;
+		this.id = id;
+		CategoryControl cc = new CategoryControl(model, view, id);
+	}
+	
+	
+	
+}

+ 33 - 0
src/ui/model/Category.java

@@ -0,0 +1,33 @@
+package ui.model;
+
+import java.util.ArrayList;
+
+public class Category {
+	private ArrayList<HolonObject> objects;
+	private String name;
+	
+	public Category(String name){
+		this.objects = new ArrayList<>();
+		this.name = name;
+	}
+	
+	public void addObject(HolonObject toAdd){
+		objects.add(toAdd);
+	}
+	
+	public void deleteObject(int idx){
+		objects.remove(idx);
+	}
+	
+	public ArrayList<HolonObject> getObjects(){
+		return objects;
+	}
+	
+	public void changeName(String name){
+		this.name = name;
+	}
+	
+	public String getName(){
+		return name;
+	}
+}

+ 3 - 2
src/ui/controller/CpsObject.java → src/ui/model/CpsObject.java

@@ -1,4 +1,4 @@
-package ui.controller;
+package ui.model;
 
 import java.util.ArrayList;
 
@@ -22,7 +22,8 @@ abstract class CpsObject {
 	/**
 	 * Constructor for an CpsObejct with an unique ID
 	 */
-	public CpsObject() {
+	public CpsObject(String objName) {
+		this.objName = objName;
 		connectedTo = new ArrayList<CpsObject>();
 		position = new Position();
 	}

+ 1 - 1
src/ui/controller/HolonElement.java → src/ui/model/HolonElement.java

@@ -1,4 +1,4 @@
-package ui.controller;
+package ui.model;
 
 public class HolonElement {
 

+ 3 - 3
src/ui/controller/HolonObject.java → src/ui/model/HolonObject.java

@@ -1,4 +1,4 @@
-package ui.controller;
+package ui.model;
 
 import java.util.ArrayList;
 
@@ -22,8 +22,8 @@ public class HolonObject extends CpsObject {
 	 * Constructor
 	 * Set by default the name of the object equals to the category name, until the user changes it.
 	 */
-	public HolonObject() {
-		super();
+	public HolonObject(String ObjName) {
+		super(ObjName);
 	}
 	
 	public void addConsumer(HolonElement consumer){

+ 3 - 3
src/ui/controller/HolonSwitch.java → src/ui/model/HolonSwitch.java

@@ -1,11 +1,11 @@
-package ui.controller;
+package ui.model;
 
 public class HolonSwitch extends CpsObject {
 	/*True, if this wire is working (capable of carrying electricity), else false*/
 	boolean isWorking;
 
-	public HolonSwitch() {
-		super();
+	public HolonSwitch(String ObjName) {
+		super(ObjName);
 		isWorking = false;
 	}
 	

+ 3 - 3
src/ui/controller/HolonTransformer.java → src/ui/model/HolonTransformer.java

@@ -1,4 +1,4 @@
-package ui.controller;
+package ui.model;
 
 public class HolonTransformer extends CpsObject {
 
@@ -9,8 +9,8 @@ public class HolonTransformer extends CpsObject {
 	 * Constructor Set type of object (Transformer), its transform ratio and the
 	 * default name ("Transformer");
 	 */
-	public HolonTransformer() {
-		super();
+	public HolonTransformer(String ObjName) {
+		super(ObjName);
 	}
 
 	/**

+ 1 - 1
src/ui/controller/IdCounter.java → src/ui/model/IdCounter.java

@@ -1,4 +1,4 @@
-package ui.controller;
+package ui.model;
 
 public class IdCounter {
 	private static int counter = 0;

+ 11 - 0
src/ui/model/Model.java

@@ -8,6 +8,7 @@ public class Model {
 	private ArrayList<Category> Categories;
 	private ArrayList<HolonObject> ObjectsOnCanvas;
 	
+	
 	public Model(){
 		Categories = new ArrayList<Category>();
 		ObjectsOnCanvas = new ArrayList<HolonObject>();
@@ -28,4 +29,14 @@ public class Model {
 	public void deleteObject(int idx){
 		ObjectsOnCanvas.remove(idx);
 	}
+	
+	public ArrayList<Category> getCategories() {
+		return Categories;
+	}
+
+	public void setCategories(ArrayList<Category> categories) {
+		Categories = categories;
+	}
+	
+	
 }

+ 17 - 0
src/ui/model/Position.java

@@ -0,0 +1,17 @@
+package ui.model;
+
+public class Position {
+	public int x;
+	public int y;
+	
+	public Position(int x, int y){
+		this.x = x;
+		this.y = y;
+	}
+	
+	//default Constructor
+	public Position(){
+		this.x = -1;
+		this.y = -1;
+	}
+}

+ 33 - 49
src/ui/view/GUI.java

@@ -18,6 +18,9 @@ import javax.swing.JTabbedPane;
 import javax.swing.JTable;
 import javax.swing.tree.DefaultTreeModel;
 
+import ui.model.Category;
+import ui.model.HolonObject;
+import ui.model.Model;
 
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.JEditorPane;
@@ -33,6 +36,8 @@ import javax.swing.JPanel;
 public class GUI {
 
 	private JFrame frmCyberPhysical;
+
+
 	private final JMenuBar menuBar = new JMenuBar();
 	private final JMenu mnNewMenu = new JMenu("File");
 	private final JMenu mnNewMenu_1 = new JMenu("Edit");
@@ -53,37 +58,14 @@ public class GUI {
 	private final JSplitPane splitPane_2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
 	private JTable table;
 	private final JTable table_2 = new JTable();
+	private Model model;
 	
-	/**
-	 * Launch the application.
-	 */
-	public static void main(String[] args) {
-		
-		//*Design
-		try {
-			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
-		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
-				| UnsupportedLookAndFeelException e1) {
-			// TODO Auto-generated catch block
-			e1.printStackTrace();
-		}
-		
-		EventQueue.invokeLater(new Runnable() {
-			public void run() {
-				try {
-					GUI window = new GUI();
-					window.frmCyberPhysical.setVisible(true);
-				} catch (Exception e) {
-					e.printStackTrace();
-				}
-			}
-		});
-	}
 
 	/**
 	 * Create the application.
 	 */
-	public GUI() {
+	public GUI(Model model) {
+		this.model = model;
 		initialize();
 	}
 
@@ -177,29 +159,7 @@ public class GUI {
 		
 		splitPane.setLeftComponent(scrollPane_1);
 		tree.setEditable(true);
-		tree.setModel(new DefaultTreeModel(
-			new DefaultMutableTreeNode("Components") {
-				{
-					DefaultMutableTreeNode node_1;
-					node_1 = new DefaultMutableTreeNode("PowerPlant");
-						node_1.add(new DefaultMutableTreeNode("Standart P.P"));
-						node_1.add(new DefaultMutableTreeNode("Power PowerPlant"));
-					add(node_1);
-					node_1 = new DefaultMutableTreeNode("Houses");
-						node_1.add(new DefaultMutableTreeNode("Hospital"));
-						node_1.add(new DefaultMutableTreeNode("Standart House"));
-						node_1.add(new DefaultMutableTreeNode("Castle"));
-						node_1.add(new DefaultMutableTreeNode("Arena"));
-					add(node_1);
-					node_1 = new DefaultMutableTreeNode("Cars");
-						node_1.add(new DefaultMutableTreeNode("Small Car"));
-						node_1.add(new DefaultMutableTreeNode("Medium  Car"));
-						node_1.add(new DefaultMutableTreeNode("Big Car"));
-						node_1.add(new DefaultMutableTreeNode("Invisible Car"));
-					add(node_1);
-				}
-			}
-		));
+		
 		
 		scrollPane_1.setViewportView(tree);
 		
@@ -219,4 +179,28 @@ public class GUI {
 	    });
 		
 	}
+	
+	public void refreshCategories(){
+		tree.setModel(new DefaultTreeModel(
+				new DefaultMutableTreeNode("Categories") {
+					{
+						DefaultMutableTreeNode node_1;
+						for (Category c : model.getCategories()) {
+							node_1 = new DefaultMutableTreeNode(c.getName());
+							
+							for (HolonObject hol : c.getObjects()) {
+								node_1.add(new DefaultMutableTreeNode(hol.getObjName()));
+							}
+							add(node_1);
+						}
+						
+					}
+				}
+			));
+	}
+	
+	public JFrame getFrmCyberPhysical() {
+		return frmCyberPhysical;
+	}
+
 }

+ 41 - 0
src/ui/view/Main.java

@@ -0,0 +1,41 @@
+package ui.view;
+import java.awt.EventQueue;
+
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+import ui.controller.*;
+import ui.model.*;
+
+public class Main {
+
+public static void main(String[] args) {
+		
+		//*Design
+		try {
+			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
+				| UnsupportedLookAndFeelException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
+		
+		EventQueue.invokeLater(new Runnable() {
+			public void run() {
+				try {
+					IdCounter id = new IdCounter();
+					Model model = new Model();
+					GUI window = new GUI(model);
+					Control control = new Control(model,window, id);
+					window.refreshCategories();
+					
+					window.getFrmCyberPhysical().setVisible(true);
+					
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+		});
+	}
+
+}