diff --git a/src/main/java/de/marhali/easyi18n/DataStore.java b/src/main/java/de/marhali/easyi18n/DataStore.java index d48444e..d8fcd83 100644 --- a/src/main/java/de/marhali/easyi18n/DataStore.java +++ b/src/main/java/de/marhali/easyi18n/DataStore.java @@ -1,6 +1,8 @@ package de.marhali.easyi18n; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Disposer; +import com.intellij.openapi.vfs.*; import de.marhali.easyi18n.io.IOStrategy; import de.marhali.easyi18n.io.json.JsonIOStrategy; @@ -9,6 +11,7 @@ import de.marhali.easyi18n.io.properties.PropertiesIOStrategy; import de.marhali.easyi18n.io.yaml.YamlIOStrategy; import de.marhali.easyi18n.model.SettingsState; import de.marhali.easyi18n.model.TranslationData; +import de.marhali.easyi18n.service.FileChangeListener; import de.marhali.easyi18n.service.SettingsService; import org.jetbrains.annotations.NotNull; @@ -31,13 +34,18 @@ public class DataStore { new PropertiesIOStrategy() )); - private final Project project; + private final @NotNull Project project; + private final @NotNull FileChangeListener changeListener; private @NotNull TranslationData data; - protected DataStore(Project project) { + protected DataStore(@NotNull Project project) { this.project = project; this.data = new TranslationData(true, true); // Initialize with hard-coded configuration + this.changeListener = new FileChangeListener(project); + + VirtualFileManager.getInstance().addAsyncFileListener( + this.changeListener, Disposer.newDisposable("EasyI18n")); } public @NotNull TranslationData getData() { @@ -58,6 +66,8 @@ public class DataStore { return; } + this.changeListener.updateLocalesPath(localesPath); + IOStrategy strategy = this.determineStrategy(state, localesPath); strategy.read(this.project, localesPath, state, (data) -> { diff --git a/src/main/java/de/marhali/easyi18n/service/FileChangeListener.java b/src/main/java/de/marhali/easyi18n/service/FileChangeListener.java new file mode 100644 index 0000000..9591a98 --- /dev/null +++ b/src/main/java/de/marhali/easyi18n/service/FileChangeListener.java @@ -0,0 +1,66 @@ +package de.marhali.easyi18n.service; + +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.AsyncFileListener; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.openapi.vfs.newvfs.events.VFileEvent; + +import de.marhali.easyi18n.InstanceManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.util.List; + +/** + * Listens for file changes inside configured @localesPath. See {@link AsyncFileListener}. + * Will trigger the reload function of the i18n instance if a relevant file was changed. + * @author marhali + */ +public class FileChangeListener implements AsyncFileListener { + + private static final Logger logger = Logger.getInstance(FileChangeListener.class); + + private final @NotNull Project project; + private @Nullable String localesPath; + + public FileChangeListener(@NotNull Project project) { + this.project = project; + this.localesPath = null; // Wait for any update before listening to file changes + } + + public void updateLocalesPath(@Nullable String localesPath) { + if(localesPath != null && !localesPath.isEmpty()) { + VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(localesPath)); + + if(file != null && file.isDirectory()) { + this.localesPath = file.getPath(); + return; + } + } + + this.localesPath = null; + } + + @Override + public ChangeApplier prepareChange(@NotNull List events) { + return new ChangeApplier() { + @Override + public void afterVfsChange() { + if(localesPath != null) { + events.forEach((e) -> { + if(e.getPath().contains(localesPath)) { // Perform reload + logger.debug("Detected file change. Reloading instance..."); + InstanceManager manager = InstanceManager.get(project); + manager.store().loadFromPersistenceLayer((success) -> { + manager.bus().propagate().onUpdateData(manager.store().getData()); + }); + } + }); + } + } + }; + } +} \ No newline at end of file