implement json configuration

This commit is contained in:
mykola2312 2024-04-08 12:02:44 +03:00
parent 41ebc47800
commit 184a75752f
5 changed files with 77 additions and 6 deletions

3
.gitignore vendored
View file

@ -38,3 +38,6 @@ build/
.DS_Store
.idea/
# mptv
config.json

10
pom.xml
View file

@ -20,6 +20,16 @@
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
<build>

View file

@ -0,0 +1,22 @@
package com.mykola2312.mptv;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.File;
import java.io.IOException;
public class Config {
public class Frame {
public short width;
public short height;
public boolean fullscreen;
}
public Frame frame;
public static Config loadConfig(String path) throws IOException {
return new ObjectMapper().readerFor(Config.class).readValue(new File(path));
}
}

View file

@ -1,10 +1,39 @@
package com.mykola2312.mptv;
import com.mykola2312.mptv.ui.MainFrame;
import org.apache.commons.cli.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
final Options options = new Options();
options.addOption(Option
.builder("c")
.longOpt("config")
.required(false)
.hasArg()
.build());
CommandLine cmd;
try {
cmd = new DefaultParser().parse(options, args);
} catch (ParseException e) {
System.err.println(e.toString());
System.exit(1);
return;
}
final String configPath = cmd.hasOption('c') ? cmd.getOptionValue('c') : "config.json";
Config config;
try {
config = Config.loadConfig(configPath);
} catch (IOException e) {
System.err.printf("failed to read config: %s\n", e.toString());
System.exit(1);
return;
}
MainFrame frame = new MainFrame();
frame.create();
frame.create(config.frame.width, config.frame.height, config.frame.fullscreen);
}
}

View file

@ -3,16 +3,23 @@ package com.mykola2312.mptv.ui;
import com.mykola2312.mptv.I18n;
import javax.swing.*;
import java.awt.*;
public class MainFrame {
private JFrame frame;
public void create() {
public void create(short width, short height, boolean fullscreen) {
this.frame = new JFrame(I18n.get("MainFrame_Title"));
if (fullscreen) {
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} else {
Dimension size = new Dimension(width, height);
frame.setPreferredSize(size);
frame.setSize(size);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}