[IMPL] GUI

This commit is contained in:
RatzzFatzz
2019-11-29 14:04:40 +01:00
parent 3531299a23
commit 5c36511774
5 changed files with 151 additions and 9 deletions

13
src/main/java/GUI.form Normal file
View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI">
<grid id="27dc6" row-count="1" column-count="1" layout-manager="GridLayoutManager">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</form>

99
src/main/java/GUI.java Normal file
View File

@@ -0,0 +1,99 @@
import lombok.extern.log4j.Log4j2;
import model.FileAttribute;
import query.QueryBuilder;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
@Log4j2
public class GUI {
private String path;
private JButton openFileBrowser;
private JButton startOperation;
private JButton openProperties;
private JTextPane outputArea;
public GUI() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("MKV Audio and Subtitle Changer");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel top = new JPanel(new GridLayout(1, 3, 20, 20));
outputArea = new JTextPane();
outputArea.setEditable(false);
openFileBrowser = new JButton("Browse directory");
openFileBrowser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
try{
if(! readFile("lastDir", Charset.defaultCharset()).isEmpty()){
String temp = readFile("dir.txt", Charset.defaultCharset());
fileChooser.setCurrentDirectory(new File(temp));
}
}catch(IOException ie){
log.info("Couldn't start FileChooser with default path");
}
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setPreferredSize(new Dimension(600, 500));
Action details = fileChooser.getActionMap().get("viewTypeDetails");
details.actionPerformed(null);
fileChooser.showOpenDialog(null);
try{
if(FileAttribute.pathIsValid(fileChooser.getSelectedFile().getAbsolutePath())){
path = fileChooser.getSelectedFile().getAbsolutePath();
try(PrintWriter out = new PrintWriter("lastDir", "UTF-8")){
out.print(fileChooser.getCurrentDirectory());
}catch(FileNotFoundException | UnsupportedEncodingException fne){
log.error(fne);
}
startOperation.setEnabled(true);
}
}catch(NullPointerException ne){
outputArea.setText("File or directory not found!\n" + (outputArea.getText() == null ? "" : outputArea.getText()));
log.error("File or directory not found!", ne);
}
}
});
openProperties = new JButton("Open properties");
openProperties.setEnabled(false);
startOperation = new JButton("Start updating");
startOperation.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
QueryBuilder queryBuilder = new QueryBuilder();
if(queryBuilder.executeUpdateOnAllFiles(path, outputArea)){
outputArea.setText("All files updated!\n" + (outputArea.getText() == null ? "" : outputArea.getText()));
}
}
});
startOperation.setEnabled(false);
top.add(openFileBrowser);
top.add(startOperation);
top.add(openProperties);
frame.add(top, BorderLayout.NORTH);
frame.add(outputArea);
frame.setVisible(true);
}
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}

View File

@@ -1,8 +1,5 @@
import config.MKVToolProperties; import config.MKVToolProperties;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import query.QueryBuilder;
import java.util.Scanner;
@Log4j2 @Log4j2
public class Main { public class Main {
@@ -10,11 +7,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
MKVToolProperties.getInstance().defineMKVToolNixPath(); MKVToolProperties.getInstance().defineMKVToolNixPath();
Scanner input = new Scanner(System.in); GUI gui = new GUI();
log.info("Please enter path to file");
String path = input.nextLine();
QueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.executeUpdateOnAllFiles(path);
} }

View File

@@ -1,7 +1,18 @@
package model; package model;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Log4j2
@Getter @Getter
public class FileAttribute { public class FileAttribute {
private int id; private int id;
@@ -19,4 +30,28 @@ public class FileAttribute {
this.forcedTrack = forcedTrack; this.forcedTrack = forcedTrack;
this.type = type; this.type = type;
} }
public static boolean pathIsValid(String path) {
File file = new File(path);
if(file.isFile()){
return file.getAbsolutePath().endsWith(".mkv");
}
if(file.isDirectory()){
try(Stream<Path> paths = Files.walk(Paths.get(path))){
List<String> allPaths = paths
.filter(Files::isRegularFile)
.map(f -> f.toAbsolutePath().toString())
.collect(Collectors.toList());
for(String filePath : allPaths){
if(! filePath.endsWith(".mkv")){
return false;
}
}
}catch(IOException e){
log.error("Couldn't find file or directory!", e);
}
return true;
}
return false;
}
} }

View File

@@ -5,6 +5,7 @@ import config.MKVToolProperties;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import model.FileAttribute; import model.FileAttribute;
import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
@@ -23,7 +24,7 @@ public class QueryBuilder {
public QueryBuilder() { public QueryBuilder() {
} }
public boolean executeUpdateOnAllFiles(String path) { public boolean executeUpdateOnAllFiles(String path, JTextPane outputArea) {
List<String> allFilePaths = getAllFilesFromDirectory(path); List<String> allFilePaths = getAllFilesFromDirectory(path);
if(allFilePaths == null){ if(allFilePaths == null){
log.error("Couldn't process path!"); log.error("Couldn't process path!");
@@ -31,6 +32,7 @@ public class QueryBuilder {
} }
for(String filePath : allFilePaths){ for(String filePath : allFilePaths){
updateAttributes(filePath, queryAttributes(filePath)); updateAttributes(filePath, queryAttributes(filePath));
outputArea.setText("Success: " + filePath + "\n" + outputArea.getPage());
} }
return true; return true;
} }