-
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.
CustomDataDirectory: introduce new setting
For some users, it might be useful to change the data dir location. This is especially useful on platforms which don't support XDG_DATA_HOME environment variable. Closes: #2
- Loading branch information
Showing
8 changed files
with
251 additions
and
17 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
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
33 changes: 33 additions & 0 deletions
33
src/main/java/dev/andrybak/resoday/gui/settings/CustomDataDirectoryDialog.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,33 @@ | ||
package dev.andrybak.resoday.gui.settings; | ||
|
||
import javax.swing.JFileChooser; | ||
import javax.swing.JFrame; | ||
import javax.swing.WindowConstants; | ||
import java.awt.Component; | ||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
|
||
public class CustomDataDirectoryDialog { | ||
public static void main(String[] args) { | ||
JFrame jFrame = new JFrame("Dir chooser demo"); | ||
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | ||
jFrame.setVisible(true); | ||
Optional<Path> maybePath = show(jFrame); | ||
System.out.println(maybePath.map(p -> "Got new path: " + p).orElse("Got no path.")); | ||
} | ||
|
||
public static Optional<Path> show(Component owner) { | ||
JFileChooser dirChooser = new JFileChooser(Paths.get(".").toFile()); | ||
dirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | ||
int selectedOption = dirChooser.showDialog(owner, "Select"); | ||
if (selectedOption == JFileChooser.APPROVE_OPTION) { | ||
File d = dirChooser.getSelectedFile(); | ||
System.out.println("Chosen: " + d); | ||
return Optional.of(d.toPath()); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
src/main/java/dev/andrybak/resoday/settings/storage/CustomDataDirectory.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,43 @@ | ||
package dev.andrybak.resoday.settings.storage; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class CustomDataDirectory { | ||
private static final Path CUSTOM_DATA_DIR_FILE = Paths.get("custom-data-directory.txt"); | ||
|
||
private CustomDataDirectory() { | ||
throw new AssertionError(); | ||
} | ||
|
||
public static Optional<Path> from(Path configDir) { | ||
Path maybeFile = configDir.resolve(CUSTOM_DATA_DIR_FILE); | ||
if (Files.isRegularFile(maybeFile) && Files.isReadable(maybeFile)) { | ||
try { | ||
List<String> strings = Files.readAllLines(maybeFile); | ||
if (strings.isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
Path customDataDir = Path.of(strings.get(0)); | ||
return Optional.of(customDataDir); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Could not read " + maybeFile, e); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static void save(Path configDir, Path customDataDir) { | ||
Path f = configDir.resolve(CUSTOM_DATA_DIR_FILE); | ||
try { | ||
Files.writeString(f, customDataDir.toAbsolutePath().toString()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
} |
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