Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Create an object from selected sub-objects (#274)
Browse files Browse the repository at this point in the history
* Create an object from selected sub-objects

Signed-off-by: Nassirou NAMBIEMA <nassirou.nambiena@rte-france.com>
  • Loading branch information
nashousni authored Mar 1, 2020
1 parent fe37db5 commit 25926f0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,12 @@ public BooleanBinding okProperty() {

public void dispose() {
}

public void setCase(ProjectFile caseFile) {
caseSelectionPane.nodeProperty().setValue(caseFile);
}

public void setScript(ModificationScript modificationScript) {
scriptSelectionPane.nodeProperty().setValue(modificationScript);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
package com.powsybl.gse.afs.ext.base;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.powsybl.afs.ProjectFile;
import com.powsybl.afs.ProjectFolder;
import com.powsybl.afs.ext.base.ImportedCase;
import com.powsybl.afs.ext.base.ModificationScript;
import com.powsybl.afs.ext.base.ProjectCase;
import com.powsybl.afs.ext.base.VirtualCase;
import com.powsybl.gse.spi.GseContext;
import com.powsybl.gse.spi.ProjectFileCreator;
Expand All @@ -19,6 +23,7 @@
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;

import java.util.List;
import java.util.ResourceBundle;

/**
Expand All @@ -34,6 +39,11 @@ public Class<? extends ProjectFile> getProjectFileType() {
return VirtualCase.class;
}

@Override
public List<Class<?>> getDependenciesTypes() {
return ImmutableList.of(ModificationScript.class, ProjectCase.class);
}

@Override
public Node getMenuGraphic() {
return BaseExtNodeGraphicProvider.createVirtualCaseGlyph();
Expand All @@ -58,4 +68,19 @@ public KeyCodeCombination getMenuKeycode() {
public ProjectFileCreator newCreator(ProjectFolder folder, Scene scene, GseContext context) {
return new VirtualCaseCreator(folder, scene, context);
}

@Override
public ProjectFileCreator newCreatorWithParameters(ProjectFolder folder, Scene scene, GseContext context, List<ProjectFile> parameters) {
VirtualCaseCreator virtualCaseCreator = new VirtualCaseCreator(folder, scene, context);
for (ProjectFile value : parameters) {
if (value instanceof ImportedCase) {
virtualCaseCreator.setCase(value);
} else if (value instanceof VirtualCase) {
virtualCaseCreator.setCase(value);
} else if (value instanceof ModificationScript) {
virtualCaseCreator.setScript((ModificationScript) value);
}
}
return virtualCaseCreator;
}
}
58 changes: 58 additions & 0 deletions gse-app/src/main/java/com/powsybl/gse/app/ProjectPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,32 @@ private static List<ProjectFileExecutionTaskExtension> findExecutionTaskExtensio

// contextual menu

private GseMenuItem createMultipleDependenciesFileItem(List<? extends TreeItem<Object>> selectedTreeItems) {
GseMenuItem menuItem = null;
List<? extends Class<?>> selectedItemsTypes = selectedTreeItems.stream().map(iem -> iem.getValue().getClass()).collect(Collectors.toList());

for (Class<? extends ProjectFile> type : project.getFileSystem().getData().getProjectFileClasses()) {
for (ProjectFileCreatorExtension creatorExtension : findCreatorExtension(type)) {
if (creatorExtension != null && !creatorExtension.getDependenciesTypes().isEmpty()) {
List<Class<?>> dependenciesTypes = creatorExtension.getDependenciesTypes();
if (areAssignable(dependenciesTypes, selectedItemsTypes)) {
menuItem = new GseMenuItem(creatorExtension.getMenuText());
menuItem.setOrder(creatorExtension.getMenuOrder());
menuItem.setGraphic(creatorExtension.getMenuGraphic());
menuItem.setAccelerator(creatorExtension.getMenuKeycode());
menuItem.setOnAction(event -> showProjectItemCreatorDialogWithFilledPaths(selectedTreeItems, creatorExtension));
}
}
}
}
return menuItem;
}

private boolean areAssignable(List<Class<?>> dependenciesTypes, List<? extends Class<?>> selectedItemsTypes) {
return dependenciesTypes.stream().distinct().count() == selectedItemsTypes.stream().distinct().count() && dependenciesTypes.stream()
.allMatch(dependencyType -> selectedItemsTypes.stream().anyMatch(dependencyType::isAssignableFrom));
}

private GseMenuItem createDeleteProjectNodeItem(List<? extends TreeItem<Object>> selectedTreeItems) {
GseMenuItem deleteMenuItem = GseMenuItem.createDeleteMenuItem();
deleteMenuItem.setOnAction(event -> deleteNodesAlert(selectedTreeItems));
Expand Down Expand Up @@ -1095,6 +1121,10 @@ private ContextMenu createMultipleContextMenu(List<? extends TreeItem<Object>> s
if (copyService.isPresent()) {
contextMenu.getItems().add(createCopyProjectNodeItem(selectedTreeItems));
}
GseMenuItem multipleDependenciesFileItem = createMultipleDependenciesFileItem(selectedTreeItems);
if (multipleDependenciesFileItem != null) {
contextMenu.getItems().add(multipleDependenciesFileItem);
}
contextMenu.getItems().add(createDeleteProjectNodeItem(selectedTreeItems));
return contextMenu;
}
Expand Down Expand Up @@ -1126,6 +1156,34 @@ private void showProjectItemCreatorDialog(TreeItem<Object> selectedTreeItem,
}
}

private void showProjectItemCreatorDialogWithFilledPaths(List<? extends TreeItem<Object>> selectedTreeItems, ProjectFileCreatorExtension creatorExtension) {
TreeItem<Object> treeItem = selectedTreeItems.get(0);
List<ProjectFile> selectedFiles = selectedTreeItems.stream().map(item -> (ProjectFile) item.getValue()).collect(Collectors.toList());
ProjectFolder folder = (ProjectFolder) treeItem.getParent().getValue();
ProjectFileCreator creator = creatorExtension.newCreatorWithParameters(folder, getContent().getScene(), context, selectedFiles);
Dialog<Boolean> dialog = createProjectItemDialog(creator.getTitle(), creator.okProperty(), creator.getContent());
try {
dialog.showAndWait().filter(result -> result).ifPresent(result -> {
ProjectCreationTask task = creator.createTask();

tasks.add(folder, task.getNamePreview());
refresh(treeItem.getParent());

GseUtil.execute(context.getExecutor(), () -> {
try {
task.run();
} finally {
tasks.remove(folder, task.getNamePreview());
Platform.runLater(() -> refresh(treeItem));
}
});
});
} finally {
dialog.close();
creator.dispose();
}
}

private void showProjectItemEditorDialog(ProjectFile file, ProjectFileEditorExtension editorExtension, String tabName) {

ProjectFileEditor editor = editorExtension.newEditor(file, getContent().getScene(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
import javafx.scene.Scene;
import javafx.scene.input.KeyCodeCombination;

import java.util.Collections;
import java.util.List;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public interface ProjectFileCreatorExtension {

Class<? extends ProjectFile> getProjectFileType();

default List<Class<?>> getDependenciesTypes() {
return Collections.emptyList();
}

default Node getMenuGraphic() {
return null;
}
Expand All @@ -36,4 +43,8 @@ default int getMenuOrder() {
KeyCodeCombination getMenuKeycode();

ProjectFileCreator newCreator(ProjectFolder folder, Scene scene, GseContext context);

default ProjectFileCreator newCreatorWithParameters(ProjectFolder folder, Scene scene, GseContext context, List<ProjectFile> parameters) {
return newCreator(folder, scene, context);
}
}

0 comments on commit 25926f0

Please sign in to comment.