Skip to content

Commit

Permalink
GeckoViewModel: delete and add methods update EditorViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
benjymin committed Jan 15, 2024
1 parent 09052af commit db4c6b8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/gecko/model/Automaton.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ public void removeEdge(Edge edge) {
public void removeEdges(List<Edge> edges) {
this.edges.removeAll(edges);
}

public List<Element> getAllElements() {
List<Element> allElements = new ArrayList<>();
allElements.addAll(regions);
allElements.addAll(states);
allElements.addAll(edges);
return allElements;
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/gecko/model/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ public void removeVariables(List<Variable> variables) {
public void accept(ElementVisitor visitor) {
visitor.visit(this);
}

public List<Element> getAllElements() {
List<Element> allElements = new ArrayList<>();
allElements.addAll(children);
allElements.addAll(variables);
allElements.addAll(connections);
return allElements;
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/gecko/viewmodel/EditorViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;
import javafx.beans.property.Property;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.geometry.Point2D;
import lombok.Data;
import org.gecko.model.Region;
Expand All @@ -27,13 +27,13 @@ public class EditorViewModel {
private Property<Double> zoomScaleProperty;
private Property<Tool> currentToolProperty;
private Property<PositionableViewModelElement<?>> focusedElementProperty;
private final ObservableList<PositionableViewModelElement<?>> containedPositionableViewModelElementsProperty;
private final ObservableSet<PositionableViewModelElement<?>> containedPositionableViewModelElementsProperty;
private final List<List<Tool>> tools;
private final SelectionManager selectionManager;

public EditorViewModel(SystemViewModel systemViewModel, boolean isAutomatonEditor) {
currentSystem = systemViewModel;
containedPositionableViewModelElementsProperty = FXCollections.observableArrayList();
containedPositionableViewModelElementsProperty = FXCollections.observableSet();
this.isAutomatonEditor = isAutomatonEditor;
tools = FXCollections.observableArrayList();
selectionManager = new SelectionManager();
Expand Down Expand Up @@ -98,7 +98,7 @@ public void removePositionableViewModelElement(PositionableViewModelElement<?> e
}

public void removePositionableViewModelElements(List<PositionableViewModelElement<?>> elements) {
containedPositionableViewModelElementsProperty.removeAll(elements);
elements.forEach(containedPositionableViewModelElementsProperty::remove);
}

private void initializeTools() {
Expand Down
48 changes: 32 additions & 16 deletions src/main/java/org/gecko/viewmodel/GeckoViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;

import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import org.gecko.model.Automaton;
import org.gecko.model.Element;
import org.gecko.model.GeckoModel;
import org.gecko.model.System;
Expand All @@ -38,12 +38,13 @@ public void switchEditor(SystemViewModel nextSystemViewModel, boolean isAutomato
.filter(editorViewModel -> (editorViewModel.getCurrentSystem() == nextSystemViewModel
&& editorViewModel.isAutomatonEditor() == isAutomatonEditor))
.findFirst()
.ifPresentOrElse(currentEditorProperty::setValue, () -> {
EditorViewModel editorViewModel = viewModelFactory.createEditorViewModel(nextSystemViewModel, isAutomatonEditor);
openedEditorsProperty.add(editorViewModel);
currentEditorProperty.setValue(editorViewModel);
initializeEditorWithPositionableViewModelElements(editorViewModel, nextSystemViewModel);
});
.ifPresentOrElse(this::setCurrentEditor, () -> setupNewEditorViewModel(nextSystemViewModel, isAutomatonEditor));
}

private void setupNewEditorViewModel(SystemViewModel nextSystemViewModel, boolean isAutomatonEditor) {
EditorViewModel editorViewModel = viewModelFactory.createEditorViewModel(nextSystemViewModel, isAutomatonEditor);
openedEditorsProperty.add(editorViewModel);
setCurrentEditor(editorViewModel);
}

public PositionableViewModelElement<?> getViewModelElement(Element element) {
Expand All @@ -58,27 +59,42 @@ public List<PositionableViewModelElement<?>> getViewModelElements(List<? extends

public void addViewModelElement(PositionableViewModelElement<?> element) {
modelToViewModel.put(element.getTarget(), element);
updateCurrentEditor();
}

public void deleteViewModelElement(PositionableViewModelElement<?> element) {
modelToViewModel.remove(element.getTarget());
updateCurrentEditor();
}

private void updateCurrentEditor() {
EditorViewModel currentEditor = getCurrentEditor();
if (currentEditor == null) {
return;
}

currentEditor.removePositionableViewModelElements(currentEditor.getContainedPositionableViewModelElementsProperty()
.stream()
.filter(element -> !modelToViewModel.containsKey(element.getTarget()))
.toList());
addPositionableViewModelElementsToEditor(currentEditor);
}

public EditorViewModel getCurrentEditor() {
return currentEditorProperty.getValue();
}

private void initializeEditorWithPositionableViewModelElements(EditorViewModel editorViewModel, SystemViewModel systemViewModel) {
private void setCurrentEditor(EditorViewModel editorViewModel) {
currentEditorProperty.setValue(editorViewModel);
updateCurrentEditor();
}

private void addPositionableViewModelElementsToEditor(EditorViewModel editorViewModel) {
System currentSystem = editorViewModel.getCurrentSystem().getTarget();
if (editorViewModel.isAutomatonEditor()) {
Automaton automaton = systemViewModel.getTarget().getAutomaton();
editorViewModel.addPositionableViewModelElements(getViewModelElements(automaton.getStates()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(automaton.getEdges()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(automaton.getRegions()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(currentSystem.getAutomaton().getAllElements()));
} else {
System system = systemViewModel.getTarget();
editorViewModel.addPositionableViewModelElements(getViewModelElements(system.getChildren()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(system.getVariables()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(system.getConnections()));
editorViewModel.addPositionableViewModelElements(getViewModelElements(currentSystem.getAllElements()));
}
}

Expand Down

0 comments on commit db4c6b8

Please sign in to comment.