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

Commit

Permalink
Add changes
Browse files Browse the repository at this point in the history
Signed-off-by: Nassirou NAMBIEMA <[email protected]>
  • Loading branch information
Nassirou NAMBIEMA committed Jan 29, 2020
1 parent 2273d36 commit ec7db3a
Showing 1 changed file with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.powsybl.afs.ext.base.AbstractScript;
import com.powsybl.afs.ext.base.ScriptListener;
import com.powsybl.afs.ext.base.ScriptType;
import com.powsybl.afs.storage.events.AppStorageListener;
import com.powsybl.afs.storage.events.DependencyAdded;
import com.powsybl.afs.storage.events.DependencyRemoved;
import com.powsybl.commons.util.ServiceLoaderCache;
Expand Down Expand Up @@ -102,7 +103,7 @@ public class ModificationScriptEditor extends BorderPane

private Optional<AbstractCodeEditorFactoryService> preferredCodeEditor;

private AbstractScript abstractScript;
private AbstractScript<? extends AbstractScript> abstractScript;

private final SimpleBooleanProperty saved = new SimpleBooleanProperty(true);

Expand All @@ -118,23 +119,25 @@ public ModificationScriptEditor(AbstractScript abstractScript, Scene scene, GseC

codeEditor = getCodeEditor();

abstractScript.getFileSystem().getEventBus().addListener(eventList -> {
LOGGER.info(abstractScript.getId());
eventList.getEvents().forEach(nodeEvent -> {
Platform.runLater(() -> {
LOGGER.info(nodeEvent.getId());
boolean nodeEventEqualsAbstractScript = nodeEvent.getId().equals(abstractScript.getId());
boolean isDependencyAddedType = DependencyAdded.TYPENAME.equals(nodeEvent.getType());
boolean isDependencyRemovedType = DependencyRemoved.TYPENAME.equals(nodeEvent.getType());

if (nodeEventEqualsAbstractScript && (isDependencyAddedType || isDependencyRemovedType)) {
updateIncludePane();
}
});

});
});

AppStorageListener l = eventList ->
eventList.getEvents().forEach(nodeEvent ->
Platform.runLater(() -> {
boolean nodeEventEqualsAbstractScript = nodeEvent.getId().equals(abstractScript.getId());
boolean oneOrMoreDependenciesHaveChanged = this.abstractScript.getIncludedScripts().stream().anyMatch(includeScript -> includeScript.getId().equals(nodeEvent.getId()));
if (oneOrMoreDependenciesHaveChanged || nodeEventEqualsAbstractScript) {
boolean isDependencyAddedType = DependencyAdded.TYPENAME.equals(nodeEvent.getType());
boolean isDependencyRemovedType = DependencyRemoved.TYPENAME.equals(nodeEvent.getType());

boolean dependenciesAreAddedOrRemoved = nodeEventEqualsAbstractScript && (isDependencyAddedType || isDependencyRemovedType);

if (dependenciesAreAddedOrRemoved || oneOrMoreDependenciesHaveChanged) {
abstractScript.clearDependenciesCache();
updateIncludePane();
}
}
}));

abstractScript.getFileSystem().getEventBus().addListener(l);
//Adding autocompletion keywords suggestions depending the context
List<String> suggestions = new ArrayList<>();
List<AutoCompletionWordsProvider> completionWordsProviderExtensions = findCompletionWordsProviderExtensions(abstractScript);
Expand All @@ -143,7 +146,7 @@ public ModificationScriptEditor(AbstractScript abstractScript, Scene scene, GseC
codeEditorWithProgressIndicator = new StackPane();
codeEditorWithIncludesPane = new MasterDetailPane();
codeEditorWithIncludesPane.setMasterNode(codeEditorWithProgressIndicator);
codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(false, abstractScript.getIncludedScripts()));
codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(false));
codeEditorWithIncludesPane.setShowDetailNode(true);
codeEditorWithIncludesPane.setDetailSide(Side.TOP);
codeEditorWithIncludesPane.setDividerPosition(DIVIDER_POSITION);
Expand Down Expand Up @@ -247,27 +250,33 @@ public void run() {
}

private void updateIncludePane() {
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true, abstractScript.getIncludedScripts())));
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true)));
}

private Node createIncludedScriptsPane(boolean isExpanded, List<AbstractScript> includedScripts) {
private Node createIncludedScriptsPane(boolean isExpanded) {
List<AbstractScript> includedScripts = abstractScript.getIncludedScripts();
List<IncludeScriptPane> includedScriptsPanes = includedScripts.stream()
.map(this::includedPane)
.collect(Collectors.toList());

List<HBox> allIncludesPanes = new ArrayList<>();
includedScriptsPanes.forEach(includedScriptPane -> {
List<AbstractScript> orderedIncludedScripts = new ArrayList<>(includedScripts);
int index = includedScripts.indexOf(includedScriptPane.getIncludedScript());
Button removeButton = createIncludedButton('\uf00d', "1.1em", event -> removeIncludedScript(includedScriptPane.getIncludedScript()));
Button upButton = createIncludedButton('\uf062', "0.9em", event -> {
reOrderIncludedScripts(includedScripts, orderedIncludedScripts, index, index - 1);
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true, orderedIncludedScripts)));
abstractScript.switchIncludedDependencies(index, index - 1);
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true)));
});
if (includedScriptsPanes.get(0) == includedScriptPane) {
upButton.setDisable(true);
}
Button downButton = createIncludedButton('\uf063', "0.9em", event -> {
reOrderIncludedScripts(includedScripts, orderedIncludedScripts, index, index + 1);
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true, orderedIncludedScripts)));
abstractScript.switchIncludedDependencies(index, index + 1);
Platform.runLater(() -> codeEditorWithIncludesPane.setDetailNode(createIncludedScriptsPane(true)));
});
if (includedScriptsPanes.get(includedScriptsPanes.size() - 1) == includedScriptPane) {
downButton.setDisable(true);
}
upButton.setPadding(new Insets(5, 5, 5, 5));
downButton.setPadding(new Insets(5, 5, 5, 5));
HBox includePaneWithEditingButtons = new HBox(includedScriptPane, removeButton, upButton, downButton);
Expand All @@ -292,19 +301,11 @@ private Node createIncludedScriptsPane(boolean isExpanded, List<AbstractScript>
return rootTitledPane;
}

private void reOrderIncludedScripts(List<AbstractScript> scripts, List<AbstractScript> orderedScripts, int currentIndex, int newIndex) {
try {
orderedScripts.set(currentIndex, scripts.get(newIndex));
orderedScripts.set(newIndex, scripts.get(currentIndex));
} catch (IndexOutOfBoundsException ignored) {
}
}

private IncludeScriptPane includedPane(AbstractScript script) {
AbstractCodeEditor includedScriptCodeEditor = getCodeEditor();
includedScriptCodeEditor.setCode(script.readScript());
int editableTimeOut = 2000;
Timer timer = new Timer();
Timer timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
Expand Down

0 comments on commit ec7db3a

Please sign in to comment.