Support multiple opened projects
This commit is contained in:
parent
a8b6938ac9
commit
d872f3e89a
@ -5,6 +5,7 @@
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
### Added
|
### Added
|
||||||
- Scroll to created / edited translation inside Tree-/Table-View
|
- Scroll to created / edited translation inside Tree-/Table-View
|
||||||
|
- Support for working with multiple projects at once
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Updated dependencies
|
- Updated dependencies
|
||||||
|
@ -19,15 +19,17 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Singleton service to manage localized messages.
|
* Factory service to manage localized messages for multiple projects at once.
|
||||||
* @author marhali
|
* @author marhali
|
||||||
*/
|
*/
|
||||||
public class DataStore {
|
public class DataStore {
|
||||||
|
|
||||||
private static DataStore INSTANCE;
|
private static final Map<Project, DataStore> INSTANCES = new WeakHashMap<>();
|
||||||
|
|
||||||
private final Project project;
|
private final Project project;
|
||||||
private final List<DataSynchronizer> synchronizer;
|
private final List<DataSynchronizer> synchronizer;
|
||||||
@ -36,7 +38,14 @@ public class DataStore {
|
|||||||
private String searchQuery;
|
private String searchQuery;
|
||||||
|
|
||||||
public static DataStore getInstance(@NotNull Project project) {
|
public static DataStore getInstance(@NotNull Project project) {
|
||||||
return INSTANCE == null ? INSTANCE = new DataStore(project) : INSTANCE;
|
DataStore store = INSTANCES.get(project);
|
||||||
|
|
||||||
|
if(store == null) {
|
||||||
|
store = new DataStore(project);
|
||||||
|
INSTANCES.put(project, store);
|
||||||
|
}
|
||||||
|
|
||||||
|
return store;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataStore(@NotNull Project project) {
|
private DataStore(@NotNull Project project) {
|
||||||
@ -64,32 +73,17 @@ public class DataStore {
|
|||||||
|
|
||||||
if(localesPath == null || localesPath.isEmpty()) {
|
if(localesPath == null || localesPath.isEmpty()) {
|
||||||
// Propagate empty state
|
// Propagate empty state
|
||||||
translations = Translations.empty();
|
this.translations = Translations.empty();
|
||||||
synchronizer.forEach(synchronizer -> synchronizer.synchronize(translations, searchQuery, null));
|
synchronize(searchQuery, null);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
TranslatorIO io = IOUtil.determineFormat(localesPath);
|
TranslatorIO io = IOUtil.determineFormat(localesPath);
|
||||||
|
|
||||||
io.read(project, localesPath, (translations) -> {
|
io.read(project, localesPath, (loadedTranslations) -> {
|
||||||
if(translations != null) { // Read was successful
|
this.translations = loadedTranslations == null ? Translations.empty() : loadedTranslations;
|
||||||
this.translations = translations;
|
synchronize(searchQuery, null);
|
||||||
|
|
||||||
// Propagate changes
|
|
||||||
synchronizer.forEach(synchronizer ->
|
|
||||||
synchronizer.synchronize(this.translations, searchQuery, null));
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// If state cannot be loaded from disk, show empty instance
|
|
||||||
this.translations = Translations.empty();
|
|
||||||
|
|
||||||
// Propagate changes
|
|
||||||
synchronizer.forEach(synchronizer ->
|
|
||||||
synchronizer.synchronize(this.translations, searchQuery, null));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("reloadFromDisk()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -113,8 +107,7 @@ public class DataStore {
|
|||||||
*/
|
*/
|
||||||
public void searchBeyKey(@Nullable String fullPath) {
|
public void searchBeyKey(@Nullable String fullPath) {
|
||||||
// Use synchronizer to propagate search instance to all views
|
// Use synchronizer to propagate search instance to all views
|
||||||
synchronizer.forEach(synchronizer ->
|
synchronize(this.searchQuery = fullPath, null);
|
||||||
synchronizer.synchronize(translations, this.searchQuery = fullPath, null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -157,7 +150,7 @@ public class DataStore {
|
|||||||
// Persist changes and propagate them on success
|
// Persist changes and propagate them on success
|
||||||
saveToDisk(success -> {
|
saveToDisk(success -> {
|
||||||
if(success) {
|
if(success) {
|
||||||
synchronizer.forEach(synchronizer -> synchronizer.synchronize(translations, searchQuery, scrollTo));
|
synchronize(searchQuery, scrollTo);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -168,4 +161,13 @@ public class DataStore {
|
|||||||
public @NotNull Translations getTranslations() {
|
public @NotNull Translations getTranslations() {
|
||||||
return translations;
|
return translations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronizes current translation's state to all connected subscribers.
|
||||||
|
* @param searchQuery Optional search by full key filter (ui view)
|
||||||
|
* @param scrollTo Optional scroll to full key (ui view)
|
||||||
|
*/
|
||||||
|
public void synchronize(@Nullable String searchQuery, @Nullable String scrollTo) {
|
||||||
|
synchronizer.forEach(subscriber -> subscriber.synchronize(this.translations, searchQuery, scrollTo));
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user