X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2FLogicUICanvas.java;h=15346c63412b4ddb867e9bdaa14bd8952670093f;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=11e46345bdcf0c20047de1066dae1cf6cbc88bb8;hpb=d8e1787969c9be7d69f2c254575750d2ffff23f0;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java index 11e46345..15346c63 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java @@ -2,6 +2,9 @@ package net.mograsim.logic.model; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; @@ -21,11 +24,11 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.haspamelodica.swt.helper.zoomablecanvas.ZoomableCanvas; import net.mograsim.logic.core.types.Bit; import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.model.model.ViewModel; -import net.mograsim.logic.model.model.components.GUIComponent; +import net.mograsim.logic.model.model.LogicModel; +import net.mograsim.logic.model.model.components.ModelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; import net.mograsim.logic.model.model.components.submodels.SubmodelInterface; -import net.mograsim.logic.model.model.wires.WireCrossPoint; +import net.mograsim.logic.model.model.wires.ModelWireCrossPoint; import net.mograsim.preferences.Preferences; /** @@ -37,11 +40,11 @@ public class LogicUICanvas extends ZoomableCanvas { private static final boolean OPEN_DEBUG_SETHIGHLEVELSTATE_SHELL = false; - private final ViewModel model; + private final LogicModel model; - public LogicUICanvas(Composite parent, int style, ViewModel model) + public LogicUICanvas(Composite parent, int style, LogicModel model) { - super(parent, style); + super(parent, style, Preferences.current().getBoolean("net.mograsim.logic.model.improvetext")); this.model = model; @@ -66,7 +69,7 @@ public class LogicUICanvas extends ZoomableCanvas if (e.button == 1) { Point click = canvasToWorldCoords(e.x, e.y); - for (GUIComponent component : model.getComponentsByName().values()) + for (ModelComponent component : model.getComponentsByName().values()) if (component.getBounds().contains(click) && component.clicked(click.x, click.y)) { redraw(); @@ -75,17 +78,29 @@ public class LogicUICanvas extends ZoomableCanvas } } - private void openDebugSetHighLevelStateShell(ViewModel model) + private void openDebugSetHighLevelStateShell(LogicModel model) { Shell debugShell = new Shell(); debugShell.setLayout(new GridLayout(2, false)); new Label(debugShell, SWT.NONE).setText("Target component: "); Combo componentSelector = new Combo(debugShell, SWT.DROP_DOWN | SWT.READ_ONLY); componentSelector.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); - List componentsByItemIndex = new ArrayList<>(); - model.addComponentAddedListener(c -> recalculateComponentSelector(componentsByItemIndex, componentSelector, model)); - model.addComponentRemovedListener(c -> recalculateComponentSelector(componentsByItemIndex, componentSelector, model)); - recalculateComponentSelector(componentsByItemIndex, componentSelector, model); + List componentsByItemIndex = new ArrayList<>(); + List models = new ArrayList<>(); + AtomicBoolean recalculateQueued = new AtomicBoolean(); + AtomicReference> compAdded = new AtomicReference<>(); + AtomicReference> compRemoved = new AtomicReference<>(); + compAdded.set(c -> compsChanged(compAdded.get(), compRemoved.get(), c, models, componentsByItemIndex, componentSelector, model, + recalculateQueued, true)); + compRemoved.set(c -> compsChanged(compAdded.get(), compRemoved.get(), c, models, componentsByItemIndex, componentSelector, model, + recalculateQueued, false)); + iterateModelTree(compAdded.get(), compRemoved.get(), model, models, true); + debugShell.addListener(SWT.Dispose, e -> models.forEach(m -> + { + m.removeComponentAddedListener(compAdded.get()); + m.removeComponentRemovedListener(compRemoved.get()); + })); + queueRecalculateComponentSelector(recalculateQueued, componentsByItemIndex, componentSelector, model); new Label(debugShell, SWT.NONE).setText("Target state ID: "); Text stateIDText = new Text(debugShell, SWT.SINGLE | SWT.LEAD | SWT.BORDER); stateIDText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); @@ -116,7 +131,7 @@ public class LogicUICanvas extends ZoomableCanvas int componentIndex = componentSelector.getSelectionIndex(); if (componentIndex < 0 || componentIndex >= componentsByItemIndex.size()) throw new RuntimeException("No component selected"); - GUIComponent target = componentsByItemIndex.get(componentIndex); + ModelComponent target = componentsByItemIndex.get(componentIndex); String valueString = valueText.getText(); Object value; if (radioBit.getSelection()) @@ -154,20 +169,66 @@ public class LogicUICanvas extends ZoomableCanvas debugShell.open(); } - private void recalculateComponentSelector(List componentsByItemIndex, Combo componentSelector, ViewModel model) + private void compsChanged(Consumer compAdded, Consumer compRemoved, ModelComponent c, + List models, List componentsByItemIndex, Combo componentSelector, LogicModel model, + AtomicBoolean recalculateQueued, boolean add) + { + iterateSubmodelTree(compAdded, compRemoved, c, models, add); + queueRecalculateComponentSelector(recalculateQueued, componentsByItemIndex, componentSelector, model); + } + + private void iterateSubmodelTree(Consumer compAdded, Consumer compRemoved, + ModelComponent c, List models, boolean add) + { + if (c instanceof SubmodelComponent) + iterateModelTree(compAdded, compRemoved, ((SubmodelComponent) c).submodel, models, add); + } + + private void iterateModelTree(Consumer compAdded, Consumer compRemoved, + LogicModel model, List models, boolean add) + { + if (add ^ models.contains(model)) + { + if (add) + { + models.add(model); + model.addComponentAddedListener(compAdded); + model.addComponentRemovedListener(compRemoved); + } else + { + models.remove(model); + model.removeComponentAddedListener(compAdded); + model.removeComponentRemovedListener(compRemoved); + } + for (ModelComponent c : model.getComponentsByName().values()) + iterateSubmodelTree(compAdded, compRemoved, c, models, add); + } + } + + private void queueRecalculateComponentSelector(AtomicBoolean recalculateQueued, List componentsByItemIndex, + Combo componentSelector, LogicModel model) + { + if (recalculateQueued.compareAndSet(false, true)) + getDisplay().asyncExec(() -> recalculateComponentSelector(recalculateQueued, componentsByItemIndex, componentSelector, model)); + } + + private void recalculateComponentSelector(AtomicBoolean recalculateQueued, List componentsByItemIndex, + Combo componentSelector, LogicModel model) { + recalculateQueued.set(false); componentsByItemIndex.clear(); componentSelector.setItems(); addComponentSelectorItems(componentsByItemIndex, "", componentSelector, model); } - private void addComponentSelectorItems(List componentsByItemIndex, String base, Combo componentSelector, ViewModel model) + private void addComponentSelectorItems(List componentsByItemIndex, String base, Combo componentSelector, + LogicModel model) { - model.getComponentsByName().values().stream().sorted((c1, c2) -> c1.name.compareTo(c2.name)).forEach(c -> + model.getComponentsByName().values().stream().sorted((c1, c2) -> c1.getName().compareTo(c2.getName())).forEach(c -> { - if (!(c instanceof WireCrossPoint || c instanceof SubmodelInterface)) + if (!(c instanceof ModelWireCrossPoint || c instanceof SubmodelInterface)) { - String item = base + c.name; + String item = base + c.getName(); componentsByItemIndex.add(c); componentSelector.add(item); if (c instanceof SubmodelComponent)