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

Add country filter #116

Merged
merged 1 commit into from
May 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public String toString() {
@FXML
public ChoiceBox<ComponentFilterType> componentTypeFilterChoice;

@FXML
public ComboBox<String> countriesFilterComboBox;

@FXML
public TreeView<Container<?>> vlTree;
@FXML
Expand Down Expand Up @@ -156,6 +159,7 @@ private void initialize() {
// to avoid bug in TreeView: it does not calculate properly the selection shift, so clearing selection
filterField.textProperty().addListener((observable, oldValue, newValue) -> clearSelection());
componentTypeFilterChoice.valueProperty().addListener((observable, oldValue, newValue) -> clearSelection());
countriesFilterComboBox.valueProperty().addListener((observable, oldValue, newValue) -> clearSelection());

String casePathPropertyValue = preferences.get(CASE_PATH_PROPERTY, null);
if (casePathPropertyValue != null) {
Expand All @@ -171,6 +175,9 @@ private void initialize() {

showNames.selectedProperty().addListener((observable, oldValue, newValue) -> vlTree.refresh());

countriesFilterComboBox.itemsProperty().bind(Bindings.createObjectBinding(() -> model.getCountriesNames()));
countriesFilterComboBox.disableProperty().bind(Bindings.createBooleanBinding(() -> model.getCountriesNames().isEmpty(), model.networkProperty()));

vlTree.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
Container<?> c = newValue.getValue();
Expand Down Expand Up @@ -416,7 +423,8 @@ private void initSubstationsTree(Network network) {
filteredList = new FilteredList<>(substationItems);
filteredList.predicateProperty().bind(Bindings.createObjectBinding(() -> this::treeItemFilter,
filterField.textProperty(),
componentTypeFilterChoice.valueProperty()));
componentTypeFilterChoice.valueProperty(),
countriesFilterComboBox.valueProperty()));

var rootTreeItem = createCheckBoxTreeItem(network, containersChecked);
Bindings.bindContent(rootTreeItem.getChildren(), filteredList);
Expand All @@ -438,15 +446,31 @@ private CheckBoxTreeItem<Container<?>> createCheckBoxTreeItem(Container<?> c, Se
private boolean treeItemFilter(TreeItem<Container<?>> item) {
String filter = filterField.getText();
ComponentFilterType idType = componentTypeFilterChoice.getValue();
String countryValue = countriesFilterComboBox.getValue();
Country country = countryValue != null ? Country.valueOf(countryValue) : null;
var container = item.getValue();
if (StringUtils.isEmpty(filter)) {
return containsComponentType(idType, container);
return containsComponentType(idType, container) && locatedIn(country, container);
} else {
boolean filterOk = getIdentifiableStringSupplier().apply(container)
.toLowerCase(Locale.getDefault())
.contains(filter.toLowerCase(Locale.getDefault()));
return (filterOk || item.getChildren().stream().anyMatch(this::treeItemFilter))
&& containsComponentType(idType, container);
&& containsComponentType(idType, container)
&& locatedIn(country, container);
}
}

private static boolean locatedIn(Country country, Container<? extends Identifiable<?>> container) {
if (country == null) {
return true; // no selection
}
if (container instanceof Substation s) {
return s.getCountry().map(c -> c == country).orElse(false);
} else if (container instanceof VoltageLevel v) {
return v.getSubstation().flatMap(Substation::getCountry).map(c -> c == country).orElse(false);
} else {
return true; // network
}
}

Expand All @@ -465,7 +489,7 @@ private static boolean containsComponentType(ComponentFilterType type, Container
default -> v.getConnectableStream(type.connectableClass).findFirst().isPresent();
};
} else {
return true;
return true; // network
}
}

Expand Down
10 changes: 10 additions & 0 deletions diagram-viewer/src/main/java/com/powsybl/diagram/viewer/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
import com.powsybl.diagram.viewer.nad.NetworkAreaDiagramModel;
import com.powsybl.diagram.viewer.sld.SingleLineDiagramModel;
import com.powsybl.iidm.network.Container;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
public class Model {
private final ObjectProperty<Network> network = new SimpleObjectProperty<>();
private final ObservableList<String> countriesNames = FXCollections.observableArrayList();

private Container<?> selectedContainer = null;

private final NetworkAreaDiagramModel nadModel;
Expand All @@ -32,6 +37,7 @@ public Model(BooleanProperty showNames, NetworkAreaDiagramModel nadModel, Single
}

public void setNetwork(Network network) {
this.countriesNames.setAll(network.getCountries().stream().map(Country::toString).toList());
this.network.setValue(network);
}

Expand All @@ -56,4 +62,8 @@ public void clean() {
nadModel.clean();
sldModel.clean();
}

public ObservableList<String> getCountriesNames() {
return countriesNames;
}
}
4 changes: 4 additions & 0 deletions diagram-viewer/src/main/resources/mainView.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<Label text="Filter by component:" minWidth="120"/>
<EnumChoiceBox enumType="com.powsybl.diagram.viewer.MainViewController$ComponentFilterType" fx:id="componentTypeFilterChoice" initialValue="All"/>
</HBox>
<HBox spacing="5">
<Label text="Filter by country:" minWidth="120"/>
<ComboBox fx:id="countriesFilterComboBox"/>
</HBox>
<HBox>
<Button onMouseClicked="#expandSubstationsTree">
<tooltip><Tooltip text="Expand all"/></tooltip>
Expand Down
Loading