Skip to content

Commit

Permalink
feat: add merge function to tip containers
Browse files Browse the repository at this point in the history
This may be useful if your plugin also loads tips using the TipLoader
  • Loading branch information
sillydan1 committed Mar 23, 2024
1 parent c0d7f66 commit 28bbc72
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
33 changes: 0 additions & 33 deletions core/src/main/java/dk/gtz/graphedit/internal/TipLoader.java

This file was deleted.

37 changes: 37 additions & 0 deletions core/src/main/java/dk/gtz/graphedit/util/TipLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dk.gtz.graphedit.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;

import dk.gtz.graphedit.viewmodel.Tip;
import dk.gtz.graphedit.viewmodel.TipContainer;

public class TipLoader {
private static final Logger logger = LoggerFactory.getLogger(TipLoader.class);

public static record Tips(ArrayList<Tip> tips) {
}

public static TipContainer loadTips(String tipsFile) {
try {
var mapper = new ObjectMapper(new YAMLFactory());
mapper.registerModule(new Jdk8Module());
var tips = mapper.readValue(TipLoader.class.getClassLoader().getResourceAsStream(tipsFile), Tips.class);
return new TipContainer(tips.tips());
} catch (Exception e) {
logger.error("Failed to load tips", e);
return new TipContainer(List.of(new Tip("Error", "Failed to load tips")));
}
}

public static TipContainer loadTips() {
return loadTips("tips/tips.yaml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public void add(Tip tip) {
tips.add(tip);
}

public void merge(TipContainer container) {
tips.addAll(container.tips);
}

public int size() {
return tips.size();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import atlantafx.base.theme.CupertinoLight;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import dk.gtz.graphedit.internal.TipLoader;
import dk.gtz.graphedit.logging.EditorLogAppender;
import dk.gtz.graphedit.logging.Toast;
import dk.gtz.graphedit.model.ModelProject;
Expand Down Expand Up @@ -41,14 +40,13 @@
import dk.gtz.graphedit.util.IObservableUndoSystem;
import dk.gtz.graphedit.util.MouseTracker;
import dk.gtz.graphedit.util.ObservableTreeUndoSystem;
import dk.gtz.graphedit.util.PlatformUtils;
import dk.gtz.graphedit.util.TipLoader;
import dk.gtz.graphedit.viewmodel.FileBufferContainer;
import dk.gtz.graphedit.viewmodel.IBufferContainer;
import dk.gtz.graphedit.viewmodel.ISelectable;
import dk.gtz.graphedit.viewmodel.LanguageServerCollection;
import dk.gtz.graphedit.viewmodel.LintContainer;
import dk.gtz.graphedit.viewmodel.SyntaxFactoryCollection;
import dk.gtz.graphedit.viewmodel.Tip;
import dk.gtz.graphedit.viewmodel.TipContainer;
import dk.gtz.graphedit.viewmodel.ViewModelEditorSettings;
import dk.gtz.graphedit.viewmodel.ViewModelProject;
Expand Down Expand Up @@ -138,10 +136,6 @@ private void setupApplication() {
DI.add(LintContainer.class, new LintContainer());
ObservableList<ISelectable> selectedElementsList = FXCollections.observableArrayList();
DI.add("selectedElements", selectedElementsList);
setupTipsOfTheDay();
}

private void setupTipsOfTheDay() {
DI.add(TipContainer.class, TipLoader.loadTips());
}

Expand Down

0 comments on commit 28bbc72

Please sign in to comment.