Browse Source

Merge branch 'updateInProgress' into HolonModel

TomTroppmann 2 years ago
parent
commit
429f17310a

BIN
exampleNetworks/undersupplied_holon_model_eval.holon


BIN
res/Images/image_not_found.PNG


+ 2 - 2
src/algorithm/objectiveFunction/ObjectiveFunctionByCarlos.java

@@ -16,9 +16,9 @@ public class ObjectiveFunctionByCarlos {
 	
 	//kappas for squashing function
 //	
-	static double k_eb = 1050000.f, k_state = 10000, k_pro = 2000, k_perf = 11000, k_holon= 150000;
+	//static double k_eb = 1050000.f, k_state = 10000, k_pro = 2000, k_perf = 11000, k_holon= 150000;
 //  oversupplied
-//	static double k_eb = 750000.f, k_state = 20000, k_pro = 3000, k_perf = 15000, k_holon= 200000;
+	static double k_eb = 750000.f, k_state = 20000, k_pro = 3000, k_perf = 15000, k_holon= 200000;
 	
 //	old values undersupplied 
 //	static double k_eb = 1000000.f, k_state = 15000, k_pro = 2100, k_perf = 12000, k_holon= 200000;

+ 9 - 3
src/ui/controller/CategoryController.java

@@ -9,6 +9,7 @@ import classes.HolonObject;
 import classes.HolonSwitch;
 import classes.Pair;
 import ui.model.Model;
+import utility.Event;
 
 /**
  * Controller for the Categories.
@@ -18,6 +19,7 @@ import ui.model.Model;
 public class CategoryController {
 	private Model model;
 	private MultiPurposeController mpC;
+	private final Event OnCategoryChanged;
 
 	/**
 	 * Constructor.
@@ -26,10 +28,12 @@ public class CategoryController {
 	 *            the Model
 	 * @param mp
 	 *            the MultiPurposeController
+	 * @param control 
 	 */
-	public CategoryController(Model model, MultiPurposeController mp) {
+	public CategoryController(Model model, MultiPurposeController mp, Control control) {
 		this.model = model;
 		this.mpC = mp;
+		this.OnCategoryChanged = control.OnCategoryChanged;
 		initCategories();
 	}
 
@@ -45,6 +49,7 @@ public class CategoryController {
 				"/Images/power-plant.png");
 		addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
 		addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
+		OnCategoryChanged.broadcast();
 	}
 
 	/**
@@ -63,6 +68,7 @@ public class CategoryController {
 		}
 		model.getCgIdx().put(category.getName(), model.getCategories().size());
 		model.getCategories().add(category);
+		OnCategoryChanged.broadcast();
 	}
 
 	/**
@@ -72,7 +78,6 @@ public class CategoryController {
 	 *            Bezeichnung der neuen Kategorie
 	 */
 	public void addNewCategory(String name) {
-
 		addCategory(new Category(name));
 	}
 
@@ -86,6 +91,7 @@ public class CategoryController {
 		mpC.decIdx(c.getName(), model.getCgIdx());
 		model.getCgIdx().remove(c.getName());
 		model.getCategories().remove(c);
+		OnCategoryChanged.broadcast();
 
 	}
 	
@@ -191,7 +197,7 @@ public class CategoryController {
 		mpC.decIdx(cps.getObjName(), category.getObjIdx());
 		category.getObjIdx().remove(cps.getObjName());
 		category.getObjects().remove(cps);
-
+		OnCategoryChanged.broadcast();
 	}
 
 	/**

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

@@ -23,6 +23,7 @@ import ui.model.Model;
 import ui.model.Model.FairnessModel;
 import ui.view.CreateTemplatePopUp;
 import ui.view.GUI;
+import utility.Event;
 
 /**
  * The Class represents the controller in the model, controller view Pattern.
@@ -51,6 +52,8 @@ public class Control {
     private String dimensionsFileName = "dimensions";
     private int rand;
 
+    
+    public Event OnCategoryChanged = new Event();
     /**
      * Constructor.
      *
@@ -60,7 +63,7 @@ public class Control {
         this.model = model;
 
         this.multiPurposeController = new MultiPurposeController(model);
-        this.categoryController = new CategoryController(model, multiPurposeController);
+        this.categoryController = new CategoryController(model, multiPurposeController, this);
         this.objectController = new ObjectController(model, multiPurposeController);
         this.canvasController = new CanvasController(model, multiPurposeController);
         this.globalController = new GlobalController(model);

+ 21 - 20
src/ui/view/GUI.java

@@ -177,7 +177,7 @@ public class GUI{
 	private final JLabel minGraph = new JLabel("0%");
 	private final JLabel elementGraph = new JLabel("None ");
 	private final ArrayList<HolonElement> selectedElements = new ArrayList<>();
-	private final JTree tree = new JTree();
+	private final JTree categoryTree = new JTree();
 	/******************************************
 	 ************* Right Container*************
 	 ******************************************
@@ -338,6 +338,7 @@ public class GUI{
 		initialize();
 		updateCategories(model.getCategories());
 		updCon = new UpdateController(model, controller);
+		control.OnCategoryChanged.addListener(() -> this.updateCategoryUI(model.getCategories()));
 	}
 
 
@@ -728,8 +729,8 @@ public class GUI{
 			} catch (Exception e2) {
 				System.out.println(e2.getMessage());
 			}
-
-			tree.repaint();
+			categoryTree.revalidate();
+			categoryTree.repaint();
 		});
 
 
@@ -1540,7 +1541,7 @@ public class GUI{
 		 ****************/
 
 		// Override Key Actions
-		inputMap = tree.getInputMap();
+		inputMap = categoryTree.getInputMap();
 		inputMap.put(KeyStroke.getKeyStroke("control C"), cntrlCDown);
 		inputMap.put(KeyStroke.getKeyStroke("control V"), cntrlVDown);
 		inputMap.put(KeyStroke.getKeyStroke("control X"), cntrlXDown);
@@ -1585,9 +1586,9 @@ public class GUI{
 			}
 		};
 		
-		tree.setCellRenderer(customRenderer);
+		categoryTree.setCellRenderer(customRenderer);
 
-		tree.addMouseMotionListener(new MouseMotionAdapter() {
+		categoryTree.addMouseMotionListener(new MouseMotionAdapter() {
 
 			public void mouseDragged(MouseEvent e){
 				checkForDragAndDrop(e);
@@ -1662,7 +1663,7 @@ public class GUI{
 			}});
 
 		
-		tree.addMouseListener(new MouseAdapter() {
+		categoryTree.addMouseListener(new MouseAdapter() {
 			
 			public void mouseReleased(MouseEvent e) {
 				try {
@@ -1754,16 +1755,16 @@ public class GUI{
 		editItem.setEnabled(false);
 		editItem.addActionListener(actionEvent -> {
 		});
-		tree.addMouseListener(new MouseAdapter() {
+		categoryTree.addMouseListener(new MouseAdapter() {
 		
 			public void mousePressed(MouseEvent e) {
 				try {
-					actualObjectClicked = tree
+					actualObjectClicked = categoryTree
 							.getPathForLocation(e.getX(), e.getY())
 							.getLastPathComponent().toString();
 					// if an Object was selected, the porperties are shown in
 					// the table
-					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree
+					DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) categoryTree
 							.getPathForLocation(e.getX(), e.getY())
 							.getLastPathComponent();
 					if (selectedNode.getLevel() == 2) {
@@ -1845,7 +1846,7 @@ public class GUI{
 				e.printStackTrace();
 			}
 		});
-		scrollPane1.setViewportView(tree);
+		scrollPane1.setViewportView(categoryTree);
 
 		scrollPane1.setColumnHeaderView(panel);
 		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
@@ -1995,7 +1996,7 @@ public class GUI{
 
 		// Del Button
 		btnDel.addActionListener(actionEvent -> {
-			Object nodeInfo = tree.getLastSelectedPathComponent();
+			Object nodeInfo = categoryTree.getLastSelectedPathComponent();
 			if (nodeInfo != null) {
 				DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) nodeInfo;
 				String nodeName = selectedNode.getUserObject().toString();
@@ -2029,7 +2030,7 @@ public class GUI{
 				JOptionPane.showMessageDialog(holegJFrame,
 						selectObjBeforeErase);
 			}
-			tree.repaint();
+			categoryTree.repaint();
 		});
 		btnDel.setIcon(new ImageIcon(ImageImport.loadImage("Images/minus.png", 16, 16)));
 		btnDel.setToolTipText("<html><b>Delete</b><br>Removes a Category or a Category Item.</html>");
@@ -2081,7 +2082,7 @@ public class GUI{
 					try {
 						controller.loadFile(file.getAbsolutePath());
 						canvas.repaint();
-						tree.repaint();
+						categoryTree.repaint();
 						controller.calculateStateAndVisualForCurrentTimeStep();
 					} catch (IOException | ArchiveException e) {
 						e.printStackTrace();
@@ -2295,21 +2296,21 @@ public class GUI{
 		openMenu.addActionListener(actionEvent -> {
 			new AddOnWindow(holegJFrame, controller);
 		});
-		openMenu.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()));
+		openMenu.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
 		menuWindow.add(openMenu);
 		//Outliner
 		JMenuItem openOutliner =  new JMenuItem("Open Outliner", new ImageIcon(ImageImport.loadImage("/Button_Images/iconOutliner.png").getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
 		openOutliner.addActionListener(actionEvent -> {
 			outlinerList.add(new Outliner(holegJFrame, model, controller));
 		});
-		openOutliner.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()));
+		openOutliner.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
 		menuWindow.add(openOutliner);
 		//FlexWindow
 		JMenuItem openFlexMenuItem =  new JMenuItem("Open Flexibility Panel", new ImageIcon(ImageImport.loadImage("/Button_Images/iconAlgo.png").getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));
 		openFlexMenuItem.addActionListener(actionEvent -> {
 			flexList.add(new FlexWindow(holegJFrame, controller));
 		});
-		openFlexMenuItem.setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMaskEx()));
+		openFlexMenuItem.setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit ().getMenuShortcutKeyMask()));
 		menuWindow.add(openFlexMenuItem);
 	}
 
@@ -2418,7 +2419,7 @@ public class GUI{
 
 			}
 		});
-		tree.setModel(treemodel);
+		categoryTree.setModel(treemodel);
 	}
 
 	/**
@@ -2427,8 +2428,8 @@ public class GUI{
 	 * @param categories
 	 *            the Categories
 	 */
-	public void onChange(ArrayList<Category> categories) {
-		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
+	public void updateCategoryUI(ArrayList<Category> categories) {
+		DefaultTreeModel model = (DefaultTreeModel) categoryTree.getModel();
 		updateCategories(categories);
 		model.reload();
 	}

+ 63 - 76
src/utility/ImageImport.java

@@ -2,6 +2,8 @@ package utility;
 
 import java.awt.Image;
 import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.WritableRaster;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -12,59 +14,35 @@ import javax.imageio.ImageIO;
 
 /**
  * 
- * @author TU-Darmstadt BP Gruppe 7 WS17/18
- *	Centralized resource loading methods
- *	for improved performance and easier troubleshooting.
+ * @author TU-Darmstadt BP Gruppe 7 WS17/18 Centralized resource loading methods
+ *         for improved performance and easier troubleshooting.
  */
 public class ImageImport {
-	
-	/**
-	 * 30x30 pixel FileNotFound Icon, which can be shown without using I/O operations
-	 * a red cross.
-	 */
-	static private BufferedImage defaultImage;
 	/*
-	 * Save nothing: Reload every image whenever requested.
-	 * Save external: Only save images that were found as external file.
-	 * Save raw: Save all non-scaled images.
-	 * Save everything: self-explanatory.
+	 * Save nothing: Reload every image whenever requested. Save external: Only save
+	 * images that were found as external file. Save raw: Save all non-scaled
+	 * images. Save everything: self-explanatory.
 	 */
 	private static HashMap<String, Image> imgStorage;
-	private static ImageImport xmp=new ImageImport();//Used to load resources from the JAR.
-	
-	static{
-		/*
-		 * creates the default Image 
-		 */
-		defaultImage = new BufferedImage(30,30,BufferedImage.TYPE_BYTE_INDEXED);
-		for(int x = 0; x < 30; x++)
-			for(int y = 0; y < 30; y++){
-				if(x == 29 || y == 29)
-					//Border Bottom/Left -> Light Grey
-					(defaultImage).setRGB(x, y, 13158600);
-				else if(x == 0 || y == 0)
-					//Border Top/Right -> Dark Grey
-					(defaultImage).setRGB(x, y, 6316128);
-				else if((x == y || x == 31 - y) && 6 < x && x < 25)
-					//Red Cross in the middle
-					(defaultImage).setRGB(x, y, 13107200);
-				else //all other Pixels are white
-					(defaultImage).setRGB(x, y, 16777215);
-			}
-		imgStorage=new HashMap<String, Image>();
+	private static ImageImport xmp = new ImageImport();// Used to load resources from the JAR.
+
+	static {
+		imgStorage = new HashMap<String, Image>();
 	}
-	
+
 	/**
 	 * The rawest function replacing the old method without any default parameters.
-	 * Currently not used in HOLEG at all(aside form calls from the convenience methods).
-	 * @param url Path to the image to be loaded.
-	 * @param w Width the loaded image should be scaled to.
-	 * @param h Height the loaded image should be scaled to.
-	 * @param hints Hints for the scaling algorithm to be used.
-	 * Same as the parameter in the getScaledInstance function of Image.
+	 * Currently not used in HOLEG at all(aside form calls from the convenience
+	 * methods).
+	 * 
+	 * @param url   Path to the image to be loaded.
+	 * @param w     Width the loaded image should be scaled to.
+	 * @param h     Height the loaded image should be scaled to.
+	 * @param hints Hints for the scaling algorithm to be used. Same as the
+	 *              parameter in the getScaledInstance function of Image.
 	 * @return A loaded and scaled image from the requested path.
 	 */
-	static Image loadImage(String url, int w, int h, int hints){
+	static Image loadImage(String url, int w, int h, int hints) {
 		String key = url + "?" + w + "?" + h + "?" + hints;
 		if (imgStorage.containsKey(key))
 			return (imgStorage.get(key));
@@ -74,26 +52,28 @@ public class ImageImport {
 			return img;
 		}
 	}
-	
+
 	/**
 	 * Loads an image from the given path and scales it using the smooth algorithm.
+	 * 
 	 * @param url Path to the image to be loaded.
-	 * @param w Width the loaded image should be scaled to.
-	 * @param h Height the loaded image should be scaled to.
+	 * @param w   Width the loaded image should be scaled to.
+	 * @param h   Height the loaded image should be scaled to.
 	 * @return A loaded and (smoothly) scaled image from the requested path.
 	 */
-	public static Image loadImage(String url, int w, int h){
-		return loadImage(url,w,h, Image.SCALE_SMOOTH);
+	public static Image loadImage(String url, int w, int h) {
+		return loadImage(url, w, h, Image.SCALE_SMOOTH);
 	}
-	
+
 	/**
 	 * Loads an image from the given path and scales it using the smooth algorithm.
+	 * 
 	 * @param url Path to the image to be loaded.
-	 * @param w Width the loaded image should be scaled to.
-	 * @param h Height the loaded image should be scaled to.
+	 * @param w   Width the loaded image should be scaled to.
+	 * @param h   Height the loaded image should be scaled to.
 	 * @return An image loaded from the requested path.
 	 */
-	public static Image loadImage(String url){
+	public static Image loadImage(String url) {
 		if (imgStorage.containsKey(url))
 			return imgStorage.get(url);
 		else {
@@ -101,41 +81,48 @@ public class ImageImport {
 			try {
 				img = ImageIO.read(loadStream(url));
 			} catch (IOException e) {
-				e.printStackTrace();
-				return defaultImage;
+				System.err.println(e);
+				return null;
 			}
 			imgStorage.put(url, img);
 			return img;
 		}
 	}
-	
+
 	/**
-	 * Loads any resource with a given path,
-	 * regardless of whether it is inside the jar or an external resource.
-	 * Every loadImage() function uses this as a basis.
+	 * Loads any resource with a given path, regardless of whether it is inside the
+	 * jar or an external resource. Every loadImage() function uses this as a basis.
+	 * 
 	 * @param url The path (and file name) of the requested resource.
 	 * @return An InputStream from the requested resource.
+	 * @throws FileNotFoundException
 	 */
-	public static InputStream loadStream(String url){
-		InputStream o=xmp.getClass().getResourceAsStream(url);
-		if(o!=null)return o;
-		else{
-			boolean rootSymbol=false;	//Whether url starts with a / or \
-			switch(url.charAt(0)){		//So we can make sure to construct res/path correctly.
-			case '/':case '\\':rootSymbol=true;
+	public static InputStream loadStream(String url) throws FileNotFoundException {
+		InputStream o = xmp.getClass().getResourceAsStream(url);
+		if (o != null)
+			return o;
+		else {
+			boolean rootSymbol = false; // Whether url starts with a / or \
+			switch (url.charAt(0)) { // So we can make sure to construct res/path correctly.
+			case '/':
+			case '\\':
+				rootSymbol = true;
 			}
-			try {
-				//Mixed separators might cause problems. Will fix later.
-				File f=new File(url);//Possible bug with duplicate names.
-				if(!f.exists())url="res"+(rootSymbol?"":"/")+url;
-				return new FileInputStream(url);
-			} catch (FileNotFoundException e1) {
-				e1.printStackTrace();
+			File f = new File(url);
+			if (!f.exists()) {
+				url = "res" + (rootSymbol ? "" : "/") + url;
+			}
+			f = new File(url);// Possible bug with duplicate names.
+			if (!f.exists()) {
+				url = "res/Images/image_not_found.png";
 			}
-			return null;
+			return new FileInputStream(url);
+
 		}
 	}
-	
-	//Nobody needs an instance of this. I do, because I use it to load images from inside the JAR.
-	private ImageImport(){}
+
+	// Nobody needs an instance of this. I do, because I use it to load images from
+	// inside the JAR.
+	private ImageImport() {
+	}
 }

+ 2 - 1
tests/tests/PraktikumHolonsTestCanvasController.java

@@ -7,6 +7,7 @@ import classes.HolonSwitch;
 import classes.IdCounter;
 import ui.controller.CanvasController;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.model.Model;
 
@@ -36,7 +37,7 @@ public class PraktikumHolonsTestCanvasController {
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		mp = new MultiPurposeController(model);
-		cg = new CategoryController(model, mp);
+		cg = new CategoryController(model, mp, new Control(model));
 		controller = new CanvasController(model, mp);
 		IdCounter.setCounter(1); 
 	}

+ 2 - 1
tests/tests/PraktikumHolonsTestCategoryController.java

@@ -8,6 +8,7 @@ import org.junit.Test;
 
 import classes.HolonObject;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.model.Model;
 
@@ -31,7 +32,7 @@ public class PraktikumHolonsTestCategoryController {
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		mp = new MultiPurposeController(model);
-		controller = new CategoryController(model, mp);
+		controller = new CategoryController(model, mp, new Control(model));
 	}
 
 	/**

+ 2 - 1
tests/tests/PraktikumHolonsTestObjectController.java

@@ -9,6 +9,7 @@ import classes.AbstractCanvasObject;
 import classes.HolonObject;
 import ui.controller.CanvasController;
 import ui.controller.CategoryController;
+import ui.controller.Control;
 import ui.controller.MultiPurposeController;
 import ui.controller.ObjectController;
 import ui.model.Model;
@@ -35,7 +36,7 @@ public class PraktikumHolonsTestObjectController {
 		adapter = new PraktikumHolonsAdapter();
 		model = new Model();
 		mp = new MultiPurposeController(model);
-		cg = new CategoryController(model, mp);
+		cg = new CategoryController(model, mp, new Control(model));
 		cvs = new CanvasController(model, mp);
 		controller = new ObjectController(model, mp);
 	}