-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#28 Implementing remember window size & position.
- Loading branch information
1 parent
25718f4
commit 53a7351
Showing
8 changed files
with
454 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/io/github/albertus82/acodec/gui/config/ApplicationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.github.albertus82.acodec.gui.config; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
import org.eclipse.jface.util.Util; | ||
|
||
import io.github.albertus82.acodec.common.util.BuildInfo; | ||
import io.github.albertus82.acodec.gui.resources.GuiMessages; | ||
import io.github.albertus82.jface.preference.IPreferencesConfiguration; | ||
import io.github.albertus82.jface.preference.PreferencesConfiguration; | ||
import io.github.albertus82.util.InitializationException; | ||
import io.github.albertus82.util.SystemUtils; | ||
import io.github.albertus82.util.config.Configuration; | ||
import io.github.albertus82.util.config.PropertiesConfiguration; | ||
|
||
public class ApplicationConfig extends Configuration { | ||
|
||
private static final String DIRECTORY_NAME = Util.isLinux() ? '.' + BuildInfo.getProperty("project.artifactId") : BuildInfo.getProperty("project.name"); | ||
|
||
public static final String APPDATA_DIRECTORY = SystemUtils.getOsSpecificLocalAppDataDir() + File.separator + DIRECTORY_NAME; | ||
|
||
private static final String CFG_FILE_NAME = (Util.isLinux() ? BuildInfo.getProperty("project.artifactId") : BuildInfo.getProperty("project.name").replace(" ", "")) + ".cfg"; | ||
|
||
private static volatile ApplicationConfig instance; // NOSONAR Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe. Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe. | ||
private static volatile IPreferencesConfiguration wrapper; // NOSONAR Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe. Use a thread-safe type; adding "volatile" is not enough to make this field thread-safe. | ||
private static int instanceCount = 0; | ||
|
||
private final LanguageConfigAccessor languageConfigAccessor; | ||
|
||
private ApplicationConfig() throws IOException { | ||
super(new PropertiesConfiguration(DIRECTORY_NAME + File.separator + CFG_FILE_NAME, true)); | ||
final IPreferencesConfiguration pc = new PreferencesConfiguration(this); | ||
languageConfigAccessor = new LanguageConfigAccessor(pc); | ||
} | ||
|
||
private static ApplicationConfig getInstance() { | ||
if (instance == null) { | ||
synchronized (ApplicationConfig.class) { | ||
if (instance == null) { // The field needs to be volatile to prevent cache incoherence issues | ||
try { | ||
instance = new ApplicationConfig(); | ||
if (++instanceCount > 1) { | ||
throw new IllegalStateException("Detected multiple instances of singleton " + instance.getClass()); | ||
} | ||
} | ||
catch (final IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
public static IPreferencesConfiguration getPreferencesConfiguration() { | ||
if (wrapper == null) { | ||
synchronized (ApplicationConfig.class) { | ||
if (wrapper == null) { // The field needs to be volatile to prevent cache incoherence issues | ||
wrapper = new PreferencesConfiguration(getInstance()); | ||
} | ||
} | ||
} | ||
return wrapper; | ||
} | ||
|
||
public static void initialize() { | ||
try { | ||
final ApplicationConfig config = getInstance(); | ||
GuiMessages.INSTANCE.setLanguage(config.languageConfigAccessor.getLanguage()); | ||
} | ||
catch (final RuntimeException e) { | ||
throw new InitializationException(e); | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/io/github/albertus82/acodec/gui/config/LanguageConfigAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.albertus82.acodec.gui.config; | ||
|
||
import java.util.Locale; | ||
|
||
import io.github.albertus82.acodec.common.resources.Language; | ||
import io.github.albertus82.acodec.gui.preference.Preference; | ||
import io.github.albertus82.jface.preference.IPreferencesConfiguration; | ||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) | ||
public class LanguageConfigAccessor { | ||
|
||
public static final String DEFAULT_LANGUAGE = Locale.getDefault().getLanguage(); | ||
|
||
@NonNull | ||
private final IPreferencesConfiguration configuration; | ||
|
||
public @NonNull Language getLanguage() { | ||
return Language.fromString(configuration.getString(Preference.LANGUAGE, DEFAULT_LANGUAGE)).orElse(Language.ENGLISH); | ||
} | ||
|
||
} |
Oops, something went wrong.