Fixed a lot of logic, replaced Files with Documents
This commit is contained in:
@@ -6,4 +6,9 @@ public class ArbitraryFile extends Document{
|
||||
public ArbitraryFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return "ARBITRARY";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,9 @@ public class CodeFile extends Document{
|
||||
public CodeFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return "CODE";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,4 +56,20 @@ public class Document extends File implements IDocument {
|
||||
public FileType getFileType() {
|
||||
return fileType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getFilesizeKB() {
|
||||
try {
|
||||
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class);
|
||||
return fileAttributes.size() / 1024;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInfo() {
|
||||
return "Name: " + getName() + " Size: " + getFilesizeKB() + "Type: " + getFileType().getTypeName();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
package org.lumijiez.base;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class ImageFile extends Document{
|
||||
public class ImageFile extends Document {
|
||||
public ImageFile(Path path) {
|
||||
super(path);
|
||||
}
|
||||
@Override
|
||||
public String getInfo() {
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(this);
|
||||
if (image != null) {
|
||||
int width = image.getWidth();
|
||||
int height = image.getHeight();
|
||||
return "IMAGE " + width + "x" + height;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,18 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public enum FileType {
|
||||
IMAGE("Image"), PLAINTEXT("Plaintext"), FILE("File"), CODE("Code"), NONE("None");
|
||||
public List<String> typeExtensions;
|
||||
public final String typeName;
|
||||
IMAGE("Image", new ArrayList<>(List.of("jpg", "png"))),
|
||||
PLAINTEXT("Plaintext", new ArrayList<>(List.of("txt", "csv"))),
|
||||
FILE("File", new ArrayList<>(List.of("doc", "pdf", "zip"))),
|
||||
CODE("Code", new ArrayList<>(List.of("java", "cpp", "py"))),
|
||||
NONE("None", new ArrayList<>());
|
||||
|
||||
FileType(String typeName) {
|
||||
private final List<String> typeExtensions;
|
||||
private final String typeName;
|
||||
|
||||
FileType(String typeName, ArrayList<String> list) {
|
||||
this.typeName = typeName;
|
||||
this.typeExtensions = list;
|
||||
}
|
||||
|
||||
public static FileType getFileType(String extension) {
|
||||
@@ -21,10 +27,9 @@ public enum FileType {
|
||||
return NONE;
|
||||
}
|
||||
|
||||
static {
|
||||
IMAGE.typeExtensions = new ArrayList<>(List.of("jpg", "png"));
|
||||
PLAINTEXT.typeExtensions = new ArrayList<>(List.of("txt", "csv"));
|
||||
FILE.typeExtensions = new ArrayList<>(List.of("doc", "pdf", "zip"));
|
||||
CODE.typeExtensions = new ArrayList<>(List.of("java", "cpp", "py"));
|
||||
public String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.lumijiez.gui;
|
||||
import org.lumijiez.base.Document;
|
||||
import org.lumijiez.tracker.TrackerThread;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
|
||||
@@ -11,7 +11,7 @@ public class MainFrame extends JFrame {
|
||||
public static Path FOLDER_PATH;
|
||||
private final JScrollPane fileListScrollPane = new JScrollPane();
|
||||
private final JScrollPane fileInfoScrollPane = new JScrollPane();
|
||||
private final JList<String> fileList = new JList<>();
|
||||
private final JList<Document> fileList = new JList<>();
|
||||
private final JLabel pathLabel = new JLabel();
|
||||
private final JScrollPane mainScrollPane = new JScrollPane();
|
||||
private final JTextPane mainTextPane = new JTextPane();
|
||||
@@ -24,7 +24,7 @@ public class MainFrame extends JFrame {
|
||||
private final JMenuItem pickFolder = new JMenuItem();
|
||||
private final JMenu settingsMenu = new JMenu();
|
||||
private final JMenuItem settings = new JMenuItem();
|
||||
private final Map<File, byte[]> fileContents = new HashMap<>();
|
||||
private final Map<Document, byte[]> fileContents = new HashMap<>();
|
||||
private TrackerThread tracker;
|
||||
|
||||
public MainFrame() {
|
||||
@@ -43,7 +43,7 @@ public class MainFrame extends JFrame {
|
||||
int returnVal = folderChooser.showOpenDialog(this);
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath());
|
||||
tracker = new TrackerThread(mainTextPane, fileContents, fileList);
|
||||
tracker = new TrackerThread(mainTextPane, fileContents, fileList, fileInfoTextPane);
|
||||
tracker.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,10 @@ import org.lumijiez.enums.FileType;
|
||||
import java.util.Date;
|
||||
|
||||
public interface IDocument {
|
||||
public String getExtension();
|
||||
public Date getCreatedTime();
|
||||
public Date getModificationTime();
|
||||
public FileType getFileType();
|
||||
String getExtension();
|
||||
Date getCreatedTime();
|
||||
Date getModificationTime();
|
||||
FileType getFileType();
|
||||
long getFilesizeKB();
|
||||
String getInfo();
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package org.lumijiez.tracker;
|
||||
|
||||
import org.lumijiez.base.Document;
|
||||
import org.lumijiez.gui.MainFrame;
|
||||
import org.lumijiez.enums.DiffType;
|
||||
import org.lumijiez.util.FileDiffer;
|
||||
import org.lumijiez.enums.StateType;
|
||||
import org.lumijiez.util.FileFactory;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -13,37 +16,57 @@ import java.util.Map;
|
||||
|
||||
public class TrackerThread extends Thread {
|
||||
private final JTextPane textPane;
|
||||
private Map<File, byte[]> fileContents;
|
||||
private Map<Document, byte[]> fileContents;
|
||||
private final Map<File, StateType> fileStates = new HashMap<>();
|
||||
private final JList<String> fileList;
|
||||
private final JList<Document> fileList;
|
||||
private final JTextPane fileInfoTextPane;
|
||||
|
||||
public TrackerThread(JTextPane textPane, Map<File, byte[]> files, JList<String> fileList) {
|
||||
public TrackerThread(JTextPane textPane, Map<Document, byte[]> files, JList<Document> fileList, JTextPane fileInfoTextPane) {
|
||||
this.textPane = textPane;
|
||||
this.fileContents = files;
|
||||
this.fileList = fileList;
|
||||
this.fileInfoTextPane = fileInfoTextPane;
|
||||
init();
|
||||
}
|
||||
|
||||
public void init() {
|
||||
System.out.println("Init called");
|
||||
fileContents = FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH);
|
||||
ArrayList<File> fList = new ArrayList<>(fileContents.keySet());
|
||||
fileList.setModel(new AbstractListModel<>() {
|
||||
public int getSize() {
|
||||
return fList.size();
|
||||
DefaultListModel<Document> listModel = new DefaultListModel<>();
|
||||
|
||||
for (File file : fileContents.keySet()) {
|
||||
Document doc = FileFactory.getDocument(file.toPath());
|
||||
listModel.addElement(doc);
|
||||
}
|
||||
|
||||
fileList.setModel(listModel);
|
||||
fileList.setCellRenderer(new DefaultListCellRenderer() {
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||||
if (value instanceof Document) {
|
||||
value = ((Document) value).getName();
|
||||
}
|
||||
return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
}
|
||||
public String getElementAt(int i) {
|
||||
return fList.get(i).getName();
|
||||
});
|
||||
|
||||
fileList.addListSelectionListener(e -> {
|
||||
if (!e.getValueIsAdjusting()) {
|
||||
Document selectedDocument = fileList.getSelectedValue();
|
||||
if (selectedDocument != null) {
|
||||
fileInfoTextPane.setText(selectedDocument.getInfo());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public void reset() {
|
||||
fileStates.clear();
|
||||
}
|
||||
|
||||
public void checkDirectory() {
|
||||
Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH));
|
||||
Map<DiffType, ArrayList<Document>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH));
|
||||
|
||||
StringBuilder toShow = new StringBuilder();
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
import org.lumijiez.base.Document;
|
||||
import org.lumijiez.enums.DiffType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -13,13 +13,13 @@ import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class FileDiffer {
|
||||
public static Map<DiffType, ArrayList<File>> diff(Map<File, byte[]> oldFiles, Map<File, byte[]> newFiles) {
|
||||
Map<DiffType, ArrayList<File>> result = Map.of(
|
||||
public static Map<DiffType, ArrayList<Document>> diff(Map<Document, byte[]> oldFiles, Map<Document, byte[]> newFiles) {
|
||||
Map<DiffType, ArrayList<Document>> result = Map.of(
|
||||
DiffType.CREATE, new ArrayList<>(),
|
||||
DiffType.DELETE, new ArrayList<>(),
|
||||
DiffType.MODIFY, new ArrayList<>());
|
||||
|
||||
for (File file : oldFiles.keySet()) {
|
||||
for (Document file : oldFiles.keySet()) {
|
||||
if (newFiles.get(file) != null) {
|
||||
if (Arrays.compare(oldFiles.get(file), newFiles.get(file)) != 0) {
|
||||
result.get(DiffType.MODIFY).add(file);
|
||||
@@ -29,7 +29,7 @@ public class FileDiffer {
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : newFiles.keySet()) {
|
||||
for (Document file : newFiles.keySet()) {
|
||||
if (oldFiles.get(file) == null) {
|
||||
result.get(DiffType.CREATE).add(file);
|
||||
}
|
||||
@@ -38,13 +38,14 @@ public class FileDiffer {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Map<File, byte[]> crawlDirectory(Path path) {
|
||||
Map<File, byte[]> newFileContents = new HashMap<>();
|
||||
public static Map<Document, byte[]> crawlDirectory(Path path) {
|
||||
Map<Document, byte[]> newFileContents = new HashMap<>();
|
||||
try (Stream<Path> paths = Files.walk(path)) {
|
||||
paths.forEach(p -> {
|
||||
if (Files.isRegularFile(p)) {
|
||||
Document doc = new Document(p);
|
||||
if (Files.isRegularFile(doc.toPath())) {
|
||||
try {
|
||||
newFileContents.put(p.toFile(), Files.readAllBytes(p));
|
||||
newFileContents.put(doc, Files.readAllBytes(doc.toPath()));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
17
Lab3/src/main/java/org/lumijiez/util/FileFactory.java
Normal file
17
Lab3/src/main/java/org/lumijiez/util/FileFactory.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.lumijiez.util;
|
||||
|
||||
import org.lumijiez.base.*;
|
||||
import org.lumijiez.enums.FileType;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class FileFactory {
|
||||
public static Document getDocument(Path path) {
|
||||
Document doc = new Document(path);
|
||||
if (doc.getFileType() == FileType.PLAINTEXT) return new TextFile(path);
|
||||
if (doc.getFileType() == FileType.IMAGE) return new ImageFile(path);
|
||||
if (doc.getFileType() == FileType.FILE) return new ArbitraryFile(path);
|
||||
if (doc.getFileType() == FileType.CODE) return new CodeFile(path);
|
||||
return doc;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user