Lab3 #2

Merged
bytegrip merged 6 commits from lab3 into master 2023-10-17 21:36:00 +00:00
10 changed files with 135 additions and 88 deletions
Showing only changes of commit 79a11cf6cb - Show all commits

View File

@@ -3,16 +3,9 @@ package org.lumijiez;
import org.lumijiez.gui.MainFrame; import org.lumijiez.gui.MainFrame;
import javax.swing.*; import javax.swing.*;
import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
try {
new MainFrame().setVisible(true);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} }
} }

View File

@@ -0,0 +1,33 @@
package org.lumijiez.base;
import org.lumijiez.enums.FileType;
import org.lumijiez.interfaces.IDocument;
import java.util.Date;
public class Document implements IDocument {
private String fullFilename = "";
private FileType fileType;
private Date createdAt;
private Date modifiedAt;
@Override
public String getFilename() {
return null;
}
@Override
public String getExtension() {
return null;
}
@Override
public Date getCreatedTime() {
return null;
}
@Override
public Date getModificationTime() {
return null;
}
}

View File

@@ -0,0 +1,15 @@
package org.lumijiez.enums;
public enum DiffType {
CREATE(StateType.NEW), DELETE(StateType.DELETED), MODIFY(StateType.MODIFIED), NONE(StateType.NONE);
private final StateType type;
DiffType(StateType type) {
this.type = type;
}
public StateType getState() {
return type;
}
}

View File

@@ -0,0 +1,16 @@
package org.lumijiez.enums;
import java.util.ArrayList;
import java.util.List;
public enum FileType {
IMAGE, PLAINTEXT, FILE, CODE;
public List<String> typeExtensions;
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"));
}
}

View File

@@ -1,14 +1,14 @@
package org.lumijiez.util; package org.lumijiez.enums;
public enum StateType { public enum StateType {
NEW("Created"), MODIFIED("Modified"), DELETED("Deleted"), NONE("Nothing"); NEW("Created"), MODIFIED("Modified"), DELETED("Deleted"), NONE("Nothing");
private final String name; private final String action;
StateType(String name) { StateType(String name) {
this.name = name; this.action = name;
} }
public String getName() { public String getAction() {
return this.name; return this.action;
} }
} }

View File

@@ -4,16 +4,18 @@ import org.lumijiez.tracker.TrackerThread;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.*; import java.util.*;
public class MainFrame extends JFrame { public class MainFrame extends JFrame {
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 JList<String> fileList = new JList<>(); private final JList<String> 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();
private final JTextPane fileInfoTextPane = new JTextPane();
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();
@@ -25,7 +27,7 @@ public class MainFrame extends JFrame {
private final Map<File, byte[]> fileContents = new HashMap<>(); private final Map<File, byte[]> fileContents = new HashMap<>();
private TrackerThread tracker; private TrackerThread tracker;
public MainFrame() throws IOException { public MainFrame() {
initComponents(); initComponents();
} }
@@ -40,20 +42,22 @@ public class MainFrame extends JFrame {
int returnVal = folderChooser.showOpenDialog(this); int returnVal = folderChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
tracker = new TrackerThread(mainTextPane, Path.of(folderChooser.getSelectedFile().getAbsolutePath()), fileContents, fileList); FOLDER_PATH = Path.of(folderChooser.getSelectedFile().getAbsolutePath());
tracker = new TrackerThread(mainTextPane, fileContents, fileList);
tracker.start(); tracker.start();
} }
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)); // NOI18N snapshotLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
snapshotLabel.setText("Last snapshot: " + new Date()); snapshotLabel.setText("Last snapshot: " + new Date());
mainScrollPane.setViewportView(mainTextPane); mainScrollPane.setViewportView(mainTextPane);
fileInfoScrollPane.setViewportView(fileInfoTextPane);
pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18)); // NOI18N pathLabel.setFont(new java.awt.Font("Trebuchet MS", Font.PLAIN, 18));
pathLabel.setText("Currently tracking: " + folderChooser.getSelectedFile().getAbsolutePath()); pathLabel.setText("Currently tracking: " + FOLDER_PATH.toString());
CommitButton.setText("Commit"); CommitButton.setText("Commit");
CommitButton.addActionListener(this::CommitButtonActionPerformed); CommitButton.addActionListener(this::CommitButtonActionPerformed);
@@ -84,35 +88,32 @@ public class MainFrame extends JFrame {
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addContainerGap() .addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(pathLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(snapshotLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(snapshotLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(pathLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(fileListScrollPane, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE) .addComponent(fileListScrollPane, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(mainScrollPane, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE) .addComponent(mainScrollPane, GroupLayout.PREFERRED_SIZE, 599, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addComponent(CommitButton, GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addComponent(CommitButton) .addComponent(fileInfoScrollPane))))
.addGap(0, 1, Short.MAX_VALUE))
.addComponent(StatusButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.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()
.addComponent(snapshotLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE) .addComponent(snapshotLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pathLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE) .addComponent(pathLabel, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false) .addComponent(fileListScrollPane, GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
.addComponent(fileListScrollPane, GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE) .addComponent(mainScrollPane)
.addComponent(mainScrollPane))
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(CommitButton) .addComponent(CommitButton)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(StatusButton))) .addComponent(fileInfoScrollPane)))
.addContainerGap())); .addContainerGap()));
pack(); pack();
} }

View File

@@ -0,0 +1,10 @@
package org.lumijiez.interfaces;
import java.util.Date;
public interface IDocument {
public String getFilename();
public String getExtension();
public Date getCreatedTime();
public Date getModificationTime();
}

View File

@@ -1,26 +1,24 @@
package org.lumijiez.tracker; package org.lumijiez.tracker;
import org.lumijiez.util.DiffType; import org.lumijiez.gui.MainFrame;
import org.lumijiez.enums.DiffType;
import org.lumijiez.util.FileDiffer; import org.lumijiez.util.FileDiffer;
import org.lumijiez.util.StateType; import org.lumijiez.enums.StateType;
import javax.swing.*; import javax.swing.*;
import java.io.File; import java.io.File;
import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class TrackerThread extends Thread { public class TrackerThread extends Thread {
private final JTextPane textPane; private final JTextPane textPane;
private final Path path;
private Map<File, byte[]> fileContents; private Map<File, byte[]> fileContents;
private final JList<String> fileList;
private final Map<File, StateType> fileStates = new HashMap<>(); private final Map<File, StateType> fileStates = new HashMap<>();
private final JList<String> fileList;
public TrackerThread(JTextPane textPane, Path path, Map<File, byte[]> files, JList<String> fileList) { public TrackerThread(JTextPane textPane, Map<File, byte[]> files, JList<String> fileList) {
this.textPane = textPane; this.textPane = textPane;
this.path = path;
this.fileContents = files; this.fileContents = files;
this.fileList = fileList; this.fileList = fileList;
init(); init();
@@ -28,19 +26,14 @@ public class TrackerThread extends Thread {
public void init() { public void init() {
System.out.println("Init called"); System.out.println("Init called");
fileContents = FileDiffer.crawlDirectory(path); fileContents = FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH);
ArrayList<File> fList = new ArrayList<>(fileContents.keySet());
ArrayList<String> fileNames = new ArrayList<>();
for (File file : fileContents.keySet()) {
fileNames.add(file.getName());
}
fileList.setModel(new AbstractListModel<>() { fileList.setModel(new AbstractListModel<>() {
public int getSize() { public int getSize() {
return fileNames.size(); return fList.size();
} }
public String getElementAt(int i) { public String getElementAt(int i) {
return fileNames.get(i); return fList.get(i).getName();
} }
}); });
} }
@@ -50,42 +43,37 @@ public class TrackerThread extends Thread {
} }
public void checkDirectory() { public void checkDirectory() {
Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(path)); Map<DiffType, ArrayList<File>> result = FileDiffer.diff(fileContents, FileDiffer.crawlDirectory(MainFrame.FOLDER_PATH));
StringBuilder toShow = new StringBuilder(); StringBuilder toShow = new StringBuilder();
boolean created = false, deleted = false, modified = false; boolean somethingNew = false;
if (!result.get(DiffType.CREATE).isEmpty()) {
created = true; for (DiffType type : result.keySet()) {
for (File file : result.get(DiffType.CREATE)) { for (File file : result.get(type)) {
fileStates.put(file, StateType.NEW); somethingNew = true;
System.out.println("File changed " + file.getName() + " " + type.getState().getAction());
fileStates.put(file, type.getState());
} }
System.out.println("Created");
} }
if (!result.get(DiffType.DELETE).isEmpty()) { if (somethingNew) {
deleted = true;
for (File file : result.get(DiffType.DELETE)) {
fileStates.put(file, StateType.DELETED);
}
System.out.println("Deleted");
}
if (!result.get(DiffType.MODIFY).isEmpty()) {
modified = true;
for (File file : result.get(DiffType.MODIFY)) {
fileStates.put(file, StateType.MODIFIED);
}
System.out.println("Modified");
}
if (created || deleted || modified) {
init(); init();
for (File file : fileStates.keySet()) { for (File file : fileStates.keySet()) {
if (fileStates.get(file) != StateType.NONE) { if (fileStates.get(file) != StateType.NONE) {
toShow.append(file.getName()).append(" has been ").append(fileStates.get(file).getName()).append("<br>"); if (fileStates.get(file) == StateType.NEW) {
toShow.append("<span color=\"green\">");
}
if (fileStates.get(file) == StateType.DELETED) {
toShow.append("<span color=\"red\">");
}
if (fileStates.get(file) == StateType.MODIFIED) {
toShow.append("<span color=\"orange\">");
}
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());
} }
} }
@@ -94,7 +82,6 @@ public class TrackerThread extends Thread {
public void run() { public void run() {
while(this.isAlive()) { while(this.isAlive()) {
checkDirectory(); checkDirectory();
//Thread.sleep(200);
} }
} }
} }

View File

@@ -1,5 +0,0 @@
package org.lumijiez.util;
public enum DiffType {
CREATE, DELETE, MODIFY, NONE
}

View File

@@ -1,5 +1,7 @@
package org.lumijiez.util; package org.lumijiez.util;
import org.lumijiez.enums.DiffType;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
@@ -12,32 +14,27 @@ import java.util.stream.Stream;
public class FileDiffer { public class FileDiffer {
public static Map<DiffType, ArrayList<File>> diff(Map<File, byte[]> oldFiles, Map<File, byte[]> newFiles) { public static Map<DiffType, ArrayList<File>> diff(Map<File, byte[]> oldFiles, Map<File, byte[]> newFiles) {
Map<DiffType, ArrayList<File>> result = new HashMap<>(); Map<DiffType, ArrayList<File>> result = Map.of(
DiffType.CREATE, new ArrayList<>(),
ArrayList<File> newFileList = new ArrayList<>(); DiffType.DELETE, new ArrayList<>(),
ArrayList<File> deletedFileList = new ArrayList<>(); DiffType.MODIFY, new ArrayList<>());
ArrayList<File> modifiedFileList = new ArrayList<>();
for (File file : oldFiles.keySet()) { for (File file : oldFiles.keySet()) {
if (newFiles.get(file) != null) { if (newFiles.get(file) != null) {
if (Arrays.compare(oldFiles.get(file), newFiles.get(file)) != 0) { if (Arrays.compare(oldFiles.get(file), newFiles.get(file)) != 0) {
modifiedFileList.add(file); result.get(DiffType.MODIFY).add(file);
} }
} else { } else {
deletedFileList.add(file); result.get(DiffType.DELETE).add(file);
} }
} }
for (File file : newFiles.keySet()) { for (File file : newFiles.keySet()) {
if (oldFiles.get(file) == null) { if (oldFiles.get(file) == null) {
newFileList.add(file); result.get(DiffType.CREATE).add(file);
} }
} }
result.put(DiffType.CREATE, newFileList);
result.put(DiffType.DELETE, deletedFileList);
result.put(DiffType.MODIFY, modifiedFileList);
return result; return result;
} }