Simplify ComponentLocator

This commit is contained in:
2025-04-27 22:52:25 +02:00
parent ac854ed4f1
commit c38835fd7b
22 changed files with 57 additions and 71 deletions

View File

@ -588,7 +588,7 @@ public class Commands extends PICComponent implements CommandInterface {
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -3,7 +3,6 @@ package fabrik.simulator.pic16f84;
import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
import fabrik.simulator.pic16f84.interfaces.*;
import fabrik.simulator.pic16f84.interfaces.WatchdogTimerInterface;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
@ -42,7 +41,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private int [] prog;
private int [][] read;
private int [] ind;
private PICComponents picComponents;
private PICComponentLocator picComponents;
private boolean isBreakpointReached = false;
@ -553,9 +552,9 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
this.picComponents = picComponents;
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
this.picComponents = locator;
System.out.println("Frontend");
}
}

View File

@ -4,11 +4,9 @@ import fabrik.simulator.pic16f84.interfaces.*;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@ -17,7 +15,7 @@ import java.io.IOException;
import java.util.Objects;
public class CreateWindow extends Application {
private PICComponents picComponents = new PICComponents(); // Subjekt
private PICComponentLocator picComponents = new PICComponentLocator(); // Subjekt
public static GridPane grid = new GridPane();
private static VBox table;
@ -40,6 +38,7 @@ public class CreateWindow extends Application {
FXMLLoader codewindow = new FXMLLoader(CreateWindow.class.getResource("frontend.fxml"));
codewindow.setController(picComponents.getComponent(FrontendControllerInterface.class));
Parent code = codewindow.load();
table = Table.init(picComponents);
grid.add(table, 1, 1);
grid.add(code, 0, 1);

View File

@ -39,8 +39,8 @@ public class DataRegister extends PICComponent implements DataRegisterInterface
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
System.out.println("DataRegister.\n");
initOrReset();
}

View File

@ -1,7 +1,6 @@
package fabrik.simulator.pic16f84;
import eu.hansolo.tilesfx.Command;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -173,7 +172,7 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -145,7 +145,7 @@ public class EmptyRegister implements DataRegisterInterface {
}
@Override
public void initialize(PICComponents picComponents) {
public void initialize(PICComponentLocator picComponents) {
}
}

View File

@ -227,8 +227,8 @@ public class IOPorts extends PICComponent implements IOPortInterface {
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
System.out.println("IOPorts.\n");
}
}

View File

@ -1,9 +1,5 @@
package fabrik.simulator.pic16f84;
import eu.hansolo.tilesfx.Command;
import fabrik.simulator.pic16f84.interfaces.CommandInterface;
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
import fabrik.simulator.pic16f84.interfaces.FrontendControllerInterface;
import fabrik.simulator.pic16f84.interfaces.InterruptInterface;
public class Interrupts extends PICComponent implements InterruptInterface {
@ -66,7 +62,7 @@ public class Interrupts extends PICComponent implements InterruptInterface {
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -9,27 +9,23 @@ public abstract class PICComponent {
IOPortInterface ioPorts;
TimerInterface timer;
InterruptInterface interrupts;
//TableInterface table;
//WindowManagement createWindow;
FrontendControllerInterface frontendController;
WatchdogTimerInterface watchdogTimer;
ProgramStackInterface programStack;
CommandInterface commands;
ToggleButtonInterface toggleButtonExt;
void initialize(PICComponents picComponents) {
toggleButtonExt = (ToggleButtonInterface) picComponents.getComponent(ToggleButtonInterface.class);
dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class);
eeprom = (EEPROMInterface) picComponents.getComponent(EEPROMInterface.class);
ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class);
preScaler = (PreScalerInterface) picComponents.getComponent(PreScalerInterface.class);
timer = (TimerInterface) picComponents.getComponent(TimerInterface.class);
interrupts = (InterruptInterface) picComponents.getComponent(InterruptInterface.class);
//table = (TableInterface) picComponents.getComponent(TableInterface.class);
//createWindow = (WindowManagement) picComponents.getComponent(WindowManagement.class);
frontendController = (FrontendControllerInterface) picComponents.getComponent(FrontendControllerInterface.class);
watchdogTimer = (WatchdogTimerInterface) picComponents.getComponent(WatchdogTimerInterface.class);
programStack = (ProgramStackInterface) picComponents.getComponent(ProgramStackInterface.class);
commands = (CommandInterface) picComponents.getComponent(CommandInterface.class);
void initialize(PICComponentLocator locator) {
toggleButtonExt = locator.getComponent(ToggleButtonInterface.class);
dataRegister = locator.getComponent(DataRegisterInterface.class);
eeprom = locator.getComponent(EEPROMInterface.class);
ioPorts = locator.getComponent(IOPortInterface.class);
preScaler = locator.getComponent(PreScalerInterface.class);
timer = locator.getComponent(TimerInterface.class);
interrupts = locator.getComponent(InterruptInterface.class);
frontendController = locator.getComponent(FrontendControllerInterface.class);
watchdogTimer = locator.getComponent(WatchdogTimerInterface.class);
programStack = locator.getComponent(ProgramStackInterface.class);
commands = locator.getComponent(CommandInterface.class);
}
}

View File

@ -4,10 +4,11 @@ import fabrik.simulator.pic16f84.interfaces.*;
import java.util.HashMap;
public class PICComponents {
public class PICComponentLocator {
// https://en.wikipedia.org/wiki/Service_locator_pattern
private final HashMap<Class<? extends PICComponentInterface>, PICComponentInterface> componentCatalogue;
public PICComponents() {
public PICComponentLocator() {
super();
this.componentCatalogue = new HashMap<>();
}
@ -16,8 +17,9 @@ public class PICComponents {
this.componentCatalogue.put(componentClass, component);
}
public PICComponentInterface getComponent(Class<? extends PICComponentInterface> componentClass) {
return this.componentCatalogue.get(componentClass);
public <T extends PICComponentInterface> T getComponent(Class<T> componentClass) {
T component = (T) this.componentCatalogue.get(componentClass);
return component;
}
public void initAll() {

View File

@ -54,7 +54,7 @@ public class PreScaler extends PICComponent implements PreScalerInterface {
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -48,7 +48,7 @@ public class ProgramStack extends PICComponent implements ProgramStackInterface
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -4,7 +4,6 @@ import fabrik.simulator.pic16f84.interfaces.*;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
@ -46,7 +45,7 @@ public class Table {
return s;
}
public static VBox init(PICComponents picComponents){
public static VBox init(PICComponentLocator picComponents){
dataRegister = (DataRegisterInterface) picComponents.getComponent(DataRegisterInterface.class);
commands = (CommandInterface) picComponents.getComponent(CommandInterface.class);
ioPorts = (IOPortInterface) picComponents.getComponent(IOPortInterface.class);

View File

@ -1,8 +1,5 @@
package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.DataRegisterInterface;
import fabrik.simulator.pic16f84.interfaces.FrontendControllerInterface;
import fabrik.simulator.pic16f84.interfaces.InterruptInterface;
import fabrik.simulator.pic16f84.interfaces.TimerInterface;
public class Timer extends PICComponent implements TimerInterface {
@ -67,7 +64,7 @@ public class Timer extends PICComponent implements TimerInterface {
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -1,7 +1,6 @@
package fabrik.simulator.pic16f84;
import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
import fabrik.simulator.pic16f84.interfaces.IOPortInterface;
import fabrik.simulator.pic16f84.interfaces.ToggleButtonInterface;
import javafx.collections.ListChangeListener;
import javafx.event.EventHandler;
@ -59,8 +58,8 @@ public class ToggleButtonGroupExt extends PICComponent implements ToggleButtonIn
};
@Override
public void initialize(PICComponents picComponents) {
public void initialize(PICComponentLocator locator) {
System.out.println("ToggleButtonGroupExt");
super.initialize(picComponents);
super.initialize(locator);
}
}

View File

@ -53,7 +53,7 @@ public class WatchdogTimer extends PICComponent implements WatchdogTimerInterfac
}
@Override
public void initialize(PICComponents picComponents) {
super.initialize(picComponents);
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -1,7 +1,7 @@
package fabrik.simulator.pic16f84.interfaces;
import fabrik.simulator.pic16f84.PICComponents;
import fabrik.simulator.pic16f84.PICComponentLocator;
public interface PICComponentInterface {
void initialize(PICComponents picComponents);
void initialize(PICComponentLocator picComponents);
}