Lab3 #2

Merged
bytegrip merged 6 commits from lab3 into master 2023-10-17 21:36:00 +00:00
10 changed files with 156 additions and 57 deletions
Showing only changes of commit 0ab6addf63 - Show all commits

View File

@@ -9,6 +9,12 @@ public class ArbitraryFile extends Document{
@Override @Override
public String getInfo() { 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; package org.lumijiez.base;
import org.lumijiez.util.Utils;
import java.nio.file.Path; import java.nio.file.Path;
public class CodeFile extends Document{ public class CodeFile extends Document{
@@ -9,6 +11,13 @@ public class CodeFile extends Document{
@Override @Override
public String getInfo() { 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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
public class Document extends File implements IDocument { public class Document extends File implements IDocument {
@@ -31,10 +32,18 @@ public class Document extends File implements IDocument {
} }
@Override @Override
public Date getCreatedTime() { public String getCreatedTime() {
try { try {
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -42,10 +51,18 @@ public class Document extends File implements IDocument {
} }
@Override @Override
public Date getModificationTime() { public String getModificationTime() {
try { try {
BasicFileAttributes fileAttributes = Files.readAttributes(Path.of(this.getPath()), BasicFileAttributes.class); 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) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

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

View File

@@ -1,9 +1,25 @@
package org.lumijiez.base; package org.lumijiez.base;
import org.lumijiez.util.Utils;
import java.nio.file.Path; import java.nio.file.Path;
public class TextFile extends Document { public class TextFile extends Document {
public TextFile(Path path) { public TextFile(Path path) {
super(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; public static Path FOLDER_PATH;
private final JScrollPane fileListScrollPane = new JScrollPane(); private final JScrollPane fileListScrollPane = new JScrollPane();
private final JScrollPane fileInfoScrollPane = new JScrollPane(); private final JScrollPane fileInfoScrollPane = new JScrollPane();
private final JList<Document> fileList = new JList<>();
private final JLabel pathLabel = new JLabel(); private final JLabel pathLabel = new JLabel();
private final JScrollPane mainScrollPane = new JScrollPane(); private final JScrollPane mainScrollPane = new JScrollPane();
private final JTextPane mainTextPane = new JTextPane(); private final JTextPane mainTextPane = new JTextPane();
@@ -19,11 +18,12 @@ public class MainFrame extends JFrame {
private final JLabel snapshotLabel = new JLabel(); private final JLabel snapshotLabel = new JLabel();
private final JButton CommitButton = new JButton(); private final JButton CommitButton = new JButton();
private final JButton StatusButton = 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 JMenu fileMenu = new JMenu();
private final JMenuItem pickFolder = new JMenuItem(); private final JMenuItem pickFolder = new JMenuItem();
private final JMenu settingsMenu = new JMenu(); private final JMenu settingsMenu = new JMenu();
private final JMenuItem settings = new JMenuItem(); private final JMenuItem settings = new JMenuItem();
private final JList<Document> fileList = new JList<>();
private final Map<Document, byte[]> fileContents = new HashMap<>(); private final Map<Document, byte[]> fileContents = new HashMap<>();
private TrackerThread tracker; private TrackerThread tracker;
@@ -33,53 +33,49 @@ public class MainFrame extends JFrame {
private void initComponents() { private void initComponents() {
setResizable(false);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainTextPane.setContentType("text/html"); mainTextPane.setContentType("text/html");
fileInfoTextPane.setContentType("text/html");
mainTextPane.setEditable(false);
fileInfoTextPane.setEditable(false);
JFileChooser folderChooser = new JFileChooser(); JFileChooser folderChooser = new JFileChooser();
folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); if (folderChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
int returnVal = folderChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath()); FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath());
tracker = new TrackerThread(mainTextPane, fileContents, fileList, fileInfoTextPane); tracker = new TrackerThread(mainTextPane, fileContents, fileList, fileInfoTextPane);
tracker.start(); 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); fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileListScrollPane.setViewportView(fileList); fileListScrollPane.setViewportView(fileList);
snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
snapshotLabel.setText("Last snapshot: " + new Date());
mainScrollPane.setViewportView(mainTextPane); mainScrollPane.setViewportView(mainTextPane);
fileInfoScrollPane.setViewportView(fileInfoTextPane); 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); CommitButton.addActionListener(this::CommitButtonActionPerformed);
StatusButton.setText("Status");
StatusButton.addActionListener(this::StatusButtonActionPerformed);
fileMenu.setText("File");
pickFolder.setText("Pick another folder");
fileMenu.add(pickFolder); fileMenu.add(pickFolder);
MainMenubar.add(fileMenu);
settingsMenu.setText("Edit");
settings.setText("Settings");
settingsMenu.add(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()); GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout); getContentPane().setLayout(layout);
@@ -98,8 +94,7 @@ public class MainFrame extends JFrame {
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(CommitButton, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) .addComponent(CommitButton, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addComponent(fileInfoScrollPane)))) .addComponent(fileInfoScrollPane))))
.addContainerGap()) .addContainerGap()));
);
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING) layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
@@ -123,7 +118,4 @@ public class MainFrame extends JFrame {
mainTextPane.setText(""); mainTextPane.setText("");
snapshotLabel.setText("Last snapshot: " + new Date()); 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 org.lumijiez.enums.FileType;
import java.util.Date;
public interface IDocument { public interface IDocument {
String getExtension(); String getExtension();
Date getCreatedTime(); String getCreatedTime();
Date getModificationTime(); String getModificationTime();
FileType getFileType(); FileType getFileType();
long getFilesizeKB(); long getFilesizeKB();
String getInfo(); String getInfo();

View File

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

View File

@@ -1,17 +1,26 @@
package org.lumijiez.util; package org.lumijiez.util;
import org.lumijiez.base.*; import org.lumijiez.base.*;
import org.lumijiez.enums.FileType;
import java.nio.file.Path; import java.nio.file.Path;
public class FileFactory { public class FileFactory {
public static Document getDocument(Path path) { public static Document getDocument(Path path) {
Document doc = new Document(path); Document doc = new Document(path);
if (doc.getFileType() == FileType.PLAINTEXT) return new TextFile(path); switch (doc.getFileType()) {
if (doc.getFileType() == FileType.IMAGE) return new ImageFile(path); case PLAINTEXT -> {
if (doc.getFileType() == FileType.FILE) return new ArbitraryFile(path); return new TextFile(path);
if (doc.getFileType() == FileType.CODE) return new CodeFile(path); }
case IMAGE -> {
return new ImageFile(path);
}
case FILE -> {
return new ArbitraryFile(path);
}
case CODE -> {
return new CodeFile(path);
}
}
return doc; 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);
}
}
}