Skip to content

Commit

Permalink
Fix - adding/removing function cleared choicebox's selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
aadamvanko committed Dec 13, 2021
1 parent 21754c4 commit 5fb33a0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.arc.viewmodels.ArcViewModel;
import cz.muni.fi.spnp.gui.components.menu.view.functions.FunctionViewModel;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.FunctionViewModelStringConverter;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;

import java.util.stream.Collectors;
Expand All @@ -18,7 +18,7 @@
public class FunctionalMultiplicitySubEditor extends ArcMultiplicitySubEditor {

private Label multiplicityFunctionLabel;
private ChoiceBox<FunctionViewModel> multiplicityFunctionChoiceBox;
private MyChoiceBox<FunctionViewModel> multiplicityFunctionChoiceBox;

private final ListChangeListener<? super FunctionViewModel> onFunctionsChangedListener;

Expand All @@ -30,7 +30,7 @@ public FunctionalMultiplicitySubEditor() {

private void createView() {
multiplicityFunctionLabel = new Label("Multiplicity function:");
multiplicityFunctionChoiceBox = new ChoiceBox<>();
multiplicityFunctionChoiceBox = new MyChoiceBox<>();
addRow(multiplicityFunctionLabel, multiplicityFunctionChoiceBox);
}

Expand All @@ -42,7 +42,7 @@ private void onFunctionsChangedListener(ListChangeListener.Change<? extends Func
// System.out.println("changed");
var functionsCopy = FXCollections.observableArrayList(arcCardinalityFunctions);
functionsCopy.add(0, null);
multiplicityFunctionChoiceBox.setItems(functionsCopy);
multiplicityFunctionChoiceBox.setItemsWithSelected(functionsCopy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cz.muni.fi.spnp.gui.components.propertieseditor.common;

import javafx.collections.ObservableList;
import javafx.scene.control.ChoiceBox;

/**
* Custom choice box to keep the selected item after the change of the items.
*
* @param <T> type of the items
*/
public class MyChoiceBox<T> extends ChoiceBox<T> {

public void setItemsWithSelected(ObservableList<T> newItems) {
var selectedItem = getSelectionModel().getSelectedItem();
super.setItems(newItems);
getSelectionModel().select(selectedItem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cz.muni.fi.spnp.gui.components.propertieseditor.ElementPropertiesEditor;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.FunctionViewModelStringConverter;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.IntegerTextField;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
Expand All @@ -25,7 +26,7 @@ public abstract class TransitionPropertiesEditor extends ElementPropertiesEditor
private final Label priorityLabel;
private final IntegerTextField priorityTextField;
private final Label guardFunctionLabel;
private final ChoiceBox<FunctionViewModel> guardFunctionChoiceBox;
private final MyChoiceBox<FunctionViewModel> guardFunctionChoiceBox;
private final ListChangeListener<? super FunctionViewModel> onFunctionsChangedListener;
private final Label orientationLabel;
private final ChoiceBox<TransitionOrientation> orientationChoiceBox;
Expand All @@ -36,7 +37,7 @@ public TransitionPropertiesEditor() {
addRow(priorityLabel, priorityTextField.getTextField());

guardFunctionLabel = new Label("Guard function:");
guardFunctionChoiceBox = new ChoiceBox<>();
guardFunctionChoiceBox = new MyChoiceBox<>();
addRow(guardFunctionLabel, guardFunctionChoiceBox);

orientationLabel = new Label("Orientation:");
Expand All @@ -58,7 +59,7 @@ private void onFunctionsChangedListener(ListChangeListener.Change<? extends Func
.collect(Collectors.toList());
var functionsCopy = FXCollections.observableArrayList(guardFunctions);
functionsCopy.add(0, null);
guardFunctionChoiceBox.setItems(functionsCopy);
guardFunctionChoiceBox.setItemsWithSelected(functionsCopy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.transition.viewmodels.immediate.TransitionProbabilityViewModel;
import cz.muni.fi.spnp.gui.components.menu.view.functions.FunctionViewModel;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.FunctionViewModelStringConverter;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;

import java.util.stream.Collectors;
Expand All @@ -20,7 +20,7 @@ public class FunctionalProbabilityPropertiesSubEditor extends TransitionProbabil

private final ListChangeListener<? super FunctionViewModel> onFunctionsChangedListener;
private Label functionLabel;
private ChoiceBox<FunctionViewModel> functionChoiceBox;
private MyChoiceBox<FunctionViewModel> functionChoiceBox;

public FunctionalProbabilityPropertiesSubEditor() {
createView();
Expand All @@ -30,7 +30,7 @@ public FunctionalProbabilityPropertiesSubEditor() {

private void createView() {
functionLabel = new Label("Function:");
functionChoiceBox = new ChoiceBox<>();
functionChoiceBox = new MyChoiceBox<>();

addRow(functionLabel, functionChoiceBox);
}
Expand All @@ -41,7 +41,7 @@ private void onFunctionsChangedListener(ListChangeListener.Change<? extends Func
.collect(Collectors.toList());
var functionsCopy = FXCollections.observableArrayList(probabilityFunctions);
functionsCopy.add(0, null);
functionChoiceBox.setItems(functionsCopy);
functionChoiceBox.setItemsWithSelected(functionsCopy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.place.PlaceViewModel;
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.transition.viewmodels.immediate.PlaceDependentTransitionProbabilityViewModel;
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.transition.viewmodels.immediate.TransitionProbabilityViewModel;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.PlaceViewModelStringConverter;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

Expand All @@ -21,7 +21,7 @@ public class PlaceDependentProbabilityPropertiesSubEditor extends TransitionProb
private Label valueLabel;
private TextField valueTextField;
private Label dependentPlaceLabel;
private ChoiceBox<PlaceViewModel> dependentPlaceChoiceBox;
private MyChoiceBox<PlaceViewModel> dependentPlaceChoiceBox;

public PlaceDependentProbabilityPropertiesSubEditor() {
createView();
Expand All @@ -35,14 +35,14 @@ private void createView() {
addRow(valueLabel, valueTextField);

dependentPlaceLabel = new Label("Dependent place:");
dependentPlaceChoiceBox = new ChoiceBox<>();
dependentPlaceChoiceBox = new MyChoiceBox<>();
addRow(dependentPlaceLabel, dependentPlaceChoiceBox);
}

private void onPlacesChangedListener(ListChangeListener.Change<? extends ElementViewModel> placesChange) {
var placesCopy = FXCollections.observableArrayList(diagramViewModel.getPlaces());
placesCopy.add(0, null);
dependentPlaceChoiceBox.setItems(placesCopy);
dependentPlaceChoiceBox.setItemsWithSelected(placesCopy);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.transition.viewmodels.timed.distributions.TransitionDistributionViewModel;
import cz.muni.fi.spnp.gui.components.menu.view.functions.FunctionViewModel;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.FunctionViewModelStringConverter;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import cz.muni.fi.spnp.gui.components.propertieseditor.transition.PropertiesEditorRow;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;

import java.util.ArrayList;
Expand All @@ -22,7 +22,7 @@ public class FunctionalTransitionDistributionSubEditor extends TransitionDistrib

private final ListChangeListener<? super FunctionViewModel> onFunctionsChangedListener;
private List<Label> valuesLabels;
private List<ChoiceBox<FunctionViewModel>> functionsChoiceBoxes;
private List<MyChoiceBox<FunctionViewModel>> functionsChoiceBoxes;

public FunctionalTransitionDistributionSubEditor() {
createView();
Expand All @@ -36,7 +36,7 @@ private void createView() {
for (int i = 0; i < 4; i++) {
var valueLabel = new Label("Value:");
valuesLabels.add(valueLabel);
var functionChoiceBox = new ChoiceBox<FunctionViewModel>();
var functionChoiceBox = new MyChoiceBox<FunctionViewModel>();
functionsChoiceBoxes.add(functionChoiceBox);
addRow(valueLabel, functionChoiceBox);
}
Expand All @@ -48,7 +48,7 @@ private void onFunctionsChangedListener(ListChangeListener.Change<? extends Func
.collect(Collectors.toList());
var functionsCopy = FXCollections.observableArrayList(distributionFunctions);
functionsCopy.add(0, null);
functionsChoiceBoxes.forEach(functionChoiceBox -> functionChoiceBox.setItems(functionsCopy));
functionsChoiceBoxes.forEach(functionChoiceBox -> functionChoiceBox.setItemsWithSelected(functionsCopy));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import cz.muni.fi.spnp.gui.components.diagram.graph.common.ElementViewModel;
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.place.PlaceViewModel;
import cz.muni.fi.spnp.gui.components.diagram.graph.elements.transition.viewmodels.timed.distributions.TransitionDistributionViewModel;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.MyChoiceBox;
import cz.muni.fi.spnp.gui.components.propertieseditor.common.PlaceViewModelStringConverter;
import cz.muni.fi.spnp.gui.components.propertieseditor.transition.PropertiesEditorRow;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;

import java.util.List;
Expand All @@ -22,7 +22,7 @@ public class PlaceDependentTransitionDistributionSubEditor extends ConstantTrans

private final ListChangeListener<? super ElementViewModel> onPlacesChangedListener;
private Label dependentPlaceLabel;
private ChoiceBox<PlaceViewModel> dependentPlaceChoiceBox;
private MyChoiceBox<PlaceViewModel> dependentPlaceChoiceBox;
private PropertiesEditorRow dependentPlaceRow;

public PlaceDependentTransitionDistributionSubEditor() {
Expand All @@ -33,14 +33,14 @@ public PlaceDependentTransitionDistributionSubEditor() {

private void createView() {
dependentPlaceLabel = new Label("Dependent place:");
dependentPlaceChoiceBox = new ChoiceBox<>();
dependentPlaceChoiceBox = new MyChoiceBox<>();
dependentPlaceRow = new PropertiesEditorRow(dependentPlaceLabel, dependentPlaceChoiceBox);
}

private void onPlacesChangedListener(ListChangeListener.Change<? extends ElementViewModel> placesChange) {
var placesCopy = FXCollections.observableArrayList(diagramViewModel.getPlaces());
placesCopy.add(0, null);
dependentPlaceChoiceBox.setItems(placesCopy);
dependentPlaceChoiceBox.setItemsWithSelected(placesCopy);
}

@Override
Expand Down

0 comments on commit 5fb33a0

Please sign in to comment.