Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus fix #188

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/java/org/gecko/model/ModelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public State copyState(@NonNull State state) {

public Edge createEdge(@NonNull Automaton automaton, @NonNull State source, @NonNull State destination)
throws ModelException {
if (automaton.getStates().isEmpty() || !automaton.getStates().contains(source)
|| !automaton.getStates().contains(destination)) {
if (automaton.getStates().isEmpty() || !automaton.getStates().contains(source) || !automaton.getStates()
.contains(destination)) {
throw new RuntimeException("Failed to create edge, because source and / or destination states "
+ "are not part of the automaton.");
}
Expand Down Expand Up @@ -158,8 +158,8 @@ public SystemConnection createSystemConnection(
return connection;
}

private boolean isConnectingAllowed(System system, System sourceParent, System destinationParent, Variable source,
Variable destination) {
private boolean isConnectingAllowed(
System system, System sourceParent, System destinationParent, Variable source, Variable destination) {
if (!sourceParent.equals(system) && !destinationParent.equals(system)) {
if (!sourceParent.getParent().equals(system) || !destinationParent.getParent().equals(system)) {
return false;
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/org/gecko/view/GeckoView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
Expand Down Expand Up @@ -42,12 +43,11 @@ public class GeckoView {
@Getter
private final GeckoViewModel viewModel;
private final ViewFactory viewFactory;
private final MenuBar menuBar;

@Getter
private Property<EditorView> currentViewProperty;
private final Property<EditorView> currentViewProperty;

private final ArrayList<EditorView> openedViews;
private final List<EditorView> openedViews;
private boolean darkMode = false;

private boolean hasBeenFocused = false;
Expand All @@ -68,10 +68,11 @@ public GeckoView(GeckoViewModel viewModel) {
viewModel.getOpenedEditorsProperty().addListener(this::onOpenedEditorChanged);

centerPane.setPickOnBounds(false);
centerPane.setFocusTraversable(false);
centerPane.getSelectionModel().selectedItemProperty().addListener(this::onUpdateCurrentEditorToViewModel);

// Menubar
menuBar = new MenuBarBuilder(this, viewModel.getActionManager()).build();
MenuBar menuBar = new MenuBarBuilder(this, viewModel.getActionManager()).build();
mainPane.setTop(menuBar);
mainPane.setCenter(centerPane);

Expand All @@ -95,18 +96,8 @@ public GeckoView(GeckoViewModel viewModel) {
return;
}
newValue.focusOwnerProperty().addListener((observable1, oldValue1, newValue1) -> {
if (!currentViewProperty.getValue().getViewModel().getPositionableViewModelElements().isEmpty()
&& !hasBeenFocused) {
EditorViewModel currentViewModel = currentViewProperty.getValue().getViewModel();

// Evaluate the center of all elements by calculating the average position
Point2D center = currentViewModel.getPositionableViewModelElements()
.stream()
.map(PositionableViewModelElement::getCenter)
.reduce(new Point2D(0, 0), Point2D::add)
.multiply(1.0 / currentViewModel.getPositionableViewModelElements().size());

currentViewModel.setPivot(center);
if (!currentViewProperty.getValue().getCurrentViewElements().isEmpty() && !hasBeenFocused) {
focusCenter(currentViewProperty.getValue().getViewModel());
}
hasBeenFocused = true;
});
Expand Down Expand Up @@ -135,7 +126,8 @@ private void onOpenedEditorChanged(
viewFactory.createEditorView(editorViewModel, editorViewModel.isAutomatonEditor());

if (!openedViews.contains(newEditorView)) {
constructTab(newEditorView, editorViewModel);
handleUserTabChange(constructTab(newEditorView, editorViewModel));
Platform.runLater(() -> focusCenter(editorViewModel));
}
}

Expand Down Expand Up @@ -164,7 +156,7 @@ private void removeEditorViews(List<EditorViewModel> editorViewModelsToRemove) {
openedViews.removeAll(editorViewsToRemove);
}

private void constructTab(EditorView editorView, EditorViewModel editorViewModel) {
private Tab constructTab(EditorView editorView, EditorViewModel editorViewModel) {
openedViews.add(editorView);
Tab tab = editorView.getCurrentView();
Node graphic = tab.getGraphic();
Expand All @@ -175,6 +167,7 @@ private void constructTab(EditorView editorView, EditorViewModel editorViewModel
openedViews.remove(editorView);
viewModel.getOpenedEditorsProperty().remove(editorViewModel);
});
return tab;
}

private void handleUserTabChange(Tab tab) {
Expand Down Expand Up @@ -214,6 +207,17 @@ private void onUpdateCurrentEditorToViewModel(
refreshView();
}

private void focusCenter(EditorViewModel editorViewModel) {
// Evaluate the center of all elements by calculating the average position
Point2D center = editorViewModel.getPositionableViewModelElements()
.stream()
.map(PositionableViewModelElement::getCenter)
.reduce(new Point2D(0, 0), Point2D::add)
.multiply(1.0 / editorViewModel.getPositionableViewModelElements().size());

editorViewModel.setPivot(center);
}

/**
* Returns all displayed elements in the current view.
*
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/org/gecko/io/ImportAndExportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ static void setUp() {
@Test
void writeToFile() {
File serializedParsedAEBFile = new File("src/test/java/org/gecko/io/files/serializedParsedAEB.sys");
AutomatonFileSerializer automatonFileSerializer = new AutomatonFileSerializer(geckoViewModel.getGeckoModel());
AutomatonFileSerializer automatonFileSerializer =
new AutomatonFileSerializer(geckoViewModel.getGeckoModel());
assertDoesNotThrow(() -> automatonFileSerializer.writeToFile(serializedParsedAEBFile));
}

Expand All @@ -51,7 +52,8 @@ void parseComplexGecko() {
}

File serializedExportedComplexFile = new File("src/test/java/org/gecko/io/files/exportedComplexGecko.sys");
AutomatonFileSerializer automatonFileSerializer = new AutomatonFileSerializer(complexViewModel.getGeckoModel());
AutomatonFileSerializer automatonFileSerializer =
new AutomatonFileSerializer(complexViewModel.getGeckoModel());
assertDoesNotThrow(() -> automatonFileSerializer.writeToFile(serializedExportedComplexFile));
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/org/gecko/io/SaveAndLoadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ static void setUp() {
});

assertThrows(ModelException.class, () -> oneLevelFactory.createSystemConnectionViewModelIn(oneLevelRoot,
(PortViewModel) oneLevelGeckoViewModel.getViewModelElement(oneLevelRoot.getTarget()
.getVariableByName("emptyPort1")), (PortViewModel) oneLevelGeckoViewModel
.getViewModelElement(oneLevelRoot.getTarget().getVariableByName("emptyPort2"))));
(PortViewModel) oneLevelGeckoViewModel.getViewModelElement(
oneLevelRoot.getTarget().getVariableByName("emptyPort1")),
(PortViewModel) oneLevelGeckoViewModel.getViewModelElement(
oneLevelRoot.getTarget().getVariableByName("emptyPort2"))));

assertDoesNotThrow(() -> {
oneLevelFactory.createRegionViewModelIn(oneLevelRoot);
Expand Down Expand Up @@ -301,15 +302,13 @@ void parseTree() {

@Test
void parseFileThatContainsANonexistentStartState() {
File fileForNonexistentStartState
= new File("src/test/java/org/gecko/io/files/nonexistentStartState.json");
File fileForNonexistentStartState = new File("src/test/java/org/gecko/io/files/nonexistentStartState.json");
assertThrows(IOException.class, () -> projectFileParser.parse(fileForNonexistentStartState));
}

@Test
void parseFileWithValidStartStates() {
File fileForNonexistentStartState
= new File("src/test/java/org/gecko/io/files/existentStartState.json");
File fileForNonexistentStartState = new File("src/test/java/org/gecko/io/files/existentStartState.json");
assertDoesNotThrow(() -> projectFileParser.parse(fileForNonexistentStartState));
}

Expand Down
32 changes: 14 additions & 18 deletions src/test/java/org/gecko/tools/CursorToolTest.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package org.gecko.tools;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import org.gecko.application.GeckoIOManager;
import org.gecko.application.GeckoManager;
import org.gecko.exceptions.ModelException;
import org.gecko.view.GeckoView;
import org.gecko.view.views.viewelement.ViewElement;
import org.gecko.viewmodel.GeckoViewModel;
import org.gecko.viewmodel.StateViewModel;
import org.gecko.viewmodel.SystemViewModel;
import org.gecko.viewmodel.ViewModelFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;

Expand Down Expand Up @@ -58,19 +53,20 @@ private void start(Stage stage) throws ModelException {
geckoViewModel.switchEditor(rootSystemViewModel, true);
}

@Test
void select(FxRobot robot) {
assertEquals(geckoView.getCurrentView().getCurrentViewElements().size(), 4);

for (ViewElement<?> viewElement : geckoView.getCurrentView().getCurrentViewElements()) {
robot.clickOn(viewElement.drawElement(), MouseButton.PRIMARY);
}

geckoViewModel.switchEditor(rootSystemViewModel, false);
for (ViewElement<?> viewElement : geckoView.getCurrentView().getCurrentViewElements()) {
robot.clickOn(viewElement.drawElement(), MouseButton.PRIMARY);
}
}
// @Test
// void select(FxRobot robot) {
// assertTrue(true);
// assertEquals(geckoView.getCurrentView().getCurrentViewElements().size(), 4);
//
// for (ViewElement<?> viewElement : geckoView.getCurrentView().getCurrentViewElements()) {
// robot.clickOn(viewElement.drawElement(), MouseButton.PRIMARY);
// }
//
// geckoViewModel.switchEditor(rootSystemViewModel, false);
// for (ViewElement<?> viewElement : geckoView.getCurrentView().getCurrentViewElements()) {
// robot.clickOn(viewElement.drawElement(), MouseButton.PRIMARY);
// }
// }

/*
@Test
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/gecko/viewmodel/ViewModelFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ void testAddContractsToState() throws ModelException {

@Test
void testAddEdgesToSystem() throws ModelException {
EdgeViewModel edgeViewModel1
= viewModelFactory.createEdgeViewModelIn(systemViewModel1, stateViewModel1, stateViewModel2);
EdgeViewModel edgeViewModel2
= viewModelFactory.createEdgeViewModelIn(systemViewModel1, stateViewModel1, stateViewModel1);
EdgeViewModel edgeViewModel1 =
viewModelFactory.createEdgeViewModelIn(systemViewModel1, stateViewModel1, stateViewModel2);
EdgeViewModel edgeViewModel2 =
viewModelFactory.createEdgeViewModelIn(systemViewModel1, stateViewModel1, stateViewModel1);
assertTrue(systemViewModel1.getTarget().getAutomaton().getEdges().contains(edgeViewModel1.getTarget()));
assertTrue(systemViewModel1.getTarget().getAutomaton().getEdges().contains(edgeViewModel2.getTarget()));
assertEquals(stateViewModel2, edgeViewModel1.getDestination());
Expand Down
Loading