Implemented information for all files, some parts WIP

This commit is contained in:
2023-10-18 00:02:06 +03:00
parent 15887bd1b6
commit 0ab6addf63
10 changed files with 156 additions and 57 deletions

View File

@@ -9,6 +9,12 @@ public class ArbitraryFile extends Document{
@Override
public String getInfo() {
return "ARBITRARY";
StringBuilder info = new StringBuilder();
info.append("Type: ").append(getFileType().getTypeName()).append("<br>");
info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>");
info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>");
info.append("Created at: ").append(getCreatedTime()).append("<br>");
info.append("Modified at: ").append(getModificationTime()).append("<br>");
return info.toString();
}
}

View File

@@ -1,5 +1,7 @@
package org.lumijiez.base;
import org.lumijiez.util.Utils;
import java.nio.file.Path;
public class CodeFile extends Document{
@@ -9,6 +11,13 @@ public class CodeFile extends Document{
@Override
public String getInfo() {
return "CODE";
StringBuilder info = new StringBuilder();
info.append("Type: ").append(getFileType().getTypeName()).append("<br>");
info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>");
info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>");
info.append("Lines: ").append(Utils.countLines(this)).append("<br>");
info.append("Created at: ").append(getCreatedTime()).append("<br>");
info.append("Modified at: ").append(getModificationTime()).append("<br>");
return info.toString();
}
}

View File

@@ -8,6 +8,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Document extends File implements IDocument {
@@ -31,10 +32,18 @@ public class Document extends File implements IDocument {
}
@Override
public Date getCreatedTime() {
public String getCreatedTime() {
try {
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class);
return new Date(fileAttributes.creationTime().toMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
Date date = new Date(fileAttributes.creationTime().toMillis());
String formattedDate = dateFormat.format(date);
String formattedTime = timeFormat.format(date);
return formattedTime + " " + formattedDate;
} catch (IOException e) {
e.printStackTrace();
}
@@ -42,10 +51,18 @@ public class Document extends File implements IDocument {
}
@Override
public Date getModificationTime() {
public String getModificationTime() {
try {
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class);
return new Date(fileAttributes.lastAccessTime().toMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yy");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
Date date = new Date(fileAttributes.lastModifiedTime().toMillis());
String formattedDate = dateFormat.format(date);
String formattedTime = timeFormat.format(date);
return formattedTime + " " + formattedDate;
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -12,12 +12,15 @@ public class ImageFile extends Document {
@Override
public String getInfo() {
try {
StringBuilder info = new StringBuilder();
info.append("Type: ").append(getFileType().getTypeName()).append("<br>");
info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>");
BufferedImage image = ImageIO.read(this);
if (image != null) {
int width = image.getWidth();
int height = image.getHeight();
return "IMAGE " + width + "x" + height;
}
info.append("Dimensions: ").append(image.getWidth()).append("x").append(image.getHeight()).append("<br>");
info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>");
info.append("Created at: ").append(getCreatedTime()).append("<br>");
info.append("Modified at: ").append(getModificationTime()).append("<br>");
return info.toString();
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -1,9 +1,25 @@
package org.lumijiez.base;
import org.lumijiez.util.Utils;
import java.nio.file.Path;
public class TextFile extends Document {
public TextFile(Path path) {
super(path);
}
@Override
public String getInfo() {
StringBuilder info = new StringBuilder();
info.append("Type: ").append(getFileType().getTypeName()).append("<br>");
info.append("Extension: ").append(getExtension().toUpperCase()).append("<br>");
info.append("File size: ").append(getFilesizeKB()).append(" KB").append("<br>");
info.append("Words: ").append(Utils.countWords(this)).append("<br>");
info.append("Lines: ").append(Utils.countLines(this)).append("<br>");
info.append("Characters: ").append(Utils.countChars(this)).append("<br>");
info.append("Created at: ").append(getCreatedTime()).append("<br>");
info.append("Modified at: ").append(getModificationTime()).append("<br>");
return info.toString();
}
}

View File

@@ -11,7 +11,6 @@ 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<Document> fileList = new JList<>();
private final JLabel pathLabel = new JLabel();
private final JScrollPane mainScrollPane = new JScrollPane();
private final JTextPane mainTextPane = new JTextPane();
@@ -19,11 +18,12 @@ public class MainFrame extends JFrame {
private final JLabel snapshotLabel = new JLabel();
private final JButton CommitButton = new JButton();
private final JButton StatusButton = new JButton();
private final JMenuBar MainMenubar = new JMenuBar();
private final JMenuBar mainMenubar = new JMenuBar();
private final JMenu fileMenu = new JMenu();
private final JMenuItem pickFolder = new JMenuItem();
private final JMenu settingsMenu = new JMenu();
private final JMenuItem settings = new JMenuItem();
private final JList<Document> fileList = new JList<>();
private final Map<Document, byte[]> fileContents = new HashMap<>();
private TrackerThread tracker;
@@ -33,53 +33,49 @@ public class MainFrame extends JFrame {
private void initComponents() {
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainTextPane.setContentType("text/html");
fileInfoTextPane.setContentType("text/html");
mainTextPane.setEditable(false);
fileInfoTextPane.setEditable(false);
JFileChooser folderChooser = new JFileChooser();
folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
int returnVal = folderChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (folderChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath());
tracker = new TrackerThread(mainTextPane, fileContents, fileList, fileInfoTextPane);
tracker.start();
}
snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileListScrollPane.setViewportView(fileList);
snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
snapshotLabel.setText("Last snapshot: " + new Date());
mainScrollPane.setViewportView(mainTextPane);
fileInfoScrollPane.setViewportView(fileInfoTextPane);
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
pathLabel.setText("Currently tracking: " + FOLDER_PATH.toString());
CommitButton.setText("Commit");
CommitButton.addActionListener(this::CommitButtonActionPerformed);
StatusButton.setText("Status");
StatusButton.addActionListener(this::StatusButtonActionPerformed);
fileMenu.setText("File");
pickFolder.setText("Pick another folder");
fileMenu.add(pickFolder);
MainMenubar.add(fileMenu);
settingsMenu.setText("Edit");
settings.setText("Settings");
settingsMenu.add(settings);
mainMenubar.add(fileMenu);
mainMenubar.add(settingsMenu);
MainMenubar.add(settingsMenu);
StatusButton.setText("Status");
pathLabel.setText("Currently tracking: " + FOLDER_PATH.toString());
CommitButton.setText("Commit");
snapshotLabel.setText("Last snapshot: " + new Date());
fileMenu.setText("File");
pickFolder.setText("Pick another folder");
settingsMenu.setText("Edit");
settings.setText("Settings");
setJMenuBar(MainMenubar);
setJMenuBar(mainMenubar);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
@@ -98,8 +94,7 @@ public class MainFrame extends JFrame {
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(CommitButton, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addComponent(fileInfoScrollPane))))
.addContainerGap())
);
.addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
@@ -123,7 +118,4 @@ public class MainFrame extends JFrame {
mainTextPane.setText("");
snapshotLabel.setText("Last snapshot: " + new Date());
}
private void StatusButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
}

View File

@@ -2,12 +2,10 @@ package org.lumijiez.interfaces;
import org.lumijiez.enums.FileType;
import java.util.Date;
public interface IDocument {
String getExtension();
Date getCreatedTime();
Date getModificationTime();
String getCreatedTime();
String getModificationTime();
FileType getFileType();
long getFilesizeKB();
String getInfo();

View File

@@ -16,10 +16,10 @@ import java.util.Map;
public class TrackerThread extends Thread {
private final JTextPane textPane;
private final JTextPane fileInfoTextPane;
private final JList<Document> fileList;
private Map<Document, byte[]> fileContents;
private final Map<File, StateType> fileStates = new HashMap<>();
private final JList<Document> fileList;
private final JTextPane fileInfoTextPane;
public TrackerThread(JTextPane textPane, Map<Document, byte[]> files, JList<Document> fileList, JTextPane fileInfoTextPane) {
this.textPane = textPane;
@@ -60,7 +60,6 @@ public class TrackerThread extends Thread {
});
}
public void reset() {
fileStates.clear();
}
@@ -96,7 +95,6 @@ public class TrackerThread extends Thread {
toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getAction()).append("</span><br>");
}
}
// System.out.println(toShow.toString());
textPane.setText(toShow.toString());
}
}

View File

@@ -1,17 +1,26 @@
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);
switch (doc.getFileType()) {
case PLAINTEXT -> {
return new TextFile(path);
}
case IMAGE -> {
return new ImageFile(path);
}
case FILE -> {
return new ArbitraryFile(path);
}
case CODE -> {
return new CodeFile(path);
}
}
return doc;
}
}

View File

@@ -0,0 +1,51 @@
package org.lumijiez.util;
import org.lumijiez.base.Document;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Utils {
public static int countWords(Document doc) {
try {
BufferedReader reader = new BufferedReader(new FileReader(doc));
int wordCount = 0;
String line;
while ((line = reader.readLine()) != null) {
String[] words = line.split("\\s+");
wordCount += words.length;
}
reader.close();
return wordCount;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static int countLines(Document doc) {
try {
BufferedReader reader = new BufferedReader(new FileReader(doc));
int lineCount = 0;
while (reader.readLine() != null) lineCount++;
reader.close();
return lineCount;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static long countChars(Document doc) {
try {
BufferedReader reader = new BufferedReader(new FileReader(doc));
long characterCount = 0;
while (reader.read() != -1) characterCount++;
reader.close();
return characterCount;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}