Browse Source

Fix Inspector

Removes dangling references from old ElementRows
TomTroppmann 2 years ago
parent
commit
2da479a804

+ 5 - 1
src/holeg/preferences/ImagePreference.java

@@ -32,7 +32,11 @@ public class ImagePreference {
 		}
 		public static class Inspector {
 			public static final String Reset = "/images/buttons/reset_circle.png";				
-			public static final String Graph = "/images/buttons/graph.png";				
+			public static final String Graph = "/images/buttons/graph.png";
+			public static final String Add = "images/buttons/plus.png";
+			public static final String Remove = "images/buttons/minus.png";
+			public static final String Duplicate = "images/buttons/duplicate.png";
+
 		}
 		public static class Menu {
 			public static final String Algo = "/images/buttons/algo.png";						

+ 0 - 2
src/holeg/ui/controller/Control.java

@@ -19,8 +19,6 @@ import java.io.IOException;
 import java.util.*;
 import java.util.logging.Logger;
 import java.util.prefs.Preferences;
-import java.util.spi.AbstractResourceBundleProvider;
-import java.util.stream.Collectors;
 
 import static holeg.serialize.ModelDeserializer.gson;
 

+ 44 - 27
src/holeg/ui/view/inspector/InspectorTable.java

@@ -16,6 +16,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Optional;
 import java.util.Set;
+import java.util.logging.Logger;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import java.util.ArrayList;
@@ -42,6 +43,7 @@ import holeg.model.HolonElement;
 import holeg.model.HolonObject;
 import holeg.model.HolonElement.Priority;
 import holeg.preferences.ColorPreference;
+import holeg.preferences.ImagePreference;
 import holeg.ui.controller.Control;
 import holeg.ui.model.GuiSettings;
 import holeg.ui.view.component.TrippleCheckBox;
@@ -53,6 +55,7 @@ import holeg.utility.pooling.Pool;
 import net.miginfocom.swing.MigLayout;
 
 public class InspectorTable extends JPanel {
+	private static final Logger log = Logger.getLogger(InspectorTable.class.getName());
 	private final Pool<ElementRow> rowPool = new Pool<>() {
 		@Override
 		public ElementRow create() {
@@ -173,7 +176,6 @@ public class InspectorTable extends JPanel {
 		Comparator<ElementRow> priorityComp = Comparator.comparing((ElementRow a) -> a.element.getPriority());
 		Comparator<ElementRow> activeComp = (ElementRow a, ElementRow b) -> Boolean.compare(a.element.active,
 				b.element.active);
-		;
 
 		headerButtonList.add(new SortButton<>("Object", objectComp));
 		headerButtonList.add(new SortButton<>("Id", idComp));
@@ -220,7 +222,7 @@ public class InspectorTable extends JPanel {
 		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
 		buttonPanel.add(Box.createRigidArea(new Dimension(2, 0)));
 
-		addButton.setIcon(new ImageIcon(Import.loadImage("images/buttons/plus.png", 16, 16)));
+		addButton.setIcon(new ImageIcon(Import.loadImage(ImagePreference.Button.Inspector.Add, 16, 16)));
 		addButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 		addButton.addActionListener(clicked -> {
 			Optional<HolonObject> last = GuiSettings.getSelectedObjects().stream()
@@ -228,13 +230,13 @@ public class InspectorTable extends JPanel {
 					.map(obj -> (HolonObject) obj);
 			last.ifPresent(obj -> {
 				obj.add(new HolonElement(obj, "Element", 0.0f));
-				control.calculateStateForCurrentIteration();
-				updateInspectorUi();
+				control.updateStateForCurrentIteration();
+				control.OnSelectionChanged.broadcast();
 			});
 		});
 		buttonPanel.add(addButton);
 
-		duplicateButton.setIcon(new ImageIcon(Import.loadImage("images/buttons/duplicate.png", 16, 16)));
+		duplicateButton.setIcon(new ImageIcon(Import.loadImage(ImagePreference.Button.Inspector.Duplicate, 16, 16)));
 		duplicateButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 		duplicateButton.addActionListener(clicked -> {
 			rowPool.getBorrowedStream().forEach(row -> {
@@ -242,21 +244,25 @@ public class InspectorTable extends JPanel {
 					row.element.parentObject.add(new HolonElement(row.element));
 				}
 			});
-			control.calculateStateForCurrentIteration();
-			updateInspectorUi();
+			control.updateStateForCurrentIteration();
+			control.OnSelectionChanged.broadcast();
 		});
 		buttonPanel.add(duplicateButton);
 
-		deleteButton.setIcon(new ImageIcon(Import.loadImage("images/buttons/minus.png", 16, 16)));
+		deleteButton.setIcon(new ImageIcon(Import.loadImage(ImagePreference.Button.Inspector.Remove, 16, 16)));
 		deleteButton.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 		deleteButton.addActionListener(clicked -> {
+			log.info("DeleteButton");
 			rowPool.getBorrowedStream().forEach(row -> {
 				if (row.isSelected()) {
 					row.element.parentObject.remove(row.element);
 				}
 			});
-			control.calculateStateForCurrentIteration();
-			updateInspectorUi();
+			log.info("row deleted");
+			control.updateStateForCurrentIteration();
+			log.info("updated");
+			control.OnSelectionChanged.broadcast();
+			log.info("selectionChanged");
 		});
 		buttonPanel.add(deleteButton);
 
@@ -280,6 +286,7 @@ public class InspectorTable extends JPanel {
 
 	private void assignElementsToRowPool(Set<AbstractCanvasObject> selection) {
 		List<HolonElement> elementList = extractElements(selection).toList();
+		rowPool.getBorrowedStream().forEach(InspectorTable.ElementRow::clear);
 		rowPool.clear();
 		for (HolonElement element : elementList) {
 			ElementRow row = rowPool.get();
@@ -297,7 +304,6 @@ public class InspectorTable extends JPanel {
 			try {
 				abortThread = true;
 				populateRowsThread.join();
-				populateRowsThread.join();
 				abortThread = false;
 			} catch (InterruptedException e) {
 				e.printStackTrace();
@@ -403,14 +409,17 @@ public class InspectorTable extends JPanel {
 		}
 
 		public void setElement(HolonElement element) {
+			objectNameTextField.setText(element.parentObject.getName());
+			idObjectTextField.setText(Integer.toString(element.parentObject.getId()));
+			elementNameTextField.setText(element.getName());
+			comboBox.setSelectedItem(element.getPriority());
+			activeCheckBox.setSelected(element.active);
+			energyTextField.setValue(element.getEnergy());
 			this.element = element;
 			setSelected(false);
-			objectNameTextField.setText(this.element.parentObject.getName());
-			idObjectTextField.setText(Integer.toString(this.element.parentObject.getId()));
-			elementNameTextField.setText(this.element.getName());
-			energyTextField.setValue(this.element.getEnergy());
-			comboBox.setSelectedItem(this.element.getPriority());
-			activeCheckBox.setSelected(this.element.active);
+		}
+		public void clear(){
+			this.element = null;
 		}
 
 		private void createEditFields() {
@@ -436,7 +445,9 @@ public class InspectorTable extends JPanel {
 			// ObjectName and ID
 			objectNameTextField = new JTextField();
 			objectNameTextField.getDocument().addDocumentListener((SimpleDocumentListener) e -> {
-				this.element.parentObject.setName(objectNameTextField.getText());
+				if(this.element != null){
+					this.element.parentObject.setName(objectNameTextField.getText());
+				}
 			});
 			objectNameTextField.addActionListener(ae -> updateInspectorUi());
 			cellsInRow[1] = objectNameTextField;
@@ -449,7 +460,9 @@ public class InspectorTable extends JPanel {
 			// Name
 			elementNameTextField = new JTextField();
 			elementNameTextField.getDocument().addDocumentListener((SimpleDocumentListener) e -> {
-				this.element.setName(elementNameTextField.getText());
+				if(this.element != null){
+					this.element.setName(elementNameTextField.getText());
+				}
 			});
 			elementNameTextField.setBackground(Color.white);
 			cellsInRow[3] = elementNameTextField;
@@ -462,7 +475,7 @@ public class InspectorTable extends JPanel {
 			energyTextField.addPropertyChangeListener(actionEvent -> {
 				try {
 					float energy = Float.parseFloat(energyTextField.getText());
-					if (this.element.getEnergy() != energy) {
+					if (this.element != null && this.element.getEnergy() != energy) {
 						this.element.setEnergy(energy);
 						control.updateStateForCurrentIteration();
 					}
@@ -474,12 +487,14 @@ public class InspectorTable extends JPanel {
 			cellsInRow[4] = energyTextField;
 
 			// Priority
-			comboBox = new JComboBox<Priority>(Priority.values());
+			comboBox = new JComboBox<>(Priority.values());
 			comboBox.setBackground(Color.white);
 			comboBox.setEditable(false);
 			comboBox.addActionListener(ae -> {
-				this.element.setPriority((Priority) comboBox.getSelectedItem());
-				control.updateStateForCurrentIteration();
+				if(this.element != null){
+					this.element.setPriority((Priority) comboBox.getSelectedItem());
+					control.updateStateForCurrentIteration();
+				}
 			});
 			cellsInRow[5] = comboBox;
 
@@ -493,8 +508,10 @@ public class InspectorTable extends JPanel {
 			activeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 0));
 			activeCheckBox.setOpaque(false);
 			activeCheckBox.addActionListener(actionEvent -> {
-				this.element.active = activeCheckBox.isSelected();
-				control.updateStateForCurrentIteration();
+				if(this.element != null){
+					this.element.active = activeCheckBox.isSelected();
+					control.updateStateForCurrentIteration();
+				}
 			});
 			checkBoxWrapperPanel.add(activeCheckBox, BorderLayout.CENTER);
 			cellsInRow[6] = checkBoxWrapperPanel;
@@ -508,7 +525,7 @@ public class InspectorTable extends JPanel {
 
 	private class SortButton<T> extends JButton {
 		private SortState state = SortState.None;
-		private Comparator<T> comp;
+		private final Comparator<T> comp;
 
 		public SortButton(String text, Comparator<T> comp) {
 			super(text);
@@ -534,7 +551,7 @@ public class InspectorTable extends JPanel {
 			this.state = state;
 			String text = this.getText();
 			// remove order symbols from text
-			text = text.replaceAll("\u25bc|\u25b2", "");
+			text = text.replaceAll("[\u25bc\u25b2]", "");
 			// update text
 			switch (state) {
 				case Descending -> this.setText(text + "\u25bc");

+ 2 - 0
src/holeg/utility/pooling/Pool.java

@@ -1,5 +1,7 @@
 package holeg.utility.pooling;
 
+import holeg.ui.view.inspector.InspectorTable;
+
 import java.util.ArrayList;
 import java.util.stream.Stream;